/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. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.Key;
  16. import com.google.appengine.api.datastore.KeyFactory;
  17. import static junit.framework.TestCase.assertEquals;
  18. import static junit.framework.TestCase.assertNull;
  19. /**
  20. * @author Max Ross <maxr@google.com>
  21. */
  22. public final class TestUtils {
  23. private TestUtils() {}
  24. public static void assertKeyParentEquals(String parentKey, Entity childEntity, Key childKey) {
  25. assertEquals(KeyFactory.stringToKey(parentKey), childEntity.getKey().getParent());
  26. assertEquals(KeyFactory.stringToKey(parentKey), childKey.getParent());
  27. }
  28. public static void assertKeyParentEquals(String parentKey, Entity childEntity, String childKey) {
  29. assertEquals(KeyFactory.stringToKey(parentKey), childEntity.getKey().getParent());
  30. assertEquals(KeyFactory.stringToKey(parentKey), KeyFactory.stringToKey(childKey).getParent());
  31. }
  32. public static void assertKeyParentEquals(Class<?> parentClass, long parentId, Entity childEntity, Key childKey) {
  33. Key parentKey = TestUtils.createKey(parentClass, parentId);
  34. assertEquals(parentKey, childEntity.getKey().getParent());
  35. assertEquals(parentKey, childKey.getParent());
  36. }
  37. public static void assertKeyParentEquals(Class<?> parentClass, long parentId, Entity childEntity, String childKey) {
  38. Key parentKey = TestUtils.createKey(parentClass, parentId);
  39. assertEquals(parentKey, childEntity.getKey().getParent());
  40. assertEquals(parentKey, KeyFactory.stringToKey(childKey).getParent());
  41. }
  42. public static void assertKeyParentEquals(Class<?> parentClass, String 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. public static void assertKeyParentEquals(Class<?> parentClass, String parentId, Entity childEntity, String childKey) {
  48. Key parentKey = TestUtils.createKey(parentClass, parentId);
  49. assertEquals(parentKey, childEntity.getKey().getParent());
  50. assertEquals(parentKey, KeyFactory.stringToKey(childKey).getParent());
  51. }
  52. public static void assertKeyParentNull(Entity childEntity, String childKey) {
  53. assertNull(childEntity.getKey().getParent());
  54. assertNull(KeyFactory.stringToKey(childKey).getParent());
  55. }
  56. public static void assertKeyParentNull(Entity childEntity, Key childKey) {
  57. assertNull(childEntity.getKey().getParent());
  58. assertNull(childKey.getParent());
  59. }
  60. public static Key createKey(Object pojo, long id) {
  61. return createKey(pojo.getClass(), id);
  62. }
  63. public static Key createKey(Class<?> clazz, long id) {
  64. return KeyFactory.createKey(clazz.getSimpleName(), id);
  65. }
  66. public static Key createKey(Object pojo, String name) {
  67. return KeyFactory.createKey(pojo.getClass().getSimpleName(), name);
  68. }
  69. public static String createKeyString(Object pojo, String name) {
  70. return createKeyString(pojo.getClass(), name);
  71. }
  72. public static String createKeyString(Class<?> clazz, String name) {
  73. return KeyFactory.keyToString(createKey(clazz, name));
  74. }
  75. public static Key createKey(Class<?> clazz, String name) {
  76. return KeyFactory.createKey(clazz.getSimpleName(), name);
  77. }
  78. }