Tests for setToManyTarget Method in CayenneDataObject Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/3be7554d Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/3be7554d Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/3be7554d
Branch: refs/heads/master Commit: 3be7554dc0362c3042904619425b73a5eba188ed Parents: 349fcde Author: jotpe <[email protected]> Authored: Mon Dec 21 14:55:04 2015 +0100 Committer: jotpe <[email protected]> Committed: Mon Dec 21 14:55:04 2015 +0100 ---------------------------------------------------------------------- ...ayenneDataObjectSetToManyCollectionTest.java | 79 +++++ .../CayenneDataObjectSetToManyListTest.java | 301 +++++++++++++++++++ .../CayenneDataObjectSetToManyMapTest.java | 96 ++++++ .../CayenneDataObjectSetToManySetTest.java | 71 +++++ 4 files changed, 547 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/3be7554d/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyCollectionTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyCollectionTest.java b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyCollectionTest.java new file mode 100644 index 0000000..10c4bf8 --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyCollectionTest.java @@ -0,0 +1,79 @@ +package org.apache.cayenne; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; + +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.relationships_collection_to_many.CollectionToMany; +import org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget; +import org.apache.cayenne.unit.di.server.CayenneProjects; +import org.apache.cayenne.unit.di.server.ServerCase; +import org.apache.cayenne.unit.di.server.UseServerRuntime; +import org.junit.Before; +import org.junit.Test; + +@UseServerRuntime(CayenneProjects.RELATIONSHIPS_COLLECTION_TO_MANY_PROJECT) +public class CayenneDataObjectSetToManyCollectionTest extends ServerCase { + + @Inject + private ObjectContext context; + + @Inject + private DBHelper dbHelper; + + @Before + public void setUp() throws Exception { + TableHelper tCollectionToMany = new TableHelper(dbHelper, "COLLECTION_TO_MANY"); + tCollectionToMany.setColumns("ID"); + + TableHelper tCollectionToManyTarget = new TableHelper(dbHelper, "COLLECTION_TO_MANY_TARGET"); + tCollectionToManyTarget.setColumns("ID", "COLLECTION_TO_MANY_ID"); + + // single data set for all tests + tCollectionToMany.insert(1).insert(2); + tCollectionToManyTarget.insert(1, 1).insert(2, 1).insert(3, 1).insert(4, 2); + } + + @Test + public void testReadToMany() throws Exception { + + CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1); + + Collection<?> targets = o1.getTargets(); + + assertNotNull(targets); + assertTrue(((ValueHolder) targets).isFault()); + + assertEquals(3, targets.size()); + + assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 1))); + assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2))); + assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 3))); + } + + /** + * Testing if collection type is Collection, everything should work fine without an + * runtimexception + * + * @throws Exception + */ + @Test + public void testRelationCollectionTypeCollection() throws Exception { + CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1); + assertTrue(o1.readProperty(CollectionToMany.TARGETS_PROPERTY) instanceof Collection); + boolean catchedSomething = false; + try { + o1.setToManyTarget(CollectionToMany.TARGETS_PROPERTY, new ArrayList<CollectionToMany>(0), true); + } catch (RuntimeException e) { + catchedSomething = true; + } + assertEquals(catchedSomething, false); + assertEquals(o1.getTargets().size(), 0); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/3be7554d/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyListTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyListTest.java b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyListTest.java new file mode 100644 index 0000000..169278b --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyListTest.java @@ -0,0 +1,301 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ + +package org.apache.cayenne; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.sql.Types; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.cayenne.configuration.server.ServerRuntime; +import org.apache.cayenne.di.Inject; +import org.apache.cayenne.query.ObjectSelect; +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.testdo.testmap.ROPainting; +import org.apache.cayenne.unit.di.server.CayenneProjects; +import org.apache.cayenne.unit.di.server.ServerCase; +import org.apache.cayenne.unit.di.server.UseServerRuntime; +import org.junit.Before; +import org.junit.Test; + +@UseServerRuntime(CayenneProjects.TESTMAP_PROJECT) +public class CayenneDataObjectSetToManyListTest extends ServerCase { + + @Inject + private ServerRuntime runtime; + + @Inject + private ObjectContext context; + + @Inject + private DBHelper dbHelper; + + protected TableHelper tArtist; + protected TableHelper tPainting; + + @Before + public void setUp() throws Exception { + tArtist = new TableHelper(dbHelper, "ARTIST"); + tArtist.setColumns("ARTIST_ID", "ARTIST_NAME"); + + tPainting = new TableHelper(dbHelper, "PAINTING"); + tPainting.setColumns("PAINTING_ID", "PAINTING_TITLE", "ARTIST_ID").setColumnTypes(Types.INTEGER, Types.VARCHAR, + Types.BIGINT); + + } + + private void createArtistWithPaintingDataSet() throws Exception { + tArtist.insert(8, "artist 8"); + tPainting.insert(6, "painting 6", 8); + tPainting.insert(7, "painting 7", 8); + tPainting.insert(8, "painting 8", 8); + } + + @Test + public void testReadRO1() throws Exception { + + createArtistWithPaintingDataSet(); + + Artist a1 = Cayenne.objectForPK(context, Artist.class, 8); + + assertEquals(a1 != null, true); + + List<ROPainting> paints = ObjectSelect.query(ROPainting.class).where(ROPainting.TO_ARTIST.eq(a1)) + .select(context); + + assertEquals(3, paints.size()); + + ROPainting rop1 = paints.get(0); + assertSame(a1, rop1.getToArtist()); + } + + @Test + public void testSetEmptyList1() throws Exception { + createArtistWithPaintingDataSet(); + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), new ArrayList<ROPainting>(0), true); + List<Painting> paints = artist.getPaintingArray(); + assertEquals(0, paints.size()); + } + + @Test + public void testSetEmptyList2() throws Exception { + createArtistWithPaintingDataSet(); + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + boolean thrown = false; + try { + artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), null, true); + } catch (IllegalArgumentException e) { + thrown = true; + } + assertEquals("should throw a IllegalArgumentException", thrown, true); + } + + @Test + public void testWrongRelName() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + boolean thrown = false; + try { + artist.setToManyTarget("doesnotexist", new ArrayList<ROPainting>(0), true); + } catch (IllegalArgumentException e) { + thrown = true; + } + assertEquals("should throw a IllegalArgumentException, because the relName does not exist", thrown, true); + + thrown = false; + try { + artist.setToManyTarget("", new ArrayList<ROPainting>(0), true); + } catch (IllegalArgumentException e) { + thrown = true; + } + assertEquals("should throw a IllegalArgumentException, because the relName is an empty string", thrown, true); + + thrown = false; + try { + artist.setToManyTarget(null, new ArrayList<ROPainting>(0), true); + } catch (IllegalArgumentException e) { + thrown = true; + } + assertEquals("should throw a IllegalArgumentException, because the relName is NULL", thrown, true); + + } + + @Test + public void testTotalDifferentPaintings() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + + // copy the paintings list. Replacing paintings wont change the copy + List<Painting> oldPaints = new ArrayList<Painting>(artist.getPaintingArray()); + System.out.println("class:" + oldPaints.getClass()); + + Painting paintX = new Painting(); + paintX.setPaintingTitle("pantingX"); + Painting paintY = new Painting(); + paintY.setPaintingTitle("paintingY"); + Painting paintZ = new Painting(); + paintZ.setPaintingTitle("paintingZ"); + + List<? extends DataObject> returnList = artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), + Arrays.asList(paintX, paintY, paintZ), true); + + assertEquals(returnList.size(), 3); + assertEquals(returnList.containsAll(oldPaints), true); + + List<Painting> newPaints = artist.getPaintingArray(); + + assertEquals(newPaints.size(), 3); + for (Painting oldPaint : oldPaints) { + // no element of oldPaints should exist in the newPaints + assertEquals(newPaints.contains(oldPaint), false); + } + } + + @Test + public void testSamePaintings() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + List<Painting> oldPaints = new ArrayList<Painting>(artist.getPaintingArray()); + + Painting paint6 = Cayenne.objectForPK(context, Painting.class, 6); + Painting paint7 = Cayenne.objectForPK(context, Painting.class, 7); + Painting paint8 = Cayenne.objectForPK(context, Painting.class, 8); + + List<Painting> newPaints = Arrays.asList(paint6, paint7, paint8); + List<? extends DataObject> returnList = artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), newPaints, + true); + + assertEquals(returnList.size(), 0); + + newPaints = artist.getPaintingArray(); + // testing if oldPaints and newPaints contain the same objects + assertEquals(newPaints.size(), 3); + assertEquals(oldPaints.size(), 3); + assertEquals(newPaints.containsAll(oldPaints), true); + } + + @Test + public void testOldPlusNewPaintings() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + List<Painting> oldPaints = artist.getPaintingArray(); + + List<Painting> newPaints = new ArrayList<Painting>(6); + for (int i = 0; i < oldPaints.size(); i++) { + newPaints.add(oldPaints.get(i)); + } + + Painting paintX = new Painting(); + paintX.setPaintingTitle("pantingX"); + Painting paintY = new Painting(); + paintY.setPaintingTitle("paintingY"); + Painting paintZ = new Painting(); + paintZ.setPaintingTitle("paintingZ"); + + newPaints.add(paintX); + newPaints.add(paintY); + newPaints.add(paintZ); + + artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), newPaints, true); + + List<Painting> newPaints2 = artist.getPaintingArray(); + Painting paint6 = Cayenne.objectForPK(context, Painting.class, 6); + Painting paint7 = Cayenne.objectForPK(context, Painting.class, 7); + Painting paint8 = Cayenne.objectForPK(context, Painting.class, 8); + + assertEquals(newPaints2.size(), 6); + assertEquals(newPaints2.contains(paintX), true); + assertEquals(newPaints2.contains(paintY), true); + assertEquals(newPaints2.contains(paintZ), true); + assertEquals(newPaints2.contains(paint6), true); + assertEquals(newPaints2.contains(paint7), true); + assertEquals(newPaints2.contains(paint8), true); + } + + @Test + public void testRemoveOneOldAndAddOneNewPaintings() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + + List<Painting> newPaints = new ArrayList<Painting>(); + + Painting paint6 = artist.getPaintingArray().get(0); + Painting paint7 = artist.getPaintingArray().get(1); + Painting paint8 = artist.getPaintingArray().get(2); + Painting paintX = new Painting(); + paintX.setPaintingTitle("pantingX"); + Painting paintY = new Painting(); + paintY.setPaintingTitle("paintingY"); + + newPaints.add(paint6); + newPaints.add(paint7); + newPaints.add(paintX); + newPaints.add(paintY); + + List<? extends DataObject> returnList = artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), newPaints, + true); + + assertEquals(returnList.size(), 1); + assertEquals(returnList.get(0) == paint8, true); + + List<Painting> newPaints2 = artist.getPaintingArray(); + + assertEquals(newPaints2.size(), 4); + assertEquals(newPaints2.contains(paintX), true); + assertEquals(newPaints2.contains(paintY), true); + assertEquals(newPaints2.contains(paint6), true); + assertEquals(newPaints2.contains(paint7), true); + } + + /** + * Testing if collection type is list, everything should work fine without an + * runtimexception + * + * @throws Exception + */ + @Test + public void testRelationCollectionTypeList() throws Exception { + createArtistWithPaintingDataSet(); + + Artist artist = Cayenne.objectForPK(context, Artist.class, 8); + assertTrue(artist.readProperty(Artist.PAINTING_ARRAY.getName()) instanceof List); + boolean catchedSomething = false; + try { + artist.setToManyTarget(Artist.PAINTING_ARRAY.getName(), new ArrayList<Painting>(0), true); + } catch (UnsupportedOperationException e) { + catchedSomething = true; + } + assertEquals(catchedSomething, false); + assertEquals(artist.getPaintingArray().size(), 0); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/3be7554d/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyMapTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyMapTest.java b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyMapTest.java new file mode 100644 index 0000000..45a1d7e --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManyMapTest.java @@ -0,0 +1,96 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ + +package org.apache.cayenne; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Map; + +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.map_to_many.MapToMany; +import org.apache.cayenne.unit.di.server.CayenneProjects; +import org.apache.cayenne.unit.di.server.ServerCase; +import org.apache.cayenne.unit.di.server.UseServerRuntime; +import org.junit.Before; +import org.junit.Test; + +@UseServerRuntime(CayenneProjects.MAP_TO_MANY_PROJECT) +public class CayenneDataObjectSetToManyMapTest extends ServerCase { + + @Inject + protected ObjectContext context; + + @Inject + protected DBHelper dbHelper; + + protected TableHelper tMapToMany; + protected TableHelper tMapToManyTarget; + protected TableHelper tIdMapToMany; + protected TableHelper tIdMapToManyTarget; + + @Before + public void setUp() throws Exception { + tMapToMany = new TableHelper(dbHelper, "MAP_TO_MANY"); + tMapToMany.setColumns("ID"); + + tMapToManyTarget = new TableHelper(dbHelper, "MAP_TO_MANY_TARGET"); + tMapToManyTarget.setColumns("ID", "MAP_TO_MANY_ID", "NAME"); + + tIdMapToMany = new TableHelper(dbHelper, "ID_MAP_TO_MANY"); + tIdMapToMany.setColumns("ID"); + + tIdMapToManyTarget = new TableHelper(dbHelper, "ID_MAP_TO_MANY_TARGET"); + tIdMapToManyTarget.setColumns("ID", "MAP_TO_MANY_ID"); + } + + + protected void createTestDataSet() throws Exception { + tMapToMany.insert(1); + tMapToMany.insert(2); + tMapToManyTarget.insert(1, 1, "A"); + tMapToManyTarget.insert(2, 1, "B"); + tMapToManyTarget.insert(3, 1, "C"); + tMapToManyTarget.insert(4, 2, "A"); + } + + /** + * Testing if collection type is map, everything should work fine without an runtimexception + * @throws Exception + */ + @Test + public void testRelationCollectionTypeMap() throws Exception { + createTestDataSet(); + + MapToMany o1 = Cayenne.objectForPK(context, MapToMany.class, 1); + assertTrue (o1.readProperty(MapToMany.TARGETS_PROPERTY) instanceof Map); + boolean catchedSomething = false; + try { + o1.setToManyTarget(MapToMany.TARGETS_PROPERTY, new ArrayList<MapToMany>(0), true); + } catch(RuntimeException e) { + catchedSomething = true; + } + assertEquals(catchedSomething,false); + assertEquals(o1.getTargets().size(),0); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/3be7554d/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManySetTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManySetTest.java b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManySetTest.java new file mode 100644 index 0000000..944c128 --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectSetToManySetTest.java @@ -0,0 +1,71 @@ +package org.apache.cayenne; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Set; + +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.map_to_many.MapToMany; +import org.apache.cayenne.testdo.relationships_set_to_many.SetToMany; +import org.apache.cayenne.unit.di.server.CayenneProjects; +import org.apache.cayenne.unit.di.server.ServerCase; +import org.apache.cayenne.unit.di.server.UseServerRuntime; +import org.junit.Before; +import org.junit.Test; + +@UseServerRuntime(CayenneProjects.RELATIONSHIPS_SET_TO_MANY_PROJECT) +public class CayenneDataObjectSetToManySetTest extends ServerCase { + + @Inject + protected ObjectContext context; + + @Inject + protected DBHelper dbHelper; + + protected TableHelper tSetToMany; + protected TableHelper tSetToManyTarget; + + @Before + public void setUp() throws Exception { + tSetToMany = new TableHelper(dbHelper, "SET_TO_MANY"); + tSetToMany.setColumns("ID"); + + tSetToManyTarget = new TableHelper(dbHelper, "SET_TO_MANY_TARGET"); + tSetToManyTarget.setColumns("ID", "SET_TO_MANY_ID"); + } + + protected void createTestDataSet() throws Exception { + tSetToMany.insert(1); + tSetToMany.insert(2); + tSetToManyTarget.insert(1, 1); + tSetToManyTarget.insert(2, 1); + tSetToManyTarget.insert(3, 1); + tSetToManyTarget.insert(4, 2); + } + + /** + * Testing if collection type is set, everything should work fine without an + * runtimexception + * + * @throws Exception + */ + @Test + public void testRelationCollectionTypeMap() throws Exception { + createTestDataSet(); + + SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1); + assertTrue(o1.readProperty(SetToMany.TARGETS_PROPERTY) instanceof Set); + boolean catchedSomething = false; + try { + o1.setToManyTarget(SetToMany.TARGETS_PROPERTY, new ArrayList<MapToMany>(0), true); + } catch (RuntimeException e) { + catchedSomething = true; + } + assertEquals(catchedSomething, false); + assertEquals(o1.getTargets().size(), 0); + } +}
