PageRenderTime 56ms CodeModel.GetById 12ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/query/JoinHelperTest.java

http://datanucleus-appengine.googlecode.com/
Java | 266 lines | 220 code | 28 blank | 18 comment | 4 complexity | ae608cb542142229405e45257857c4e8 MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.query;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.Key;
 20import com.google.appengine.api.datastore.KeyFactory;
 21import com.google.appengine.api.datastore.Link;
 22import com.google.appengine.datanucleus.Utils;
 23import com.google.appengine.datanucleus.jdo.JDOTestCase;
 24
 25
 26import java.util.Arrays;
 27import java.util.Collections;
 28import java.util.Date;
 29import java.util.List;
 30
 31/**
 32 * @author Max Ross <maxr@google.com>
 33 */
 34public class JoinHelperTest extends JDOTestCase {
 35
 36  private static final String JOIN_PROP = "joinProp";
 37
 38  private JoinHelper joinHelper;
 39
 40  @Override
 41  protected void setUp() throws Exception {
 42    super.setUp();
 43    joinHelper = new JoinHelper();
 44  }
 45
 46  public void testNoData() {
 47    assertNoResults(Collections.<Entity>emptyList());
 48    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 49  }
 50
 51  public void testNoParentData() {
 52    Entity e = new Entity("Child", "k1");
 53    assertNoResults(Collections.<Entity>emptyList(), e);
 54    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 55  }
 56
 57  public void testNoChildData() {
 58    Entity e = newParentEntity("k1", KeyFactory.createKey("Child", 10));
 59    assertNoResults(Collections.<Entity>singleton(e));
 60    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 61  }
 62
 63  public void testParentHasNoJoinKeyProperty() {
 64    Entity e = new Entity("Parent", "k1");
 65    Entity c1 = new Entity("Child", "k0");
 66    Entity c2 = new Entity("Child", "k1");
 67    Entity c3 = new Entity("Child", "k2");
 68    assertNoResults(Collections.singleton(e), c1, c2, c3);
 69    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 70  }
 71
 72  public void testParentHasNullJoinKeyProperty() {
 73    Entity e = new Entity("Parent", "k1");
 74    e.setProperty(JOIN_PROP, null);
 75    Entity c1 = new Entity("Child", "k0");
 76    Entity c2 = new Entity("Child", "k1");
 77    Entity c3 = new Entity("Child", "k2");
 78    assertNoResults(Collections.singleton(e), c1, c2, c3);
 79    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 80  }
 81
 82  public void testParentHasNonKeyJoinKeyProperty() {
 83    Entity e = new Entity("Parent", "k1");
 84    e.setProperty(JOIN_PROP, "not a key");
 85    Entity c1 = new Entity("Child", "k0");
 86    Entity c2 = new Entity("Child", "k1");
 87    Entity c3 = new Entity("Child", "k2");
 88    assertNoResults(Collections.singleton(e), c1, c2, c3);
 89    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 90  }
 91
 92  public void testParentHasNonKeyInJoinKeyList() {
 93    Entity e = newParentEntity("k1", 23, "not a key");
 94    Entity c1 = new Entity("Child", "k0");
 95    Entity c2 = new Entity("Child", "k1");
 96    Entity c3 = new Entity("Child", "k2");
 97    assertNoResults(Collections.singleton(e), c1, c2, c3);
 98    assertTrue(joinHelper.getMaterializedChildKeys().isEmpty());
 99  }
100
101  public void testAllChildrenLargerThanAllJoinKeys() {
102    Key key1 = KeyFactory.createKey("Child", "k0");
103    Key key2 = KeyFactory.createKey("Child", "k1");
104    Key key3 = KeyFactory.createKey("Child", "k2");
105    Entity c1 = new Entity("Child", "k3");
106    Entity p1 = newParentEntity("p1", key1);
107    Entity p2 = newParentEntity("p2", key2);
108    Entity p3 = newParentEntity("p3", key3);
109    assertNoResults(Utils.newArrayList(p1, p2, p3), c1);
110    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
111  }
112
113  public void testAllChildrenSmallerThanAllJoinKeys() {
114    Key key1 = KeyFactory.createKey("Child", "k1");
115    Key key2 = KeyFactory.createKey("Child", "k2");
116    Key key3 = KeyFactory.createKey("Child", "k3");
117    Entity c1 = new Entity("Child", "k0");
118    Entity p1 = newParentEntity("p1", key1);
119    Entity p2 = newParentEntity("p2", key2);
120    Entity p3 = newParentEntity("p3", key3);
121    assertNoResults(Utils.newArrayList(p1, p2, p3), c1);
122    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
123  }
124
125  public void testOneMatch_OnlyJoinKey() {
126    Key key1 = KeyFactory.createKey("Child", "k1");
127    Key key2 = KeyFactory.createKey("Child", "k2");
128    Key key3 = KeyFactory.createKey("Child", "k3");
129    Entity c1 = new Entity("Child", "k1");
130    Entity p1 = newParentEntity("p1", key1);
131    Entity p2 = newParentEntity("p2", key2);
132    Entity p3 = newParentEntity("p3", key3);
133    assertEquals(Utils.newArrayList(p1), mergeJoin(Utils.newArrayList(p1, p2, p3), c1));
134    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
135  }
136
137  public void testOneMatch_FirstJoinKey() {
138    Key key1 = KeyFactory.createKey("Child", "k1");
139    Key key2 = KeyFactory.createKey("Child", "k2");
140    Key key3 = KeyFactory.createKey("Child", "k3");
141    Entity c1 = new Entity("Child", "k1");
142    Entity p1 = newParentEntity("p1", key1, key2, key3);
143    Entity p2 = newParentEntity("p2", key2);
144    Entity p3 = newParentEntity("p3", key3);
145    assertEquals(Utils.newArrayList(p1), mergeJoin(Utils.newArrayList(p1, p2, p3), c1));
146    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
147  }
148
149  public void testOneMatch_LastJoinKey() {
150    Key key1 = KeyFactory.createKey("Child", "k1");
151    Key key2 = KeyFactory.createKey("Child", "k2");
152    Key key3 = KeyFactory.createKey("Child", "k3");
153    Entity c1 = new Entity("Child", "k1");
154    Entity p1 = newParentEntity("p1", key3, key2, key1);
155    Entity p2 = newParentEntity("p2", key2);
156    Entity p3 = newParentEntity("p3", key3);
157    assertEquals(Utils.newArrayList(p1), mergeJoin(Utils.newArrayList(p1, p2, p3), c1));
158    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
159  }
160
161  public void testOneMatch_MultipleJoinKeys() {
162    Key key1 = KeyFactory.createKey("Child", "k1");
163    Key key2 = KeyFactory.createKey("Child", "k2");
164    Key key3 = KeyFactory.createKey("Child", "k3");
165    Entity c1 = new Entity("Child", "k1");
166    Entity c2 = new Entity("Child", "k2");
167    Entity p1 = newParentEntity("p1", key3, key2, key1);
168    assertEquals(Utils.newArrayList(p1), mergeJoin(Utils.newArrayList(p1), c1, c2));
169    assertEquals(Utils.newHashSet(c1.getKey(), c2.getKey()), joinHelper.getMaterializedChildKeys());
170  }
171
172  public void testOneMatch_SameJoinKeyMultipleTimes() {
173    Key key1 = KeyFactory.createKey("Child", "k1");
174    Key key2 = KeyFactory.createKey("Child", "k2");
175    Key key3 = KeyFactory.createKey("Child", "k3");
176    Entity c1 = new Entity(key1);
177    Entity p1 = newParentEntity("p1", key1, key2, key1);
178    Entity p2 = newParentEntity("p2", key2);
179    Entity p3 = newParentEntity("p3", key3);
180    assertEquals(Utils.newArrayList(p1), mergeJoin(Utils.newArrayList(p1, p2, p3), c1));
181    assertEquals(Collections.singleton(c1.getKey()), joinHelper.getMaterializedChildKeys());
182  }
183
184  public void testComplicatedScenario() {
185    List<Key> childKeys = Utils.newArrayList();
186    List<Entity> childEntities = Utils.newArrayList();
187    for (int i = 1; i <= 100; i++) {
188      Key k = KeyFactory.createKey("Child", i);
189      childKeys.add(k);
190      childEntities.add(new Entity(k));
191    }
192
193    Entity p1 = newParentEntity("p1", childEntities.get(3).getKey());
194    Entity p2 = newParentEntity(
195        "p2",
196        childEntities.get(3).getKey(),
197        new Date(),
198        childEntities.get(60).getKey(),
199        childEntities.get(12).getKey());
200    Entity p3 = newParentEntity(
201        "p3",
202        childEntities.get(18).getKey(),
203        childEntities.get(19).getKey(),
204        44L,
205        childEntities.get(22).getKey());
206    Entity p4 = newParentEntity(
207        "p4",
208        childEntities.get(38).getKey(),
209        childEntities.get(89).getKey(),
210        new Link("blar"),
211        childEntities.get(60).getKey(), 
212        childEntities.get(80).getKey());
213    Entity p5 = newParentEntity(
214        "p5",
215        childEntities.get(49).getKey(),
216        childEntities.get(65).getKey(),
217        childEntities.get(99).getKey(),
218        childEntities.get(78).getKey());
219    Entity p6 = newParentEntity(
220        "p6",
221        childEntities.get(51).getKey(),
222        childEntities.get(98).getKey(),
223        childEntities.get(88).getKey(),
224        childEntities.get(52).getKey());
225    assertEquals(
226        Utils.newArrayList(p1, p2, p3, p4, p5),
227        mergeJoin(Utils.newArrayList(p1, p2, p3, p4, p5, p6), childEntities.subList(0, 50)));
228    assertEquals(Utils.newHashSet(childKeys.subList(0, 50)), joinHelper.getMaterializedChildKeys());
229  }
230
231  public void testStreaming() {
232    List<Key> childKeys = Utils.newArrayList();
233    List<Entity> childEntities = Utils.newArrayList();
234    List<Entity> parentEntities = Utils.newArrayList();
235    for (int i = 1; i <= 100; i++) {
236      Key k = KeyFactory.createKey("Child", i);
237      childKeys.add(k);
238      childEntities.add(new Entity(k));
239      parentEntities.add(newParentEntity("p" + i, k));
240    }
241    int i = 0;
242    for (Entity e : mergeJoin(parentEntities, childEntities)) {
243      assertEquals(parentEntities.get(i++), e);
244      assertEquals(Utils.newHashSet(childKeys.subList(0, i)), joinHelper.getMaterializedChildKeys());
245    }
246  }
247
248  private static Entity newParentEntity(String name, Object... joinKeys) {
249    Entity e = new Entity("parent", name);
250    Object val = joinKeys.length == 1 ? joinKeys[0] : Arrays.asList(joinKeys);
251    e.setProperty(JOIN_PROP, val);
252    return e;
253  }
254
255  private Iterable<Entity> mergeJoin(Iterable<Entity> parents, Entity... kids) {
256    return mergeJoin(parents, Arrays.asList(kids));
257  }
258
259  private Iterable<Entity> mergeJoin(Iterable<Entity> parents, Iterable<Entity> kids) {
260    return joinHelper.mergeJoin(JOIN_PROP, parents, kids.iterator());
261  }
262
263  private void assertNoResults(Iterable<Entity> parents, Entity... kids) {
264    assertFalse(mergeJoin(parents, kids).iterator().hasNext());
265  }
266}