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