Skip to content

Commit

Permalink
org.eclipse.persistence.jpa.jse.test test fixes against Oracle DB (#1370
Browse files Browse the repository at this point in the history
)

JPA JSE Tests against Oracle DB fixes

Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
  • Loading branch information
rfelcman committed Jan 31, 2022
1 parent 5f242dd commit 5763e7e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ public void removeIdentitySequences(
* Override this method if the platform needs to use a custom function based on the DatabaseField
* @return An expression for the given field set equal to a parameter matching the field
*/
public Expression createExpressionFor(DatabaseField field, Expression builder) {
public Expression createExpressionFor(DatabaseField field, Expression builder, String fieldClassificationClassName) {
Expression subExp1 = builder.getField(field);
Expression subExp2 = builder.getParameter(field);
return subExp1.equal(subExp2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,11 @@ public void createPrimaryKeyExpression(AbstractSession session) {
if(null != primaryKeyFields) {
for (int index = 0; index < primaryKeyFields.size(); index++) {
DatabaseField primaryKeyField = primaryKeyFields.get(index);
subExpression = ((DatasourcePlatform)session.getDatasourcePlatform()).createExpressionFor(primaryKeyField, builder);
String fieldClassificationClassName = null;
if (this.getBaseMappingForField(primaryKeyField) instanceof AbstractDirectMapping) {
fieldClassificationClassName = ((AbstractDirectMapping)this.getBaseMappingForField(primaryKeyField)).getFieldClassificationClassName();
}
subExpression = ((DatasourcePlatform)session.getDatasourcePlatform()).createExpressionFor(primaryKeyField, builder, fieldClassificationClassName);

if (expression == null) {
expression = subExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,4 +1361,12 @@ public void writeUpdateFieldsIntoRow(AbstractRecord databaseRow, AbstractSession
databaseRow.add(getField(), null);
}
}

/**
* INTERNAL:
* Get fieldClassificationClassName. Value usually exist for fields with some kind of embedded converter like <code>@Lob</code> or <code>@Temporal</code>.
*/
public String getFieldClassificationClassName() {
return this.fieldClassificationClassName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1206,15 +1206,17 @@ public boolean shouldPrintForUpdateClause() {
}

@Override
public Expression createExpressionFor(DatabaseField field, Expression builder) {
public Expression createExpressionFor(DatabaseField field, Expression builder, String fieldClassificationClassName) {
if (field.getType() == java.sql.Clob.class ||
field.getType() == java.sql.Blob.class) {
field.getType() == java.sql.Blob.class ||
"java.sql.Clob".equals(fieldClassificationClassName) ||
"java.sql.Blob".equals(fieldClassificationClassName)) {
Expression subExp1 = builder.getField(field);
Expression subExp2 = builder.getParameter(field);
subExp1 = subExp1.getFunction("dbms_lob.compare", subExp2);
return subExp1.equal(0);
}
return super.createExpressionFor(field, builder);
return super.createExpressionFor(field, builder, fieldClassificationClassName);
}

// Value of shouldCheckResultTableExistsQuery must be true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.Date;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
Expand All @@ -21,6 +22,7 @@
public class ElementCollectionEmbeddableTemporal {

@Temporal(value = TemporalType.DATE)
@Column(name = "TEMPORALVALUE", columnDefinition = "DATE")
private Date temporalValue;

public ElementCollectionEmbeddableTemporal() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public class TestLobMerge {
@Emf(createTables = DDLGen.DROP_CREATE, classes = { CollectedEntity.class, ParentEntity.class })
private EntityManagerFactory emf;

/**
* Merging ElementCollections on Oracle fails when EclipseLink generates
* a DELETE SQL statement with a WHERE clause containing a CLOB.
*
*/
@Test
public void testLobMerge() throws Exception {
//Test for Oracle only
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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
Expand All @@ -15,6 +15,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.internal.jpa.EntityManagerImpl;
import org.eclipse.persistence.jpa.test.framework.DDLGen;
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
Expand Down Expand Up @@ -51,67 +52,77 @@ 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();
//MySQL only due permissions for CREATE SCHEMA command.
if (((EntityManagerImpl)em).getDatabaseSession().getPlatform().isMySQL()) {
supportedPlatform = true;
try {
em.getTransaction().begin();
em.createNativeQuery("DROP SCHEMA IF EXISTS tenant_1").executeUpdate();
em.createNativeQuery("DROP SCHEMA IF EXISTS tenant_2").executeUpdate();
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

0 comments on commit 5763e7e

Please sign in to comment.