Search in sources :

Example 36 with PlatformLocal

use of org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal in project tomee by apache.

the class ManyToManyComplexPkTests method testModifyCmrCollectionInNewTx.

public void testModifyCmrCollectionInNewTx() throws Exception {
    resetDB();
    beginTransaction();
    Set games;
    GameLocal newGame;
    try {
        final PlatformLocal platform = findPlatform(new Integer(1));
        newGame = createGame(new Integer(33));
        games = platform.getGames();
    } finally {
        completeTransaction();
    }
    beginTransaction();
    try {
        // CMR collections should still be readable
        assertFalse(games.isEmpty());
        assertEquals(2, games.size());
        for (final Iterator iter = games.iterator(); iter.hasNext(); ) {
            final GameLocal game = (GameLocal) iter.next();
            if (game.getId().equals(new Integer(11))) {
                assertEquals("value11", game.getName());
            } else if (game.getId().equals(new Integer(22))) {
                assertEquals("value22", game.getName());
            } else {
                fail();
            }
        }
        // But CMR collections should not be modifiable
        try {
            games.add(newGame);
            fail("expected games.add(game) to throw an IllegalStateException");
        } catch (final IllegalStateException expected) {
        }
        try {
            games.addAll(Arrays.asList(newGame));
            fail("expected games.addAll(Arrays.asList(game)) to throw an IllegalStateException");
        } catch (final IllegalStateException expected) {
        }
        try {
            games.remove(newGame);
            fail("expected games.remove(game) to throw an IllegalStateException");
        } catch (final IllegalStateException expected) {
        }
        try {
            games.removeAll(Arrays.asList(newGame));
            fail("expected games.removeAll(game) to throw an IllegalStateException");
        } catch (final IllegalStateException expected) {
        }
        final Iterator iterator = games.iterator();
        try {
            iterator.remove();
            fail("expected iterator.remove() to throw an ConcurrentModificationException");
        } catch (final ConcurrentModificationException expected) {
        }
    } finally {
        completeTransaction();
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) GameLocal(org.apache.openejb.test.entity.cmr.manytomany.GameLocal) PlatformLocal(org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal)

Example 37 with PlatformLocal

use of org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal in project tomee by apache.

the class ManyToManyComplexPkTests method testIteratorAndRemove.

public void testIteratorAndRemove() throws Exception {
    resetDB();
    beginTransaction();
    final Set games;
    try {
        final PlatformLocal platform = findPlatform(new Integer(1));
        final GameLocal game = findGame(new Integer(11));
        games = platform.getGames();
        assertFalse(games.isEmpty());
        assertEquals(2, games.size());
        final Iterator iterator = games.iterator();
        assertTrue(games.contains(game));
        platform.remove();
        assertFalse(games.contains(game));
        assertEquals(0, games.size());
        try {
            iterator.next();
            fail("expected iterator.next() to throw an ConcurrentModificationException");
        } catch (final ConcurrentModificationException expected) {
        }
    } finally {
        completeTransaction();
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) GameLocal(org.apache.openejb.test.entity.cmr.manytomany.GameLocal) PlatformLocal(org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal)

Aggregations

PlatformLocal (org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal)37 GameLocal (org.apache.openejb.test.entity.cmr.manytomany.GameLocal)30 ResultSet (java.sql.ResultSet)12 HashSet (java.util.HashSet)12 Iterator (java.util.Iterator)12 Set (java.util.Set)12 ConcurrentModificationException (java.util.ConcurrentModificationException)8 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 TransactionRolledbackLocalException (javax.ejb.TransactionRolledbackLocalException)2