This is an automated email from the ASF dual-hosted git repository. pcristof pushed a commit to branch OPENJPA-2940 in repository https://gitbox.apache.org/repos/asf/openjpa.git
commit f1a8b2773c316f7a72e54beef1fb605c39c5bb84 Author: Paulo Cristovão de Araújo Silva Filho <pcris...@gmail.com> AuthorDate: Tue Jul 15 09:07:58 2025 -0300 [OPENJPA-2940][WIP] Intermmediate commit * Changing signature of BrokerFactory API on schema dealing validate method * Adding test to check if validate operation throws exception when it fails * Changing GH CI workflow to allow usage of both self-hosted and GH hosted runners * Tested on derby, h2-2, postgresql:latest, mysql:lts, mariadb:lts --- .github/workflows/ci.yml | 2 +- .../openjpa/jdbc/kernel/JDBCBrokerFactory.java | 5 +-- .../openjpa/kernel/AbstractBrokerFactory.java | 2 +- .../org/apache/openjpa/kernel/BrokerFactory.java | 2 +- .../openjpa/kernel/DelegatingBrokerFactory.java | 7 ++-- .../openjpa/jdbc/meta/TestJDBCSchemaManager.java | 47 +++++++++++++++++----- .../openjpa/persistence/SchemaManagerImpl.java | 7 ++-- 7 files changed, 49 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 553c2d9eb..01ef55787 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ env: MAVEN_OPTS: -Dmaven.artifact.threads=256 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn jobs: build: - runs-on: openjpa-runner + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Java JDK diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCBrokerFactory.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCBrokerFactory.java index efc5d9ff0..5ff06083e 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCBrokerFactory.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCBrokerFactory.java @@ -45,7 +45,6 @@ import org.apache.openjpa.lib.conf.ConfigurationProvider; import org.apache.openjpa.lib.conf.Configurations; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.lib.util.StringUtil; -import org.apache.openjpa.util.OpenJPAException; import org.apache.openjpa.util.UserException; /** @@ -159,10 +158,10 @@ public class JDBCBrokerFactory extends AbstractBrokerFactory { } @Override - public void validatePersistenceStruture() throws OpenJPAException { + public void validatePersistenceStruture() throws Exception { JDBCConfiguration conf = (JDBCConfiguration) getConfiguration(); Broker broker = super.newBrokerImpl(conf.getConnectionUserName(), conf.getConnectionPassword()); - synchronizeMappings(broker.getClassLoader(), conf, "validate(ForeignKeys=true,SchemaAction='retain')"); + synchronizeMappings(broker.getClassLoader(), conf, "validate(ForeignKeys=true)"); } @Override diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java index c5bc52a15..18e1cd865 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java @@ -481,7 +481,7 @@ public abstract class AbstractBrokerFactory implements BrokerFactory { } @Override - public void validatePersistenceStruture() throws OpenJPAException { + public void validatePersistenceStruture() throws Exception { throw new UnsupportedOperationException(); } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerFactory.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerFactory.java index 92d990558..509f2df26 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerFactory.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerFactory.java @@ -167,7 +167,7 @@ public interface BrokerFactory void dropPersistenceStrucuture(boolean dropSchemas); - void validatePersistenceStruture() throws OpenJPAException; + void validatePersistenceStruture() throws Exception; void truncateData(); diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBrokerFactory.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBrokerFactory.java index e4d119677..2fad4bf86 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBrokerFactory.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBrokerFactory.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Set; import org.apache.openjpa.conf.OpenJPAConfiguration; +import org.apache.openjpa.util.MetaDataException; import org.apache.openjpa.util.OpenJPAException; import org.apache.openjpa.util.RuntimeExceptionTranslator; @@ -280,11 +281,11 @@ public class DelegatingBrokerFactory } @Override - public void validatePersistenceStruture() throws OpenJPAException { + public void validatePersistenceStruture() throws Exception { try { _factory.validatePersistenceStruture(); - } catch (RuntimeException re) { - throw translate(re); + } catch (MetaDataException mde) { + throw new IllegalStateException(mde.getLocalizedMessage(), (OpenJPAException) mde); } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java index 5d3fd00f2..240d352e4 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java @@ -16,26 +16,19 @@ */ package org.apache.openjpa.jdbc.meta; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.StringWriter; +import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import java.util.UUID; -import org.apache.openjpa.jdbc.conf.JDBCConfiguration; -import org.apache.openjpa.jdbc.schema.SchemaTool; -import org.apache.openjpa.persistence.OpenJPAEntityManager; -import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory; -import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; -import org.apache.openjpa.persistence.common.apps.CompUser; -import org.apache.openjpa.persistence.common.utils.AbstractTestCase; -import org.apache.openjpa.persistence.jdbc.common.apps.BuildSchemaPC; +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; import org.junit.Test; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.SchemaValidationException; /** * Test that a {@link MappingTool#ACTION_REFRESH} uses the right @@ -110,5 +103,37 @@ public class TestJDBCSchemaManager extends AbstractPersistenceTestCase { assertNull(em.find(UUIDEntity.class, ue1.getId())); closeEM(em); } + + @Test + public void testValidate() { + EntityManagerFactory emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES); + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + em.getTransaction().commit(); + + Connection conn = (Connection) ((OpenJPAEntityManagerSPI) em.getDelegate()).getConnection(); + + try (Statement stmt = conn.createStatement()) { + stmt.executeUpdate("ALTER TABLE UUIDEntity DROP COLUMN value_"); + stmt.executeUpdate("ALTER TABLE UUIDEntity ADD COLUMN value_ BOOLEAN DEFAULT FALSE"); + Long n = (Long) em.createNativeQuery("SELECT COUNT(1) FROM UUIDEntity WHERE id_ = ? AND value_ = false", Long.class) + .setParameter(1, ue1.getId()) + .getSingleResult(); + assertTrue(n > 0L); + + emf.getSchemaManager().validate(); + fail("Should have thrown a SchemaValidationException"); + } catch (SQLException sex) { + fail("Could not change database for test."); + } catch (SchemaValidationException ex) { + assertTrue(ex.getMessage().startsWith("Schema could not be validated")); + } finally { + closeEM(em); + } + } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/SchemaManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/SchemaManagerImpl.java index a60ef4664..bc82fdd55 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/SchemaManagerImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/SchemaManagerImpl.java @@ -19,7 +19,6 @@ package org.apache.openjpa.persistence; import org.apache.openjpa.kernel.BrokerFactory; -import org.apache.openjpa.util.OpenJPAException; import jakarta.persistence.SchemaManager; import jakarta.persistence.SchemaValidationException; @@ -53,8 +52,10 @@ public class SchemaManagerImpl implements SchemaManager { public void validate() throws SchemaValidationException { try { _factory.validatePersistenceStruture(); - } catch (OpenJPAException ex) { - throw new SchemaValidationException("Schema could not be validated", ex); + } catch (Exception ex) { + throw new SchemaValidationException( + String.format("Schema could not be validated: %s", ex.getLocalizedMessage()), + (Exception) ex); } }