001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.entity.cmr;
017:
018: import org.apache.openejb.test.entity.cmr.manytomany.GameLocal;
019: import org.apache.openejb.test.entity.cmr.manytomany.GameLocalHome;
020: import org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal;
021: import org.apache.openejb.test.entity.cmr.manytomany.PlatformLocalHome;
022: import org.apache.openejb.test.entity.cmr.manytomany.GamePk;
023: import org.apache.openejb.test.entity.cmr.manytomany.PlatformPk;
024:
025: import javax.ejb.CreateException;
026: import javax.ejb.FinderException;
027: import javax.ejb.TransactionRolledbackLocalException;
028: import java.sql.Connection;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Set;
035: import java.util.Arrays;
036: import java.util.ConcurrentModificationException;
037:
038: /**
039: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
040: */
041: public class ManyToManyComplexPkTests extends AbstractCMRTest {
042: private PlatformLocalHome platformLocalHome;
043: private GameLocalHome gameLocalHome;
044:
045: public ManyToManyComplexPkTests() {
046: super ("ManyToManyComplex.");
047: }
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051:
052: platformLocalHome = (PlatformLocalHome) initialContext
053: .lookup("client/tests/entity/cmr/manyToMany/ComplexPlatformLocal");
054: gameLocalHome = (GameLocalHome) initialContext
055: .lookup("client/tests/entity/cmr/manyToMany/ComplexGameLocal");
056: }
057:
058: public void testAGetBExistingAB() throws Exception {
059: resetDB();
060: beginTransaction();
061: try {
062: PlatformLocal platform = findPlatform(new Integer(1));
063: Set<GameLocal> gameSets = platform.getGames();
064: assertEquals(2, gameSets.size());
065: for (Iterator iter = gameSets.iterator(); iter.hasNext();) {
066: GameLocal game = (GameLocal) iter.next();
067: if (game.getId().equals(new Integer(11))) {
068: assertEquals("value11", game.getName());
069: } else if (game.getId().equals(new Integer(22))) {
070: assertEquals("value22", game.getName());
071: } else {
072: fail();
073: }
074: }
075: } finally {
076: completeTransaction();
077: }
078: }
079:
080: public void testSetCmrNull() throws Exception {
081: resetDB();
082: beginTransaction();
083: try {
084: PlatformLocal platform = findPlatform(new Integer(1));
085: try {
086: platform.setGames(null);
087: fail("expected platform.setGames(null) to throw an IllegalArgumentException");
088: } catch (TransactionRolledbackLocalException e) {
089: Throwable cause = e.getCause();
090: assertNotNull("cause is null", cause);
091: assertTrue(
092: "cause is not a instance of IllegalArgumentException",
093: cause instanceof IllegalArgumentException);
094: }
095: } finally {
096: completeTransaction();
097: }
098: }
099:
100: public void testBGetAExistingAB() throws Exception {
101: resetDB();
102: beginTransaction();
103: try {
104: GameLocal game = findGame(new Integer(22));
105: Set aSet = game.getPlatforms();
106: assertEquals(3, aSet.size());
107: for (Iterator iter = aSet.iterator(); iter.hasNext();) {
108: PlatformLocal platform = (PlatformLocal) iter.next();
109: if (platform.getId().equals(new Integer(1))) {
110: assertEquals("value1", platform.getName());
111: } else if (platform.getId().equals(new Integer(2))) {
112: assertEquals("value2", platform.getName());
113: } else if (platform.getId().equals(new Integer(3))) {
114: assertEquals("value3", platform.getName());
115: } else {
116: fail();
117: }
118: }
119: } finally {
120: completeTransaction();
121: }
122: }
123:
124: public void testASetBDropExisting() throws Exception {
125: resetDB();
126: beginTransaction();
127: try {
128: PlatformLocal platform = findPlatform(new Integer(1));
129: platform.setGames(new HashSet<GameLocal>());
130: platform = findPlatform(new Integer(2));
131: platform.setGames(new HashSet<GameLocal>());
132: platform = findPlatform(new Integer(3));
133: platform.setGames(new HashSet<GameLocal>());
134: } finally {
135: completeTransaction();
136: }
137:
138: assertAllUnlinked();
139: }
140:
141: public void testBSetADropExisting() throws Exception {
142: resetDB();
143: beginTransaction();
144: try {
145: GameLocal game = findGame(new Integer(11));
146: game.setPlatforms(new HashSet<PlatformLocal>());
147: game = findGame(new Integer(22));
148: game.setPlatforms(new HashSet<PlatformLocal>());
149: } finally {
150: completeTransaction();
151: }
152:
153: assertAllUnlinked();
154: }
155:
156: public void testASetBNewAB() throws Exception {
157: resetDB();
158: beginTransaction();
159: try {
160: PlatformLocal platform = createPlatform(new Integer(4));
161: GameLocal game = createGame(new Integer(33));
162: Set<GameLocal> gameSets = platform.getGames();
163: gameSets.add(game);
164: } finally {
165: completeTransaction();
166: }
167:
168: assertLinked(4, 33);
169: }
170:
171: public void testBSetANewAB() throws Exception {
172: resetDB();
173: beginTransaction();
174: try {
175: PlatformLocal platform = createPlatform(new Integer(4));
176: GameLocal game = createGame(new Integer(33));
177: Set<PlatformLocal> platformSets = game.getPlatforms();
178: platformSets.add(platform);
179: } finally {
180: completeTransaction();
181: }
182:
183: assertLinked(4, 33);
184: }
185:
186: public void testASetBExistingBNewA() throws Exception {
187: resetDB();
188: beginTransaction();
189: try {
190: PlatformLocal platform = createPlatform(new Integer(4));
191: GameLocal game = findGame(new Integer(11));
192: Set<GameLocal> gameSets = platform.getGames();
193: gameSets.add(game);
194: } finally {
195: completeTransaction();
196: }
197:
198: assertLinked(4, 11);
199: }
200:
201: public void testBSetAExistingBNewA() throws Exception {
202: resetDB();
203: beginTransaction();
204: try {
205: PlatformLocal platform = createPlatform(new Integer(4));
206: GameLocal game = findGame(new Integer(11));
207: Set<PlatformLocal> platformSets = game.getPlatforms();
208: platformSets.add(platform);
209: } finally {
210: completeTransaction();
211: }
212:
213: assertLinked(4, 11);
214: }
215:
216: public void testASetBExistingANewB() throws Exception {
217: resetDB();
218: beginTransaction();
219: try {
220: PlatformLocal platform = findPlatform(new Integer(1));
221: GameLocal game = createGame(new Integer(33));
222: Set<GameLocal> gameSets = platform.getGames();
223: gameSets.add(game);
224: } finally {
225: completeTransaction();
226: }
227:
228: assertLinked(1, 33);
229: }
230:
231: public void testBSetAExistingANewB() throws Exception {
232: resetDB();
233: beginTransaction();
234: try {
235: PlatformLocal platform = findPlatform(new Integer(1));
236: GameLocal game = createGame(new Integer(33));
237: Set<PlatformLocal> platformSets = game.getPlatforms();
238: platformSets.add(platform);
239: } finally {
240: completeTransaction();
241: }
242:
243: assertLinked(1, 33);
244: }
245:
246: public void testRemoveRelationships() throws Exception {
247: resetDB();
248: beginTransaction();
249: try {
250: PlatformLocal platform = findPlatform(new Integer(1));
251: platform.remove();
252: } finally {
253: completeTransaction();
254: }
255:
256: assertPlatformDeleted(1);
257: }
258:
259: public void testIllegalCmrCollectionArgument() throws Exception {
260: resetDB();
261: beginTransaction();
262: try {
263: PlatformLocal platform = findPlatform(new Integer(1));
264: Set games = platform.getGames();
265:
266: try {
267: games.add(new Object());
268: fail("expected games.add(new Object()) to throw an IllegalArgumentException");
269: } catch (IllegalArgumentException e) {
270: }
271:
272: try {
273: games.addAll(Arrays.asList(new Object()));
274: fail("expected games.addAll(Arrays.asList(new Object())) to throw an IllegalArgumentException");
275: } catch (IllegalArgumentException expected) {
276: }
277: } finally {
278: completeTransaction();
279: }
280: }
281:
282: public void testModifyCmrCollectionOusideTx() throws Exception {
283: resetDB();
284: beginTransaction();
285: Set games;
286: GameLocal newGame;
287: try {
288: PlatformLocal platform = findPlatform(new Integer(1));
289: newGame = createGame(new Integer(33));
290: games = platform.getGames();
291: } finally {
292: completeTransaction();
293: }
294:
295: // CMR collections should still be readable
296: assertFalse(games.isEmpty());
297: assertEquals(2, games.size());
298: for (Iterator iter = games.iterator(); iter.hasNext();) {
299: GameLocal game = (GameLocal) iter.next();
300: if (game.getId().equals(new Integer(11))) {
301: assertEquals("value11", game.getName());
302: } else if (game.getId().equals(new Integer(22))) {
303: assertEquals("value22", game.getName());
304: } else {
305: fail();
306: }
307: }
308:
309: // But CMR collections should not be modifiable
310: try {
311: games.add(newGame);
312: fail("expected games.add(game) to throw an IllegalStateException");
313: } catch (IllegalStateException expected) {
314: }
315: try {
316: games.addAll(Arrays.asList(newGame));
317: fail("expected games.addAll(Arrays.asList(game)) to throw an IllegalStateException");
318: } catch (IllegalStateException expected) {
319: }
320: try {
321: games.remove(newGame);
322: fail("expected games.remove(game) to throw an IllegalStateException");
323: } catch (IllegalStateException expected) {
324: }
325: try {
326: games.removeAll(Arrays.asList(newGame));
327: fail("expected games.removeAll(game) to throw an IllegalStateException");
328: } catch (IllegalStateException expected) {
329: }
330: Iterator iterator = games.iterator();
331: try {
332: iterator.remove();
333: fail("expected iterator.remove() to throw an ConcurrentModificationException");
334: } catch (ConcurrentModificationException expected) {
335: }
336: }
337:
338: public void testModifyCmrCollectionInNewTx() throws Exception {
339: resetDB();
340: beginTransaction();
341: Set games;
342: GameLocal newGame;
343: try {
344: PlatformLocal platform = findPlatform(new Integer(1));
345: newGame = createGame(new Integer(33));
346: games = platform.getGames();
347: } finally {
348: completeTransaction();
349: }
350:
351: beginTransaction();
352: try {
353: // CMR collections should still be readable
354: assertFalse(games.isEmpty());
355: assertEquals(2, games.size());
356: for (Iterator iter = games.iterator(); iter.hasNext();) {
357: GameLocal game = (GameLocal) iter.next();
358: if (game.getId().equals(new Integer(11))) {
359: assertEquals("value11", game.getName());
360: } else if (game.getId().equals(new Integer(22))) {
361: assertEquals("value22", game.getName());
362: } else {
363: fail();
364: }
365: }
366:
367: // But CMR collections should not be modifiable
368: try {
369: games.add(newGame);
370: fail("expected games.add(game) to throw an IllegalStateException");
371: } catch (IllegalStateException expected) {
372: }
373: try {
374: games.addAll(Arrays.asList(newGame));
375: fail("expected games.addAll(Arrays.asList(game)) to throw an IllegalStateException");
376: } catch (IllegalStateException expected) {
377: }
378: try {
379: games.remove(newGame);
380: fail("expected games.remove(game) to throw an IllegalStateException");
381: } catch (IllegalStateException expected) {
382: }
383: try {
384: games.removeAll(Arrays.asList(newGame));
385: fail("expected games.removeAll(game) to throw an IllegalStateException");
386: } catch (IllegalStateException expected) {
387: }
388: Iterator iterator = games.iterator();
389: try {
390: iterator.remove();
391: fail("expected iterator.remove() to throw an ConcurrentModificationException");
392: } catch (ConcurrentModificationException expected) {
393: }
394: } finally {
395: completeTransaction();
396: }
397: }
398:
399: public void testIteratorConcurrentModification() throws Exception {
400: resetDB();
401: beginTransaction();
402: Set games;
403: try {
404: PlatformLocal platform = findPlatform(new Integer(1));
405: GameLocal game = findGame(new Integer(11));
406: games = platform.getGames();
407: assertFalse(games.isEmpty());
408: assertEquals(2, games.size());
409:
410: Iterator iterator = games.iterator();
411:
412: games.remove(game);
413: assertEquals(1, games.size());
414:
415: try {
416: iterator.next();
417: fail("expected iterator.next() to throw an ConcurrentModificationException");
418: } catch (ConcurrentModificationException expected) {
419: }
420: } finally {
421: completeTransaction();
422: }
423: }
424:
425: public void testIteratorAndRemove() throws Exception {
426: resetDB();
427: beginTransaction();
428: Set games;
429: try {
430: PlatformLocal platform = findPlatform(new Integer(1));
431: GameLocal game = findGame(new Integer(11));
432: games = platform.getGames();
433: assertFalse(games.isEmpty());
434: assertEquals(2, games.size());
435:
436: Iterator iterator = games.iterator();
437:
438: assertTrue(games.contains(game));
439: platform.remove();
440: assertFalse(games.contains(game));
441: assertEquals(0, games.size());
442:
443: try {
444: iterator.next();
445: fail("expected iterator.next() to throw an ConcurrentModificationException");
446: } catch (ConcurrentModificationException expected) {
447: }
448: } finally {
449: completeTransaction();
450: }
451: }
452:
453: private void assertPlatformDeleted(int platformId) throws Exception {
454: Connection c = ds.getConnection();
455: Statement s = c.createStatement();
456:
457: ResultSet rs = s
458: .executeQuery("SELECT COUNT(*) FROM ComplexGame_ComplexPlatform WHERE Platforms_id="
459: + platformId);
460: assertTrue(rs.next());
461: assertEquals(0, rs.getInt(1));
462: rs.close();
463:
464: rs = s
465: .executeQuery("SELECT COUNT(*) FROM ComplexPlatform WHERE id="
466: + platformId);
467: assertTrue(rs.next());
468: assertEquals(0, rs.getInt(1));
469: rs.close();
470:
471: s.close();
472: c.close();
473: }
474:
475: private void assertAllUnlinked() throws Exception {
476: Connection c = ds.getConnection();
477: Statement s = c.createStatement();
478:
479: ResultSet rs = s
480: .executeQuery("SELECT COUNT(*) FROM ComplexGame_ComplexPlatform");
481: assertTrue(rs.next());
482: assertEquals(0, rs.getInt(1));
483: rs.close();
484:
485: s.close();
486: c.close();
487: }
488:
489: private void assertLinked(int platformId, int gameId)
490: throws Exception {
491: Connection c = ds.getConnection();
492: Statement s = c.createStatement();
493:
494: ResultSet rs = s
495: .executeQuery("SELECT COUNT(*) FROM ComplexGame_ComplexPlatform WHERE Platforms_id = "
496: + platformId + " AND Games_id = " + gameId);
497: assertTrue(rs.next());
498: assertEquals(1, rs.getInt(1));
499: rs.close();
500:
501: rs = s
502: .executeQuery("SELECT name FROM ComplexPlatform WHERE id = "
503: + platformId);
504: assertTrue(rs.next());
505: assertEquals("value" + platformId, rs.getString(1));
506:
507: rs = s.executeQuery("SELECT name FROM ComplexGame WHERE id = "
508: + gameId);
509: assertTrue(rs.next());
510: assertEquals("value" + gameId, rs.getString(1));
511: rs.close();
512:
513: s.close();
514: c.close();
515: }
516:
517: private GameLocal createGame(int gameId) throws CreateException {
518: GameLocal menu = gameLocalHome.create(new GamePk(gameId,
519: "value" + gameId));
520: return menu;
521: }
522:
523: private GameLocal findGame(int gameId) throws FinderException {
524: return gameLocalHome.findByPrimaryKey(new GamePk(gameId,
525: "value" + gameId));
526: }
527:
528: private PlatformLocal createPlatform(int platformId)
529: throws CreateException {
530: PlatformLocal platform = platformLocalHome
531: .create(new PlatformPk(platformId, "value" + platformId));
532: return platform;
533: }
534:
535: private PlatformLocal findPlatform(int platformId)
536: throws FinderException {
537: return platformLocalHome.findByPrimaryKey(new PlatformPk(
538: platformId, "value" + platformId));
539: }
540:
541: private void resetDB() throws Exception {
542: Connection connection = ds.getConnection();
543: Statement statement = null;
544: try {
545: statement = connection.createStatement();
546:
547: try {
548: statement
549: .execute("DELETE FROM ComplexGame_ComplexPlatform");
550: } catch (SQLException ignored) {
551: }
552: try {
553: statement.execute("DELETE FROM ComplexGame");
554: } catch (SQLException ignored) {
555: }
556: try {
557: statement.execute("DELETE FROM ComplexPlatform");
558: } catch (SQLException ignored) {
559: }
560: } finally {
561: close(statement);
562: close(connection);
563: }
564:
565: beginTransaction();
566: try {
567: PlatformLocal platform1 = createPlatform(1);
568: assertNotNull("platform1.getGames() is null", platform1
569: .getGames());
570: PlatformLocal platform2 = createPlatform(2);
571: assertNotNull("platform2.getGames() is null", platform2
572: .getGames());
573: PlatformLocal platform3 = createPlatform(3);
574: assertNotNull("platform3.getGames() is null", platform3
575: .getGames());
576:
577: GameLocal game1 = createGame(11);
578: assertNotNull("game1.getPlatforms() is null", game1
579: .getPlatforms());
580: GameLocal game2 = createGame(22);
581: assertNotNull("game2.getPlatforms() is null", game2
582: .getPlatforms());
583:
584: platform1.getGames().add(game1);
585:
586: platform1.getGames().add(game2);
587: platform2.getGames().add(game2);
588: platform3.getGames().add(game2);
589: } finally {
590: completeTransaction();
591: }
592: }
593:
594: protected void dump() throws SQLException {
595: dumpTable(ds, "ComplexGame");
596: dumpTable(ds, "ComplexPlatform");
597: dumpTable(ds, "ComplexGame_ComplexPlatform");
598: }
599: }
|