@Test public void testTypedKeySet() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); Key<Trivial> key7 = Key.create(Trivial.class, 7); Key<Trivial> key8 = Key.create(Trivial.class, 8); Key<Trivial> key9 = Key.create(Trivial.class, 9); HasCollections hc = new HasCollections(); hc.typedKeySet = new HashSet<Key<Trivial>>(); hc.typedKeySet.add(key7); hc.typedKeySet.add(key8); hc.typedKeySet.add(key9); Key<HasCollections> key = ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); assert hc.typedKeySet instanceof HashSet<?>; assert hc.typedKeySet.size() == 3; assert hc.typedKeySet.contains(key7); assert hc.typedKeySet.contains(key8); assert hc.typedKeySet.contains(key9); }
@Test public void testCustomSet() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.customSet = new CustomSet(); hc.customSet.add(1); hc.customSet.add(2); hc.customSet.add(3); Key<HasCollections> key = ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); assertContains123(hc.customSet, CustomSet.class); }
@Test public void testBasicLists() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.integerList = Arrays.asList(1, 2, 3); hc.integerArrayList = new ArrayList<Integer>(hc.integerList); hc.integerLinkedList = new LinkedList<Integer>(hc.integerList); Key<HasCollections> key = ofy.save().entity(hc).now(); ofy.clear(); hc = ofy.load().key(key).get(); assertContains123(hc.integerList, ArrayList.class); assertContains123(hc.integerArrayList, ArrayList.class); assertContains123(hc.integerLinkedList, LinkedList.class); }
/** Rule: never store a null Collection, always leave it alone when loaded */ @Test public void testNullCollections() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.integerList = null; Key<HasCollections> key = ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); assert hc.integerList == null; // not loaded Entity e = ds().get(fact.getRawKey(key)); // rule : never store a null collection assert !e.hasProperty("integerList"); }
@Test public void testBasicSets() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.integerSet = new HashSet<Integer>(); hc.integerSet.add(1); hc.integerSet.add(2); hc.integerSet.add(3); hc.integerSortedSet = new TreeSet<Integer>(hc.integerSet); hc.integerHashSet = new HashSet<Integer>(hc.integerSet); hc.integerTreeSet = new TreeSet<Integer>(hc.integerSet); hc.integerLinkedHashSet = new LinkedHashSet<Integer>(hc.integerSet); Key<HasCollections> key = ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); assertContains123(hc.integerSet, HashSet.class); assertContains123(hc.integerSortedSet, TreeSet.class); assertContains123(hc.integerHashSet, HashSet.class); assertContains123(hc.integerTreeSet, TreeSet.class); assertContains123(hc.integerLinkedHashSet, LinkedHashSet.class); }
@Test public void testCollectionContainingNull() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.integerList = Arrays.asList((Integer) null); Key<HasCollections> key = ofy.save().entity(hc).now(); hc = ofy.load().key(key).get(); assert hc.integerList != null; assert hc.integerList.size() == 1; assert hc.integerList.get(0) == null; Entity e = ds().get(fact.getRawKey(key)); assert e.hasProperty("integerList"); List<?> l = (List<?>) e.getProperty("integerList"); assert l != null; assert l.size() == 1; assert l.get(0) == null; }
/** Test rule: never store an empty Collection, leaves value as null */ @Test public void testEmptyCollections() throws Exception { fact.register(HasCollections.class); TestObjectify ofy = this.fact.begin(); HasCollections hc = new HasCollections(); hc.integerList = new ArrayList<Integer>(); Key<HasCollections> key = ofy.save().entity(hc).now(); ofy.clear(); hc = ofy.load().key(key).get(); System.out.println(ds().get(fact.getRawKey(hc))); // This wouldn't be valid if we didn't clear the session assert hc.integerList == null; Entity e = ds().get(fact.getRawKey(key)); // rule : never store an empty collection assert !e.hasProperty("integerList"); assert hc.initializedList != null; assert hc.initializedList instanceof LinkedList<?>; }