@Test public void testSerialization2() throws Exception { Map2 map2 = new Map2(); map2.shouldBeOk.put(3, new Foo("peter")); map2.shouldBeOk.put(27, new Foo("paul")); getDs().save(map2); map2 = getDs().get(map2); Assert.assertEquals("peter", map2.shouldBeOk.get(3).id); Assert.assertEquals("paul", map2.shouldBeOk.get(27).id); }
@Test public void testExistsWithEntity() { final FacebookUser facebookUser = new FacebookUser(1, "user one"); getDs().save(facebookUser); assertEquals(1, getDs().getCount(FacebookUser.class)); assertNotNull(getDs().get(FacebookUser.class, 1)); assertNotNull(getDs().exists(facebookUser)); getDs().delete(getDs().find(FacebookUser.class)); assertEquals(0, getDs().getCount(FacebookUser.class)); assertNull(getDs().exists(facebookUser)); }
@Test public void testMapAsId() { getMorphia().map(MapAsId.class); final MapAsId mai = new MapAsId(); mai.id.put("test", "string"); final Key<MapAsId> maiKey = getDs().save(mai); final MapAsId maiLoaded = getDs().get(MapAsId.class, new BasicDBObject("test", "string")); assertNotNull(maiLoaded); assertNotNull(maiKey); }
@Test public void testTimestampMapping() { getMorphia().map(ContainsTimestamp.class); final ContainsTimestamp cts = new ContainsTimestamp(); final Key<ContainsTimestamp> savedKey = getDs().save(cts); final ContainsTimestamp loaded = getDs().get(ContainsTimestamp.class, savedKey.getId()); assertNotNull(loaded.ts); assertEquals(loaded.ts.getTime(), cts.ts.getTime()); }
@Test public void singleLookup() throws Exception { A.loadCount = 0; final A a = new A(); HoldsMultipleA holder = new HoldsMultipleA(); holder.a1 = a; holder.a2 = a; getDs().save(asList(a, holder)); holder = getDs().get(HoldsMultipleA.class, holder.id); Assert.assertEquals(1, A.loadCount); Assert.assertTrue(holder.a1 == holder.a2); }
@Test public void testFloat() throws Exception { final ContainsFloat cf = new ContainsFloat(); getDs().save(cf); final ContainsFloat loaded = getDs().get(cf); assertNotNull(loaded); assertTrue(loaded.val0 == cf.val0); assertTrue(loaded.val1.equals(cf.val1)); }
@Test public void testMapping() { E e = new E(); e.testClass = LinkedList.class; getDs().save(e); e = getDs().get(e); Assert.assertEquals(LinkedList.class, e.testClass); }
@Test public void test() throws Exception { final E e1 = new E(); Assert.assertEquals("A", e1.a1.s); Assert.assertEquals("A", e1.a2.s); getDs().save(e1); final E e2 = getDs().get(e1); Assert.assertEquals("A", e2.a1.s); Assert.assertEquals("A", e2.a2.s); Assert.assertEquals(B.class, e2.a2.getClass()); Assert.assertEquals(A.class, e2.a1.getClass()); }
@Test public void testExistsWhenItemSaved() { // given long id = System.currentTimeMillis(); final Key<FacebookUser> key = getDs().save(new FacebookUser(id, "user 1")); // expect assertNotNull(getDs().get(FacebookUser.class, id)); assertNotNull(getDs().exists(key)); }
@Test public void testUpdatesToVersionedFileAreReflectedInTheDatastore() { final Versioned version1 = new Versioned(); version1.setName("foo"); this.getDs().save(version1); final Versioned version1Updated = getDs().get(Versioned.class, version1.getId()); version1Updated.setName("bar"); this.getDs().merge(version1Updated); final Versioned versionedEntityFromDs = this.getDs().get(Versioned.class, version1.getId()); assertEquals(version1Updated.getName(), versionedEntityFromDs.getName()); }
@Test public void testMapping() throws Exception { E e = new E(); e.id = new CId("test"); getDs().save(e); e = getDs().get(e); Assert.assertEquals("test", e.id.name); Assert.assertNotNull(e.id.id); }
@Test @Ignore("does not work since the EnumConverter stores as a single string value -- no type info") public void testEnumBehindAnInterfacePersistence() throws Exception { getMorphia().map(ContainerEntity.class); ContainerEntity n = new ContainerEntity(); getDs().save(n); n = getDs().get(n); Assert.assertSame(EnumBehindAnInterface.A, n.foo); }
@Test public void testShort() throws Exception { final ContainsShort cs = new ContainsShort(); getDs().save(cs); final ContainsShort loaded = getDs().get(cs); assertNotNull(loaded); assertTrue(loaded.val0 == cs.val0); assertTrue(loaded.val1.equals(cs.val1)); }
@Test(expected = ConcurrentModificationException.class) public void testConcurrentModDetectionLong() throws Exception { final ALong a = new ALong(); Assert.assertEquals(null, a.v); getDs().save(a); getDs().save(getDs().get(a)); getDs().save(a); }
@Test(expected = ConcurrentModificationException.class) public void testConcurrentModDetectionLongWithMerge() throws Exception { final ALong a = new ALong(); Assert.assertEquals(null, a.v); getDs().save(a); a.text = " foosdfds "; final ALong a2 = getDs().get(a); getDs().save(a2); getDs().merge(a); }
@Test public void testSavingAndLoadingAClassWithDeepInheritance() { // given final Child jimmy = new Child(); jimmy.setName("jimmy"); getDs().save(jimmy); // when final Child loaded = getDs().get(Child.class, jimmy.getId()); // then assertNotNull(loaded); assertEquals(jimmy.getName(), loaded.getName()); }
@Test public void testCollectionMapping() { getMorphia().map(ContainsCollection.class); final Key<ContainsCollection> savedKey = getDs().save(new ContainsCollection()); final ContainsCollection loaded = getDs().get(ContainsCollection.class, savedKey.getId()); assertEquals(loaded.coll, (new ContainsCollection()).coll); assertNotNull(loaded.id); }
@Test public void testSerializedMapping() { getMorphia().map(ContainsSerializedData.class); final Key<ContainsSerializedData> savedKey = getDs().save(new ContainsSerializedData()); final ContainsSerializedData loaded = getDs().get(ContainsSerializedData.class, savedKey.getId()); assertNotNull(loaded.data); assertEquals(loaded.data.someString, (new ContainsSerializedData()).data.someString); assertNotNull(loaded.id); }
@Test(expected = ConcurrentModificationException.class) public void testConcurrentModDetection() throws Exception { getMorphia().map(ALongPrimitive.class); final ALongPrimitive a = new ALongPrimitive(); Assert.assertEquals(0, a.version); getDs().save(a); getDs().save(getDs().get(a)); getDs().save(a); }
@Test public void embeddedIds() { final MyId id = new MyId("1", "2"); final EmbeddedId a = new EmbeddedId(id, "data"); final EmbeddedId b = new EmbeddedId(new MyId("2", "3"), "data, too"); getDs().save(a); getDs().save(b); assertEquals(a.data, getDs().get(EmbeddedId.class, id).data); final EmbeddedId embeddedId = getDs().find(EmbeddedId.class).field("_id").in(Arrays.asList(id)).find().next(); Assert.assertEquals(a.data, embeddedId.data); Assert.assertEquals(a.id, embeddedId.id); }