Search in sources :

Example 16 with SongLocal

use of org.apache.openejb.test.entity.cmr.onetomany.SongLocal in project tomee by apache.

the class OneToManyTests method test11_Delete.

public void test11_Delete() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final ArtistLocal artist = findArtist(1);
        artist.setPerformed(new HashSet<SongLocal>());
        final Set<SongLocal> songs = artist.getComposed();
        final Set<SongLocal> bsCopies = new HashSet<SongLocal>(songs);
        assertSame(songs, artist.getComposed());
        artist.remove();
        assertTrue("CMR collection is not empty " + System.identityHashCode(songs), songs.isEmpty());
        for (final SongLocal songLocal : bsCopies) {
            assertNull(songLocal.getComposer());
        }
    } finally {
        completeTransaction();
    }
    final Connection c = ds.getConnection();
    final Statement s = c.createStatement();
    final ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM Song");
    assertTrue(rs.next());
    assertEquals(2, rs.getInt(1));
    rs.close();
    s.close();
    c.close();
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 17 with SongLocal

use of org.apache.openejb.test.entity.cmr.onetomany.SongLocal in project tomee by apache.

the class OneToManyTests method test12_CascadeDelete.

public void test12_CascadeDelete() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final ArtistLocal artist = findArtist(1);
        final Set<SongLocal> songs = artist.getPerformed();
        assertFalse(songs.isEmpty());
        artist.remove();
        assertTrue(songs.isEmpty());
    } finally {
        completeTransaction();
    }
    final Connection c = ds.getConnection();
    final Statement s = c.createStatement();
    final ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM Song");
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    rs.close();
    s.close();
    c.close();
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 18 with SongLocal

use of org.apache.openejb.test.entity.cmr.onetomany.SongLocal in project tomee by apache.

the class OneToManyComplexPkTests method test07_BSetAExistingBNewA.

public void test07_BSetAExistingBNewA() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final ArtistLocal artist = findArtist(2);
        final SongLocal song = findSong(11);
        song.setPerformer(artist);
    } finally {
        completeTransaction();
    }
    assertLinked(2, 11);
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal)

Example 19 with SongLocal

use of org.apache.openejb.test.entity.cmr.onetomany.SongLocal in project tomee by apache.

the class OneToManyComplexPkTests method TODO_testSetCMPMappedToForeignKeyColumn.

// uncomment when cmp to cmr is supported
public void TODO_testSetCMPMappedToForeignKeyColumn() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final SongLocal song = findSong(11);
        song.setBpm(2);
        final ArtistLocal artist = song.getPerformer();
        assertEquals(new Integer(2), artist.getId());
        assertEquals("value2", artist.getName());
    } finally {
        completeTransaction();
    }
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal)

Example 20 with SongLocal

use of org.apache.openejb.test.entity.cmr.onetomany.SongLocal in project tomee by apache.

the class OneToManyComplexPkTests method testModifyCmrCollectionInNewTx.

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

Aggregations

SongLocal (org.apache.openejb.test.entity.cmr.onetomany.SongLocal)41 ArtistLocal (org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal)36 ResultSet (java.sql.ResultSet)14 HashSet (java.util.HashSet)14 Set (java.util.Set)10 ConcurrentModificationException (java.util.ConcurrentModificationException)8 Iterator (java.util.Iterator)8 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 SQLException (java.sql.SQLException)2