1818
1919import java .sql .ResultSet ;
2020import java .sql .SQLException ;
21+ import java .util .ArrayList ;
22+ import java .util .Collection ;
23+ import java .util .HashMap ;
24+ import java .util .Map ;
2125
2226import org .junit .Assert ;
2327import org .junit .Before ;
@@ -36,6 +40,7 @@ public class GenericDaoBaseTest {
3640 @ Mock
3741 SQLException mockedSQLException ;
3842
43+ private static final DbTestDao dbTestDao = new DbTestDao ();
3944 private static final String INTEGRITY_CONSTRAINT_VIOLATION = "23000" ;
4045 private static final int DUPLICATE_ENTRY_ERRO_CODE = 1062 ;
4146
@@ -214,4 +219,51 @@ public void checkCountOfRecordsAgainstTheResultSetSizeTestCountSmallerThanResult
214219
215220 Assert .assertEquals (resultSetSize , result );
216221 }
222+
223+ @ Test
224+ public void addJoinsTest () {
225+ StringBuilder joinString = new StringBuilder ();
226+ Collection <JoinBuilder <SearchCriteria <?>>> joins = new ArrayList <>();
227+
228+ Attribute attr1 = new Attribute ("table1" , "column1" );
229+ Attribute attr2 = new Attribute ("table2" , "column2" );
230+ Attribute attr3 = new Attribute ("table3" , "column1" );
231+ Attribute attr4 = new Attribute ("table4" , "column2" );
232+
233+ joins .add (new JoinBuilder <>(dbTestDao .createSearchCriteria (), attr1 , attr2 , JoinBuilder .JoinType .INNER ));
234+ joins .add (new JoinBuilder <>(dbTestDao .createSearchCriteria (), attr3 , attr4 , JoinBuilder .JoinType .INNER ));
235+ dbTestDao .addJoins (joinString , joins );
236+
237+ Assert .assertEquals (" INNER JOIN table2 ON table1.column1=table2.column2 INNER JOIN table4 ON table3.column1=table4.column2 " , joinString .toString ());
238+ }
239+
240+ @ Test
241+ public void multiJoinSameTableTest () {
242+ StringBuilder joinString = new StringBuilder ();
243+ Collection <JoinBuilder <SearchCriteria <?>>> joins = new ArrayList <>();
244+
245+ Attribute tAc1 = new Attribute ("tableA" , "column1" );
246+ Attribute tAc2 = new Attribute ("tableA" , "column2" );
247+ Attribute tAc3 = new Attribute ("tableA" , "column3" );
248+ Attribute tBc2 = new Attribute ("tableB" , "column2" );
249+ Attribute tCc3 = new Attribute ("tableC" , "column3" );
250+ Attribute tDc4 = new Attribute ("tableD" , "column4" );
251+
252+ joins .add (new JoinBuilder <>(dbTestDao .createSearchCriteria (), tBc2 , tAc1 , JoinBuilder .JoinType .INNER ));
253+ joins .add (new JoinBuilder <>(dbTestDao .createSearchCriteria (), tCc3 , tAc2 , JoinBuilder .JoinType .INNER ));
254+ joins .add (new JoinBuilder <>(dbTestDao .createSearchCriteria (), tDc4 , tAc3 , JoinBuilder .JoinType .INNER ));
255+ dbTestDao .addJoins (joinString , joins );
256+
257+ Assert .assertEquals (" INNER JOIN tableA ON tableB.column2=tableA.column1 INNER JOIN tableA tableA1 ON tableC.column3=tableA1.column2 INNER JOIN tableA tableA2 ON tableD.column4=tableA2.column3 " , joinString .toString ());
258+ }
259+
260+ @ Test
261+ public void findNextTableNameTest () {
262+ Map <String , Integer > usedTables = new HashMap <>();
263+
264+ Assert .assertEquals ("tableA" , GenericDaoBase .findNextJoinTableName ("tableA" , usedTables ));
265+ Assert .assertEquals ("tableA1" , GenericDaoBase .findNextJoinTableName ("tableA" , usedTables ));
266+ Assert .assertEquals ("tableA2" , GenericDaoBase .findNextJoinTableName ("tableA" , usedTables ));
267+ Assert .assertEquals ("tableA3" , GenericDaoBase .findNextJoinTableName ("tableA" , usedTables ));
268+ }
217269}
0 commit comments