From 096f0577c6bc044af7b7441b2872a4015e301bcb Mon Sep 17 00:00:00 2001 From: vavishal Date: Tue, 4 Mar 2025 12:27:30 +0530 Subject: [PATCH] Testcase for invalid_query_key_in_expression Signed-off-by: Vaibhav Vishal --- .../advanced/jpql/CarProductionCompany.java | 19 ++++ .../models/jpa21/advanced/jpql/Company.java | 29 ++++++ .../models/jpa21/advanced/jpql/Continent.java | 30 ++++++ .../models/jpa21/advanced/jpql/Country.java | 24 +++++ .../advanced/jpql/ProductionCompany.java | 19 ++++ .../main/resources/META-INF/persistence.xml | 15 +++ .../tests/advanced2/jpql/JPQLTest.java | 93 +++++++++++++++++++ 7 files changed, 229 insertions(+) create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/CarProductionCompany.java create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Company.java create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Continent.java create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Country.java create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/ProductionCompany.java create mode 100644 jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/test/java/org/eclipse/persistence/testing/tests/advanced2/jpql/JPQLTest.java diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/CarProductionCompany.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/CarProductionCompany.java new file mode 100644 index 0000000000..e4b5e6d303 --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/CarProductionCompany.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2025 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.models.jpa21.advanced.jpql; + +import jakarta.persistence.Entity; + +@Entity +public class CarProductionCompany extends ProductionCompany { + +} diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Company.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Company.java new file mode 100644 index 0000000000..c019b3ad99 --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Company.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 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.models.jpa21.advanced.jpql; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; + +@Entity +@Table(name = "JPA21_COMPANY") +public class Company { + + @Id + Long companyKey; + + @ManyToOne + Country originCountry; + +} diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Continent.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Continent.java new file mode 100644 index 0000000000..f43239c056 --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Continent.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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.models.jpa21.advanced.jpql; + +import java.util.List; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +@Entity +@Table(name = "JPA21_CONTINENT") +public class Continent { + + @Id + private long continentKey; + + @OneToMany + List countries; +} diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Country.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Country.java new file mode 100644 index 0000000000..8a2a096d17 --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/Country.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 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.models.jpa21.advanced.jpql; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "JPA21_COUNTRY") +public class Country { + + @Id + private long countryKey; +} diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/ProductionCompany.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/ProductionCompany.java new file mode 100644 index 0000000000..83d558885b --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/java/org/eclipse/persistence/testing/models/jpa21/advanced/jpql/ProductionCompany.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2025 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.models.jpa21.advanced.jpql; + +import jakarta.persistence.Entity; + +@Entity +public class ProductionCompany extends Company { + +} diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/resources/META-INF/persistence.xml b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/resources/META-INF/persistence.xml index e1c69c8381..1da22ad459 100644 --- a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/resources/META-INF/persistence.xml +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/main/resources/META-INF/persistence.xml @@ -223,4 +223,19 @@ + + + org.eclipse.persistence.jpa.PersistenceProvider + org.eclipse.persistence.testing.models.jpa21.advanced.jpql.CarProductionCompany + org.eclipse.persistence.testing.models.jpa21.advanced.jpql.Company + org.eclipse.persistence.testing.models.jpa21.advanced.jpql.Continent + org.eclipse.persistence.testing.models.jpa21.advanced.jpql.Country + org.eclipse.persistence.testing.models.jpa21.advanced.jpql.ProductionCompany + true + DISABLE_SELECTIVE + + + + + diff --git a/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/test/java/org/eclipse/persistence/testing/tests/advanced2/jpql/JPQLTest.java b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/test/java/org/eclipse/persistence/testing/tests/advanced2/jpql/JPQLTest.java new file mode 100644 index 0000000000..56edf3bcc8 --- /dev/null +++ b/jpa/eclipselink.jpa.testapps/jpa.test.advanced2/src/test/java/org/eclipse/persistence/testing/tests/advanced2/jpql/JPQLTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 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.advanced2.jpql; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.persistence.testing.framework.jpa.junit.JUnitTestCase; +import org.eclipse.persistence.testing.models.jpa21.advanced.jpql.CarProductionCompany; +import org.eclipse.persistence.testing.models.jpa21.advanced.jpql.Company; +import org.eclipse.persistence.testing.models.jpa21.advanced.jpql.Continent; +import org.eclipse.persistence.testing.models.jpa21.advanced.jpql.ProductionCompany; + +public class JPQLTest extends JUnitTestCase { + + public JPQLTest() { + } + + public JPQLTest(String name) { + super(name); + setPuName(getPersistenceUnitName()); + } + + @Override + public String getPersistenceUnitName() { + return "pu-jpql"; + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.setName("JPQLTest"); + suite.addTest(new JPQLTest("testJPQLs")); + return suite; + } + + public void testJPQLs() { + EntityManagerFactory emf = getEntityManagerFactory(); + EntityManager em = createEntityManager(); + beginTransaction(em); + try { + String jpql = "SELECT continent" + + " FROM Continent continent " + + " WHERE NOT EXISTS (SELECT c FROM Company c WHERE c.originCountry MEMBER OF continent.countries)"; + em.createQuery(jpql, Object[].class).getResultList(); + + jpql = "SELECT continent" + + " FROM Continent continent " + + " WHERE NOT EXISTS (SELECT c FROM ProductionCompany c WHERE c.originCountry MEMBER OF continent.countries)"; + em.createQuery(jpql, Object[].class).getResultList(); + + jpql = "SELECT continent" + + " FROM Continent continent " + + " WHERE NOT EXISTS (SELECT c FROM CarProductionCompany c WHERE c.originCountry MEMBER OF continent.countries)"; + em.createQuery(jpql, Object[].class).getResultList(); + } finally { + if (this.isTransactionActive(em)) { + rollbackTransaction(em); + } + closeEntityManager(em); + } + } + + @Override + public void tearDown() { + EntityManager em = createEntityManager(); + em.getTransaction().begin(); + try { + em.createQuery("delete from CarProductionCompany c").executeUpdate(); + em.createQuery("delete from Company d").executeUpdate(); + em.createQuery("delete from Continent e").executeUpdate(); + em.createQuery("delete from Country f").executeUpdate(); + em.createQuery("delete from ProductionCompany p").executeUpdate(); + commitTransaction(em); + } finally { + if (this.isTransactionActive(em)) { + rollbackTransaction(em); + } + closeEntityManager(em); + } + } +}