PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/TestUtils.java

http://datanucleus-appengine.googlecode.com/
Java | 99 lines | 63 code | 18 blank | 18 comment | 0 complexity | 043b150edd96f7edc2baacfffa34c0bf 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;
17
18import com.google.appengine.api.datastore.Entity;
19import com.google.appengine.api.datastore.Key;
20import com.google.appengine.api.datastore.KeyFactory;
21
22import static junit.framework.TestCase.assertEquals;
23import static junit.framework.TestCase.assertNull;
24
25/**
26 * @author Max Ross <maxr@google.com>
27 */
28public final class TestUtils {
29
30  private TestUtils() {}
31
32  public static void assertKeyParentEquals(String parentKey, Entity childEntity, Key childKey) {
33    assertEquals(KeyFactory.stringToKey(parentKey), childEntity.getKey().getParent());
34    assertEquals(KeyFactory.stringToKey(parentKey), childKey.getParent());
35  }
36
37  public static void assertKeyParentEquals(String parentKey, Entity childEntity, String childKey) {
38    assertEquals(KeyFactory.stringToKey(parentKey), childEntity.getKey().getParent());
39    assertEquals(KeyFactory.stringToKey(parentKey), KeyFactory.stringToKey(childKey).getParent());
40  }
41
42  public static void assertKeyParentEquals(Class<?> parentClass, long parentId, Entity childEntity, Key childKey) {
43    Key parentKey = TestUtils.createKey(parentClass, parentId);
44    assertEquals(parentKey, childEntity.getKey().getParent());
45    assertEquals(parentKey, childKey.getParent());
46  }
47
48  public static void assertKeyParentEquals(Class<?> parentClass, long parentId, Entity childEntity, String childKey) {
49    Key parentKey = TestUtils.createKey(parentClass, parentId);
50    assertEquals(parentKey, childEntity.getKey().getParent());
51    assertEquals(parentKey, KeyFactory.stringToKey(childKey).getParent());
52  }
53
54  public static void assertKeyParentEquals(Class<?> parentClass, String parentId, Entity childEntity, Key childKey) {
55    Key parentKey = TestUtils.createKey(parentClass, parentId);
56    assertEquals(parentKey, childEntity.getKey().getParent());
57    assertEquals(parentKey, childKey.getParent());
58  }
59
60  public static void assertKeyParentEquals(Class<?> parentClass, String parentId, Entity childEntity, String childKey) {
61    Key parentKey = TestUtils.createKey(parentClass, parentId);
62    assertEquals(parentKey, childEntity.getKey().getParent());
63    assertEquals(parentKey, KeyFactory.stringToKey(childKey).getParent());
64  }
65
66  public static void assertKeyParentNull(Entity childEntity, String childKey) {
67    assertNull(childEntity.getKey().getParent());
68    assertNull(KeyFactory.stringToKey(childKey).getParent());
69  }
70
71  public static void assertKeyParentNull(Entity childEntity, Key childKey) {
72    assertNull(childEntity.getKey().getParent());
73    assertNull(childKey.getParent());
74  }
75
76  public static Key createKey(Object pojo, long id) {
77    return createKey(pojo.getClass(), id);
78  }
79
80  public static Key createKey(Class<?> clazz, long id) {
81    return KeyFactory.createKey(clazz.getSimpleName(), id);
82  }
83
84  public static Key createKey(Object pojo, String name) {
85    return KeyFactory.createKey(pojo.getClass().getSimpleName(), name);
86  }
87
88  public static String createKeyString(Object pojo, String name) {
89    return createKeyString(pojo.getClass(), name);
90  }
91
92  public static String createKeyString(Class<?> clazz, String name) {
93    return KeyFactory.keyToString(createKey(clazz, name));
94  }
95
96  public static Key createKey(Class<?> clazz, String name) {
97    return KeyFactory.createKey(clazz.getSimpleName(), name);
98  }
99}