PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jpa/JPABytesTest.java

http://datanucleus-appengine.googlecode.com/
Java | 115 lines | 85 code | 12 blank | 18 comment | 0 complexity | a0f4320979daac55a54dec0a8a3c241b MD5 | raw file
Possible License(s): Apache-2.0
  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.jpa;
  14. import com.google.appengine.api.datastore.Blob;
  15. import com.google.appengine.api.datastore.Entity;
  16. import com.google.appengine.api.datastore.EntityNotFoundException;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.api.datastore.ShortBlob;
  19. import com.google.appengine.datanucleus.PrimitiveArrays;
  20. import com.google.appengine.datanucleus.SerializationManager;
  21. import com.google.appengine.datanucleus.SerializationStrategy;
  22. import com.google.appengine.datanucleus.test.jpa.HasBytesJPA;
  23. import java.util.Arrays;
  24. import java.util.HashSet;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. public class JPABytesTest extends JPATestCase {
  29. private static final SerializationStrategy SS =
  30. SerializationManager.DEFAULT_SERIALIZATION_STRATEGY;
  31. public void testInsert() throws EntityNotFoundException {
  32. HasBytesJPA pojo = new HasBytesJPA();
  33. pojo.setPrimBytes("prim bytes".getBytes());
  34. pojo.setBytes(PrimitiveArrays.asList("bytes".getBytes()).toArray(new Byte[5]));
  35. pojo.setByteList(PrimitiveArrays.asList("byte list".getBytes()));
  36. pojo.setByteSet(new HashSet<Byte>(PrimitiveArrays.asList("byte set".getBytes())));
  37. pojo.setByteCollection(PrimitiveArrays.asList("byte collection".getBytes()));
  38. pojo.setOnePrimByte(Integer.valueOf(1).byteValue());
  39. pojo.setOneByte(Integer.valueOf(2).byteValue());
  40. pojo.setShortBlob(new ShortBlob("short blob".getBytes()));
  41. pojo.setSerializedPrimBytes("serialized prim bytes".getBytes());
  42. pojo.setSerializedBytes(PrimitiveArrays.asList("serialized bytes".getBytes()).toArray(new Byte[5]));
  43. pojo.setSerializedByteList(PrimitiveArrays.asList("serialized byte list".getBytes()));
  44. pojo.setSerializedByteSet(new HashSet<Byte>(PrimitiveArrays.asList("serialized byte set".getBytes())));
  45. beginTxn();
  46. em.persist(pojo);
  47. commitTxn();
  48. Entity e = ds.get(KeyFactory.createKey(HasBytesJPA.class.getSimpleName(), pojo.getId()));
  49. assertEquals(new ShortBlob("prim bytes".getBytes()), e.getProperty("primBytes"));
  50. assertEquals(new ShortBlob("bytes".getBytes()), e.getProperty("bytes"));
  51. assertEquals(new ShortBlob("byte list".getBytes()), e.getProperty("byteList"));
  52. assertEquals(new HashSet<Byte>(PrimitiveArrays.asList("byte set".getBytes())),
  53. new HashSet<Byte>(PrimitiveArrays.asList(
  54. ((ShortBlob) e.getProperty("byteSet")).getBytes())));
  55. assertEquals(new ShortBlob("byte collection".getBytes()), e.getProperty("byteCollection"));
  56. assertEquals(1L, e.getProperty("onePrimByte"));
  57. assertEquals(2L, e.getProperty("oneByte"));
  58. assertEquals(new ShortBlob("short blob".getBytes()), e.getProperty("shortBlob"));
  59. assertEquals(new Blob("serialized prim bytes".getBytes()), e.getProperty("serializedPrimBytes"));
  60. assertEquals(new Blob("serialized bytes".getBytes()), e.getProperty("serializedBytes"));
  61. assertEquals(SS.serialize(PrimitiveArrays.asList("serialized byte list".getBytes())),
  62. e.getProperty("serializedByteList"));
  63. assertEquals(SS.serialize(new HashSet<Byte>(PrimitiveArrays.asList(
  64. "serialized byte set".getBytes()))), e.getProperty("serializedByteSet"));
  65. }
  66. public void testFetch() {
  67. Entity e = new Entity(HasBytesJPA.class.getSimpleName());
  68. e.setProperty("primBytes", new ShortBlob("prim bytes".getBytes()));
  69. e.setProperty("bytes", new ShortBlob("bytes".getBytes()));
  70. e.setProperty("byteList", new ShortBlob("byte list".getBytes()));
  71. e.setProperty("byteSet", new ShortBlob("byte set".getBytes()));
  72. e.setProperty("byteCollection", new ShortBlob("byte collection".getBytes()));
  73. e.setProperty("onePrimByte", 1L);
  74. e.setProperty("oneByte", 2L);
  75. e.setProperty("shortBlob", new ShortBlob("short blob".getBytes()));
  76. e.setProperty("serializedPrimBytes", new Blob("serialized prim bytes".getBytes()));
  77. e.setProperty("serializedBytes", new Blob("serialized bytes".getBytes()));
  78. e.setProperty("serializedByteList", SS.serialize(PrimitiveArrays.asList("serialized byte list".getBytes())));
  79. e.setProperty("serializedByteSet", SS.serialize(new HashSet<Byte>(PrimitiveArrays.asList("serialized byte set".getBytes()))));
  80. ds.put(e);
  81. beginTxn();
  82. HasBytesJPA pojo = em.find(HasBytesJPA.class, e.getKey());
  83. assertTrue(Arrays.equals("prim bytes".getBytes(), pojo.getPrimBytes()));
  84. assertEquals(PrimitiveArrays.asList("bytes".getBytes()), Arrays.asList(pojo.getBytes()));
  85. assertEquals(PrimitiveArrays.asList("byte list".getBytes()), pojo.getByteList());
  86. assertEquals(new HashSet<Byte>(PrimitiveArrays.asList("byte set".getBytes())), pojo.getByteSet());
  87. assertEquals(PrimitiveArrays.asList("byte collection".getBytes()), pojo.getByteCollection());
  88. assertEquals(Integer.valueOf(1).byteValue(), pojo.getOnePrimByte());
  89. assertEquals(Integer.valueOf(2).byteValue(), pojo.getOneByte().byteValue());
  90. assertEquals(new ShortBlob("short blob".getBytes()), pojo.getShortBlob());
  91. assertEquals(new Blob("serialized prim bytes".getBytes()), new Blob(pojo.getSerializedPrimBytes()));
  92. assertEquals(new Blob("serialized bytes".getBytes()),
  93. new Blob(PrimitiveArrays.toByteArray(Arrays.asList(pojo.getSerializedBytes()))));
  94. assertEquals(PrimitiveArrays.asList("serialized byte list".getBytes()),
  95. pojo.getSerializedByteList());
  96. assertEquals(new HashSet<Byte>(PrimitiveArrays.asList("serialized byte set".getBytes())),
  97. pojo.getSerializedByteSet());
  98. commitTxn();
  99. }
  100. }