package org.litesoft.orsup.selection; import junit.framework.*; // Copyright Status: // // All Software available from LiteSoft.org (including this file) is // hereby released into the public domain. // // It is free! As in, you may use it freely in both commercial and // non-commercial applications, bundle it with your software // distribution, include it on a CD-ROM, list the source code in a book, // mirror the documentation at your own web site, or use it in any other // way you see fit. // // NO Warranty! // // All software is provided "as is". // // There is ABSOLUTELY NO WARRANTY OF ANY KIND: not for the design, fitness // (for a particular purpose), level of errors (or lack thereof), or // applicability of this software. The entire risk as to the quality // and performance of this software is with you. Should this software // prove defective, you assume the cost of all necessary servicing, repair // or correction. // // In no event unless required by applicable law or agreed to in writing // will any party who created or may modify and/or redistribute this // software, be liable to you for damages, including any general, // special, incidental or consequential damages arising out of the use or // inability to use this software (including but not limited to loss of // data or data being rendered inaccurate or losses sustained by you or // third parties or a failure of this software to operate with any // other programs), even if such holder or other party has been advised // of the possibility of such damages. // // NOTE: Should you discover a bug, have a recogmendation for a change, wish // to submit modifications, or wish to add new classes/functionality, // please email them to: // // changes44@litesoft.org // public class WhereClauseFactoryTest extends org.litesoft.Extend4Test { private static final Class THIS = WhereClauseFactoryTest.class; public WhereClauseFactoryTest( String name ) { super( name ); } public static void main( String[] args ) { run( suite() ); } public static Test suite() { return new TestSuite( THIS ); } static class WCtableID implements SimpleTableIdentifier { public String getTableName() { return "TheTable"; } } static class CDstring extends AbstractColumnDefinition { CDstring( String pName ) { this( pName + "Attr" , pName + "Col" ); } CDstring( String pName , String pColumnName ) { super( pName , pColumnName , String.class ); } } static class CDinteger extends AbstractColumnDefinition { CDinteger( String pName ) { this( pName + "Attr" , pName + "Col" ); } CDinteger( String pName , String pColumnName ) { super( pName , pColumnName , Integer.class ); } } private WhereClauseFactory F = new WhereClauseFactory(); private CDstring TheString = new CDstring( "TheStr" ); private CDinteger TheInteger = new CDinteger( "TheInt" ); private CDinteger TheInteger2 = new CDinteger( "TheInt2" ); private WhereClause wcStrNull = F.isNull( TheString ); private WhereClause wcStrNotNull = F.isNotNull( TheString ); private WhereClause wcStrEqual = F.isEqual( TheString , "gas" ); private WhereClause wcIntEqual = F.isEqual( TheInteger , 5 ); private WhereClause wcIntEqual2 = F.isEqual( TheInteger2 , 6 ); private WhereClause wcStrNotEqual = F.isNotEqual( TheString , "gas" ); private WhereClause wcintNotEqual = F.isNotEqual( TheInteger , 5 ); private SingleColumnSelect zSingleColumnSelect = new SingleColumnSelect( TheInteger , new WCtableID() , wcStrNotNull ); public void test_isNull() throws Exception { WhereClause wc = F.isNull( TheString ); assertEquals( "Where TheStrAttr == null" , wc.toString() ); assertEquals( "WHERE TheStrCol IS NULL" , wc.toSQL() ); } public void test_isNotNull() throws Exception { WhereClause wc = F.isNotNull( TheString ); assertEquals( "Where TheStrAttr != null" , wc.toString() ); assertEquals( "WHERE TheStrCol IS NOT NULL" , wc.toSQL() ); } public void test_isEqual_Null() throws Exception { WhereClause wc = F.isEqual( TheString , null ); assertEquals( wcStrNull.toString() , wc.toString() ); assertEquals( wcStrNull.toSQL() , wc.toSQL() ); } public void test_isNotEqual_Null() throws Exception { WhereClause wc = F.isNotEqual( TheString , null ); assertEquals( wcStrNotNull.toString() , wc.toString() ); assertEquals( wcStrNotNull.toSQL() , wc.toSQL() ); } public void test_isEqual() throws Exception { WhereClause wc = F.isEqual( TheString , "gas" ); assertEquals( "Where TheStrAttr == \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol = 'gas'" , wc.toSQL() ); } public void test_isNotEqual() throws Exception { WhereClause wc = F.isNotEqual( TheString , "gas" ); assertEquals( "Where TheStrAttr != \"gas\"" , wc.toString() ); assertEquals( "WHERE (TheStrCol <> 'gas') OR (TheStrCol IS NULL)" , wc.toSQL() ); } public void test_isEqual_int() throws Exception { WhereClause wc = F.isEqual( TheInteger , 5 ); assertEquals( "Where TheIntAttr == 5" , wc.toString() ); assertEquals( "WHERE TheIntCol = 5" , wc.toSQL() ); } public void test_isNotEqual_int() throws Exception { WhereClause wc = F.isNotEqual( TheInteger , 5 ); assertEquals( "Where TheIntAttr != 5" , wc.toString() ); assertEquals( "WHERE (TheIntCol <> 5) OR (TheIntCol IS NULL)" , wc.toSQL() ); } public void test_isGreaterThan_Null() throws Exception { try { F.isGreaterThan( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isNotGreaterThan_Null() throws Exception { try { F.isNotGreaterThan( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isGreaterThan() throws Exception { WhereClause wc = F.isGreaterThan( TheString , "gas" ); assertEquals( "Where TheStrAttr > \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol > 'gas'" , wc.toSQL() ); } public void test_isNotGreaterThan() throws Exception { WhereClause wc = F.isNotGreaterThan( TheString , "gas" ); assertEquals( "Where TheStrAttr <= \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol <= 'gas'" , wc.toSQL() ); } public void test_isGreaterThan_int() throws Exception { WhereClause wc = F.isGreaterThan( TheInteger , 5 ); assertEquals( "Where TheIntAttr > 5" , wc.toString() ); assertEquals( "WHERE TheIntCol > 5" , wc.toSQL() ); } public void test_isNotGreaterThan_int() throws Exception { WhereClause wc = F.isNotGreaterThan( TheInteger , 5 ); assertEquals( "Where TheIntAttr <= 5" , wc.toString() ); assertEquals( "WHERE TheIntCol <= 5" , wc.toSQL() ); } public void test_isLessThan_Null() throws Exception { try { F.isLessThan( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isNotLessThan_Null() throws Exception { try { F.isNotLessThan( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isLessThan() throws Exception { WhereClause wc = F.isLessThan( TheString , "gas" ); assertEquals( "Where TheStrAttr < \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol < 'gas'" , wc.toSQL() ); } public void test_isNotLessThan() throws Exception { WhereClause wc = F.isNotLessThan( TheString , "gas" ); assertEquals( "Where TheStrAttr >= \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol >= 'gas'" , wc.toSQL() ); } public void test_isLessThan_int() throws Exception { WhereClause wc = F.isLessThan( TheInteger , 5 ); assertEquals( "Where TheIntAttr < 5" , wc.toString() ); assertEquals( "WHERE TheIntCol < 5" , wc.toSQL() ); } public void test_isNotLessThan_int() throws Exception { WhereClause wc = F.isNotLessThan( TheInteger , 5 ); assertEquals( "Where TheIntAttr >= 5" , wc.toString() ); assertEquals( "WHERE TheIntCol >= 5" , wc.toSQL() ); } public void test_isBetween_NullLeft() throws Exception { try { F.isBetween( TheString , null , "kls" ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isNotBetween_NullLeft() throws Exception { try { F.isNotBetween( TheString , null , "kls" ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isBetween_NullRight() throws Exception { try { F.isBetween( TheString , "gas" , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isNotBetween_NullRight() throws Exception { try { F.isNotBetween( TheString , "gas" , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_isBetween_EqualNull() throws Exception { WhereClause wc = F.isBetween( TheString , null , null ); assertEquals( wcStrNull.toString() , wc.toString() ); assertEquals( wcStrNull.toSQL() , wc.toSQL() ); } public void test_isNotBetween_EqualNull() throws Exception { WhereClause wc = F.isNotBetween( TheString , null , null ); assertEquals( wcStrNotNull.toString() , wc.toString() ); assertEquals( wcStrNotNull.toSQL() , wc.toSQL() ); } public void test_isBetween_Equal() throws Exception { WhereClause wc = F.isBetween( TheString , "gas" , "g" + "a" + "s" ); assertEquals( wcStrEqual.toString() , wc.toString() ); assertEquals( wcStrEqual.toSQL() , wc.toSQL() ); } public void test_isNotBetween_Equal() throws Exception { WhereClause wc = F.isNotBetween( TheString , "gas" , "g" + "a" + "s" ); assertEquals( wcStrNotEqual.toString() , wc.toString() ); assertEquals( wcStrNotEqual.toSQL() , wc.toSQL() ); } public void test_isBetween_Equal_int() throws Exception { WhereClause wc = F.isBetween( TheInteger , 5 , 5 ); assertEquals( wcIntEqual.toString() , wc.toString() ); assertEquals( wcIntEqual.toSQL() , wc.toSQL() ); } public void test_isNotBetween_Equal_int() throws Exception { WhereClause wc = F.isNotBetween( TheInteger , 5 , 5 ); assertEquals( wcintNotEqual.toString() , wc.toString() ); assertEquals( wcintNotEqual.toSQL() , wc.toSQL() ); } public void test_isBetween() throws Exception { WhereClause wc = F.isBetween( TheString , "gas" , "kls" ); assertEquals( "Where \"gas\" <= TheStrAttr <= \"kls\"" , wc.toString() ); assertEquals( "WHERE TheStrCol BETWEEN 'gas' AND 'kls'" , wc.toSQL() ); } public void test_isNotBetween() throws Exception { WhereClause wc = F.isNotBetween( TheString , "gas" , "kls" ); assertEquals( "Where NOT (\"gas\" <= TheStrAttr <= \"kls\")" , wc.toString() ); assertEquals( "WHERE TheStrCol NOT BETWEEN 'gas' AND 'kls'" , wc.toSQL() ); } public void test_isBetween_int() throws Exception { WhereClause wc = F.isBetween( TheInteger , 5 , 6 ); assertEquals( "Where 5 <= TheIntAttr <= 6" , wc.toString() ); assertEquals( "WHERE TheIntCol BETWEEN 5 AND 6" , wc.toSQL() ); } public void test_isNotBetween_int() throws Exception { WhereClause wc = F.isNotBetween( TheInteger , 5 , 6 ); assertEquals( "Where NOT (5 <= TheIntAttr <= 6)" , wc.toString() ); assertEquals( "WHERE TheIntCol NOT BETWEEN 5 AND 6" , wc.toSQL() ); } public void test_endsWith_Null() throws Exception { try { F.endsWith( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_doesNotEndWith_Null() throws Exception { try { F.doesNotEndWith( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_endsWith() throws Exception { WhereClause wc = F.endsWith( TheString , "gas" ); assertEquals( "Where TheStrAttr EndsWith \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol LIKE '%gas'" , wc.toSQL() ); } public void test_doesNotEndWith() throws Exception { WhereClause wc = F.doesNotEndWith( TheString , "gas" ); assertEquals( "Where TheStrAttr Does NOT EndsWith \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol NOT LIKE '%gas'" , wc.toSQL() ); } public void test_startsWith_Null() throws Exception { try { F.startsWith( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_doesNotStartWith_Null() throws Exception { try { F.doesNotStartWith( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_startsWith() throws Exception { WhereClause wc = F.startsWith( TheString , "gas" ); assertEquals( "Where TheStrAttr StartsWith \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol LIKE 'gas%'" , wc.toSQL() ); } public void test_doesNotStartWith() throws Exception { WhereClause wc = F.doesNotStartWith( TheString , "gas" ); assertEquals( "Where TheStrAttr Does NOT StartsWith \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol NOT LIKE 'gas%'" , wc.toSQL() ); } public void test_contains_Null() throws Exception { try { F.contains( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_doesNotContain_Null() throws Exception { try { F.doesNotContain( TheString , null ); fail(); } catch ( IllegalArgumentException expected ) { } } public void test_contains() throws Exception { WhereClause wc = F.contains( TheString , "gas" ); assertEquals( "Where TheStrAttr Contains \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol LIKE '%gas%'" , wc.toSQL() ); } public void test_doesNotContain() throws Exception { WhereClause wc = F.doesNotContain( TheString , "gas" ); assertEquals( "Where TheStrAttr Does NOT Contains \"gas\"" , wc.toString() ); assertEquals( "WHERE TheStrCol NOT LIKE '%gas%'" , wc.toSQL() ); } public void test_and() throws Exception { WhereClause wc = F.and( wcStrEqual , wcIntEqual ); assertEquals( "Where (TheStrAttr == \"gas\") AND (TheIntAttr == 5)" , wc.toString() ); assertEquals( "WHERE (TheStrCol = 'gas') AND (TheIntCol = 5)" , wc.toSQL() ); } public void test_and_and() throws Exception { WhereClause wc = F.and( wcStrEqual , F.and( wcIntEqual , wcIntEqual2 ) ); assertEquals( "Where (TheStrAttr == \"gas\") AND (TheIntAttr == 5) AND (TheInt2Attr == 6)" , wc.toString() ); assertEquals( "WHERE (TheStrCol = 'gas') AND (TheIntCol = 5) AND (TheInt2Col = 6)" , wc.toSQL() ); } public void test_not_and() throws Exception { WhereClause wc = F.not( F.and( wcStrEqual , wcIntEqual ) ); assertEquals( "Where NOT ((TheStrAttr == \"gas\") AND (TheIntAttr == 5))" , wc.toString() ); assertEquals( "WHERE NOT ((TheStrCol = 'gas') AND (TheIntCol = 5))" , wc.toSQL() ); } public void test_or() throws Exception { WhereClause wc = F.or( wcStrEqual , wcIntEqual ); assertEquals( "Where (TheStrAttr == \"gas\") OR (TheIntAttr == 5)" , wc.toString() ); assertEquals( "WHERE (TheStrCol = 'gas') OR (TheIntCol = 5)" , wc.toSQL() ); } public void test_or_or() throws Exception { WhereClause wc = F.or( wcStrEqual , F.or( wcIntEqual , wcIntEqual2 ) ); assertEquals( "Where (TheStrAttr == \"gas\") OR (TheIntAttr == 5) OR (TheInt2Attr == 6)" , wc.toString() ); assertEquals( "WHERE (TheStrCol = 'gas') OR (TheIntCol = 5) OR (TheInt2Col = 6)" , wc.toSQL() ); } public void test_and_or() throws Exception { WhereClause wc = F.and( wcStrEqual , F.or( wcIntEqual , wcIntEqual2 ) ); assertEquals( "Where (TheStrAttr == \"gas\") AND ((TheIntAttr == 5) OR (TheInt2Attr == 6))" , wc.toString() ); assertEquals( "WHERE (TheStrCol = 'gas') AND ((TheIntCol = 5) OR (TheInt2Col = 6))" , wc.toSQL() ); } public void test_isIn() throws Exception { WhereClause wc = F.isIn( TheInteger2 , zSingleColumnSelect ); assertEquals( "Where TheInt2Attr IsIn (Select TheIntAttr From TheTable Where TheStrAttr != null)" , wc.toString() ); assertEquals( "WHERE TheInt2Col IN (SELECT TheIntCol FROM TheTable WHERE TheStrCol IS NOT NULL)" , wc.toSQL() ); } public void test_isNotIn() throws Exception { WhereClause wc = F.isNotIn( TheInteger2 , zSingleColumnSelect ); assertEquals( "Where TheInt2Attr IsNotIn (Select TheIntAttr From TheTable Where TheStrAttr != null)" , wc.toString() ); assertEquals( "WHERE TheInt2Col NOT IN (SELECT TheIntCol FROM TheTable WHERE TheStrCol IS NOT NULL)" , wc.toSQL() ); } }