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