View Javadoc
1   package test.integration;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.sql.Connection;
6   import java.sql.Statement;
7   
8   import org.junit.After;
9   import org.junit.Before;
10  
11  import de.fhdw.wtf.context.core.ApplicationContainer;
12  import de.fhdw.wtf.context.core.ApplicationStarter;
13  import de.fhdw.wtf.persistence.exception.PersistenceException;
14  import de.fhdw.wtf.persistence.facade.ClassFacade;
15  import de.fhdw.wtf.persistence.facade.DatabaseManager;
16  import de.fhdw.wtf.persistence.facade.IDManager;
17  import de.fhdw.wtf.persistence.facade.ObjectFacade;
18  import de.fhdw.wtf.persistence.facade.OracleDataBasePreparator;
19  import de.fhdw.wtf.persistence.facade.OracleDatabaseManager;
20  import de.fhdw.wtf.persistence.facade.OracleObjectFacadeImplementation;
21  import de.fhdw.wtf.persistence.meta.IntegerType;
22  import de.fhdw.wtf.persistence.meta.MapAssociation;
23  import de.fhdw.wtf.persistence.meta.StringType;
24  import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
25  import de.fhdw.wtf.persistence.meta.UserType;
26  import de.fhdw.wtf.persistence.utils.PropertiesReaderFactory;
27  
28  public class TestBaseWithDB extends TestBase {
29  	
30  	private ClassFacade classFacade;
31  	private ObjectFacade objectFacade;
32  	
33  	private UserType personService;
34  	private UserType hausService;
35  	
36  	private UserType anyType;
37  	
38  	private UserType mutableList;
39  	private UserType mutableMap;
40  	private UserType persistentList;
41  	private UserType persistentMap;
42  	
43  	private UserType account;
44  	private UserType accountNumber;
45  	private UserType bankidentifier;
46  	private UserType city;
47  	private UserType country;
48  	private UserType globalTelefonbuch;
49  	private UserType ibanFinder;
50  	private UserType kommunikationskanal;
51  	private UserType natuerlichePerson;
52  	private UserType personGroups;
53  	private UserType postfach;
54  	private UserType telefon;
55  	private UserType telefonbuch;
56  	private UserType telefonbuchMitPersonAlsKey;
57  	private UserType telefonbuecher;
58  	private UserType haus;
59  	
60  	private UnidirectionalAssociation mutableSetLinks;
61  	private MapAssociation mutableMapLinks;
62  	
63  	private UnidirectionalAssociation accountNumberId;
64  	private UnidirectionalAssociation bankidentifierId;
65  	private UnidirectionalAssociation cityName;
66  	private UnidirectionalAssociation countryName;
67  	private UnidirectionalAssociation globalTelefonbuchTelefonbuchMapsPerCity;
68  	private UnidirectionalAssociation ibanFinderAccountTree;
69  	private UnidirectionalAssociation natuerlichePersonName;
70  	private UnidirectionalAssociation natuerlichePersonKanal;
71  	private UnidirectionalAssociation personGroupsPersonGroups;
72  	private UnidirectionalAssociation postfachStadt;
73  	private UnidirectionalAssociation postfachNr;
74  	private UnidirectionalAssociation telefonNr;
75  	private UnidirectionalAssociation telefonbuchEintraege;
76  	private UnidirectionalAssociation telefonbuchMitPersonAlsKeyEintraege;
77  	private UnidirectionalAssociation telefonbuecherEintraege;
78  	private UnidirectionalAssociation wohnhaft;
79  	private UnidirectionalAssociation hausNummer;
80  	
81  	public ClassFacade getClassFacade() {
82  		return this.classFacade;
83  	}
84  	
85  	public ObjectFacade getObjectFacade() {
86  		return this.objectFacade;
87  	}
88  	
89  	/**
90  	 * Initializes userTypes, specializations and associations on classFacade.
91  	 * 
92  	 * @throws PersistenceException
93  	 */
94  	protected void initializeTypesAndSpecializationAndAssociations() throws PersistenceException {
95  		// types
96  		this.personService =
97  				this.classFacade.createUserType("generated.model.de>fhdw>partner>service>PersonService", false, true);
98  		this.hausService =
99  				this.classFacade.createUserType("generated.model.de>fhdw>partner>service>HausService", false, true);
100 		
101 		this.anyType = this.classFacade.createUserType("AnyType", true, false);
102 		
103 		this.account = this.classFacade.createUserType("generated.model.de>fhdw>partner>Account", false, false);
104 		this.accountNumber =
105 				this.classFacade.createUserType("generated.model.de>fhdw>partner>AccountNumber", false, false);
106 		this.bankidentifier =
107 				this.classFacade.createUserType("generated.model.de>fhdw>partner>Bankidentifier", false, false);
108 		this.city = this.classFacade.createUserType("generated.model.de>fhdw>partner>City", false, false);
109 		this.country = this.classFacade.createUserType("generated.model.de>fhdw>partner>Country", false, false);
110 		this.globalTelefonbuch =
111 				this.classFacade.createUserType("generated.model.de>fhdw>partner>GlobalTelefonbuch", false, false);
112 		this.ibanFinder = this.classFacade.createUserType("generated.model.de>fhdw>partner>IBAN_Finder", false, false);
113 		this.kommunikationskanal =
114 				this.classFacade.createUserType("generated.model.de.fhdw.partner.Kommunikationskanal", true, false);
115 		this.natuerlichePerson =
116 				this.classFacade.createUserType("generated.model.de>fhdw>partner>NatuerlichePerson", false, false);
117 		this.personGroups =
118 				this.classFacade.createUserType("generated.model.de>fhdw>partner>PersonGroups", false, false);
119 		this.postfach = this.classFacade.createUserType("generated.model.de>fhdw>partner>Postfach", false, false);
120 		this.telefon = this.classFacade.createUserType("generated.model.de>fhdw>partner>Telefon", false, false);
121 		this.telefonbuch = this.classFacade.createUserType("generated.model.de>fhdw>partner>Telefonbuch", false, false);
122 		this.telefonbuchMitPersonAlsKey =
123 				this.classFacade.createUserType(
124 						"generated.model.de>fhdw>partner>TelefonbuchMitPersonAlsKey",
125 						false,
126 						false);
127 		this.telefonbuecher =
128 				this.classFacade.createUserType("generated.model.de>fhdw>partner>Telefonbuecher", false, false);
129 		this.haus = this.classFacade.createUserType("generated.model.de>fhdw>partner>Haus", false, false);
130 		
131 		// collection-types
132 		this.mutableList =
133 				this.classFacade.createUserType("de.fhdw.wtf.context.model.collections.MutableList", true, false);
134 		this.persistentList =
135 				this.classFacade.createUserType("de.fhdw.wtf.context.model.collections.PersistentList", false, false);
136 		this.mutableMap =
137 				this.classFacade.createUserType("de.fhdw.wtf.context.model.collections.MutableMap", true, false);
138 		this.persistentMap =
139 				this.classFacade.createUserType("de.fhdw.wtf.context.model.collections.PersistentMap", false, false);
140 		
141 		// specializations
142 		this.classFacade.createSpecializationBetween(this.kommunikationskanal, this.postfach);
143 		this.classFacade.createSpecializationBetween(this.kommunikationskanal, this.telefon);
144 		
145 		this.classFacade.createSpecializationBetween(this.mutableList, this.persistentList);
146 		this.classFacade.createSpecializationBetween(this.mutableMap, this.persistentMap);
147 		
148 		// anyType extension
149 		this.classFacade.createSpecializationBetween(this.anyType, this.mutableList);
150 		this.classFacade.createSpecializationBetween(this.anyType, this.mutableMap);
151 		
152 		this.classFacade.createSpecializationBetween(this.anyType, this.account);
153 		this.classFacade.createSpecializationBetween(this.anyType, this.accountNumber);
154 		this.classFacade.createSpecializationBetween(this.anyType, this.bankidentifier);
155 		this.classFacade.createSpecializationBetween(this.anyType, this.city);
156 		this.classFacade.createSpecializationBetween(this.anyType, this.country);
157 		this.classFacade.createSpecializationBetween(this.anyType, this.globalTelefonbuch);
158 		this.classFacade.createSpecializationBetween(this.anyType, this.ibanFinder);
159 		this.classFacade.createSpecializationBetween(this.anyType, this.kommunikationskanal);
160 		this.classFacade.createSpecializationBetween(this.anyType, this.natuerlichePerson);
161 		this.classFacade.createSpecializationBetween(this.anyType, this.personGroups);
162 		this.classFacade.createSpecializationBetween(this.anyType, this.telefonbuch);
163 		this.classFacade.createSpecializationBetween(this.anyType, this.telefonbuchMitPersonAlsKey);
164 		this.classFacade.createSpecializationBetween(this.anyType, this.telefonbuecher);
165 		this.classFacade.createSpecializationBetween(this.anyType, this.haus);
166 		// associations
167 		this.mutableSetLinks =
168 				this.classFacade.createUnidirectionalAssociation(
169 						"links",
170 						false,
171 						false,
172 						this.persistentList,
173 						this.anyType);
174 		this.mutableMapLinks =
175 				this.classFacade.createMapAssociation("links", false, this.persistentMap, this.anyType, this.anyType);
176 		
177 		this.accountNumberId =
178 				this.classFacade.createUnidirectionalAssociation(
179 						"id",
180 						true,
181 						true,
182 						this.accountNumber,
183 						IntegerType.getInstance());
184 		
185 		this.bankidentifierId =
186 				this.classFacade.createUnidirectionalAssociation(
187 						"id",
188 						true,
189 						true,
190 						this.bankidentifier,
191 						IntegerType.getInstance());
192 		
193 		this.cityName =
194 				this.classFacade.createUnidirectionalAssociation(
195 						"name",
196 						true,
197 						true,
198 						this.city,
199 						StringType.getInstance());
200 		
201 		this.countryName =
202 				this.classFacade.createUnidirectionalAssociation(
203 						"name",
204 						true,
205 						true,
206 						this.country,
207 						StringType.getInstance());
208 		
209 		this.globalTelefonbuchTelefonbuchMapsPerCity =
210 				this.classFacade.createUnidirectionalAssociation(
211 						"telefonbuchMapsPerCity",
212 						false,
213 						true,
214 						this.globalTelefonbuch,
215 						this.mutableMap);
216 		
217 		this.ibanFinderAccountTree =
218 				this.classFacade.createUnidirectionalAssociation(
219 						"accountTree",
220 						false,
221 						true,
222 						this.ibanFinder,
223 						this.mutableMap);
224 		
225 		this.natuerlichePersonKanal =
226 				this.classFacade.createUnidirectionalAssociation(
227 						"kanal",
228 						false,
229 						true,
230 						this.natuerlichePerson,
231 						this.mutableList);
232 		this.natuerlichePersonName =
233 				this.classFacade.createUnidirectionalAssociation(
234 						"name",
235 						true,
236 						true,
237 						this.natuerlichePerson,
238 						StringType.getInstance());
239 		
240 		this.personGroupsPersonGroups =
241 				this.classFacade.createUnidirectionalAssociation(
242 						"personGroups",
243 						false,
244 						true,
245 						this.personGroups,
246 						this.mutableList);
247 		
248 		this.postfachStadt =
249 				this.classFacade.createUnidirectionalAssociation(
250 						"stadt",
251 						true,
252 						true,
253 						this.postfach,
254 						StringType.getInstance());
255 		this.postfachNr =
256 				this.classFacade.createUnidirectionalAssociation(
257 						"nr",
258 						true,
259 						true,
260 						this.postfach,
261 						IntegerType.getInstance());
262 		
263 		this.telefonNr =
264 				this.classFacade.createUnidirectionalAssociation(
265 						"nr",
266 						true,
267 						true,
268 						this.telefon,
269 						IntegerType.getInstance());
270 		
271 		this.telefonbuchEintraege =
272 				this.classFacade.createUnidirectionalAssociation(
273 						"eintraege",
274 						false,
275 						true,
276 						this.telefonbuch,
277 						this.mutableMap);
278 		
279 		this.telefonbuchMitPersonAlsKeyEintraege =
280 				this.classFacade.createUnidirectionalAssociation(
281 						"eintraege",
282 						false,
283 						true,
284 						this.telefonbuchMitPersonAlsKey,
285 						this.mutableMap);
286 		
287 		this.telefonbuecherEintraege =
288 				this.classFacade.createUnidirectionalAssociation(
289 						"eintraege",
290 						false,
291 						true,
292 						this.telefonbuecher,
293 						this.mutableList);
294 		
295 		this.wohnhaft =
296 				this.classFacade.createUnidirectionalAssociation(
297 						"wohnhaft",
298 						false,
299 						true,
300 						this.natuerlichePerson,
301 						this.haus);
302 		this.hausNummer =
303 				this.classFacade.createUnidirectionalAssociation(
304 						"hausNummer",
305 						false,
306 						true,
307 						this.haus,
308 						StringType.getInstance());
309 	}
310 	
311 	@Override
312 	@Before
313 	public void setUp() throws Exception {
314 		super.setUp();
315 		
316 		final OracleDatabaseManager manager = OracleDatabaseManager.getInstance();
317 		manager.setConnectionConstantsFromFile(PropertiesReaderFactory.getInstance().getPropertiesReaderOracle());
318 		manager.connect();
319 		this.resetDatabase(manager);
320 		this.resetIDManager();
321 		this.classFacade = manager.getClassFacade();
322 		this.classFacade.initialize();
323 		
324 		this.initializeTypesAndSpecializationAndAssociations();
325 		
326 		this.classFacade.finalizeSpecialization();
327 		manager.disconnect();
328 		
329 		final ApplicationStarter starter = this.getApplicationStarter();
330 		// register Type factories?
331 		starter.startStandalone();
332 		
333 		this.objectFacade = ApplicationContainer.getInstance().getDatabaseManager().getObjectFacade();
334 	}
335 	
336 	@Override
337 	@After
338 	public void tearDown() throws Exception {
339 		final Connection connection = OracleDatabaseManager.getInstance().getConnection();
340 		final Statement statement = connection.createStatement();
341 		statement.addBatch("delete from Accessed");
342 		statement.addBatch("delete from String_Value");
343 		statement.addBatch("delete from Integer_value");
344 		statement.addBatch("delete from maplink");
345 		statement.addBatch("delete from link");
346 		statement.addBatch("delete from user_object");
347 		statement.addBatch("delete from object");
348 		statement.addBatch("delete from short_transaction");
349 		statement.addBatch("delete from long_transaction");
350 		statement.executeBatch();
351 		statement.close();
352 		this.classFacade.clear();
353 		OracleDatabaseManager.getInstance().disconnect();
354 	}
355 	
356 	private void resetDatabase(final DatabaseManager database) throws PersistenceException, IOException {
357 		final OracleDataBasePreparator prep = new OracleDataBasePreparator();
358 		final boolean tableStructureValid = prep.isTableStructureValid();
359 		if (tableStructureValid) {
360 			this.clearTables(database);
361 		} else {
362 			prep.dropWholeSchema();
363 			prep.createWholeSchema();
364 		}
365 		final boolean areProceduresCreated = prep.areProceduresCreated();
366 		if (!areProceduresCreated) {
367 			prep.createProcedures();
368 		}
369 	}
370 	
371 	private void resetIDManager() {
372 		final File filePersistAsoc = new File(IDManager.ASSOCIATION_IDS_FILENAME);
373 		final File filePersistType = new File(IDManager.TYPE_IDS_FILENAME);
374 		
375 		if (filePersistAsoc.exists()) {
376 			filePersistAsoc.delete();
377 		}
378 		
379 		if (filePersistType.exists()) {
380 			filePersistType.delete();
381 		}
382 		
383 		IDManager.instance().clearInformation();
384 	}
385 	
386 	/**
387 	 * Clears all database tables.
388 	 * 
389 	 * @throws PersistenceException
390 	 *             access to database failed
391 	 */
392 	private void clearTables(final DatabaseManager database) throws PersistenceException {
393 		new OracleObjectFacadeImplementation((OracleDatabaseManager) database, null).clear();
394 		database.getClassFacade().clear();
395 	}
396 }