[java-plugin-storage-jdbc] 02/02: OSJ-342 Investigate Strategies to end of life our use of Hibernate in V5
Rod Widdowson
rdw at steadingsoftware.com
Mon May 30 15:22:39 UTC 2022
This is an automated email from the git hooks/post-receive script.
rdw pushed a commit to branch main
in repository java-plugin-storage-jdbc.
View the commit online:
http://git.shibboleth.net/view/?p=java-plugin-storage-jdbc.git;a=commit;h=0a3cb72c5d301c5d084774e6394208f345cb40d3
commit 0a3cb72c5d301c5d084774e6394208f345cb40d3
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon May 30 16:15:13 2022 +0100
OSJ-342 Investigate Strategies to end of life our use of Hibernate in V5
https://shibboleth.atlassian.net/browse/OSJ-342
Add JDBC/JPA interop testing
---
.../jdbc/impl/JDCBJPAMixedStorageServiceTest.java | 273 +++++++--------------
jdbc-storage-impl/src/test/resources/mixed.xml | 47 ++++
2 files changed, 139 insertions(+), 181 deletions(-)
diff --git a/jdbc-storage-impl/src/test/java/net/shibboleth/plugin/storage/jdbc/impl/JDCBJPAMixedStorageServiceTest.java b/jdbc-storage-impl/src/test/java/net/shibboleth/plugin/storage/jdbc/impl/JDCBJPAMixedStorageServiceTest.java
index f055baf..6421492 100644
--- a/jdbc-storage-impl/src/test/java/net/shibboleth/plugin/storage/jdbc/impl/JDCBJPAMixedStorageServiceTest.java
+++ b/jdbc-storage-impl/src/test/java/net/shibboleth/plugin/storage/jdbc/impl/JDCBJPAMixedStorageServiceTest.java
@@ -17,43 +17,40 @@
package net.shibboleth.plugin.storage.jdbc.impl;
+import static org.testng.Assert.assertTrue;
+
import java.io.IOException;
import java.security.SecureRandom;
-import java.time.Duration;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
-import java.util.UUID;
-
-import javax.annotation.Nonnull;
-import javax.persistence.EntityManagerFactory;
-
-import net.shibboleth.ext.spring.util.ApplicationContextBuilder;
-import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
-import org.apache.commons.dbcp2.BasicDataSource;
import org.opensaml.storage.StorageRecord;
-import org.opensaml.storage.StorageService;
import org.opensaml.storage.impl.JPAStorageService;
-import org.opensaml.storage.testing.StorageServiceTest;
-import org.springframework.beans.factory.FactoryBean;
+import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
-import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
+import net.shibboleth.ext.spring.config.StringToDurationConverter;
+import net.shibboleth.ext.spring.util.ApplicationContextBuilder;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+
/**
* Test of {@link JPAStorageService} implementation.
*/
- at SuppressWarnings("javadoc")
-public class JDCBJPAMixedStorageServiceTest extends StorageServiceTest {
+public class JDCBJPAMixedStorageServiceTest {
/** Storage service. */
- private JPAStorageService storageService;
+ private JPAStorageService jpaService;
- private JDBCStorageService jdbmsService;
+ private JDBCStorageService jdbcService;
+
+ private final SecureRandom random = new SecureRandom();
/** Contexts used for testing. */
private Object[][] contexts;
@@ -70,247 +67,161 @@ public class JDCBJPAMixedStorageServiceTest extends StorageServiceTest {
* Creates the shared instance of the entity manager factory.
*/
@BeforeClass public void setUp() throws ComponentInitializationException {
- storageService = new JPAStorageService(createEntityManagerFactory());
- storageService.setId("test");
- storageService.setCleanupInterval(Duration.ofSeconds(5));
- storageService.setTransactionRetry(2);
- super.setUp();
- }
+ final Resource resource = new ClassPathResource("/mixed.xml");
+ final ConversionServiceFactoryBean service = new ConversionServiceFactoryBean();
+ service.setConverters(new HashSet<>(Arrays.asList(
+ new StringToDurationConverter())));
+ service.afterPropertiesSet();
- /**
- * Creates an entity manager factory instance.
- *
- * @return an entity manager factory instance
- *
- * @throws ComponentInitializationException ...
- */
- private EntityManagerFactory createEntityManagerFactory() throws ComponentInitializationException
- {
- final Resource resource = new ClassPathResource("/org/opensaml/storage/impl/jpa-spring-context.xml");
final GenericApplicationContext context =
new ApplicationContextBuilder()
- .setName("JPAStorageService")
+ .setName("StorageServiceTest")
.setServiceConfiguration(resource)
+ .setConversionService(service.getObject())
.build();
- final FactoryBean<EntityManagerFactory> factoryBean =
- context.getBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.class);
- final BasicDataSource source = context.getBean("hibernateDataSource", BasicDataSource.class);
- jdbmsService = new JDBCStorageService();
- jdbmsService.setDataSource(source);
- jdbmsService.setId("TEstJDBCS");
- jdbmsService.initialize();
-
- try {
- return factoryBean.getObject();
- } catch (Exception e) {
- throw new ComponentInitializationException(e);
- }
+ jdbcService = context.getBean(JDBCStorageService.class);
+ jpaService = context.getBean(JPAStorageService.class);
}
@AfterClass
protected void tearDown() {
try {
- List<String> contexts1 = storageService.readContexts();
+ List<String> contexts1 = jpaService.readContexts();
for (String ctx : contexts1) {
- storageService.deleteContext(ctx);
+ jdbcService.deleteContext(ctx);
}
- List<?> recs = storageService.readAll();
+ List<?> recs = jpaService.readAll();
Assert.assertEquals(recs.size(), 0);
} catch (IOException e){
throw new RuntimeException(e);
}
- super.tearDown();
}
- @Nonnull protected StorageService getStorageService() {
- return storageService;
- }
-
- @Test
- public void cleanup() throws ComponentInitializationException, IOException {
+ @Test
+ public void cleanup() throws ComponentInitializationException, IOException {
String context = Long.toString(random.nextLong());
for (int i = 1; i <= 100; i++) {
- storageService.create(context, Integer.toString(i), Integer.toString(i + 1), System.currentTimeMillis() + 100);
+ jpaService.create(context, Integer.toString(i), Integer.toString(i + 1), System.currentTimeMillis() + 100);
+ jdbcService.create(context, Integer.toString(110 + i), Integer.toString(110 + i + 2), System.currentTimeMillis() + 100);
}
+ Assert.assertEquals(jdbcService.readAll(context).size(), 200);
+ Assert.assertEquals(jpaService.readAll(context).size(), 200);
try {
Thread.sleep(7500);
} catch (InterruptedException e) {
throw new IOException(e);
}
- List<?> recs = storageService.readAll(context);
- Assert.assertEquals(recs.size(), 0);
- }
-
- @DataProvider(name = "contexts")
- public Object[][] contexts() throws Exception {
- return contexts;
- }
-
- @Test(dataProvider = "contexts", singleThreaded = false, threadPoolSize = 25, invocationCount = 100)
- public void multithread(final String context) throws IOException {
- shared.create(context, "mt", "bar", System.currentTimeMillis() + 300000);
- StorageRecord<?> rec = shared.read(context, "mt");
- Assert.assertNotNull(rec);
- shared.update(context, "mt", "baz", System.currentTimeMillis() + 300000);
- rec = shared.read(context, "mt");
- Assert.assertNotNull(rec);
- boolean result = shared.create(context, "mt", "qux", null);
- Assert.assertFalse(result, "createString should have failed");
- }
-
- @Test(singleThreaded = false, threadPoolSize = 25, invocationCount = 100)
- public void multithreadCaseSensitiveKey() throws IOException {
- shared.create("unit_test", "foo", "bar", null);
- shared.create("unit_test", "FOO", "bar", null);
- StorageRecord<?> rec1 = shared.read("unit_test", "foo");
- StorageRecord<?> rec2 = shared.read("unit_test", "FOO");
- Assert.assertNotNull(rec1);
- Assert.assertNotNull(rec2);
- Assert.assertNotEquals(rec1, rec2);
+ Assert.assertEquals(jdbcService.readAll(context).size(), 0);
+ Assert.assertEquals(jpaService.readAll(context).size(), 0);
}
@Test
public void keyCollision() throws IOException {
- shared.create("unit_test", "dlo1", "value", null);
- shared.create("unit_test", "dn11", "value", null);
- StorageRecord<?> rec1 = shared.read("unit_test", "dlo1");
- StorageRecord<?> rec2 = shared.read("unit_test", "dn11");
+ jpaService.create("unit_test", "dlo1", "value", null);
+ jdbcService.create("unit_test", "dn11", "value", null);
+ StorageRecord<?> rec1 = jdbcService.read("unit_test", "dlo1");
+ StorageRecord<?> rec2 = jpaService.read("unit_test", "dn11");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
- shared.update("unit_test", "dlo1", "value2", null);
- shared.update("unit_test", "dn11", "value2", null);
- rec1 = shared.read("unit_test", "dlo1");
- rec2 = shared.read("unit_test", "dn11");
+ jpaService.update("unit_test", "dlo1", "value2", null);
+ jdbcService.update("unit_test", "dn11", "value2", null);
+ rec1 = jdbcService.read("unit_test", "dlo1");
+ rec2 = jpaService.read("unit_test", "dn11");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
- Assert.assertEquals(2, storageService.readAll().size());
- Assert.assertEquals(2, storageService.readAll("unit_test").size());
+ Assert.assertEquals(2, jdbcService.readAll().size());
+ Assert.assertEquals(2, jdbcService.readAll("unit_test").size());
+ Assert.assertEquals(2, jpaService.readAll().size());
+ Assert.assertEquals(2, jpaService.readAll("unit_test").size());
- shared.delete("unit_test", "dlo1");
- rec1 = shared.read("unit_test", "dlo1");
- rec2 = shared.read("unit_test", "dn11");
+ jpaService.delete("unit_test", "dlo1");
+ rec1 = jdbcService.read("unit_test", "dlo1");
+ rec2 = jdbcService.read("unit_test", "dn11");
Assert.assertNull(rec1);
Assert.assertNotNull(rec2);
- shared.delete("unit_test", "dn11");
- rec1 = shared.read("unit_test", "dlo1");
- rec2 = shared.read("unit_test", "dn11");
+ jdbcService.delete("unit_test", "dn11");
+ rec1 = jpaService.read("unit_test", "dlo1");
+ rec2 = jpaService.read("unit_test", "dn11");
Assert.assertNull(rec1);
Assert.assertNull(rec2);
}
@Test
public void caseSensitiveContext() throws IOException {
- shared.create("foo", "bar", "value", null);
- shared.create("FOO", "bar", "value", null);
- StorageRecord<?> rec1 = shared.read("foo", "bar");
- StorageRecord<?> rec2 = shared.read("FOO", "bar");
+ assertTrue(jpaService.create("foo", "bar", "value", null));
+ assertTrue(jdbcService.create("FOO", "bar", "value", null));
+ StorageRecord<?> rec1 = jdbcService.read("foo", "bar");
+ StorageRecord<?> rec2 = jpaService.read("FOO", "bar");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
+ Assert.assertEquals(2, jpaService.readAll().size());
+ Assert.assertEquals(2, jdbcService.readAll().size());
- shared.update("foo", "bar", "value2", null);
- shared.update("FOO", "bar", "value2", null);
- rec1 = shared.read("foo", "bar");
- rec2 = shared.read("FOO", "bar");
+ jdbcService.update("foo", "bar", "value2", null);
+ jpaService.update("FOO", "bar", "value2", null);
+ rec1 = jpaService.read("foo", "bar");
+ rec2 = jdbcService.read("FOO", "bar");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
- Assert.assertEquals(2, storageService.readAll().size());
- Assert.assertEquals(1, storageService.readAll("foo").size());
- Assert.assertEquals(1, storageService.readAll("FOO").size());
+ Assert.assertEquals(jpaService.readAll().size(), 2);
+ Assert.assertEquals(jpaService.readAll("foo").size(), 1);
+ Assert.assertEquals(jpaService.readAll("FOO").size(), 1);
+ Assert.assertEquals(jdbcService.readAll().size(), 2);
+ Assert.assertEquals(jdbcService.readAll("foo").size(), 1);
+ Assert.assertEquals(jdbcService.readAll("FOO").size(), 1);
- shared.delete("foo", "bar");
- rec1 = shared.read("foo", "bar");
- rec2 = shared.read("FOO", "bar");
+ jpaService.delete("foo", "bar");
+ rec1 = jdbcService.read("foo", "bar");
+ rec2 = jdbcService.read("FOO", "bar");
Assert.assertNull(rec1);
Assert.assertNotNull(rec2);
- shared.delete("FOO", "bar");
- rec1 = shared.read("foo", "bar");
- rec2 = shared.read("FOO", "bar");
+ jdbcService.delete("FOO", "bar");
+ rec1 = jpaService.read("foo", "bar");
+ rec2 = jpaService.read("FOO", "bar");
Assert.assertNull(rec1);
Assert.assertNull(rec2);
}
@Test
public void caseSensitiveKey() throws IOException {
- shared.create("unit_test", "foo", "value", null);
- shared.create("unit_test", "FOO", "value", null);
- StorageRecord<?> rec1 = shared.read("unit_test", "foo");
- StorageRecord<?> rec2 = shared.read("unit_test", "FOO");
+ jdbcService.create("unit_test", "foo", "value", null);
+ jpaService.create("unit_test", "FOO", "value", null);
+ StorageRecord<?> rec1 = jpaService.read("unit_test", "foo");
+ StorageRecord<?> rec2 = jdbcService.read("unit_test", "FOO");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
- shared.update("unit_test", "foo", "value2", null);
- shared.update("unit_test", "FOO", "value2", null);
- rec1 = shared.read("unit_test", "foo");
- rec2 = shared.read("unit_test", "FOO");
+ jpaService.update("unit_test", "foo", "value2", null);
+ jdbcService.update("unit_test", "FOO", "value2", null);
+ rec1 = jdbcService.read("unit_test", "foo");
+ rec2 = jpaService.read("unit_test", "FOO");
Assert.assertNotNull(rec1);
Assert.assertNotNull(rec2);
Assert.assertNotEquals(rec1, rec2);
- Assert.assertEquals(2, storageService.readAll().size());
- Assert.assertEquals(2, storageService.readAll("unit_test").size());
+ Assert.assertEquals(2, jpaService.readAll().size());
+ Assert.assertEquals(2, jpaService.readAll("unit_test").size());
+ Assert.assertEquals(2, jdbcService.readAll().size());
+ Assert.assertEquals(2, jdbcService.readAll("unit_test").size());
- shared.delete("unit_test", "foo");
- rec1 = shared.read("unit_test", "foo");
- rec2 = shared.read("unit_test", "FOO");
+ jdbcService.delete("unit_test", "foo");
+ rec1 = jpaService.read("unit_test", "foo");
+ rec2 = jpaService.read("unit_test", "FOO");
Assert.assertNull(rec1);
Assert.assertNotNull(rec2);
- shared.delete("unit_test", "FOO");
- rec1 = shared.read("unit_test", "foo");
- rec2 = shared.read("unit_test", "FOO");
+ jpaService.delete("unit_test", "FOO");
+ rec1 = jdbcService.read("unit_test", "foo");
+ rec2 = jdbcService.read("unit_test", "FOO");
Assert.assertNull(rec1);
Assert.assertNull(rec2);
}
- @Test(enabled = false)
- public void largeValue() throws IOException {
- // hsqldb defaults LOB length to 255 chars; disabled for now
- StringBuilder sb = new StringBuilder(1000 * 36);
- for (int i = 0; i < 1000; i++) {
- sb.append(UUID.randomUUID());
- }
- shared.create("unit_test", "large", sb.toString(), System.currentTimeMillis() + 300000);
- StorageRecord<?> rec = shared.read("unit_test", "large");
- Assert.assertNotNull(rec);
- Assert.assertEquals(sb.toString(), rec.getValue());
- }
-
- @Test
- public void jpaWriteRDBMSRead() throws IOException {
- StringBuilder sb = new StringBuilder(255);
- for (int i = 0; i < 255/36; i++) {
- sb.append(UUID.randomUUID());
- }
- shared.create("mixed1", "large", sb.toString(), System.currentTimeMillis() + 300000);
- final StorageRecord<?> jpa = shared.read("mixed1", "large");
- Assert.assertNotNull(jpa);
- Assert.assertEquals(sb.toString(), jpa.getValue());
-
- final StorageRecord<?> rdbms = jdbmsService.read("mixed1", "large");
- Assert.assertNotNull(rdbms );
- Assert.assertEquals(sb.toString(), rdbms .getValue());
- }
- @Test
- public void jpaReadDBMSWrite() throws IOException {
- StringBuilder sb = new StringBuilder(255);
- for (int i = 0; i < 255/36; i++) {
- sb.append(UUID.randomUUID());
- }
- Assert.assertTrue(jdbmsService.create("mixed1", "rrrwd", sb.toString(), System.currentTimeMillis() + 300000));
- final StorageRecord<?> rdbms = jdbmsService.read("mixed1", "rrrwd");
- Assert.assertNotNull(rdbms );
- Assert.assertEquals(sb.toString(), rdbms .getValue());
- final StorageRecord<?> jpa = shared.read("mixed1", "rrrwd");
- Assert.assertNotNull(jpa);
- Assert.assertEquals(sb.toString(), jpa.getValue());
-
- Assert.assertFalse(jdbmsService.create("mixed1", "rrrwd", sb.toString(), System.currentTimeMillis() + 300000));
- }
}
diff --git a/jdbc-storage-impl/src/test/resources/mixed.xml b/jdbc-storage-impl/src/test/resources/mixed.xml
new file mode 100644
index 0000000..18cf14a
--- /dev/null
+++ b/jdbc-storage-impl/src/test/resources/mixed.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:p="http://www.springframework.org/schema/p"
+ xmlns:c="http://www.springframework.org/schema/c"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
+ default-init-method="initialize"
+ default-destroy-method="destroy">
+
+ <bean id="shibboleth.IdentifiableBeanPostProcessor" class="net.shibboleth.ext.spring.config.IdentifiableBeanPostProcessor" />
+
+ <context:property-placeholder />
+
+ <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true"
+ p:driverClassName="org.hsqldb.jdbcDriver"
+ p:url="jdbc:hsqldb:mem:StorageService"
+ p:username="SA"
+ p:password="" />
+
+ <bean id="dialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
+
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
+ p:persistenceUnitName="unit-tests"
+ p:packagesToScan="org.opensaml.storage.impl"
+ p:dataSource-ref="dataSource"
+ p:jpaVendorAdapter-ref="jpaVendorAdapter"
+ p:jpaDialect-ref="dialect" />
+
+ <bean id="JPAStorageService" class="org.opensaml.storage.impl.JPAStorageService"
+ p:cleanupInterval="PT5S"
+ p:transactionRetry="4"
+ c:factory-ref="entityManagerFactory" />
+
+ <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
+ p:database="HSQL"
+ p:generateDdl="true"
+ p:showSql="false"/>
+
+ <bean id="JDBCStorageService" class="net.shibboleth.plugin.storage.jdbc.impl.JDBCStorageService"
+ p:dataSource-ref="dataSource"
+ p:transactionIsolation="4"
+ p:cleanupInterval="PT5S"
+ p:localLocking="false" />
+
+</beans>
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list