diff options
Diffstat (limited to 'sqlelements/src/test/java/org')
22 files changed, 1909 insertions, 0 deletions
diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/AbstractAliasedSQLElementTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/AbstractAliasedSQLElementTest.java new file mode 100644 index 0000000..9b0ac2c --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/AbstractAliasedSQLElementTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: AbstractAliasedSQLElementTest.java 20 2005-12-10 21:08:36Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.mock.MockAliasedElement; + +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 20 $ + */ +public class AbstractAliasedSQLElementTest extends TestCase { + + AbstractAliasedSQLElement aae; + static final String aliasString0 = "AliasString0"; + static final String aliasString1 = "AliasString1"; + + protected void setUp() throws Exception { + super.setUp(); + aae = new MockAliasedElement(); + } + + protected void tearDown() throws Exception { + aae = null; + super.tearDown(); + } + + public void testGetAlias() { + aae = new MockAliasedElement(aliasString0); + Assert.assertEquals(aae.getAlias(),aliasString0); + } + + public void testSetAlias() { + aae.setAlias(aliasString1); + Assert.assertEquals(aae.getAlias(),aliasString1); + } + + public void testHasAlias() { + Assert.assertFalse(aae.hasAlias()); + aae.setAlias(aliasString0); + Assert.assertTrue(aae.hasAlias()); + } + + + public void testUnaliasedSLQElement() + { + aae = new MockAliasedElement(); + Assert.assertFalse(aae.hasAlias()); + Assert.assertEquals(null,aae.getAlias()); + } + + +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/AllTests.java b/sqlelements/src/test/java/org/israfil/sqlelements/AllTests.java new file mode 100644 index 0000000..d5d7078 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/AllTests.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: AllTests.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Test; +import junit.framework.TestSuite; +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class AllTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Test for org.israfil.sqlelements"); + //$JUnit-BEGIN$ + suite.addTest(org.israfil.sqlelements.constraints.AllTests.suite()); + suite.addTestSuite(AbstractAliasedSQLElementTest.class); + suite.addTestSuite(ColumnTest.class); + suite.addTestSuite(ComplexSampleQueriesTest.class); + suite.addTestSuite(DatabaseTypeTest.class); + suite.addTestSuite(DeleteTest.class); + suite.addTestSuite(JoinTest.class); + suite.addTestSuite(ParameterizedCommandTest.class); + suite.addTestSuite(QueryTest.class); + suite.addTestSuite(SimpleTableTest.class); + suite.addTestSuite(UpdateTest.class); + //$JUnit-END$ + return suite; + } + +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/ColumnTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/ColumnTest.java new file mode 100644 index 0000000..2537a04 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/ColumnTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: ColumnTest.java 14 2005-12-08 22:29:05Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.Date; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 14 $ + */ +public class ColumnTest extends TestCase { + + Column c; + Table t; + + protected void setUp() throws Exception { + super.setUp(); + t = new SimpleTable("MyTable"); + } + + protected void tearDown() throws Exception { + c = null; + t = null; + super.tearDown(); + } + + public void testSimpleColumn() + { + c = new SimpleColumn(new SimpleTable("MyTable"),"MyColumn"); + Assert.assertEquals("MyColumn",c.render(new SQLRenderContext(DatabaseType.Generic))); + } + + public void testLiteralColumn() + { + Assert.assertEquals("null",new LiteralColumn(null).render(new SQLRenderContext(DatabaseType.Generic))); + Assert.assertEquals("'MyColumn'",new LiteralColumn("MyColumn").render(new SQLRenderContext(DatabaseType.Generic))); + Assert.assertEquals("Fri Apr 07 00:00:00 EDT 3905",new LiteralColumn(new Date(2005,3,7)).render(new SQLRenderContext(DatabaseType.Generic))); + TestEnum e = TestEnum.TestOne; + Assert.assertEquals("'TestOne'",new LiteralColumn(e).render(new SQLRenderContext(DatabaseType.Generic))); + } + + public enum TestEnum { + TestOne,TestTwo,TestThree; + private TestEnum() {} + } +} + + + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/ComplexSampleQueriesTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/ComplexSampleQueriesTest.java new file mode 100644 index 0000000..24ce8c1 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/ComplexSampleQueriesTest.java @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: ComplexSampleQueriesTest.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.constraints.Equals; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public class ComplexSampleQueriesTest extends TestCase { + SQLRenderContext context; + + + public void setUp() { + context = new SQLRenderContext(DatabaseType.Generic); + } + + public static Table TABLE_A = new SimpleTable("table_a", "ta"); + public static Table TABLE_B = new SimpleTable("table_b", "tb"); + public static Table TABLE_C = new SimpleTable("table_c"); + public static Table TABLE_D = new SimpleTable("table_d"); + + public static final String[] A_COLUMN_NAMES = new String[] { + "a_col1", "a_col2", "a_col3", "a_col4" + }; + + public static String[] B_COLUMN_NAMES = new String[] { + "b_col1", "b_col2", "b_col3" + }; + + public static String[] C_COLUMN_NAMES = new String[] { + "c_col1" + }; + + public static String[] D_COLUMN_NAMES = new String[] { + "d_col1" + }; + + public final static Query COMPLEX_SELECT_QUERY = new Select( + Select.aggregate( + new Column[] { + new LiteralColumn(1,"literal_column"), + new SimpleColumn(TABLE_A,"extra_column"), + new SimpleColumn(TABLE_B,"extra_column2", "alias2") + }, + SimpleColumn.createColumns(TABLE_A, A_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_B, B_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_C, C_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_D, D_COLUMN_NAMES) + ), + new Equals(new SimpleColumn(TABLE_A,"comparison_column"),new LiteralColumn(0)), + new InnerJoin("id",TABLE_A, TABLE_B), + new LeftOuterJoin("id",TABLE_B,TABLE_C), + new LeftOuterJoin("id",TABLE_C,TABLE_D) + ); + + public void testComplexQuery() { + System.out.println(COMPLEX_SELECT_QUERY.render(context)); + Assert.assertEquals("select " + + "1 as \"literal_column\", ta.extra_column, tb.extra_column2 as \"alias2\", " + + "ta.a_col1, ta.a_col2, ta.a_col3, ta.a_col4, " + + "tb.b_col1, tb.b_col2, tb.b_col3, " + + "t0.c_col1, " + + "t1.d_col1 " + + "from table_a ta, table_b tb, table_c t0, table_d t1 " + + "where (ta.id = tb.id) and " + + "(tb.id = t0.id (+)) and " + + "(t0.id = t1.id (+)) and " + + "(ta.comparison_column = 0)", + COMPLEX_SELECT_QUERY.render(context)); + } + + public final static Query COMPLEX_SELECT_QUERY2 = new Select( + Select.aggregate( + new Column[] { + new LiteralColumn(1,"literal_column"), + new SimpleColumn(TABLE_A,"extra_column"), + new SimpleColumn(TABLE_B,"extra_column2", "alias2") + }, + SimpleColumn.createColumns(TABLE_A, A_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_B, B_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_C, C_COLUMN_NAMES), + SimpleColumn.createColumns(TABLE_D, D_COLUMN_NAMES) + ), + new Equals(new SimpleColumn(TABLE_A,"comparison_column"),new LiteralColumn(0)), + new InnerJoin("id",TABLE_A, TABLE_B), + new LeftOuterJoin("id",TABLE_B,TABLE_C), + new LeftOuterJoin("id",TABLE_C,TABLE_D) + ); + /* + public void testComplexQuery2() { + System.out.println(COMPLEX_SELECT_QUERY.render(context)); + Assert.assertEquals("select " + + "ta.a_col1, ta.a_col2, " + + "tb.b_col1, tb.b_col2, tb.b_col3, " + + "t0.c_col1, " + + "t1.d_col1 " + + "from table_a ta, table_b tb, table_c t0, table_d t1 " + + "where (ta.id = tb.id) and " + + "(tb.id = t0.id (+)) and " + + "(t0.id = t1.id (+)) and " + + "(ta.comparison_column = 0)", + COMPLEX_SELECT_QUERY2.render(context)); + } + */ +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/DatabaseTypeTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/DatabaseTypeTest.java new file mode 100644 index 0000000..34e10d7 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/DatabaseTypeTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: DatabaseTypeTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.TestCase; + +import org.israfil.sqlelements.render.DatabaseType; +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class DatabaseTypeTest extends TestCase { + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testInitialization() { + DatabaseType generic = DatabaseType.Generic; + DatabaseType oracle = DatabaseType.Oracle; + } + +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/DeleteTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/DeleteTest.java new file mode 100644 index 0000000..b0705f5 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/DeleteTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: DeleteTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.constraints.Equals; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + + +// TODO: Migrate some tests into AbstractQuery tests and Select tests, since the functionality has been reworked +public class DeleteTest extends TestCase { + SQLRenderContext context; + Table t0; + Column col0; + Column col1; + Constraint c0; + + public void setUp() + { + context = new SQLRenderContext(DatabaseType.Generic); + t0 = new SimpleTable("Table0"); + col0 = new SimpleColumn(t0,"column0"); + col1 = new LiteralColumn(5); + c0 = new Equals(col0,col1); + } + + public void testCreateDelete() + { + Delete delete = new Delete(); + Assert.assertNotNull(delete); + } + + public void testFailRenderWithNoTable() + { + Delete delete = new Delete(); + Assert.assertNotNull(delete); + try { + delete.render(context); + } catch (Exception e) { + return; + } + Assert.fail("Failed to trap exception."); + } + + public void testDeleteWithTable() + { + Delete delete = new Delete(t0,null); + Assert.assertNotNull(delete); + Assert.assertEquals("delete from Table0",delete.render(context)); + } + + public void testDeleteWithTableAndConstraint() + { + Delete delete = new Delete(t0,c0); + Assert.assertNotNull(delete); + Assert.assertEquals("delete from Table0 t0 where t0.column0 = 5",delete.render(context)); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/JoinTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/JoinTest.java new file mode 100644 index 0000000..18aaeb3 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/JoinTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: JoinTest.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class JoinTest extends TestCase +{ + SQLRenderContext context; + + + public void setUp() { + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testJoinColumnAccess() { + SimpleColumn c1 = new SimpleColumn(new SimpleTable("table1"),"col_A"); + SimpleColumn c2 = new SimpleColumn(new SimpleTable("table2"),"col_B"); + Join j = new InnerJoin(c1,c2); + Assert.assertEquals(c1,j.getLeftColumn()); + Assert.assertEquals(c2,j.getRightColumn()); + } + + public void InnerJoinCreation() { + SimpleColumn c1 = new SimpleColumn(new SimpleTable("table1"),"col_A"); + SimpleColumn c2 = new SimpleColumn(new SimpleTable("table2"),"col_B"); + Join j = new InnerJoin(c1,c2); + Assert.assertEquals("t0.col_A = t1.col_B",j.render(context)); + } + + public void LeftOuterJoinCreation() { + SimpleColumn c1 = new SimpleColumn(new SimpleTable("table1"),"col_A"); + SimpleColumn c2 = new SimpleColumn(new SimpleTable("table2"),"col_B"); + Join j = new LeftOuterJoin(c1,c2); + Assert.assertEquals("t0.col_A = t1.col_B (+)",j.render(context)); + } + +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/ParameterizedCommandTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/ParameterizedCommandTest.java new file mode 100644 index 0000000..e59db77 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/ParameterizedCommandTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: ParameterizedCommandTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + +// TODO: Migrate some tests into AbstractQuery tests and Select tests, since the functionality has been reworked +public class ParameterizedCommandTest extends TestCase { + + SQLRenderContext context; + + public void setUp() + { + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testGetParameterValueTest() + { + Column c = new SimpleColumn(new SimpleTable("Table0"),"columnA"); + MockAbstractParameterizedCommand cmd = new MockAbstractParameterizedCommand(); + Assert.assertNotNull(cmd); + SQLParameter parm0 = new SQLParameter(c,5); + cmd.setParameter(parm0); + for (SQLParameter p : cmd.parameters) Assert.assertEquals(parm0,p); + Assert.assertEquals(5,cmd.getParameterValue(c)); + } + + public class MockAbstractParameterizedCommand extends AbstractParameterizedCommand + { + public String render(SQLRenderContext context) { return null; } + public Object clone() throws CloneNotSupportedException { return null; } + } +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/QueryTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/QueryTest.java new file mode 100644 index 0000000..3ab5cc1 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/QueryTest.java @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: QueryTest.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.List; +import java.util.Set; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.constraints.Equals; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + + + +// TODO: Migrate some tests into AbstractQuery tests and Select tests, since the functionality has been reworked + +public class QueryTest extends TestCase { + + SQLRenderContext context; + + public void setUp() { + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testCloneQuery() throws CloneNotSupportedException { + Table table = new SimpleTable("Table1"); + LiteralColumn col1 = new LiteralColumn("5"); + Column col2 = new SimpleColumn(table,"id"); + Column col3 = new SimpleColumn(table,"recursive_id"); + Constraint constraint = new Equals(col1,col2); + Join j = new InnerJoin(col2,col3); + Query select1 = new Select(new Column[] {col1,col2},constraint,j); + Query select2 = (Query)select1.clone(); + Assert.assertFalse("Objects should not be identical",select1==select2); + Assert.assertEquals(select1.render(context),select2.render(context)); + } + + public void testColumns() + { + Table t0 = new SimpleTable("FOO_TABLE"); + Table t1 = new SimpleTable("BAR_TABLE","bar"); + Column c0 = new SimpleColumn(t0,"colA"); + Column c1 = new SimpleColumn(t1,"colB"); + Query q = new Select(new Column[] {c0,c1}); + List<Column> cols = q.getColumns(); + Assert.assertEquals(cols.get(0),c0); + Assert.assertEquals(cols.get(1),c1); + } + + public void testTables() + { + Table t0 = new SimpleTable("FOO_TABLE"); + Table t1 = new SimpleTable("BAR_TABLE","bar"); + Column c0 = new SimpleColumn(t0,"colA"); + Column c1 = new SimpleColumn(t1,"colB"); + Join j = new InnerJoin(c0,c1); + //Query q = new Select(c0,(Constraint)null,(Join)j); + Query q = new Select(new Column[]{},j); + Set tables = q.getTables(); + Assert.assertTrue("Missing table " + t0,tables.contains(t0)); + Assert.assertTrue("Missing table " + t1,tables.contains(t1)); + } + + public void testSelectTest() + { + Query q = new Select(new SimpleColumn(new SimpleTable("y"),"x")); + Assert.assertEquals("select t0.x from y t0",q.render(context)); + } + + public void testSelectTestWithAlias() + { + Query q = new Select(new SimpleColumn(new SimpleTable("table1","a"),"column1","b")); + Assert.assertEquals("select a.column1 as \"b\" from table1 a",q.render(context)); + } + + public void testMultiColumnSelectTest() + { + Table t1 = new SimpleTable("table1"); + Column c1 = new SimpleColumn(t1,"c1"); + Column c2 = new SimpleColumn(t1,"c2"); + Query q = new Select(new Column[] { c1,c2} ); + Assert.assertEquals("select t0.c1, t0.c2 from table1 t0",q.render(context)); + } + + public void testMultiColumnSelectTestWithAlias() + { + Table t1 = new SimpleTable("t1","tab1"); + Column c1 = new SimpleColumn(t1,"c1","foo"); + Column c2 = new SimpleColumn(t1,"c2"); + Query q = new Select(new Column[] { c1,c2} ); + Assert.assertEquals("select tab1.c1 as \"foo\", tab1.c2 from t1 tab1",q.render(context)); + } + + public void testSelectWithConstraint() + { + Table t1 = new SimpleTable("table1"); + Column c1 = new SimpleColumn(t1,"c1"); + Column c2 = new SimpleColumn(t1,"c2"); + Constraint constraint = new Equals(c1,c2); + Query q = new Select(new Column[] { c1,c2 }, constraint); + Assert.assertEquals("select t0.c1, t0.c2 from table1 t0 where t0.c1 = t0.c2",q.render(context)); + } + + public void testSelectWithConstraintUsingInnerJoin() + { + Table t1 = new SimpleTable("table1","tab1"); + Table t2 = new SimpleTable("table2","tab2"); + Column c1_1 = new SimpleColumn(t1,"c1"); + Column c2_1 = new SimpleColumn(t2,"c1"); + Column c2_2 = new SimpleColumn(t2,"c2"); + Join join = new InnerJoin(c1_1,c2_1); + Constraint constraint = new Equals(c2_2,new LiteralColumn(1)); + Query q = new Select(new Column[] { c2_2 }, constraint, join); + Assert.assertEquals("select tab2.c2 from table2 tab2, table1 tab1 where (tab1.c1 = tab2.c1) and (tab2.c2 = 1)",q.render(context)); + } + + public void testSelectWithConstraintUsingInnerJoinNoTableAlias() + { + Table t1 = new SimpleTable("table1"); + Table t2 = new SimpleTable("table2"); + Column c1_1 = new SimpleColumn(t1,"c1"); + Column c2_1 = new SimpleColumn(t2,"c1"); + Column c2_2 = new SimpleColumn(t2,"c2"); + Join join = new InnerJoin(c1_1,c2_1); + Constraint constraint = new Equals(c2_2,new LiteralColumn(1)); + Query q = new Select(new Column[] { c2_2 }, constraint, join); + Assert.assertEquals("select t0.c2 from table2 t0, table1 t1 where (t1.c1 = t0.c1) and (t0.c2 = 1)",q.render(context)); + } + + public void testComplexInnerJoin() + { + Table t1 = new SimpleTable("table1", "tab1"); + Table t2 = new SimpleTable("table2", "tab2"); + Table t3 = new SimpleTable("table3"); + Table t4 = new SimpleTable("table4"); + Column c1_A = new SimpleColumn(t1,"A"); + Column c1_1 = new SimpleColumn(t1,"colZ"); + Column c2_1 = new SimpleColumn(t2,"colZ"); + Column c2_2 = new SimpleColumn(t2,"colY"); + Column c3_2 = new SimpleColumn(t3,"colY"); + Column c3_3 = new SimpleColumn(t3,"colW"); + Column c4_3 = new SimpleColumn(t4,"colW"); + Column c4_B = new SimpleColumn(t4,"B"); + Join join1 = new InnerJoin(c1_1,c2_1); + Join join2 = new InnerJoin(c2_2,c3_2); + Join join3 = new InnerJoin(c3_3,c4_3); + Constraint constraint = new Equals(c2_2,new LiteralColumn(1)); + Query q = new Select(new Column[] { c1_A, c4_B }, constraint, join1, join2, join3); + Assert.assertEquals("select tab1.A, t0.B from table1 tab1, table4 t0, table2 tab2, table3 t1 where (tab1.colZ = tab2.colZ) and (tab2.colY = t1.colY) and (t1.colW = t0.colW) and (tab2.colY = 1)",q.render(context)); + } + + public void testComplexLeftOuterJoin() + { + Table t1 = new SimpleTable("table1", "tab1"); + Table t2 = new SimpleTable("table2", "tab2"); + Table t3 = new SimpleTable("table3"); + Table t4 = new SimpleTable("table4"); + Column c1_A = new SimpleColumn(t1,"A"); + Column c1_1 = new SimpleColumn(t1,"colZ"); + Column c2_1 = new SimpleColumn(t2,"colZ"); + Column c2_2 = new SimpleColumn(t2,"colY"); + Column c3_2 = new SimpleColumn(t3,"colY"); + Column c3_3 = new SimpleColumn(t3,"colW"); + Column c4_3 = new SimpleColumn(t4,"colW"); + Column c4_B = new SimpleColumn(t4,"B"); + Join join1 = new InnerJoin(c1_1,c2_1); + Join join2 = new LeftOuterJoin(c2_2,c3_2); + Join join3 = new LeftOuterJoin(c3_3,c4_3); + Constraint constraint = new Equals(c2_2,new LiteralColumn(1)); + Query q = new Select(new Column[] { c1_A, c4_B }, constraint, join1, join2, join3); + Assert.assertEquals("select tab1.A, t0.B from table1 tab1, table4 t0, table2 tab2, table3 t1 where (tab1.colZ = tab2.colZ) and (tab2.colY = t1.colY (+)) and (t1.colW = t0.colW (+)) and (tab2.colY = 1)",q.render(context)); + } +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/SimpleTableTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/SimpleTableTest.java new file mode 100644 index 0000000..a1614c0 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/SimpleTableTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: SimpleTableTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class SimpleTableTest extends TestCase { + + static final String table0Name = "Table0"; + static final String table0Alias = "TableAlias0"; + SimpleTable table0; + + + protected void setUp() throws Exception { + super.setUp(); + table0 = new SimpleTable(table0Name,table0Alias); + } + + protected void tearDown() throws Exception { + table0 = null; + super.tearDown(); + } + + public SimpleTableTest(String name) { + super(name); + } + + public void testConstructSimpleTable() { + table0 = new SimpleTable(table0Name); + table0 = new SimpleTable(table0Name,table0Alias); + } + + public void testGetName() { + //TODO Implement getName(). + Assert.assertEquals(table0.getName(),table0Name); + } + + public void testToString() { + Assert.assertEquals("SimpleTable["+table0Name+" "+table0Alias+"]",table0.toString()); + table0 = new SimpleTable(table0Name); + Assert.assertEquals("SimpleTable["+table0Name+"]",table0.toString()); + } + + public void testClone() { + SimpleTable clone = (SimpleTable)table0.clone(); + Assert.assertEquals(table0.getName(),clone.getName()); + Assert.assertEquals(table0.getAlias(),clone.getAlias()); + Assert.assertEquals(table0.hasAlias(),clone.hasAlias()); + Assert.assertEquals(table0.toString(),clone.toString()); + } + + public void testRender() { + Assert.assertEquals(table0Name,table0.render(new SQLRenderContext(DatabaseType.Generic))); + } + + public void testAliasesForUnaliasedTables() + { + SQLRenderContext context = new SQLRenderContext(DatabaseType.Mock); + Table t0 = new SimpleTable("Table0"); + Table t1 = new SimpleTable("Table1"); + Assert.assertEquals("t0",context.getAlias(t0)); + Assert.assertEquals("t1",context.getAlias(t1)); + } +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/UpdateTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/UpdateTest.java new file mode 100644 index 0000000..3f8d153 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/UpdateTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: UpdateTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.constraints.Equals; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + +// TODO: Migrate some tests into AbstractQuery tests and Select tests, since the functionality has been reworked +public class UpdateTest extends TestCase { + SQLRenderContext context; + + public void setUp() + { + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testCreateUpdate() + { + Update update = new Update(); + Assert.assertNotNull(update); + } + + public void testRenderWithNoTable() + { + Update update = new Update(); + Assert.assertNotNull(update); + try { + update.render(context); + } catch (Exception e) { + return; + } + Assert.fail("Failed to trap exception."); + } + + public void testNormalUpdate() + { + Column c = new SimpleColumn(new SimpleTable("Table0"),"columnA"); + Update update = new Update(new SQLParameter(c,5)); + Assert.assertNotNull(update); + Assert.assertEquals("update Table0 set columnA = 5",update.render(context)); + } + + public void testNormalUpdateWithConstraints() + { + Column c = new SimpleColumn(new SimpleTable("Table0"),"columnA"); + Constraint constraint = new Equals(c,new LiteralColumn(3)); + Update update = new Update(constraint,new SQLParameter(c,5)); + Assert.assertNotNull(update); + Assert.assertEquals("update Table0 t0 set columnA = 5 where t0.columnA = 3",update.render(context)); + } +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/AllTests.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/AllTests.java new file mode 100644 index 0000000..20e22a1 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/AllTests.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: AllTests.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Test; +import junit.framework.TestSuite; +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class AllTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Test for org.israfil.sqlelements"); + //$JUnit-BEGIN$ + suite.addTestSuite(BetweenRangeConstraintTest.class); + suite.addTestSuite(ColumnConstraintTest.class); + suite.addTestSuite(EqualsConstraintTest.class); + suite.addTestSuite(LogicalConstraintsTest.class); + suite.addTestSuite(MockConstraintTest.class); + suite.addTestSuite(NaryConstraintTest.class); + suite.addTestSuite(NotEqualsConstraintTest.class); + //$JUnit-END$ + return suite; + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/BetweenRangeConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/BetweenRangeConstraintTest.java new file mode 100644 index 0000000..8f1f1bd --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/BetweenRangeConstraintTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: BetweenRangeConstraintTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.LiteralColumn; +import org.israfil.sqlelements.SimpleColumn; +import org.israfil.sqlelements.SimpleTable; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class BetweenRangeConstraintTest extends TestCase { + SQLRenderContext context; + + public void setUp() throws Exception { + super.setUp(); + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testBetweenRangeWithLiterals() { + Constraint c = new BetweenRange(new SimpleColumn(new SimpleTable("Table1"),"column1"),new LiteralColumn(0),new LiteralColumn(5)); + Assert.assertEquals("t0.column1 >= 0 and t0.column1 <= 5",c.render(context)); + } + + + public void testBetweenRangeWithRealColumns() { + Constraint c = new BetweenRange(new LiteralColumn("column1"),new SimpleColumn(new SimpleTable("Table2"),"col1"),new SimpleColumn(new SimpleTable("Table3"),"col1")); + Assert.assertEquals("'column1' >= t0.col1 and 'column1' <= t1.col1",c.render(context)); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/ColumnConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/ColumnConstraintTest.java new file mode 100644 index 0000000..513013f --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/ColumnConstraintTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: ColumnConstraintTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.Column; +import org.israfil.sqlelements.SimpleColumn; +import org.israfil.sqlelements.SimpleTable; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class ColumnConstraintTest extends TestCase { + SQLRenderContext context; + + public void setUp() throws Exception { + super.setUp(); + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testColumnConstraintCreationWithoutTable() { + Column col = new SimpleColumn(null,"col"); + ColumnConstraint c = new ColumnConstraint(col); + Assert.assertEquals("col",c.render(context)); + } + + public void testColumnConstraintCreationWithoutAlias() { + Column col = new SimpleColumn(new SimpleTable("foo"),"col"); + ColumnConstraint c = new ColumnConstraint(col); + Assert.assertEquals("t0.col",c.render(context)); + } + + public void testColumnConstraintCreationWithAlias() { + Column col = new SimpleColumn(new SimpleTable("foo","my_foo_table"),"col"); + ColumnConstraint c = new ColumnConstraint(col); + Assert.assertEquals("my_foo_table.col",c.render(context)); + } + + public void testColumnAccessInColumnConstraint() { + Column col = new SimpleColumn(null,"col"); + ColumnConstraint c = new ColumnConstraint(col); + Assert.assertEquals(col,c.getColumn()); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/EqualsConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/EqualsConstraintTest.java new file mode 100644 index 0000000..50cc1d5 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/EqualsConstraintTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: EqualsConstraintTest.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.LiteralColumn; +import org.israfil.sqlelements.SimpleColumn; +import org.israfil.sqlelements.SimpleTable; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class EqualsConstraintTest extends TestCase { + SQLRenderContext context; + + public void setUp() throws Exception { + super.setUp(); + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testColumnEqualsLiteral() { + Constraint c = new Equals(new SimpleColumn(new SimpleTable("Table1"),"column1"),new LiteralColumn(0)); + Assert.assertEquals("t0.column1 = 0",c.render(context)); + } + + public void testLiteralEqualsColumn() { + Constraint c = new Equals(new LiteralColumn(0),new SimpleColumn(new SimpleTable("Table1"),"column1")); + Assert.assertEquals("0 = t0.column1",c.render(context)); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/LogicalConstraintsTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/LogicalConstraintsTest.java new file mode 100644 index 0000000..347eb08 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/LogicalConstraintsTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: LogicalConstraintsTest.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import java.util.Set; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import net.israfil.foundation.collections.ArraySet; + +import org.israfil.sqlelements.mock.MockConstraint; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A suite of tests for all the so-called Logical constraints, such as + * and, or, not, etc. + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public class LogicalConstraintsTest extends TestCase { + + Constraint constraint1 = null; + Constraint constraint2 = null; + String expectedResult = null; + String constraintString1 = null; + String constraintString2 = null; + SQLRenderContext context = null; + + public void setUp() throws Exception { + super.setUp(); + context = new SQLRenderContext(DatabaseType.Generic); + constraintString1 = "'A' = 'B'"; + constraintString2 = "'B' = 'C'"; + constraint1 = new MockConstraint(constraintString1); + constraint2 = new MockConstraint(constraintString2); + } + + public void tearDown() throws Exception { + constraintString1 = null; + constraintString2 = null; + constraint1 = null; + constraint2 = null; + expectedResult = null; + super.tearDown(); + } + + public void testAndRender() { + expectedResult = constraintString1 + " and " + constraintString2; + Constraint andc = new And(constraint1,constraint2); + Assert.assertEquals(expectedResult,andc.render(context)); + } + + public void testAndWithSetOfConstraints() { + expectedResult = constraintString1 + " and " + constraintString2; + Set<Constraint> set = new ArraySet<Constraint>(); + set.add(constraint1); + set.add(constraint2); + And arc = new And(set); + Assert.assertEquals(expectedResult,arc.render(context)); + } + + public void testOrWithConstraints() { + expectedResult = constraintString1 + " or " + constraintString2; + Or orc = new Or(constraint1,constraint2); + Assert.assertEquals(expectedResult,orc.render(context)); + } + + public void testOrWithSetOfConstraints() { + expectedResult = constraintString1 + " or " + constraintString2; + Set<Constraint> set = new ArraySet<Constraint>(); + set.add(constraint1); + set.add(constraint2); + Or orc = new Or(set); + Assert.assertEquals(expectedResult,orc.render(context)); + } + + public void testNotRender() { + expectedResult = "not (" + constraintString1 + ")"; + Not nc = new Not(constraint1); + Assert.assertEquals(expectedResult,nc.render(context)); + } + +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/MockConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/MockConstraintTest.java new file mode 100644 index 0000000..8f39388 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/MockConstraintTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: MockConstraintTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.mock.MockConstraint; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * A test of the MockConstraint object + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class MockConstraintTest extends TestCase +{ + + public void testMockConstraint() + { + String stringValue = "Foo"; + Constraint ic = new MockConstraint(stringValue); + Assert.assertEquals(stringValue,ic.render(new SQLRenderContext(DatabaseType.Generic))); + } + +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NaryConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NaryConstraintTest.java new file mode 100644 index 0000000..477424c --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NaryConstraintTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: NaryConstraintTest.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import java.util.HashSet; +import java.util.Set; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.mock.MockNaryConstraint; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class NaryConstraintTest extends TestCase +{ + SQLRenderContext context; + Constraint c1; + Constraint c2; + Constraint c3; + Constraint c4; + + + public void setUp() throws Exception { + super.setUp(); + c1 = new ArbitraryStringConstraint("(a=b)"); + c2 = new ArbitraryStringConstraint("(b=c)"); + c3 = new ArbitraryStringConstraint("(c=d)"); + c4 = new ArbitraryStringConstraint("(c=q)"); + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void tearDown() throws Exception { + super.tearDown(); + c1 = null; + c2 = null; + c3 = null; + c4 = null; + context = null; + } + + public void testConstructWithOneConstraint() + { + NaryConstraint nac = new MockNaryConstraint("foo",c1); + Assert.assertNotNull(nac); + } + + public void testConstructWithTwoConstraints() + { + NaryConstraint nac = new MockNaryConstraint("foo",c1,c2); + Assert.assertNotNull(nac); + } + + public void testConstructWithMultipleConstraints() + { + NaryConstraint nac = new MockNaryConstraint("foo",c1,c2,c3,c4); + Assert.assertNotNull(nac); + } + + public void testConstructWithNoConstraints() + { + + NaryConstraint nac = new MockNaryConstraint("foo"); + Assert.assertNotNull(nac); + } + + public void testConstructWithNullSetOfConstraints() { + NaryConstraint nac = new MockNaryConstraint("foo",(Set<Constraint>)null); + Assert.assertNotNull(nac); + } + + public void testConstructWithSelfSameConstraint() { + NaryConstraint nac = new MockNaryConstraint("foo",c1,c2); + NaryConstraint nac1 = new MockNaryConstraint("foo",nac,c3,c4); + Assert.assertNotNull(nac); + Assert.assertEquals("(a=b) foo (b=c) foo (c=d) foo (c=q)",nac1.render(context)); + } + + public void testRenderWithAConstraintSetWithNullElement() { + Set<Constraint> sc = new HashSet<Constraint>(); + sc.add(null); + NaryConstraint nac = new MockNaryConstraint("foo",sc); + Assert.assertNotNull(nac); + Assert.assertEquals("",nac.render(context)); + } + + public void testRenderWithOneConstraint() { + NaryConstraint nac = new MockNaryConstraint("foo",c1); + Assert.assertNotNull(nac); + Assert.assertEquals("(a=b)",nac.render(context)); + } + + public void testRenderWithOneNullConstraint(){ + NaryConstraint nac = new MockNaryConstraint("foo",(Constraint[])null); + Assert.assertNotNull(nac); + Assert.assertEquals("",nac.render(context)); + } + + public void testNaryConstraintWithMultipleConstraints() + { + NaryConstraint nac = new MockNaryConstraint("foo",c1,c2,c3,c4); + Assert.assertNotNull(nac); + Assert.assertEquals("(a=b) foo (b=c) foo (c=d) foo (c=q)",nac.render(context)); + } + + public void testNaryConstraintWithNullConstraints() + { + NaryConstraint nac = new MockNaryConstraint("foo",null,c1,c2,c3,null,c4); + Assert.assertNotNull(nac); + Assert.assertEquals("(a=b) foo (b=c) foo (c=d) foo (c=q)",nac.render(context)); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NotEqualsConstraintTest.java b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NotEqualsConstraintTest.java new file mode 100644 index 0000000..6e9cdf2 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/constraints/NotEqualsConstraintTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: NotEqualsConstraintTest.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.israfil.sqlelements.LiteralColumn; +import org.israfil.sqlelements.SimpleColumn; +import org.israfil.sqlelements.SimpleTable; +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + +public class NotEqualsConstraintTest extends TestCase { + SQLRenderContext context; + + public void setUp() throws Exception { + super.setUp(); + context = new SQLRenderContext(DatabaseType.Generic); + } + + public void testColumnEqualsLiteral() { + Constraint c = new NotEquals(new SimpleColumn(new SimpleTable("Table1"),"column1"),new LiteralColumn(0)); + Assert.assertEquals("t0.column1 != 0",c.render(context)); + } + + public void testLiteralEqualsColumn() { + Constraint c = new NotEquals(new LiteralColumn(0),new SimpleColumn(new SimpleTable("Table1"),"column1")); + Assert.assertEquals("0 != t0.column1",c.render(context)); + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockAliasedElement.java b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockAliasedElement.java new file mode 100644 index 0000000..3cefddf --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockAliasedElement.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: MockAliasedElement.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.mock; + +import org.israfil.sqlelements.AbstractAliasedSQLElement; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class MockAliasedElement extends AbstractAliasedSQLElement { + public MockAliasedElement () { + } + + public MockAliasedElement (String alias) { + this.alias = alias; + } + + public String render(SQLRenderContext context) { + return null; + } +} diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockConstraint.java b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockConstraint.java new file mode 100644 index 0000000..1468a78 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockConstraint.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: MockConstraint.java 15 2005-12-09 03:56:01Z cgruber $ + */ +package org.israfil.sqlelements.mock; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A Mock object implementing Constraint for use in testing. + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 15 $ + */ +public class MockConstraint implements Constraint +{ + + private Object textValue = null; + public String getText() { return textValue.toString(); } + + public MockConstraint(Object textValue) + { + this.textValue=textValue; + } + + + public String render(SQLRenderContext context) + { + return getText(); + } + +} + diff --git a/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockNaryConstraint.java b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockNaryConstraint.java new file mode 100644 index 0000000..7acf612 --- /dev/null +++ b/sqlelements/src/test/java/org/israfil/sqlelements/mock/MockNaryConstraint.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This software is licensed under the Berkeley Standard Distribution license, + * (BSD license), as defined below: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Israfil Consulting Services nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Id: MockNaryConstraint.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.mock; + +import java.util.Set; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.constraints.NaryConstraint; + +/** + * A Mock object implementing Constraint for use in testing. + * + * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a> + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class MockNaryConstraint extends NaryConstraint +{ + public MockNaryConstraint(String op, Constraint ... constraints) { + super(op,constraints); + } + public MockNaryConstraint(String op, Set<Constraint> constraints) { + super(op,constraints); + } +} + |
