Skip to content

Commit

Permalink
(eclipse-ee4j#2217) Moved tests to jpa.test.jpql
Browse files Browse the repository at this point in the history
Uses existing Room entity with a field named "length"

Note: the test "JUnitJPQLJakartaDataNoAliasTest.tesUpdateQueryWithThisVariable" is still failing
  • Loading branch information
OndroMih committed Jul 31, 2024
1 parent 87fad3c commit 5a0a76b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!--
Copyright (c) 2018, 2024 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
Expand Down Expand Up @@ -327,6 +328,8 @@
<persistence-unit name="advanced-jakartadata" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.datatypes.WrapperTypes</class>
<class>org.eclipse.persistence.testing.models.jpa.advanced.Room</class>
<class>org.eclipse.persistence.testing.models.jpa.advanced.Door</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="${eclipselink.logging.level}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
Expand Down Expand Up @@ -28,6 +29,11 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Stream;
import org.eclipse.persistence.sessions.server.ServerSession;
import org.eclipse.persistence.testing.models.jpa.advanced.AdvancedTableCreator;
import org.eclipse.persistence.testing.models.jpa.advanced.Room;

/**
* <p>
Expand All @@ -45,6 +51,14 @@
*/
public class JUnitJPQLJakartaDataNoAliasTest extends JUnitTestCase {
private static final String STRING_DATA = "A String";
private static final Room[] ROOMS = new Room[] {
null, // Skip array index 0
aRoom(1, 1, 1, 1),
aRoom(2, 1, 1, 1),
aRoom(3, 1, 1, 1),
aRoom(4, 1, 1, 1)
};
private static final long ROOMS_COUNT = ROOMS.length -1; // we ignore the first one with index 0

private static int wrapperId;

Expand Down Expand Up @@ -86,6 +100,11 @@ public static Test suite() {
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testNoAliasFromWhereAndUPPER"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testGeneratedSelectNoAliasFromWhere"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testGeneratedSelect"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testUpdateQueryLengthInAssignmentAndExpression"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("tesUpdateQueryWithThisVariable"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryLengthInAssignmentAndExpression"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testDeleteQueryLengthInExpressionOnLeft"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testDeleteQueryLengthInExpressionOnRight"));
return suite;
}

Expand All @@ -95,10 +114,13 @@ public static Test suite() {
public void testSetup() {
//initialize the global comparer object
comparer = new JUnitDomainObjectComparer();
final ServerSession session = getPersistenceUnitServerSession();

//set the session for the comparer to use
comparer.setSession(getPersistenceUnitServerSession());
comparer.setSession(session);

new DataTypesTableCreator().replaceTables(getPersistenceUnitServerSession());
new DataTypesTableCreator().replaceTables(session);
new AdvancedTableCreator().replaceTables(session);
clearCache();
EntityManager em = createEntityManager();
WrapperTypes wt;
Expand Down Expand Up @@ -246,4 +268,71 @@ public void testGeneratedSelectNoAliasFromWhere() {
WrapperTypes tlWrapperTypes = (WrapperTypes) getPersistenceUnitServerSession().executeQuery(tlQuery);
Assert.assertTrue("GeneratedSelectNoAliasFromWhere Test Failed", comparer.compareObjects(wrapperTypes, tlWrapperTypes));
}

public void testUpdateQueryLengthInAssignmentAndExpression() {
testUpdateAllRooms("UPDATE Room SET length = length + 1");
}

public void tesUpdateQueryWithThisVariable() {
testUpdateAllRooms("UPDATE Room SET length = this.length + 1");
}

private void testUpdateAllRooms(String query) {
resetRooms();

long numberOfChanges = getEntityManagerFactory().callInTransaction(em -> em.createQuery(query).executeUpdate());
assertTrue("All rooms should be updated", numberOfChanges == ROOMS_COUNT);

long numberOfRoomsWithLengthChanged = getAllRooms()
.filter(room -> room.getLength() == 2)
.count();
assertTrue("All rooms should have increased length", numberOfRoomsWithLengthChanged == ROOMS_COUNT);
}

public void testSelectQueryLengthInAssignmentAndExpression() {
List<Room> roomsWithIdOne = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"SELECT this FROM Room WHERE id + length = length + 1", Room.class).getResultList());
assertTrue("Number of rooms with ID = 1", roomsWithIdOne.size() == 1);
}

public void testDeleteQueryLengthInExpressionOnLeft() {
resetRooms();
assertTrue("Number of remaining rooms", getAllRooms().count() == ROOMS_COUNT);
int numberOfChanges = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"DELETE FROM Room WHERE length = id - 1").executeUpdate());
long allRoomsCount = getAllRooms().count();
assertTrue("Number of rooms with ID = 1 deleted", numberOfChanges == 1);
assertTrue("Number of remaining rooms", allRoomsCount == ROOMS_COUNT - 1);
}

public void testDeleteQueryLengthInExpressionOnRight() {
resetRooms();
int numberOfChanges = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"DELETE FROM Room WHERE id = length + 1").executeUpdate());
assertTrue("Number of rooms with ID = 1 deleted", numberOfChanges == 1);
assertTrue("Number of remaining rooms", getAllRooms().count() == ROOMS_COUNT - 1);
}

private Stream<Room> getAllRooms() {
return getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"SELECT r FROM Room r", Room.class).getResultStream());
}

private static Room aRoom(int id, int width, int length, int height) {
Room room = new Room();
room.setId(id);
room.setWidth(width);
room.setLength(length);
room.setHeight(height);
return room;
}

private void resetRooms() {
getEntityManagerFactory().runInTransaction(em -> {
em.createQuery("DELETE FROM Room").executeUpdate();
for (int i = 1; i < ROOMS.length; i++) {
em.persist(ROOMS[i]);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
Expand Down Expand Up @@ -56,9 +55,6 @@ public class Pokemon {

private String name;

// to test queries where entity field clashes with a function name in QueryTest
private int length = 0;

@ManyToOne
@JoinColumn(name = "TRAINER_ID")
private Trainer trainer;
Expand Down Expand Up @@ -133,14 +129,6 @@ public void setTrainer(Trainer trainer) {
this.trainer = trainer;
}

public int getLength() {
return length;
}

public void setLength(int length) {
this.length = length;
}

@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Contributors to the Eclipse Foundation. 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
Expand All @@ -16,7 +15,6 @@

import jakarta.persistence.NoResultException;
import jakarta.persistence.NonUniqueResultException;
import java.util.stream.Stream;
import junit.framework.Test;
import org.eclipse.persistence.testing.models.jpa.persistence32.Pokemon;
import org.junit.Assert;
Expand All @@ -36,8 +34,6 @@ public class QueryTest extends AbstractPokemon {
new Pokemon(4, "Caterpie", List.of(TYPES[7]))
};

static final long POKEMONS_COUNT = POKEMONS.length -1; // we ignore the first one with index 0

public static Test suite() {
return suite(
"QueryTest",
Expand All @@ -46,12 +42,7 @@ public static Test suite() {
new QueryTest("testGetSingleResultWithMultipleResults"),
new QueryTest("testGetSingleResultOrNullWithEmptyResult"),
new QueryTest("testGetSingleResultOrNullWithSingleResult"),
new QueryTest("testGetSingleResultOrNullWithMultipleResults"),
new QueryTest("testUpdateQueryLengthInAssignmentAndExpression"),
new QueryTest("tesUpdateQueryWithThisVariable"),
new QueryTest("testSelectQueryLengthInAssignmentAndExpression"),
new QueryTest("testDeleteQueryLengthInExpressionOnLeft"),
new QueryTest("testDeleteQueryLengthInExpressionOnRight")
new QueryTest("testGetSingleResultOrNullWithMultipleResults")
);
}

Expand All @@ -63,12 +54,11 @@ public QueryTest(String name) {
setPuName(getPersistenceUnitName());
}

// Initialize data for each test
// Initialize data
@Override
public void setUp() {
super.setUp();
protected void suiteSetUp() {
super.suiteSetUp();
emf.runInTransaction(em -> {
em.createQuery("DELETE FROM Pokemon").executeUpdate();
for (int i = 1; i < POKEMONS.length; i++) {
em.persist(POKEMONS[i]);
}
Expand Down Expand Up @@ -118,49 +108,4 @@ public void testGetSingleResultOrNullWithMultipleResults() {
"SELECT p FROM Pokemon p ", Pokemon.class).getSingleResultOrNull()));
}

public void testUpdateQueryLengthInAssignmentAndExpression() {
testUpdateAllPokemons("UPDATE Pokemon SET length = length + 1");
}

public void tesUpdateQueryWithThisVariable() {
testUpdateAllPokemons("UPDATE Pokemon SET length = this.length + 1");
}

private void testUpdateAllPokemons(String query) {
long numberOfChanges = emf.callInTransaction(em -> em.createQuery(query).executeUpdate());
assertTrue("All pokemons should be updated", numberOfChanges == POKEMONS_COUNT);

long numberOfPokemonsWithLengthChanged = getAllPokemons()
.filter(pokemon -> pokemon.getLength() == 1)
.count();
assertTrue("All pokemons should have increased length", numberOfPokemonsWithLengthChanged == POKEMONS_COUNT);
}

public void testSelectQueryLengthInAssignmentAndExpression() {
List<Pokemon> pokemonsWithIdOne = emf.callInTransaction(em -> em.createQuery(
"SELECT this FROM Pokemon WHERE id + length = length + 1", Pokemon.class).getResultList());
assertTrue("Number of pokemons with ID = 1", pokemonsWithIdOne.size() == 1);
}

public void testDeleteQueryLengthInExpressionOnLeft() {
assertTrue("Number of remaining pokemons", getAllPokemons().count() == POKEMONS_COUNT);
int numberOfChanges = emf.callInTransaction(em -> em.createQuery(
"DELETE FROM Pokemon WHERE length = id - 1").executeUpdate());
assertTrue("Number of pokemons with ID = 1 deleted", numberOfChanges == 1);
assertTrue("Number of remaining pokemons", getAllPokemons().count() == POKEMONS_COUNT - 1);
}

public void testDeleteQueryLengthInExpressionOnRight() {
assertTrue("Number of remaining pokemons", getAllPokemons().count() == POKEMONS_COUNT);
int numberOfChanges = emf.callInTransaction(em -> em.createQuery(
"DELETE FROM Pokemon WHERE id = length + 1").executeUpdate());
assertTrue("Number of pokemons with ID = 1 deleted", numberOfChanges == 1);
assertTrue("Number of remaining pokemons", getAllPokemons().count() == POKEMONS_COUNT - 1);
}

private Stream<Pokemon> getAllPokemons() {
return emf.callInTransaction(em -> em.createQuery(
"SELECT p FROM Pokemon p", Pokemon.class).getResultStream());
}

}

0 comments on commit 5a0a76b

Please sign in to comment.