Skip to content

Commit

Permalink
jakartaee/persistence#384 - add Query.getSingleResultOrNull()
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Sep 14, 2023
1 parent 6f711e5 commit d8f0dfb
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;

@Entity
@Table(name="PERSISTENCE32_POKEMON")
@NamedQuery(name="Pokemon.get", query="SELECT p FROM Pokemon p WHERE p.id = :id")
@NamedNativeQuery(name="Pokemon.deleteAllTypes", query="DELETE FROM PERSISTENCE32_POKEMON_TYPE")
@NamedNativeQuery(name="Pokemon.deleteAll", query="DELETE FROM PERSISTENCE32_POKEMON")
public class Pokemon {

// ID is assigned in tests to avoid collisions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;

@Entity
@Table(name="PERSISTENCE32_TYPE")
@NamedQuery(name="Type.all", query="SELECT t FROM Type t")
@NamedNativeQuery(name="Type.deleteAll", query="DELETE FROM PERSISTENCE32_TYPE")
public class Type {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.persistence.testing.tests.jpa.persistence32;

import java.util.HashMap;
import java.util.Map;

import jakarta.persistence.EntityManager;
import junit.framework.TestSuite;
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;
import org.eclipse.persistence.jpa.JpaEntityManagerFactory;
import org.eclipse.persistence.testing.framework.jpa.junit.JUnitTestCase;
import org.eclipse.persistence.testing.models.jpa.persistence32.Persistence32TableCreator;
import org.eclipse.persistence.testing.models.jpa.persistence32.Type;

/**
* Abstract jUnit test suite with Pokemon model.
*/
public abstract class AbstractPokemon extends JUnitTestCase {

// Pokemon types. Array index is ID value.
// Value of ID = 0 does not exist so it's array instance is set to null.
static final Type[] TYPES = new Type[] {
null, // Skip array index 0
new Type( 1, "Normal"),
new Type( 2, "Fighting"),
new Type( 3, "Flying"),
new Type( 4, "Poison"),
new Type( 5, "Ground"),
new Type( 6, "Rock"),
new Type( 7, "Bug"),
new Type( 8, "Ghost"),
new Type( 9, "Steel"),
new Type(10, "Fire"),
new Type(11, "Water"),
new Type(12, "Grass"),
new Type(13, "Electric"),
new Type(14, "Psychic"),
new Type(15, "Ice"),
new Type(16, "Dragon"),
new Type(17, "Dark"),
new Type(18, "Fairy")
};

/**
* Build test suite.
* Adds model test setup as first and model test cleanup as last test
* in the returned tests collection.
*
* @param name name of the suite
* @param tests tests to add to the suite
* @return collection of tests to execute
*/
static TestSuite suite(String name, AbstractPokemon... tests) {
TestSuite suite = new TestSuite();
suite.setName(name);
suite.addTest(new AbstractPokemon("testSetup") {});
for (AbstractPokemon test : tests) {
suite.addTest(test);
}
suite.addTest(new AbstractPokemon("testCleanup") {});
return suite;
}

JpaEntityManagerFactory emf = null;

public AbstractPokemon() {
}

public AbstractPokemon(String name) {
super(name);
setPuName(getPersistenceUnitName());
}

@Override
public String getPersistenceUnitName() {
return "persistence32";
}

@Override
public void setUp() {
super.setUp();
emf = getEntityManagerFactory(getPersistenceUnitName())
.unwrap(EntityManagerFactoryImpl.class);
}

/**
* Return all pokemon types as ID {@link Map}.
*
* @param em {@link EntityManager} instance to execute the query
* @return {@link Map} with pokemon types
*/
Map<Integer, Type> pokemonTypes(EntityManager em) {
Map<Integer, Type> types = new HashMap<>(TYPES.length);
em.createNamedQuery("Type.all", Type.class)
.getResultList()
.forEach(type -> types.put(type.getId(), type));
return types;
}

/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testSetup() {
new Persistence32TableCreator().replaceTables(JUnitTestCase.getServerSession(getPersistenceUnitName()));
clearCache();
emf.runInTransaction(em -> {
for (int i = 1; i < TYPES.length; i++) {
em.persist(TYPES[i]);
}
});
}

/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testCleanup() {
emf.runInTransaction(em -> {
em.createNamedQuery("Pokemon.deleteAllTypes").executeUpdate();
em.createNamedQuery("Pokemon.deleteAll").executeUpdate();
em.createNamedQuery("Type.deleteAll").executeUpdate();
});
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.persistence.testing.tests.jpa.persistence32;

import java.time.LocalDate;
Expand Down Expand Up @@ -26,7 +38,7 @@
import org.eclipse.persistence.testing.models.jpa.persistence32.SyntaxEntity;
import org.junit.Assert;

public class CriteriaBuilderTests extends JUnitTestCase {
public class CriteriaBuilderTest extends JUnitTestCase {

// SyntaxEntity instances, array index is equal to ID
// Values must be unique to get just single result and verify it by ID
Expand All @@ -44,38 +56,38 @@ public class CriteriaBuilderTests extends JUnitTestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.setName("CriteriaBuilderTests");
suite.addTest(new CriteriaBuilderTests("testSetup"));
suite.addTest(new CriteriaBuilderTests("testAndPredicateAsListOf0"));
suite.addTest(new CriteriaBuilderTests("testOrPredicateAsListOf0"));
suite.addTest(new CriteriaBuilderTests("testAndPredicateAsListOf1"));
suite.addTest(new CriteriaBuilderTests("testOrPredicateAsListOf1"));
suite.addTest(new CriteriaBuilderTests("testAndPredicateAsListOf2"));
suite.addTest(new CriteriaBuilderTests("testOrPredicateAsListOf2"));
suite.addTest(new CriteriaBuilderTests("testAndPredicateAsListOfN"));
suite.addTest(new CriteriaBuilderTests("testOrPredicateAsListOfN"));
suite.addTest(new CriteriaBuilderTests("testLeftIntLen"));
suite.addTest(new CriteriaBuilderTests("testLeftExprLen"));
suite.addTest(new CriteriaBuilderTests("testRightIntLen"));
suite.addTest(new CriteriaBuilderTests("testRightExprLen"));
suite.addTest(new CriteriaBuilderTests("testReplaceExprExpr"));
suite.addTest(new CriteriaBuilderTests("testReplaceExprStr"));
suite.addTest(new CriteriaBuilderTests("testReplaceStrExpr"));
suite.addTest(new CriteriaBuilderTests("testReplaceStrStr"));
suite.addTest(new CriteriaBuilderTests("testExtractHourFromTime"));
suite.addTest(new CriteriaBuilderTests("testExtractMinuteFromTime"));
suite.addTest(new CriteriaBuilderTests("testExtractSecondFromTime"));
suite.addTest(new CriteriaBuilderTests("testExtractYearFromDate"));
suite.addTest(new CriteriaBuilderTests("testExtractMonthFromDate"));
suite.addTest(new CriteriaBuilderTests("testExtractDayFromDate"));
suite.addTest(new CriteriaBuilderTests("testExtractQuarterFromDate"));
suite.addTest(new CriteriaBuilderTests("testExtractWeekFromDate"));
suite.addTest(new CriteriaBuilderTest("testSetup"));
suite.addTest(new CriteriaBuilderTest("testAndPredicateAsListOf0"));
suite.addTest(new CriteriaBuilderTest("testOrPredicateAsListOf0"));
suite.addTest(new CriteriaBuilderTest("testAndPredicateAsListOf1"));
suite.addTest(new CriteriaBuilderTest("testOrPredicateAsListOf1"));
suite.addTest(new CriteriaBuilderTest("testAndPredicateAsListOf2"));
suite.addTest(new CriteriaBuilderTest("testOrPredicateAsListOf2"));
suite.addTest(new CriteriaBuilderTest("testAndPredicateAsListOfN"));
suite.addTest(new CriteriaBuilderTest("testOrPredicateAsListOfN"));
suite.addTest(new CriteriaBuilderTest("testLeftIntLen"));
suite.addTest(new CriteriaBuilderTest("testLeftExprLen"));
suite.addTest(new CriteriaBuilderTest("testRightIntLen"));
suite.addTest(new CriteriaBuilderTest("testRightExprLen"));
suite.addTest(new CriteriaBuilderTest("testReplaceExprExpr"));
suite.addTest(new CriteriaBuilderTest("testReplaceExprStr"));
suite.addTest(new CriteriaBuilderTest("testReplaceStrExpr"));
suite.addTest(new CriteriaBuilderTest("testReplaceStrStr"));
suite.addTest(new CriteriaBuilderTest("testExtractHourFromTime"));
suite.addTest(new CriteriaBuilderTest("testExtractMinuteFromTime"));
suite.addTest(new CriteriaBuilderTest("testExtractSecondFromTime"));
suite.addTest(new CriteriaBuilderTest("testExtractYearFromDate"));
suite.addTest(new CriteriaBuilderTest("testExtractMonthFromDate"));
suite.addTest(new CriteriaBuilderTest("testExtractDayFromDate"));
suite.addTest(new CriteriaBuilderTest("testExtractQuarterFromDate"));
suite.addTest(new CriteriaBuilderTest("testExtractWeekFromDate"));
return suite;
}

public CriteriaBuilderTests() {
public CriteriaBuilderTest() {
}

public CriteriaBuilderTests(String name) {
public CriteriaBuilderTest(String name) {
super(name);
setPuName(getPersistenceUnitName());
}
Expand Down
Loading

0 comments on commit d8f0dfb

Please sign in to comment.