Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/b8d4f01b0e66bcdb2560 to your computer and use it in GitHub Desktop.
Save thomasdarimont/b8d4f01b0e66bcdb2560 to your computer and use it in GitHub Desktop.
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.
*/
package org.example.domain;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;
import org.apache.openjpa.persistence.PersistenceProviderImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Standalone JPA Test-suite for OpenJpa persistence provider tests / bug reports.
*
* @author Thomas Darimont
*/
public class EmptyParameterOpenJpaPlainJpaTests {
private static final String PERSISTENCE_UNIT_NAME = "default";
private static EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
@BeforeClass
public static void setupClass() {
PersistenceProviderResolverHolder.setPersistenceProviderResolver(new OpenJpaOnlyPersistenceProviderResolver());
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
@Before
public void setup() {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
}
@After
public void teardown() {
try {
tx.rollback();
} catch (Exception e) {
System.err.println("Could not rollback EntityTransaction!");
e.printStackTrace();
}
try {
em.close();
} catch (Exception e) {
System.err.println("Could not close EntityManager!");
e.printStackTrace();
}
}
public static void afterClass() {
try {
emf.close();
} catch (Exception e) {
System.err.println("Could not close EntityManagerFactory!");
e.printStackTrace();
}
}
@Test
public void dummy() {}
@Test
public void shouldSupportNullValueAsASubstituateForAnEmptyParameterList() throws Exception {
Example example1 = new Example();
example1.setAttributeString("foo@example.org");
example1.setAttributeInt(42);
em.persist(example1);
List<User> result = em.createQuery("select e from Example e where e.attributeInt in :values")
.setParameter("values", null).getResultList();
assertEquals(true, result.isEmpty());
}
static class OpenJpaOnlyPersistenceProviderResolver implements PersistenceProviderResolver {
@Override
public List<PersistenceProvider> getPersistenceProviders() {
return Arrays.<PersistenceProvider> asList(new PersistenceProviderImpl());
}
@Override
public void clearCachedProviders() {}
}
}
package org.example.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Example {
@Id @GeneratedValue Long id;
int attributeInt;
String attributeString;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getAttributeInt() {
return attributeInt;
}
public void setAttributeInt(int attributeInt) {
this.attributeInt = attributeInt;
}
public String getAttributeString() {
return attributeString;
}
public void setAttributeString(String attributeString) {
this.attributeString = attributeString;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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="default">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>org.example.domain.Example</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:openjpa_plainjpa" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.Log" value="SQL=Trace" />
</properties>
</persistence-unit>
</persistence>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thomasdarimont</groupId>
<artifactId>com.thomasdarimont.java.jpa.openjpa.bugs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<hsqldb>2.2.8</hsqldb>
<jpa>2.0.0</jpa>
<openjpa>2.3.0</openjpa>
<junit>4.11</junit>
<hamcrest>1.3</hamcrest>
</properties>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-persistence-jdbc</artifactId>
<version>${openjpa}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment