3636import java .lang .reflect .Method ;
3737import java .sql .*;
3838import java .util .List ;
39+ import java .util .logging .Level ;
3940import java .util .logging .Logger ;
4041
4142import static com .ninja_squad .dbsetup .Operations .*;
@@ -55,72 +56,70 @@ public abstract class AbstractTest {
5556 @ Rule
5657 public TestName testNameRule = new TestName ();
5758
58- @ Autowired ( required = true )
59+ @ Autowired
5960 protected DataSource dataSource ;
6061
6162 protected final Source source = new Source ("jdbc:h2:mem:test" , "sa" , "" );
6263
63- private static final DbSetupTracker dbSetupTracker = new DbSetupTracker ();
64-
6564 private static final Operation DELETE_ALL = deleteAllFrom ("test2" , "test" , "interpretation" , "actor" , "movie" );
6665
6766 private static final Operation INSERT_MOVIE = sequenceOf (
68- insertInto ("movie" ).columns ("id" , "title" , "year" , "movie_imdb" )
69- .values (1 , "Alien" , 1979 , "30B443AE-C0C9-4790-9BEC-CE1380808435" )
70- .values (2 , "The Village" , 2004 , "16319617-AE95-4087-9264-D3D21BF611B6" )
71- .values (3 , "Avatar" , 2009 , "D735221B-5DE5-4112-AA1E-49090CB75ADA" ).build ());
67+ insertInto ("movie" ).columns ("id" , "title" , "year" , "movie_imdb" )
68+ .values (1 , "Alien" , 1979 , "30B443AE-C0C9-4790-9BEC-CE1380808435" )
69+ .values (2 , "The Village" , 2004 , "16319617-AE95-4087-9264-D3D21BF611B6" )
70+ .values (3 , "Avatar" , 2009 , "D735221B-5DE5-4112-AA1E-49090CB75ADA" ).build ());
7271
7372 private static final Operation INSERT_ACTOR = insertInto ("actor" )
74- .columns ("id" , "name" , "firstname" , "birth" , "actor_imdb" )
75- .values (1 , "Weaver" , "Sigourney" , Date .valueOf ("1949-10-08" ), "30B443AE-C0C9-4790-9BEC-CE1380808435" )
76- .values (2 , "Phoenix" , "Joaquim" , Date .valueOf ("1974-10-28" ), "16319617-AE95-4087-9264-D3D21BF611B6" )
77- .values (3 , "Worthington" , "Sam" , Date .valueOf ("1976-08-02" ), "D735221B-5DE5-4112-AA1E-49090CB75ADA" )
78- .build ();
73+ .columns ("id" , "name" , "firstname" , "birth" , "actor_imdb" )
74+ .values (1 , "Weaver" , "Sigourney" , Date .valueOf ("1949-10-08" ), "30B443AE-C0C9-4790-9BEC-CE1380808435" )
75+ .values (2 , "Phoenix" , "Joaquim" , Date .valueOf ("1974-10-28" ), "16319617-AE95-4087-9264-D3D21BF611B6" )
76+ .values (3 , "Worthington" , "Sam" , Date .valueOf ("1976-08-02" ), "D735221B-5DE5-4112-AA1E-49090CB75ADA" )
77+ .build ();
7978
8079 private static final Operation INSERT_INTERPRETATION = insertInto ("interpretation" )
81- .columns ("id" , "id_movie" , "id_actor" , "character" )
82- .values (1 , 1 , 1 , "Ellen Louise Ripley" )
83- .values (2 , 2 , 1 , "Alice Hunt" )
84- .values (3 , 3 , 1 , "Dr Grace Augustine" )
85- .values (4 , 2 , 2 , "Lucius Hunt" )
86- .values (5 , 3 , 3 , "Jake Sully" )
87- .build ();
80+ .columns ("id" , "id_movie" , "id_actor" , "character" )
81+ .values (1 , 1 , 1 , "Ellen Louise Ripley" )
82+ .values (2 , 2 , 1 , "Alice Hunt" )
83+ .values (3 , 3 , 1 , "Dr Grace Augustine" )
84+ .values (4 , 2 , 2 , "Lucius Hunt" )
85+ .values (5 , 3 , 3 , "Jake Sully" )
86+ .build ();
8887
8988 private static final Operation INSERT_TEST = insertInto ("test" )
90- .columns ("var1" , "var2" , "var3" , "var4" , "var5" , "var6" , "var7" , "var8" , "var9" , "var10" , "var11" , "var12" ,
91- "var13" , "var14" , "var15" , "var16" )
92- .values (1 , true , 2 , 3 , 4 , 5.6 , 7.8 , Time .valueOf ("09:46:30" ), Date .valueOf ("2014-05-24" ),
93- Timestamp .valueOf ("2014-05-24 09:46:30" ), new byte [0 ], "text" , 5 , 7 ,
94- "30B443AE-C0C9-4790-9BEC-CE1380808435" , 'T' )
89+ .columns ("var1" , "var2" , "var3" , "var4" , "var5" , "var6" , "var7" , "var8" , "var9" , "var10" , "var11" , "var12" ,
90+ "var13" , "var14" , "var15" , "var16" )
91+ .values (1 , true , 2 , 3 , 4 , 5.6 , 7.8 , Time .valueOf ("09:46:30" ), Date .valueOf ("2014-05-24" ),
92+ Timestamp .valueOf ("2014-05-24 09:46:30" ), new byte [0 ], "text" , 5 , 7 ,
93+ "30B443AE-C0C9-4790-9BEC-CE1380808435" , 'T' )
9594
96- .values (10 , false , 20 , 30 , 40 , 50.6 , 70.8 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
97- Timestamp .valueOf ("2014-05-30 12:29:49" ), new byte [0 ], "another text" , 50 , 70 ,
98- "0E2A1269-EFF0-4233-B87B-B53E8B6F164D" , 'e' )
95+ .values (10 , false , 20 , 30 , 40 , 50.6 , 70.8 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
96+ Timestamp .valueOf ("2014-05-30 12:29:49" ), new byte [0 ], "another text" , 50 , 70 ,
97+ "0E2A1269-EFF0-4233-B87B-B53E8B6F164D" , 'e' )
9998
100- .values (100 , false , 25 , 300 , 400 , 500.6 , 700.8 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
101- Timestamp .valueOf ("2014-05-30 00:00:00" ), new byte [0 ], "another text again" , 500 , 700 ,
102- "2B0D1BDD-909E-4362-BA10-C930BA82718D" , 's' )
99+ .values (100 , false , 25 , 300 , 400 , 500.6 , 700.8 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
100+ Timestamp .valueOf ("2014-05-30 00:00:00" ), new byte [0 ], "another text again" , 500 , 700 ,
101+ "2B0D1BDD-909E-4362-BA10-C930BA82718D" , 's' )
103102
104- .values (1000 , false , 0 , 0 , 0 , 0 , 0 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
105- Timestamp .valueOf ("2014-05-30 00:00:00" ), new byte [0 ], "another text again" , 500 , 700 ,
106- "399FFFCA-7874-4225-9903-E227C4E9DCC1" , 't' )
107- .build ();
103+ .values (1000 , false , 0 , 0 , 0 , 0 , 0 , Time .valueOf ("12:29:49" ), Date .valueOf ("2014-05-30" ),
104+ Timestamp .valueOf ("2014-05-30 00:00:00" ), new byte [0 ], "another text again" , 500 , 700 ,
105+ "399FFFCA-7874-4225-9903-E227C4E9DCC1" , 't' )
106+ .build ();
108107
109108 private static final Operation INSERT_TEST2 = insertInto ("test2" )
110- .columns ("var1" , "var2" , "var3" , "var4" , "var5" , "var6" , "var7" , "var8" , "var9" , "var10" , "var11" , "var12" ,
111- "var13" , "var14" , "var15" , "var16" , "var17" )
112- .values (1 , true , 2 , 3 , 4 , 5.6 , 7.8 , Time .valueOf ("09:46:30" ), Date .valueOf ("2014-05-24" ),
113- Timestamp .valueOf ("2014-05-24 09:46:30" ), new byte [0 ], "text" , 5 , 7 , null ,
114- "30B443AE-C0C9-4790-9BEC-CE1380808435" , 'T' )
115- .values (null , null , null , null , null , null , null , null , null , null , null , null , null , null , null , null , null )
116- .build ();
109+ .columns ("var1" , "var2" , "var3" , "var4" , "var5" , "var6" , "var7" , "var8" , "var9" , "var10" , "var11" , "var12" ,
110+ "var13" , "var14" , "var15" , "var16" , "var17" )
111+ .values (1 , true , 2 , 3 , 4 , 5.6 , 7.8 , Time .valueOf ("09:46:30" ), Date .valueOf ("2014-05-24" ),
112+ Timestamp .valueOf ("2014-05-24 09:46:30" ), new byte [0 ], "text" , 5 , 7 , null ,
113+ "30B443AE-C0C9-4790-9BEC-CE1380808435" , 'T' )
114+ .values (null , null , null , null , null , null , null , null , null , null , null , null , null , null , null , null , null )
115+ .build ();
117116
118117 private static final Operation SQL = sql (
119- "update test set var11 = FILE_READ('classpath:h2-logo-2.png') where var1 = 1" ,
120- "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 10" ,
121- "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 100" ,
122- "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 1000" ,
123- "update test2 set var11 = FILE_READ('classpath:h2-logo-2.png') where var1 = 1"
118+ "update test set var11 = FILE_READ('classpath:h2-logo-2.png') where var1 = 1" ,
119+ "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 10" ,
120+ "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 100" ,
121+ "update test set var11 = FILE_READ('classpath:logo-dev.jpg') where var1 = 1000" ,
122+ "update test2 set var11 = FILE_READ('classpath:h2-logo-2.png') where var1 = 1"
124123 );
125124
126125 private static final Operation OPERATIONS = sequenceOf (DELETE_ALL , INSERT_MOVIE , INSERT_ACTOR , INSERT_INTERPRETATION ,
@@ -129,22 +128,11 @@ public abstract class AbstractTest {
129128 private static final DbSetup DB_SETUP = new DbSetup (new DriverManagerDestination ("jdbc:h2:mem:test" , "SA" , "" ),
130129 OPERATIONS );
131130
131+ private static final DbSetupTracker DB_SETUP_TRACKER = new DbSetupTracker ();
132+
132133 @ Before
133- public void initiate () throws IllegalArgumentException , IllegalAccessException , NoSuchFieldException ,
134- SecurityException {
135-
136- Field fieldLastSetup = DbSetupTracker .class .getDeclaredField ("lastSetupLaunched" );
137- Field fieldNextLaunch = DbSetupTracker .class .getDeclaredField ("nextLaunchSkipped" );
138- fieldLastSetup .setAccessible (true );
139- fieldNextLaunch .setAccessible (true );
140- Boolean nextLaunchSkipped = fieldNextLaunch .getBoolean (dbSetupTracker );
141- DbSetup lastSetupLaunched = (DbSetup ) fieldLastSetup .get (dbSetupTracker );
142- boolean skipLaunch = nextLaunchSkipped && DB_SETUP .equals (lastSetupLaunched );
143- LOG .warning ("--------------------------------------------------" );
144- LOG .warning (getClass ().getCanonicalName () + " - " + testNameRule .getMethodName () + " - skipLaunch : " + skipLaunch
145- + " (" + nextLaunchSkipped + " && " + DB_SETUP .equals (lastSetupLaunched ) + ")" );
146- LOG .warning ("--------------------------------------------------" );
147- dbSetupTracker .launchIfNecessary (DB_SETUP );
134+ public void initiate () {
135+ DB_SETUP_TRACKER .launchIfNecessary (DB_SETUP );
148136 }
149137
150138 @ After
@@ -159,7 +147,7 @@ public void determineIfReloadIsNeeded() throws NoSuchMethodException, SecurityEx
159147 if (methodAnnotation != null ) {
160148 return ;
161149 }
162- dbSetupTracker .skipNextLaunch ();
150+ DB_SETUP_TRACKER .skipNextLaunch ();
163151 }
164152
165153 /**
@@ -170,8 +158,7 @@ public void determineIfReloadIsNeeded() throws NoSuchMethodException, SecurityEx
170158 * @return An instance.
171159 * @throws Exception Exception
172160 */
173- protected static Column getColumn (String columnName , List <Value > valuesList )
174- throws Exception {
161+ protected static Column getColumn (String columnName , List <Value > valuesList ) throws Exception {
175162 Constructor <Column > constructor = Column .class .getDeclaredConstructor (String .class , List .class , LetterCase .class );
176163 constructor .setAccessible (true );
177164 return constructor .newInstance (columnName , valuesList , LetterCase .COLUMN_DEFAULT );
@@ -187,10 +174,12 @@ protected static Column getColumn(String columnName, List<Value> valuesList)
187174 * @throws Exception Exception
188175 */
189176 protected static Row getRow (List <String > pksNameList , List <String > columnsNameList , List <Value > valuesList )
190- throws Exception {
191- Constructor <Row > constructor = Row .class .getDeclaredConstructor (List .class , List .class , List .class , LetterCase .class , LetterCase .class );
177+ throws Exception {
178+ Constructor <Row > constructor = Row .class
179+ .getDeclaredConstructor (List .class , List .class , List .class , LetterCase .class , LetterCase .class );
192180 constructor .setAccessible (true );
193- return constructor .newInstance (pksNameList , columnsNameList , valuesList , LetterCase .COLUMN_DEFAULT , LetterCase .PRIMARY_KEY_DEFAULT );
181+ return constructor
182+ .newInstance (pksNameList , columnsNameList , valuesList , LetterCase .COLUMN_DEFAULT , LetterCase .PRIMARY_KEY_DEFAULT );
194183 }
195184
196185 /**
@@ -203,7 +192,7 @@ protected static Row getRow(List<String> pksNameList, List<String> columnsNameLi
203192 * @throws Exception Exception
204193 */
205194 protected static Table getTable (List <String > columnsNameList , List <String > pksNameList , List <Row > rowsList )
206- throws Exception {
195+ throws Exception {
207196 Constructor <Table > constructor = Table .class .getDeclaredConstructor ();
208197 constructor .setAccessible (true );
209198 Table table = constructor .newInstance ();
@@ -232,7 +221,7 @@ protected static Table getTable(List<String> columnsNameList, List<String> pksNa
232221 * @throws Exception Exception
233222 */
234223 protected static Request getRequest (List <String > columnsNameList , List <String > pksNameList , List <Row > rowsList )
235- throws Exception {
224+ throws Exception {
236225 Constructor <Request > constructor = Request .class .getDeclaredConstructor ();
237226 constructor .setAccessible (true );
238227 Request request = constructor .newInstance ();
@@ -259,7 +248,7 @@ protected static Request getRequest(List<String> columnsNameList, List<String> p
259248 * @return An instance.
260249 * @throws Exception Exception
261250 */
262- protected static Value getValue (String columnName , Object object )throws Exception {
251+ protected static Value getValue (String columnName , Object object ) throws Exception {
263252 Constructor <Value > constructor = Value .class .getDeclaredConstructor (String .class , Object .class , LetterCase .class );
264253 constructor .setAccessible (true );
265254 Value value = constructor .newInstance (columnName , object , LetterCase .COLUMN_DEFAULT );
@@ -296,10 +285,10 @@ protected static Changes getChanges(List<Change> changesList) throws Exception {
296285 */
297286 protected static Change getChange (DataType dataType , String dataName , ChangeType changeType , Row rowAtStartPoint ,
298287 Row rowAtEndPoint )
299- throws Exception {
288+ throws Exception {
300289 Constructor <Change > constructor = Change .class
301- .getDeclaredConstructor (DataType .class , String .class , ChangeType .class , Row .class , Row .class ,
302- LetterCase .class , LetterCase .class , LetterCase .class );
290+ .getDeclaredConstructor (DataType .class , String .class , ChangeType .class , Row .class , Row .class ,
291+ LetterCase .class , LetterCase .class , LetterCase .class );
303292 constructor .setAccessible (true );
304293 return constructor .newInstance (dataType , dataName , changeType , rowAtStartPoint , rowAtEndPoint ,
305294 LetterCase .TABLE_DEFAULT , LetterCase .COLUMN_DEFAULT , LetterCase .PRIMARY_KEY_DEFAULT );
@@ -327,7 +316,7 @@ protected static Change getTableCreationChange(String dataName, Row rowAtEndPoin
327316 * @throws Exception Exception
328317 */
329318 protected static Change getTableModificationChange (String dataName , Row rowAtStartPoint , Row rowAtEndPoint )
330- throws Exception {
319+ throws Exception {
331320 return getChange (DataType .TABLE , dataName , ChangeType .MODIFICATION , rowAtStartPoint , rowAtEndPoint );
332321 }
333322
@@ -358,7 +347,7 @@ protected void update(String request, Object... parameters) {
358347 statement .executeUpdate ();
359348 }
360349 } catch (Exception e ) {
361- e . printStackTrace ( );
350+ LOG . log ( Level . SEVERE , "Cannot apply update SQL query" , e );
362351 }
363352 }
364353
@@ -367,7 +356,8 @@ protected void update(String request, Object... parameters) {
367356 */
368357 protected void updateChangesForTests () {
369358 update ("insert into movie values(4, 'Ghostbusters', 1984, '30B443AE-C0C9-4790-9BEC-CE1380808435')" );
370- update ("insert into actor values(4, 'Murray', 'Bill', PARSEDATETIME('21/09/1950', 'dd/MM/yyyy'), '30B443AE-C0C9-4790-9BEC-CE1380808435')" );
359+ update (
360+ "insert into actor values(4, 'Murray', 'Bill', PARSEDATETIME('21/09/1950', 'dd/MM/yyyy'), '30B443AE-C0C9-4790-9BEC-CE1380808435')" );
371361 update ("insert into interpretation values(6, 4, 4, 'Dr Peter Venkman')" );
372362
373363 update ("delete from interpretation where id = 5" );
0 commit comments