Package com.google.appengine.datanucleus.test.jpa

Examples of com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkJPA


  private void testInsert_NewParentAndChild(StartEnd startEnd) throws Exception {
    Book b = newBook();
    HasKeyPkJPA hasKeyPk = new HasKeyPkJPA();
    HasOneToOneParentJPA hasParent = new HasOneToOneParentJPA();
    HasOneToOneParentKeyPkJPA hasParentKeyPk = new HasOneToOneParentKeyPkJPA();
    HasEncodedStringPkJPA notDependent = new HasEncodedStringPkJPA();

    HasOneToOneJPA pojo = new HasOneToOneJPA();
    pojo.setBook(b);
    pojo.setHasKeyPK(hasKeyPk);
    pojo.setHasParent(hasParent);
    hasParent.setParent(pojo);
    pojo.setHasParentKeyPK(hasParentKeyPk);
    hasParentKeyPk.setParent(pojo);
    pojo.setNotDependent(notDependent);

    startEnd.start();
    em.persist(pojo);
    startEnd.end();

    assertNotNull(b.getId());
    assertNotNull(hasKeyPk.getId());
    assertNotNull(hasParent.getId());
    assertNotNull(hasParentKeyPk.getId());
    assertNotNull(notDependent.getId());
    assertNotNull(pojo.getId());

    Entity bookEntity = ds.get(KeyFactory.stringToKey(b.getId()));
    assertNotNull(bookEntity);
    assertEquals("max", bookEntity.getProperty("author"));
    assertEquals("22333", bookEntity.getProperty("isbn"));
    assertEquals("yam", bookEntity.getProperty("title"));
    assertEquals(KeyFactory.stringToKey(b.getId()), bookEntity.getKey());
    assertKeyParentEquals(pojo.getId(), bookEntity, b.getId());

    Entity hasKeyPkEntity = ds.get(hasKeyPk.getId());
    assertNotNull(hasKeyPkEntity);
    assertEquals(hasKeyPk.getId(), hasKeyPkEntity.getKey());
    assertKeyParentEquals(pojo.getId(), hasKeyPkEntity, hasKeyPk.getId());

    Entity hasParentEntity = ds.get(KeyFactory.stringToKey(hasParent.getId()));
    assertNotNull(hasParentEntity);
    assertEquals(KeyFactory.stringToKey(hasParent.getId()), hasParentEntity.getKey());
    assertKeyParentEquals(pojo.getId(), hasParentEntity, hasParent.getId());

    Entity hasParentKeyPkEntity = ds.get(hasParentKeyPk.getId());
    assertNotNull(hasParentKeyPkEntity);
    assertEquals(hasParentKeyPk.getId(), hasParentKeyPkEntity.getKey());
    assertKeyParentEquals(pojo.getId(), hasParentKeyPkEntity, hasParentKeyPk.getId());

    Entity notDependentEntity = ds.get(KeyFactory.stringToKey(notDependent.getId()));
    assertNotNull(notDependentEntity);

    Entity pojoEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
    assertNotNull(pojoEntity);
    assertEquals(bookEntity.getKey(), pojoEntity.getProperty("book_id"));
    assertEquals(hasKeyPkEntity.getKey(), pojoEntity.getProperty("haskeypk_id"));
    assertEquals(hasParentEntity.getKey(), pojoEntity.getProperty("hasparent_id"));
    assertEquals(hasParentKeyPkEntity.getKey(), pojoEntity.getProperty("hasparentkeypk_id"));
    assertEquals(notDependentEntity.getKey(), pojoEntity.getProperty("notdependent_id"));

    assertCountsInDatastore(1, 1);
    assertEquals(1, countForClass(notDependent.getClass()));
  }
View Full Code Here


  public void testKeyQueryWithEncodedStringPk() {
    Entity e = new Entity(HasEncodedStringPkJPA.class.getSimpleName(), "yar");
    ds.put(e);
    Query q = em.createQuery("select from " + HasEncodedStringPkJPA.class.getName() + " c where id = :p");
    q.setParameter("p", e.getKey().getName());
    HasEncodedStringPkJPA result = (HasEncodedStringPkJPA) q.getSingleResult();
    assertEquals(KeyFactory.keyToString(e.getKey()), result.getId());

    q = em.createQuery("select from " + HasEncodedStringPkJPA.class.getName() + " c where id = :p");
    q.setParameter("p", e.getKey());
    result = (HasEncodedStringPkJPA) q.getSingleResult();
    assertEquals(KeyFactory.keyToString(e.getKey()), result.getId());

    q = em.createQuery("select from " + HasEncodedStringPkJPA.class.getName() + " c where id = :p");
    q.setParameter("p", e.getKey().getName());
    result = (HasEncodedStringPkJPA) q.getSingleResult();
    assertEquals(KeyFactory.keyToString(e.getKey()), result.getId());
  }
View Full Code Here

      rollbackTxn();
    }
  }

  public void testEncodedStringPk() throws EntityNotFoundException {
    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
    Key key = KeyFactory.createKey(HasEncodedStringPkJPA.class.getSimpleName(), "a name");
    pojo.setId(KeyFactory.keyToString(key));
    beginTxn();
    em.persist(pojo);
    commitTxn();

    assertEquals(KeyFactory.keyToString(key), pojo.getId());
    Entity e = ds.get(KeyFactory.stringToKey(pojo.getId()));

    beginTxn();
    em.find(HasEncodedStringPkJPA.class, e.getKey().getName());
    em.find(HasEncodedStringPkJPA.class, e.getKey());
    em.find(HasEncodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
View Full Code Here

    em.find(HasEncodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
    commitTxn();
  }

  public void testEncodedStringPk_NullValue() throws EntityNotFoundException {
    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
    beginTxn();
    em.persist(pojo);
    commitTxn();
    assertNotNull(pojo.getId());
    Key key = KeyFactory.stringToKey(pojo.getId());
    Entity e = ds.get(key);

    beginTxn();
    em.find(HasEncodedStringPkJPA.class, e.getKey().getId());
    em.find(HasEncodedStringPkJPA.class, e.getKey());
View Full Code Here

    em.find(HasEncodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
    commitTxn();
  }

  public void testEncodedStringPk_NonKeyValue() throws EntityNotFoundException {
    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
    pojo.setId("yar");
    beginTxn();
    em.persist(pojo);
    try {
      commitTxn();
      fail("expected exception");
View Full Code Here

    commitTxn();
  }

  public void testEncodedStringPk_BatchGet() throws EntityNotFoundException {
    switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
    beginTxn();
    String key1 = new KeyFactory.Builder("parent", 44)
        .addChild(HasEncodedStringPkJPA.class.getSimpleName(), "yar1").getString();
    pojo.setId(key1);
    em.persist(pojo);
    commitTxn();

    HasEncodedStringPkJPA pojo2 = new HasEncodedStringPkJPA();
    beginTxn();
    String key2 = new KeyFactory.Builder("parent", 44)
        .addChild(HasEncodedStringPkJPA.class.getSimpleName(), "yar2").getString();       
    pojo2.setId(key2);
    em.persist(pojo2);
    commitTxn();

    assertNotNull(pojo.getId());
    assertNotNull(pojo2.getId());

    beginTxn();
    Query q = em.createQuery("select from " + HasEncodedStringPkJPA.class.getName() + " b where id = :ids");
    q.setParameter("ids", Utils.newArrayList(pojo.getId(), pojo2.getId()));
    List<HasEncodedStringPkJPA> pojos = (List<HasEncodedStringPkJPA>) q.getResultList();
    assertEquals(2, pojos.size());
    // we should preserve order but right now we don't
    Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
    assertEquals(pks, Utils.newHashSet(key1, key2));
View Full Code Here

TOP

Related Classes of com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkJPA

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.