OPENJPA-2018 improve unit test setup

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1535838 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Struberg 2013-10-25 19:40:31 +00:00
parent c428609da7
commit 9da7935946
2 changed files with 55 additions and 92 deletions

View File

@ -18,12 +18,8 @@
*/
package org.apache.openjpa.openjpa2018;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@ -33,87 +29,87 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class OpenJPA2018Test extends TestCase {
private EntityManager em;
private EntityTransaction transaction;
private EntityManagerFactory factory;
public class OpenJPA2018Test extends SingleEMTestCase
{
@Override
public void setUp() {
factory = OpenJPAPersistence.createEntityManagerFactory("openjpa2018", "META-INF/openjpa2018.xml");
em = factory.createEntityManager();
transaction = em.getTransaction();
transaction.begin();
}
@Override
public void tearDown() {
transaction.rollback();
em.close();
factory.close();
super.setUp(User2018.class, CLEAR_TABLES);
}
public void testInCriteriaWithArray() {
em.getTransaction().begin();
try {
User2018 user = new User2018();
em.persist(user);
em.flush();
User2018 user = new User2018();
em.persist(user);
em.flush();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(root.get("id").in(builder.parameter(Long[].class)));
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(root.get("id").in(builder.parameter(Long[].class)));
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, new Long[] { user.id });
}
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, new Long[] { user.id });
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
} finally {
em.getTransaction().commit();
}
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
}
public void testInCriteriaWithCollection() {
em.getTransaction().begin();
try {
User2018 user = new User2018();
em.persist(user);
em.flush();
User2018 user = new User2018();
em.persist(user);
em.flush();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(root.get("id").in(builder.parameter(Collection.class)));
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(root.get("id").in(builder.parameter(Collection.class)));
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, Arrays.asList(user.id));
}
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, Arrays.asList(user.id));
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
} finally {
em.getTransaction().commit();
}
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
}
public void testId() {
em.getTransaction().begin();
try {
User2018 user = new User2018();
em.persist(user);
em.flush();
User2018 user = new User2018();
em.persist(user);
em.flush();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(builder.equal(root.get("id"), user.id));
CriteriaQuery<User2018> criteria = builder.createQuery(User2018.class);
Root<User2018> root = criteria.from(User2018.class);
criteria.where(builder.equal(root.get("id"), user.id));
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, user.id);
}
TypedQuery<User2018> query = em.createQuery(criteria);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, user.id);
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
} finally {
em.getTransaction().commit();
}
List<User2018> result = query.getResultList();
assertTrue(!result.isEmpty());
}
}

View File

@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="openjpa2018">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>org.apache.openjpa.openjpa2018.User2018</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="openjpa.ConnectionURL" value="jdbc:derby:memory:openjpa2018;create=true"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
</properties>
</persistence-unit>
</persistence>