Search in sources :

Example 26 with ArtistLocal

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

the class OneToManyComplexPkTests method resetDB.

private void resetDB() throws Exception {
    final Connection connection = ds.getConnection();
    Statement statement = null;
    try {
        statement = connection.createStatement();
        try {
            statement.execute("DELETE FROM ComplexArtist");
        } catch (final SQLException ignored) {
        }
        try {
            statement.execute("DELETE FROM ComplexSong");
        } catch (final SQLException ignored) {
        }
    } finally {
        close(statement);
        close(connection);
    }
    final ArtistLocal artist1 = createArtist(1);
    createArtist(2);
    final SongLocal song1 = createSong(11);
    final SongLocal song2 = createSong(22);
    song1.setPerformer(artist1);
    song2.setPerformer(artist1);
    song1.setComposer(artist1);
    song2.setComposer(artist1);
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 27 with ArtistLocal

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

the class OneToManyTests method testModifyCmrCollectionOusideTx.

public void testModifyCmrCollectionOusideTx() 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();
    }
    // 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 ignored) {
    }
    try {
        songs.addAll(Arrays.asList(newSong));
        fail("expected songs.addAll(Arrays.asList(newSong)) to throw an IllegalStateException");
    } catch (final IllegalStateException ignored) {
    }
    try {
        songs.remove(newSong);
        fail("expected songs.remove(newSong) to throw an IllegalStateException");
    } catch (final IllegalStateException ignored) {
    }
    try {
        songs.removeAll(Arrays.asList(newSong));
        fail("expected songs.removeAll(Arrays.asList(newSong)) to throw an IllegalStateException");
    } catch (final IllegalStateException ignored) {
    }
    final Iterator iterator = songs.iterator();
    try {
        iterator.remove();
        fail("expected iterator.remove() to throw an ConcurrentModificationException");
    } catch (final ConcurrentModificationException ignored) {
    }
}
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)

Example 28 with ArtistLocal

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

the class OneToManyTests method test06_ASetBExistingBNewA.

public void test06_ASetBExistingBNewA() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final ArtistLocal artist = findArtist(2);
        final SongLocal song = findSong(11);
        final Set<SongLocal> songSets = artist.getPerformed();
        songSets.add(song);
    } 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 29 with ArtistLocal

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

the class OneToManyTests 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 30 with ArtistLocal

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

the class OneToManyTests method testIteratorAndRemove.

public void testIteratorAndRemove() throws Exception {
    resetDB();
    beginTransaction();
    try {
        final ArtistLocal artist = findArtist(new Integer(1));
        final SongLocal song = findSong(new Integer(11));
        final Set games = artist.getComposed();
        assertFalse(games.isEmpty());
        assertEquals(2, games.size());
        final Iterator iterator = games.iterator();
        assertTrue(games.contains(song));
        artist.remove();
        assertFalse(games.contains(song));
        assertEquals(0, games.size());
        try {
            iterator.next();
            fail("expected iterator.next() to throw an ConcurrentModificationException");
        } catch (final ConcurrentModificationException ignored) {
        }
    } finally {
        completeTransaction();
    }
}
Also used : ArtistLocal(org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal) ConcurrentModificationException(java.util.ConcurrentModificationException) SongLocal(org.apache.openejb.test.entity.cmr.onetomany.SongLocal) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator)

Aggregations

ArtistLocal (org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal)39 SongLocal (org.apache.openejb.test.entity.cmr.onetomany.SongLocal)36 ResultSet (java.sql.ResultSet)16 HashSet (java.util.HashSet)16 Set (java.util.Set)12 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