Skip to content

Commit

Permalink
Query syntax and parameter binding testing
Browse files Browse the repository at this point in the history
Signed-off-by: Will Dazey <[email protected]>
  • Loading branch information
dazey3 authored and rfelcman committed Jun 23, 2022
1 parent 9dfb545 commit fc5106d
Show file tree
Hide file tree
Showing 12 changed files with 26,513 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022 IBM Corporation. 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 @@ -27,23 +28,23 @@ public ExpressionOperatorUnitTestSuite() {
}

public void _testEquals$nullTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator2 = null;
if (operator.equals(operator2)) {
throw new TestErrorException("Equals() must handle null case.");
}
}

public void _testEquals$ObjectTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.Between));
Object operator2 = new Integer(5);
if (operator.equals(operator2)) {
throw new TestErrorException("Equals() must handle other class case.");
}
}

public void _testEqualsTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator2 = new ExpressionOperator(ExpressionOperator.Between, new Vector());
if (!operator.equals(operator2)) {
throw new TestErrorException("Equals() must do comparison by selector only.");
Expand All @@ -64,21 +65,21 @@ public void _testEqualsArrayTest() {
}

public void _testIsComparisonOperatorTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.Between));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.Between));
if (!operator.isComparisonOperator()) {
throw new TestErrorException("IsComparisonOperator() invalid.");
}
}

public void _testIsFunctionOperatorTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.Not));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.Not));
if (!operator.isFunctionOperator()) {
throw new TestErrorException("IsFunctionOperator() invalid.");
}
}

public void _testIsLogicalOperatorTest() {
ExpressionOperator operator = ExpressionOperator.getOperator(new Integer(ExpressionOperator.And));
ExpressionOperator operator = Expression.getOperator(new Integer(ExpressionOperator.And));
if (!operator.isLogicalOperator()) {
throw new TestErrorException("IsLogicalOperator() invalid.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021 IBM Corporation. All rights reserved.
* Copyright (c) 2014, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022 IBM Corporation. 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 @@ -285,7 +285,7 @@ private class SqlCallCollector extends SessionEventAdapter {

@Override
public void preExecuteCall(SessionEvent event) {
super.preExecuteQuery(event);
super.preExecuteCall(event);
Call call = event.getCall();
if(call instanceof DatabaseCall) {
_sql.add(((DatabaseCall)call).getSQLString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.internal.jpa.EntityManagerImpl;
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;
import org.eclipse.persistence.jpa.test.framework.DDLGen;
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.jpa.test.mapping.model.ChildMultitenant;
import org.eclipse.persistence.jpa.test.mapping.model.ParentMultitenant;

import org.eclipse.persistence.platform.database.DatabasePlatform;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -52,85 +54,75 @@ public class TestMultitenantOneToMany {
)
private EntityManagerFactory emf;

private boolean supportedPlatform = false;

@Before
public void setup() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.createNativeQuery("CREATE SCHEMA tenant_1").executeUpdate();
em.createNativeQuery("CREATE SCHEMA tenant_2").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_1.parent(id bigint primary key)").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_2.parent(id bigint primary key)").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_1.children(id bigint NOT NULL, parent_id bigint, PRIMARY KEY " +
"(id), CONSTRAINT parent_fkey FOREIGN KEY (parent_id) REFERENCES tenant_1.parent (id))")
.executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_2.children(id bigint NOT NULL, parent_id bigint, PRIMARY KEY " +
"(id), CONSTRAINT parent_fkey FOREIGN KEY (parent_id) REFERENCES tenant_2.parent (id))")
.executeUpdate();
em.createNativeQuery("INSERT INTO tenant_1.parent(id) VALUES(1)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_2.parent(id) VALUES(2)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_1.children(id, parent_id) VALUES(10, 1)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_2.children(id, parent_id) VALUES(11, 2)").executeUpdate();
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
}
}
}

@After
public void cleanUp() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.createNativeQuery("DROP SCHEMA IF EXISTS tenant_1").executeUpdate();
em.createNativeQuery("DROP SCHEMA IF EXISTS tenant_2").executeUpdate();
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
//MySQL only due permissions for CREATE SCHEMA command.
if (((EntityManagerImpl)em).getDatabaseSession().getPlatform().isMySQL()) {
supportedPlatform = true;
try {
em.getTransaction().begin();
em.createNativeQuery("CREATE SCHEMA tenant_1").executeUpdate();
em.createNativeQuery("CREATE SCHEMA tenant_2").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_1.parent(id bigint primary key)").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_2.parent(id bigint primary key)").executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_1.children(id bigint NOT NULL, parent_id bigint, PRIMARY KEY " +
"(id), CONSTRAINT parent_fkey FOREIGN KEY (parent_id) REFERENCES tenant_1.parent (id))")
.executeUpdate();
em.createNativeQuery("CREATE TABLE tenant_2.children(id bigint NOT NULL, parent_id bigint, PRIMARY KEY " +
"(id), CONSTRAINT parent_fkey FOREIGN KEY (parent_id) REFERENCES tenant_2.parent (id))")
.executeUpdate();
em.createNativeQuery("INSERT INTO tenant_1.parent(id) VALUES(1)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_2.parent(id) VALUES(2)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_1.children(id, parent_id) VALUES(10, 1)").executeUpdate();
em.createNativeQuery("INSERT INTO tenant_2.children(id, parent_id) VALUES(11, 2)").executeUpdate();
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
}
}
}
}

@Test
public void testMultitenancySchemaDescriminatorWithOneToMany() {
boolean awaitTermination = false;
List<Future<ParentMultitenant>> parent1Results = new ArrayList<>();
List<Future<ParentMultitenant>> parent2Results = new ArrayList<>();
try {
ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (int i = 1; i <= 10000; i++) {
parent1Results.add(es.submit(() -> load("tenant_1", 1L)));
parent2Results.add(es.submit(() -> load("tenant_2", 2L)));
if (supportedPlatform) {
boolean awaitTermination = false;
List<Future<ParentMultitenant>> parent1Results = new ArrayList<>();
List<Future<ParentMultitenant>> parent2Results = new ArrayList<>();
try {
ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (int i = 1; i <= 10000; i++) {
parent1Results.add(es.submit(() -> load("tenant_1", 1L)));
parent2Results.add(es.submit(() -> load("tenant_2", 2L)));
}
es.shutdown();
awaitTermination = es.awaitTermination(10, TimeUnit.MINUTES);
for (Future<ParentMultitenant> parentFuture : parent1Results) {
ParentMultitenant parent = parentFuture.get();
assertEquals(1L, (long) parent.getId());
assertEquals(10L, (long) parent.getChildren().get(0).getId());
}
for (Future<ParentMultitenant> parentFuture : parent2Results) {
ParentMultitenant parent = parentFuture.get();
assertEquals(2L, (long) parent.getId());
assertEquals(11L, (long) parent.getChildren().get(0).getId());
}
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
fail("Exception was caught: " + sw.toString());
}
es.shutdown();
awaitTermination = es.awaitTermination(10, TimeUnit.MINUTES);
for (Future<ParentMultitenant> parentFuture : parent1Results) {
ParentMultitenant parent = parentFuture.get();
assertEquals(1L, (long) parent.getId());
assertEquals(10L, (long) parent.getChildren().get(0).getId());
if (!awaitTermination) {
fail("timeout elapsed before termination of the threads");
}
for (Future<ParentMultitenant> parentFuture : parent2Results) {
ParentMultitenant parent = parentFuture.get();
assertEquals(2L, (long) parent.getId());
assertEquals(11L, (long) parent.getChildren().get(0).getId());
}
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
fail("Exception was caught: " + sw.toString());
}
if (!awaitTermination) {
fail("timeout elapsed before termination of the threads");
}
}

Expand Down
Loading

0 comments on commit fc5106d

Please sign in to comment.