cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r957022 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne: ./ unit/
Date Tue, 22 Jun 2010 20:42:44 GMT
Author: aadamchik
Date: Tue Jun 22 20:42:43 2010
New Revision: 957022

URL: http://svn.apache.org/viewvc?rev=957022&view=rev
Log:
 DI-enabled unit tests

* removing redundant test case
* converting test cases to DI approach

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInContextTest.java
      - copied, changed from r956895, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInCtxtTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsTest.java
      - copied, changed from r956895, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelTest.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInCtxtTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/DOPrefetchTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CaseDataFactory.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CDOMany2OneTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CDOMany2OneTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CDOMany2OneTest.java?rev=957022&r1=957021&r2=957022&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CDOMany2OneTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CDOMany2OneTest.java
Tue Jun 22 20:42:43 2010
@@ -21,55 +21,97 @@ package org.apache.cayenne;
 
 import java.util.List;
 
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.testdo.testmap.Gallery;
 import org.apache.cayenne.testdo.testmap.Painting;
 import org.apache.cayenne.testdo.testmap.ROPainting;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
 
-public class CDOMany2OneTest extends CayenneDOTestBase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class CDOMany2OneTest extends ServerCase {
+
+    @Inject
+    private ServerRuntime runtime;
+
+    @Inject
+    private ObjectContext context;
+
+    @Inject
+    private DBHelper dbHelper;
+
+    protected TableHelper tArtist;
+    protected TableHelper tPainting;
+    protected TableHelper tGallery;
+
+    @Override
+    protected void setUpAfterInjection() throws Exception {
+        dbHelper.deleteAll("PAINTING_INFO");
+        dbHelper.deleteAll("PAINTING");
+        dbHelper.deleteAll("ARTIST_EXHIBIT");
+        dbHelper.deleteAll("ARTIST");
+        dbHelper.deleteAll("EXHIBIT");
+        dbHelper.deleteAll("GALLERY");
+
+        tArtist = new TableHelper(dbHelper, "ARTIST");
+        tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+        tPainting = new TableHelper(dbHelper, "PAINTING");
+        tPainting.setColumns("PAINTING_ID", "PAINTING_TITLE", "ARTIST_ID", "GALLERY_ID");
+
+        tGallery = new TableHelper(dbHelper, "GALLERY");
+        tGallery.setColumns("GALLERY_ID", "GALLERY_NAME");
+    }
+
+    private void createArtistWithPaintingDataSet() throws Exception {
+        tArtist.insert(8, "aX");
+        tPainting.insert(6, "pW", 8, null);
+    }
+
+    private void createArtistWithPaintingsInGalleryDataSet() throws Exception {
+        tArtist.insert(8, "aX");
+        tGallery.insert(11, "Ge");
+        tPainting.insert(6, "pW1", 8, 11);
+        tPainting.insert(7, "pW2", 8, 11);
+
+    }
 
     public void testReadRO1() throws Exception {
 
-        // setup test
-        Artist a1 = newArtist();
-        Painting p1 = newPainting();
-        a1.addToPaintingArray(p1);
-        ctxt.commitChanges();
-
-        // do select
-        Expression e = ExpressionFactory.matchExp("toArtist", a1);
-        SelectQuery q = new SelectQuery("ROPainting", e);
+        createArtistWithPaintingDataSet();
+
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 8);
 
-        // *** TESTING THIS ***
-        List paints = ctxt.performQuery(q);
+        Expression e = ExpressionFactory.matchExp(ROPainting.TO_ARTIST_PROPERTY, a1);
+        SelectQuery q = new SelectQuery(ROPainting.class, e);
+
+        List<ROPainting> paints = context.performQuery(q);
         assertEquals(1, paints.size());
 
-        ROPainting rop1 = (ROPainting) paints.get(0);
+        ROPainting rop1 = paints.get(0);
         assertSame(a1, rop1.getToArtist());
     }
 
     public void testReadRO2() throws Exception {
 
-        // setup test
-        Artist a1 = newArtist();
-        Painting p1 = newPainting();
-        a1.addToPaintingArray(p1);
-        ctxt.commitChanges();
-
-        ctxt = createDataContext();
-
-        // do select
-        Expression e = ExpressionFactory.matchExp("toArtist", a1);
-        SelectQuery q = new SelectQuery("ROPainting", e);
+        createArtistWithPaintingDataSet();
+
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 8);
 
-        // *** TESTING THIS ***
-        List paints = ctxt.performQuery(q);
+        Expression e = ExpressionFactory.matchExp(ROPainting.TO_ARTIST_PROPERTY, a1);
+        SelectQuery q = new SelectQuery(ROPainting.class, e);
+
+        List<ROPainting> paints = context.performQuery(q);
         assertEquals(1, paints.size());
 
-        ROPainting rop1 = (ROPainting) paints.get(0);
+        ROPainting rop1 = paints.get(0);
         assertNotNull(rop1.getToArtist());
 
         // trigger fetch
@@ -79,82 +121,71 @@ public class CDOMany2OneTest extends Cay
 
     public void testSelectViaRelationship() throws Exception {
 
-        // setup test
-        Artist a1 = newArtist();
-        Painting p1 = newPainting();
-        a1.addToPaintingArray(p1);
-        ctxt.commitChanges();
-
-        // do select
-        Expression e = ExpressionFactory.matchExp("toArtist", a1);
-        SelectQuery q = new SelectQuery("Painting", e);
+        createArtistWithPaintingDataSet();
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 8);
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
+
+        Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1);
+        SelectQuery q = new SelectQuery(Painting.class, e);
 
-        // *** TESTING THIS ***
-        List paints = ctxt.performQuery(q);
+        List<Painting> paints = context.performQuery(q);
         assertEquals(1, paints.size());
         assertSame(p1, paints.get(0));
     }
 
     public void testSelectViaMultiRelationship() throws Exception {
 
-        // setup test
-        Artist a1 = newArtist();
-        Painting p1 = newPainting();
-        Painting p2 = newPainting();
-        Gallery g1 = newGallery();
-        a1.addToPaintingArray(p1);
-        a1.addToPaintingArray(p2);
-        p1.setToGallery(g1);
-        p2.setToGallery(g1);
-        ctxt.commitChanges();
+        createArtistWithPaintingsInGalleryDataSet();
+
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 8);
+        Gallery g1 = Cayenne.objectForPK(context, Gallery.class, 11);
 
-        // do select
         Expression e = ExpressionFactory.matchExp("paintingArray.toGallery", g1);
         SelectQuery q = new SelectQuery("Artist", e);
 
-        // *** TESTING THIS ***
-        List artists = ctxt.performQuery(q);
+        List<Artist> artists = context.performQuery(q);
         assertEquals(1, artists.size());
         assertSame(a1, artists.get(0));
     }
 
     public void testNewAdd() throws Exception {
-        Artist a1 = newArtist();
-        Painting p1 = newPainting();
+        Artist a1 = context.newObject(Artist.class);
+        a1.setArtistName("bL");
+
+        Painting p1 = context.newObject(Painting.class);
+        p1.setPaintingTitle("xa");
 
-        // *** TESTING THIS ***
         p1.setToArtist(a1);
 
-        // test before save
         assertSame(a1, p1.getToArtist());
         assertEquals(1, a1.getPaintingArray().size());
         assertSame(p1, a1.getPaintingArray().get(0));
 
-        // do save
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context.commitChanges();
 
-        // test database data
-        Painting p2 = fetchPainting();
-        Artist a2 = p2.getToArtist();
-        assertNotNull(a2);
-        assertEquals(artistName, a2.getArtistName());
+        assertEquals(Cayenne.longPKForObject(a1), tArtist.getLong("ARTIST_ID"));
+        assertEquals(Cayenne.longPKForObject(a1), tPainting.getLong("ARTIST_ID"));
     }
 
     public void testRemove() throws Exception {
-        Painting p1 = newPainting();
-        Gallery g1 = newGallery();
+        Painting p1 = context.newObject(Painting.class);
+        p1.setPaintingTitle("xa");
+
+        Gallery g1 = context.newObject(Gallery.class);
+        g1.setGalleryName("yT");
+
         p1.setToGallery(g1);
 
         // do save
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context.commitChanges();
+
+        ObjectContext context2 = runtime.getContext();
 
         // test database data
-        Painting p2 = fetchPainting();
+        Painting p2 = (Painting) Cayenne.objectForQuery(context2, new SelectQuery(
+                Painting.class));
         Gallery g2 = p2.getToGallery();
 
-        // *** TESTING THIS ***
         p2.setToGallery(null);
 
         // test before save
@@ -162,37 +193,39 @@ public class CDOMany2OneTest extends Cay
         assertNull(p2.getToGallery());
 
         // do save II
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context2.commitChanges();
+
+        ObjectContext context3 = runtime.getContext();
 
-        Painting p3 = fetchPainting();
+        Painting p3 = (Painting) Cayenne.objectForQuery(context3, new SelectQuery(
+                Painting.class));
         assertNull(p3.getToGallery());
     }
 
     public void testReplace() throws Exception {
-        String altGalleryName = "alt gallery";
 
-        Painting p1 = newPainting();
-        Gallery g1 = newGallery();
-        g1.setGalleryName(altGalleryName);
+        Painting p1 = context.newObject(Painting.class);
+        p1.setPaintingTitle("xa");
+
+        Gallery g1 = context.newObject(Gallery.class);
+        g1.setGalleryName("yTW");
 
         p1.setToGallery(g1);
 
-        // do save
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context.commitChanges();
+        ObjectContext context2 = runtime.getContext();
 
         // test database data
-        Painting p2 = fetchPainting();
+        Painting p2 = (Painting) Cayenne.objectForQuery(context2, new SelectQuery(
+                Painting.class));
         Gallery g21 = p2.getToGallery();
         assertNotNull(g21);
-        assertEquals(altGalleryName, g21.getGalleryName());
+        assertEquals("yTW", g21.getGalleryName());
         assertEquals(1, g21.getPaintingArray().size());
         assertSame(p2, g21.getPaintingArray().get(0));
 
-        Gallery g22 = newGallery();
-
-        // *** TESTING THIS ***
+        Gallery g22 = context2.newObject(Gallery.class);
+        g22.setGalleryName("rE");
         p2.setToGallery(g22);
 
         // test before save
@@ -201,35 +234,37 @@ public class CDOMany2OneTest extends Cay
         assertSame(p2, g22.getPaintingArray().get(0));
 
         // do save II
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context2.commitChanges();
+
+        ObjectContext context3 = runtime.getContext();
 
-        Painting p3 = fetchPainting();
+        Painting p3 = (Painting) Cayenne.objectForQuery(context3, new SelectQuery(
+                Painting.class));
         Gallery g3 = p3.getToGallery();
         assertNotNull(g3);
-        assertEquals(galleryName, g3.getGalleryName());
+        assertEquals("rE", g3.getGalleryName());
         assertEquals(1, g3.getPaintingArray().size());
         assertSame(p3, g3.getPaintingArray().get(0));
     }
 
     public void testSavedAdd() throws Exception {
-        Painting p1 = newPainting();
-        assertEquals(p1.getObjectId(), ctxt
-                .localObject(p1.getObjectId(), null)
-                .getObjectId());
-        assertTrue(ctxt.hasChanges());
+        Painting p1 = context.newObject(Painting.class);
+        p1.setPaintingTitle("xa");
+
+        assertTrue(context.hasChanges());
 
         // do save
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context.commitChanges();
+        ObjectContext context2 = runtime.getContext();
 
         // test database data
-        Painting p2 = fetchPainting();
+        Painting p2 = (Painting) Cayenne.objectForQuery(context2, new SelectQuery(
+                Painting.class));
         assertNull(p2.getToGallery());
 
-        Gallery g2 = newGallery();
+        Gallery g2 = context2.newObject(Gallery.class);
+        g2.setGalleryName("rE");
 
-        // *** TESTING THIS ***
         p2.setToGallery(g2);
 
         // test before save
@@ -237,13 +272,14 @@ public class CDOMany2OneTest extends Cay
         assertSame(p2, g2.getPaintingArray().get(0));
 
         // do save II
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+        context2.commitChanges();
+        ObjectContext context3 = runtime.getContext();
 
-        Painting p3 = fetchPainting();
+        Painting p3 = (Painting) Cayenne.objectForQuery(context3, new SelectQuery(
+                Painting.class));
         Gallery g3 = p3.getToGallery();
         assertNotNull(g3);
-        assertEquals(galleryName, g3.getGalleryName());
+        assertEquals("rE", g3.getGalleryName());
         assertEquals(1, g3.getPaintingArray().size());
         assertSame(p3, g3.getPaintingArray().get(0));
     }

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInContextTest.java
(from r956895, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInCtxtTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInContextTest.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInContextTest.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInCtxtTest.java&r1=956895&r2=957022&rev=957022&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInCtxtTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectInContextTest.java
Tue Jun 22 20:42:43 2010
@@ -23,24 +23,44 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.testdo.testmap.Painting;
-import org.apache.cayenne.unit.CaseDataFactory;
-import org.apache.cayenne.unit.CayenneCase;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
 
-public class CayenneDataObjectInCtxtTest extends CayenneCase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class CayenneDataObjectInContextTest extends ServerCase {
+
+    @Inject
+    protected ServerRuntime runtime;
+
+    @Inject
+    protected DataContext context;
+
+    @Inject
+    protected DBHelper dbHelper;
+
+    protected TableHelper tArtist;
 
     @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        deleteTestData();
+    protected void setUpAfterInjection() throws Exception {
+        dbHelper.deleteAll("PAINTING_INFO");
+        dbHelper.deleteAll("PAINTING");
+        dbHelper.deleteAll("ARTIST_EXHIBIT");
+        dbHelper.deleteAll("ARTIST");
+
+        tArtist = new TableHelper(dbHelper, "ARTIST");
+        tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
     }
 
     public void testDoubleRegistration() {
-        DataContext context = createDataContext();
 
         DataObject object = new Artist();
         assertNull(object.getObjectId());
@@ -57,7 +77,7 @@ public class CayenneDataObjectInCtxtTest
         assertSame(object, context.getGraphManager().getNode(tempID));
 
         // registering in another context should throw an exception
-        DataContext anotherContext = createDataContext();
+        ObjectContext anotherContext = runtime.getContext();
         try {
             anotherContext.registerNewObject(object);
             fail("registerNewObject should've failed - object is already in another context");
@@ -67,8 +87,8 @@ public class CayenneDataObjectInCtxtTest
         }
     }
 
+    @Deprecated
     public void testObjEntity() {
-        DataContext context = createDataContext();
 
         Artist a = new Artist();
         assertNull(a.getObjEntity());
@@ -88,25 +108,23 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testCommitChangesInBatch() {
-        DataContext context = createDataContext();
 
-        Artist a1 = (Artist) context.newObject("Artist");
+        Artist a1 = context.newObject(Artist.class);
         a1.setArtistName("abc1");
 
-        Artist a2 = (Artist) context.newObject("Artist");
+        Artist a2 = context.newObject(Artist.class);
         a2.setArtistName("abc2");
 
-        Artist a3 = (Artist) context.newObject("Artist");
+        Artist a3 = context.newObject(Artist.class);
         a3.setArtistName("abc3");
 
         context.commitChanges();
 
-        List artists = context.performQuery(new SelectQuery(Artist.class));
+        List<Artist> artists = context.performQuery(new SelectQuery(Artist.class));
         assertEquals(3, artists.size());
     }
 
     public void testSetObjectId() {
-        DataContext context = createDataContext();
 
         Artist o1 = new Artist();
         assertNull(o1.getObjectId());
@@ -116,7 +134,7 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateTransToNew() {
-        DataContext context = createDataContext();
+
         Artist o1 = new Artist();
         assertEquals(PersistenceState.TRANSIENT, o1.getPersistenceState());
 
@@ -125,7 +143,6 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateNewToCommitted() {
-        DataContext context = createDataContext();
 
         Artist o1 = new Artist();
         o1.setArtistName("a");
@@ -138,7 +155,7 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateCommittedToModified() {
-        DataContext context = createDataContext();
+
         Artist o1 = new Artist();
         o1.setArtistName("a");
         context.registerNewObject(o1);
@@ -150,9 +167,11 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateModifiedToCommitted() {
-        DataContext context = createDataContext();
 
-        Artist o1 = newSavedArtist(context);
+        Artist o1 = context.newObject(Artist.class);
+        o1.setArtistName("qY");
+        context.commitChanges();
+
         o1.setArtistName(o1.getArtistName() + "_1");
         assertEquals(PersistenceState.MODIFIED, o1.getPersistenceState());
 
@@ -161,7 +180,6 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateCommittedToDeleted() {
-        DataContext context = createDataContext();
 
         Artist o1 = new Artist();
         o1.setArtistName("a");
@@ -174,9 +192,11 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testStateDeletedToTransient() {
-        DataContext context = createDataContext();
 
-        Artist o1 = newSavedArtist(context);
+        Artist o1 = context.newObject(Artist.class);
+        o1.setArtistName("qY");
+        context.commitChanges();
+
         context.deleteObject(o1);
         assertEquals(PersistenceState.DELETED, o1.getPersistenceState());
 
@@ -186,8 +206,7 @@ public class CayenneDataObjectInCtxtTest
         assertNull(o1.getObjectContext());
     }
 
-    public void testSetDataContext() {
-        DataContext context = createDataContext();
+    public void testSetContext() {
 
         Artist o1 = new Artist();
         assertNull(o1.getObjectContext());
@@ -196,31 +215,31 @@ public class CayenneDataObjectInCtxtTest
         assertSame(context, o1.getObjectContext());
     }
 
-    public void testFetchByAttr() throws Exception {
-        DataContext context = createDataContext();
+    public void testFetchByAttribute() throws Exception {
 
-        String artistName = "artist with one painting";
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {}, false);
+        tArtist.insert(7, "m6");
 
-        SelectQuery q = new SelectQuery("Artist", ExpressionFactory.matchExp(
-                "artistName",
-                artistName));
+        SelectQuery q = new SelectQuery(Artist.class, ExpressionFactory.matchExp(
+                Artist.ARTIST_NAME_PROPERTY,
+                "m6"));
 
-        List artists = context.performQuery(q);
+        List<Artist> artists = context.performQuery(q);
         assertEquals(1, artists.size());
-        Artist o1 = (Artist) artists.get(0);
+        Artist o1 = artists.get(0);
         assertNotNull(o1);
-        assertEquals(artistName, o1.getArtistName());
+        assertEquals("m6", o1.getArtistName());
     }
 
     public void testUniquing() throws Exception {
-        DataContext context = createDataContext();
 
-        String artistName = "unique artist with no paintings";
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {}, false);
+        tArtist.insert(7, "m6");
+
+        SelectQuery q = new SelectQuery(Artist.class, ExpressionFactory.matchExp(
+                Artist.ARTIST_NAME_PROPERTY,
+                "m6"));
 
-        Artist a1 = fetchArtist(context, artistName);
-        Artist a2 = fetchArtist(context, artistName);
+        Artist a1 = (Artist) Cayenne.objectForQuery(context, q);
+        Artist a2 = (Artist) Cayenne.objectForQuery(context, q);
 
         assertNotNull(a1);
         assertNotNull(a2);
@@ -229,9 +248,8 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testSnapshotVersion1() {
-        DataContext context = createDataContext();
 
-        Artist artist = (Artist) context.newObject("Artist");
+        Artist artist = context.newObject(Artist.class);
         assertEquals(DataObject.DEFAULT_VERSION, artist.getSnapshotVersion());
 
         // test versions set on commit
@@ -246,20 +264,14 @@ public class CayenneDataObjectInCtxtTest
         assertEquals(cachedSnapshot.getVersion(), artist.getSnapshotVersion());
     }
 
-    public void testSnapshotVersion2() {
-        DataContext context = createDataContext();
+    public void testSnapshotVersion2() throws Exception {
 
-        newSavedArtist(context);
+        tArtist.insert(7, "m6");
 
         // test versions assigned on fetch... clean up domain cache
-        // before doing it
-        getDomain().getEventManager().removeAllListeners(
-                getDomain().getSharedSnapshotCache().getSnapshotEventSubject());
-        getDomain().getSharedSnapshotCache().clear();
-        context = createDataContext();
 
-        List artists = context.performQuery(new SelectQuery(Artist.class));
-        Artist artist = (Artist) artists.get(0);
+        List<Artist> artists = context.performQuery(new SelectQuery(Artist.class));
+        Artist artist = artists.get(0);
 
         assertFalse(DataObject.DEFAULT_VERSION == artist.getSnapshotVersion());
         assertEquals(context
@@ -269,9 +281,10 @@ public class CayenneDataObjectInCtxtTest
     }
 
     public void testSnapshotVersion3() {
-        DataContext context = createDataContext();
 
-        Artist artist = newSavedArtist(context);
+        Artist artist = context.newObject(Artist.class);
+        artist.setArtistName("qY");
+        context.commitChanges();
 
         // test versions assigned after update
         long oldVersion = artist.getSnapshotVersion();
@@ -292,7 +305,6 @@ public class CayenneDataObjectInCtxtTest
      * http://objectstyle.org/cayenne/lists/cayenne-user/2005/01/0210.html
      */
     public void testObjectsCommittedManualOID() {
-        DataContext context = createDataContext();
 
         Artist object = context.newObject(Artist.class);
         object.setArtistName("ABC1");
@@ -315,20 +327,4 @@ public class CayenneDataObjectInCtxtTest
         assertEquals("ABC1", object2.getArtistName());
     }
 
-    private Artist newSavedArtist(DataContext context) {
-        Artist o1 = new Artist();
-        o1.setArtistName("a");
-        o1.setDateOfBirth(new java.sql.Date(System.currentTimeMillis()));
-        context.registerNewObject(o1);
-        context.commitChanges();
-        return o1;
-    }
-
-    private Artist fetchArtist(DataContext context, String name) {
-        SelectQuery q = new SelectQuery("Artist", ExpressionFactory.matchExp(
-                "artistName",
-                name));
-        List ats = context.performQuery(q);
-        return (ats.size() > 0) ? (Artist) ats.get(0) : null;
-    }
 }

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsTest.java
(from r956895, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsTest.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsTest.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelTest.java&r1=956895&r2=957022&rev=957022&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsTest.java
Tue Jun 22 20:42:43 2010
@@ -21,85 +21,112 @@ package org.apache.cayenne;
 
 import java.util.List;
 
-import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.access.ToManyList;
-import org.apache.cayenne.exp.ExpressionFactory;
-import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.testmap.ArtGroup;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.testdo.testmap.Gallery;
 import org.apache.cayenne.testdo.testmap.Painting;
 import org.apache.cayenne.testdo.testmap.PaintingInfo;
-import org.apache.cayenne.unit.CaseDataFactory;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
 
-public class CayenneDataObjectRelTest extends CayenneDOTestBase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class CayenneDataObjectRelationshipsTest extends ServerCase {
 
-    private void prepareNestedProperties() throws Exception {
-        Artist a1 = super.newArtist();
-        Painting p1 = super.newPainting();
-        PaintingInfo pi1 = super.newPaintingInfo();
-        Gallery g1 = super.newGallery();
-
-        p1.setToArtist(a1);
-        p1.setToPaintingInfo(pi1);
-        p1.setToGallery(g1);
-        ctxt.commitChanges();
-        ctxt = createDataContext();
+    @Inject
+    private ObjectContext context;
+
+    @Inject
+    private ServerRuntime runtime;
+
+    @Inject
+    private DBHelper dbHelper;
+
+    private TableHelper tArtist;
+    private TableHelper tPaintingInfo;
+    private TableHelper tPainting;
+
+    @Override
+    protected void setUpAfterInjection() throws Exception {
+        dbHelper.deleteAll("PAINTING_INFO");
+        dbHelper.deleteAll("PAINTING");
+        dbHelper.deleteAll("ARTIST_EXHIBIT");
+        dbHelper.deleteAll("ARTIST");
+
+        tArtist = new TableHelper(dbHelper, "ARTIST");
+        tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+        tPainting = new TableHelper(dbHelper, "PAINTING");
+        tPainting.setColumns(
+                "PAINTING_ID",
+                "PAINTING_TITLE",
+                "ARTIST_ID");
+
+        tPaintingInfo = new TableHelper(dbHelper, "PAINTING_INFO");
+        tPaintingInfo.setColumns("PAINTING_ID", "TEXT_REVIEW");
+    }
+
+    private void createArtistWithPaintingDataSet() throws Exception {
+        tArtist.insert(8, "aX");
+        tPainting.insert(6, "pW", 8);
+    }
+
+    private void createArtistWithPaintingAndInfoDataSet() throws Exception {
+        tArtist.insert(8, "aX");
+        tPainting.insert(6, "pW", 8);
+        tPaintingInfo.insert(6, "mE");
     }
 
     public void testReadNestedProperty1() throws Exception {
-        prepareNestedProperties();
+        createArtistWithPaintingDataSet();
 
-        Painting p1 = fetchPainting();
-        assertEquals(artistName, p1.readNestedProperty("toArtist.artistName"));
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
+        assertEquals("aX", p1.readNestedProperty("toArtist.artistName"));
     }
 
     public void testReadNestedProperty2() throws Exception {
-        prepareNestedProperties();
+        createArtistWithPaintingDataSet();
 
-        Painting p1 = fetchPainting();
-        assertTrue(p1.getToArtist().readNestedProperty("paintingArray") instanceof List);
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
+        assertTrue(p1.getToArtist().readNestedProperty("paintingArray") instanceof List<?>);
     }
 
     public void testReciprocalRel1() throws Exception {
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {
-            paintingName
-        }, false);
+        createArtistWithPaintingDataSet();
 
-        Painting p1 = fetchPainting();
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
         Artist a1 = p1.getToArtist();
 
         assertNotNull(a1);
-        assertEquals(artistName, a1.getArtistName());
+        assertEquals("aX", a1.getArtistName());
 
-        List paintings = a1.getPaintingArray();
+        List<Painting> paintings = a1.getPaintingArray();
         assertEquals(1, paintings.size());
-        Painting p2 = (Painting) paintings.get(0);
+        Painting p2 = paintings.get(0);
         assertSame(p1, p2);
     }
 
     public void testReadToOneRel1() throws Exception {
-        // read to-one relationship
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {
-            paintingName
-        }, false);
+        createArtistWithPaintingDataSet();
 
-        Painting p1 = fetchPainting();
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
         Artist a1 = p1.getToArtist();
 
         assertNotNull(a1);
         assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
-        assertEquals(artistName, a1.getArtistName());
+        assertEquals("aX", a1.getArtistName());
         assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
     }
 
     public void testReadToOneRel2() throws Exception {
         // test chained calls to read relationships
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {
-            paintingName
-        }, true);
+        createArtistWithPaintingAndInfoDataSet();
 
-        PaintingInfo pi1 = fetchPaintingInfo(paintingName);
+        PaintingInfo pi1 = Cayenne.objectForPK(context, PaintingInfo.class, 6);
         Painting p1 = pi1.getPainting();
         p1.getPaintingTitle();
 
@@ -107,115 +134,108 @@ public class CayenneDataObjectRelTest ex
 
         assertNotNull(a1);
         assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
-        assertEquals(artistName, a1.getArtistName());
+        assertEquals("aX", a1.getArtistName());
         assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
     }
 
     public void testReadToOneRel3() throws Exception {
-        // test null relationship destination
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {
-            paintingName
-        }, false);
+        createArtistWithPaintingDataSet();
 
-        Painting p1 = fetchPainting();
+        Painting p1 = Cayenne.objectForPK(context, Painting.class, 6);
         Gallery g1 = p1.getToGallery();
         assertNull(g1);
     }
 
     public void testReadToManyRel1() throws Exception {
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {
-            paintingName
-        }, false);
+        createArtistWithPaintingDataSet();
 
-        Artist a1 = fetchArtist();
-        List plist = a1.getPaintingArray();
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 8);
+        List<Painting> plist = a1.getPaintingArray();
 
         assertNotNull(plist);
         assertEquals(1, plist.size());
-        assertEquals(PersistenceState.COMMITTED, ((Painting) plist.get(0))
-                .getPersistenceState());
-        assertEquals(paintingName, ((Painting) plist.get(0)).getPaintingTitle());
+        assertEquals(PersistenceState.COMMITTED, plist.get(0).getPersistenceState());
+        assertEquals("pW", plist.get(0).getPaintingTitle());
     }
 
     public void testReadToManyRel2() throws Exception {
         // test empty relationship
-        CaseDataFactory.createArtistWithPainting(artistName, new String[] {}, false);
+        tArtist.insert(11, "aX");
 
-        Artist a1 = fetchArtist();
-        List plist = a1.getPaintingArray();
+        Artist a1 = Cayenne.objectForPK(context, Artist.class, 11);
+        List<Painting> plist = a1.getPaintingArray();
 
         assertNotNull(plist);
         assertEquals(0, plist.size());
     }
 
     public void testReflexiveRelationshipInsertOrder1() {
-        DataContext dc = this.createDataContext();
-        ArtGroup parentGroup = (ArtGroup) dc.newObject("ArtGroup");
+
+        ArtGroup parentGroup = context.newObject(ArtGroup.class);
         parentGroup.setName("parent");
 
-        ArtGroup childGroup1 = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup childGroup1 = context.newObject(ArtGroup.class);
         childGroup1.setName("child1");
         childGroup1.setToParentGroup(parentGroup);
-        dc.commitChanges();
+        context.commitChanges();
     }
 
     public void testReflexiveRelationshipInsertOrder2() {
-        // Create in a different order and see what happens
-        DataContext dc = this.createDataContext();
-        ArtGroup childGroup1 = (ArtGroup) dc.newObject("ArtGroup");
+
+        ArtGroup childGroup1 = context.newObject(ArtGroup.class);
         childGroup1.setName("child1");
 
-        ArtGroup parentGroup = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup parentGroup = context.newObject(ArtGroup.class);
         parentGroup.setName("parent");
 
         childGroup1.setToParentGroup(parentGroup);
 
-        dc.commitChanges();
+        context.commitChanges();
     }
 
     public void testReflexiveRelationshipInsertOrder3() {
-        // Tey multiple children, one created before parent, one after
-        DataContext dc = this.createDataContext();
-        ArtGroup childGroup1 = (ArtGroup) dc.newObject("ArtGroup");
+        // multiple children, one created before parent, one after
+
+        ArtGroup childGroup1 = context.newObject(ArtGroup.class);
         childGroup1.setName("child1");
 
-        ArtGroup parentGroup = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup parentGroup = context.newObject(ArtGroup.class);
         parentGroup.setName("parent");
 
         childGroup1.setToParentGroup(parentGroup);
 
-        ArtGroup childGroup2 = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup childGroup2 = context.newObject(ArtGroup.class);
         childGroup2.setName("child2");
         childGroup2.setToParentGroup(parentGroup);
 
-        dc.commitChanges();
+        context.commitChanges();
     }
 
     public void testReflexiveRelationshipInsertOrder4() {
-        // Tey multiple children, one created before parent, one after
-        DataContext dc = this.createDataContext();
-        ArtGroup childGroup1 = (ArtGroup) dc.newObject("ArtGroup");
+        // multiple children, one created before parent, one after
+
+        ArtGroup childGroup1 = context.newObject(ArtGroup.class);
         childGroup1.setName("child1");
 
-        ArtGroup parentGroup = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup parentGroup = context.newObject(ArtGroup.class);
         parentGroup.setName("parent");
 
         childGroup1.setToParentGroup(parentGroup);
 
-        ArtGroup childGroup2 = (ArtGroup) dc.newObject("ArtGroup");
+        ArtGroup childGroup2 = context.newObject(ArtGroup.class);
         childGroup2.setName("subchild");
         childGroup2.setToParentGroup(childGroup1);
 
-        dc.commitChanges();
+        context.commitChanges();
     }
 
     public void testCrossContextRelationshipException() {
-        DataContext otherContext = createDataContext();
+
         // Create this object in one context...
-        Artist artist = (Artist) ctxt.newObject("Artist");
+        Artist artist = context.newObject(Artist.class);
+
         // ...and this object in another context
-        Painting painting = (Painting) otherContext
-                .newObject("Painting");
+        Painting painting = runtime.getContext().newObject(Painting.class);
 
         // Check setting a toOne relationship
         try {
@@ -238,43 +258,34 @@ public class CayenneDataObjectRelTest ex
         }
 
         assertEquals(0, artist.getPaintingArray().size());
-
     }
 
     public void testComplexInsertUpdateOrdering() {
-        Artist artist = (Artist) ctxt.newObject("Artist");
+        Artist artist = context.newObject(Artist.class);
         artist.setArtistName("a name");
 
-        ctxt.commitChanges();
+        context.commitChanges();
 
         // Cause an update and an insert that need correct ordering
-        Painting painting = (Painting) ctxt.newObject("Painting");
+        Painting painting = context.newObject(Painting.class);
         painting.setPaintingTitle("a painting");
         artist.addToPaintingArray(painting);
 
-        ctxt.commitChanges();
-
-        ctxt.deleteObject(artist);
-        ctxt.commitChanges();
-    }
+        context.commitChanges();
 
-    private PaintingInfo fetchPaintingInfo(String name) {
-        SelectQuery q = new SelectQuery("PaintingInfo", ExpressionFactory.matchExp(
-                "painting.paintingTitle",
-                name));
-        List pts = ctxt.performQuery(q);
-        return (pts.size() > 0) ? (PaintingInfo) pts.get(0) : null;
+        context.deleteObject(artist);
+        context.commitChanges();
     }
 
     public void testNewToMany() throws Exception {
-        Artist artist = ctxt.newObject(Artist.class);
+        Artist artist = context.newObject(Artist.class);
         artist.setArtistName("test");
         assertTrue(artist.readPropertyDirectly("paintingArray") instanceof ToManyList);
 
         ToManyList list = (ToManyList) artist.readPropertyDirectly("paintingArray");
         assertFalse(list.isFault());
 
-        ctxt.commitChanges();
+        context.commitChanges();
 
         assertFalse(list.isFault());
     }



Mime
View raw message