From 1ea0a66bcf2900c68292e5e6466cf76432f8a51a Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Wed, 26 Jun 2024 22:24:19 -0700 Subject: [PATCH] test: migrate type support tests from examples (#2008) --- .../it/typesupport/FsTypeSupportConfig.java | 69 +++++++++ .../it/typesupport/FsTypeSupportTest.java | 139 ++++++++++++++++++ .../model/BigIntegerBasedContentEntity.java | 19 +++ .../BigIntegerBasedContentEntityStore.java | 9 ++ .../model/LongBasedContentEntity.java | 17 +++ .../model/LongBasedContentEntityStore.java | 7 + .../model/URIBasedContentEntity.java | 19 +++ .../model/URIBasedContentEntityStore.java | 9 ++ .../model/UUIDBasedContentEntity.java | 19 +++ .../model/UUIDBasedContentEntityStore.java | 9 ++ 10 files changed, 316 insertions(+) create mode 100644 spring-content-fs/src/test/java/it/typesupport/FsTypeSupportConfig.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/FsTypeSupportTest.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntity.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntityStore.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntity.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntityStore.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntity.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntityStore.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntity.java create mode 100644 spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntityStore.java diff --git a/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportConfig.java b/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportConfig.java new file mode 100644 index 000000000..03b925ef9 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportConfig.java @@ -0,0 +1,69 @@ +package it.typesupport; + +import org.springframework.content.fs.config.EnableFilesystemStores; +import org.springframework.content.fs.io.FileSystemResourceLoader; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.Database; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +@Configuration +@EnableJpaRepositories(basePackages="it.typesupport") +@EnableTransactionManagement +@EnableFilesystemStores +public class FsTypeSupportConfig { + + @Bean + File filesystemRoot() { + try { + return Files.createTempDirectory("").toFile(); + } catch (IOException ioe) {} + return null; + } + + @Bean + FileSystemResourceLoader fileSystemResourceLoader() { + return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath()); + } + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + return builder.setType(EmbeddedDatabaseType.HSQL).build(); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + vendorAdapter.setDatabase(Database.HSQL); + vendorAdapter.setGenerateDdl(true); + + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setJpaVendorAdapter(vendorAdapter); + factory.setPackagesToScan("examples.models"); + factory.setDataSource(dataSource()); + + return factory; + } + + @Bean + public PlatformTransactionManager transactionManager() { + + JpaTransactionManager txManager = new JpaTransactionManager(); + txManager.setEntityManagerFactory(entityManagerFactory().getObject()); + return txManager; + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportTest.java b/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportTest.java new file mode 100644 index 000000000..941015fc3 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/FsTypeSupportTest.java @@ -0,0 +1,139 @@ +package it.typesupport; + +import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration; +import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner; +import it.typesupport.model.*; +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.net.URI; +import java.util.UUID; + +import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.*; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; + +@RunWith(Ginkgo4jSpringRunner.class) +@Ginkgo4jConfiguration(threads=1) +@ContextConfiguration(classes = { FsTypeSupportConfig.class }) +public class FsTypeSupportTest { + + @Autowired protected UUIDBasedContentEntityStore uuidStore; + @Autowired protected URIBasedContentEntityStore uriStore; + @Autowired protected LongBasedContentEntityStore longStore; + @Autowired protected BigIntegerBasedContentEntityStore bigIntStore; + + Object entity; + Object id; + + { + Describe("java.util.UUID", () -> { + Context("given a content entity", () -> { + BeforeEach(() -> { + entity = new UUIDBasedContentEntity(); + }); + Context("given the Application sets the ID", () -> { + BeforeEach(() -> { + id = UUID.randomUUID(); + ((UUIDBasedContentEntity)entity).setContentId((UUID)id); + + uuidStore.setContent((UUIDBasedContentEntity)entity, new ByteArrayInputStream("uuid".getBytes())); + }); + It("should store the content successfully", () -> { + Assert.assertThat(IOUtils.contentEquals(uuidStore.getContent((UUIDBasedContentEntity)entity), IOUtils.toInputStream("uuid")), is(true)); + }); + }); + Context("given Spring Content generates the ID", () -> { + BeforeEach(() -> { + uuidStore.setContent((UUIDBasedContentEntity)entity, new ByteArrayInputStream("uuid".getBytes())); + }); + It("should store the content successfully", () -> { + Assert.assertThat(IOUtils.contentEquals(uuidStore.getContent((UUIDBasedContentEntity)entity), IOUtils.toInputStream("uuid")), is(true)); + }); + }); + }); + AfterEach(() -> { + uuidStore.unsetContent((UUIDBasedContentEntity)entity); + Assert.assertThat(((UUIDBasedContentEntity) entity).getContentId(), is(nullValue())); + }); + }); + Describe("java.net.URI", () -> { + Context("given a content entity", () -> { + BeforeEach(() -> { + entity = new URIBasedContentEntity(); + }); + Context("given the Application sets the ID", () -> { + BeforeEach(() -> { + id = new URI("/some/deep/location"); + ((URIBasedContentEntity)entity).setContentId((URI)id); + + uriStore.setContent((URIBasedContentEntity)entity, new ByteArrayInputStream("uri".getBytes())); + }); + It("should store the content successfully", () -> { + Assert.assertThat(IOUtils.contentEquals(uriStore.getContent((URIBasedContentEntity)entity), IOUtils.toInputStream("uri")), is(true)); + }); + }); + }); + AfterEach(() -> { + uriStore.unsetContent((URIBasedContentEntity)entity); + Assert.assertThat(((URIBasedContentEntity) entity).getContentId(), is(nullValue())); + }); + }); + Describe("java.lang.Long", () -> { + Context("given a content entity", () -> { + BeforeEach(() -> { + entity = new LongBasedContentEntity(); + }); + Context("given the Application sets the ID", () -> { + BeforeEach(() -> { + id = Long.MAX_VALUE; + ((LongBasedContentEntity)entity).setContentId((Long)id); + + longStore.setContent((LongBasedContentEntity)entity, new ByteArrayInputStream("long".getBytes())); + }); + It("should store the content successfully", () -> { + Assert.assertThat(IOUtils.contentEquals(longStore.getContent((LongBasedContentEntity)entity), IOUtils.toInputStream("long")), is(true)); + }); + }); + }); + AfterEach(() -> { + longStore.unsetContent((LongBasedContentEntity)entity); + Assert.assertThat(((LongBasedContentEntity) entity).getContentId(), is(nullValue())); + }); + }); + Describe("java.math.BigInteger", () -> { + Context("given a content entity", () -> { + BeforeEach(() -> { + entity = new BigIntegerBasedContentEntity(); + }); + Context("given the Application sets the ID", () -> { + BeforeEach(() -> { + id = BigInteger.valueOf(Long.MAX_VALUE); + ((BigIntegerBasedContentEntity)entity).setContentId((BigInteger)id); + + bigIntStore.setContent((BigIntegerBasedContentEntity)entity, new ByteArrayInputStream("big-int".getBytes())); + }); + It("should store the content successfully", () -> { + Assert.assertThat(IOUtils.contentEquals(bigIntStore.getContent((BigIntegerBasedContentEntity)entity), IOUtils.toInputStream("big-int")), is(true)); + }); + }); + }); + AfterEach(() -> { + bigIntStore.unsetContent((BigIntegerBasedContentEntity)entity); + Assert.assertThat(((BigIntegerBasedContentEntity) entity).getContentId(), is(nullValue())); + }); + }); + } + + + @Test + public void noop() throws IOException { + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntity.java b/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntity.java new file mode 100644 index 000000000..6f8105c51 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntity.java @@ -0,0 +1,19 @@ +package it.typesupport.model; + +import org.springframework.content.commons.annotations.ContentId; + +import java.math.BigInteger; + +public class BigIntegerBasedContentEntity { + + @ContentId + private BigInteger contentId; + + public BigInteger getContentId() { + return contentId; + } + + public void setContentId(BigInteger contentId) { + this.contentId = contentId; + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntityStore.java b/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntityStore.java new file mode 100644 index 000000000..28195d8f6 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/BigIntegerBasedContentEntityStore.java @@ -0,0 +1,9 @@ +package it.typesupport.model; + +import org.springframework.content.commons.repository.ContentStore; + +import java.math.BigInteger; + +public interface BigIntegerBasedContentEntityStore extends ContentStore { + +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntity.java b/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntity.java new file mode 100644 index 000000000..21b0fac4d --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntity.java @@ -0,0 +1,17 @@ +package it.typesupport.model; + +import org.springframework.content.commons.annotations.ContentId; + +public class LongBasedContentEntity { + + @ContentId + private Long contentId; + + public Long getContentId() { + return contentId; + } + + public void setContentId(Long contentId) { + this.contentId = contentId; + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntityStore.java b/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntityStore.java new file mode 100644 index 000000000..809a1319c --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/LongBasedContentEntityStore.java @@ -0,0 +1,7 @@ +package it.typesupport.model; + +import org.springframework.content.commons.repository.ContentStore; + +public interface LongBasedContentEntityStore extends ContentStore { + +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntity.java b/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntity.java new file mode 100644 index 000000000..85dd9c5cb --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntity.java @@ -0,0 +1,19 @@ +package it.typesupport.model; + +import org.springframework.content.commons.annotations.ContentId; + +import java.net.URI; + +public class URIBasedContentEntity { + + @ContentId + private URI contentId; + + public URI getContentId() { + return contentId; + } + + public void setContentId(URI contentId) { + this.contentId = contentId; + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntityStore.java b/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntityStore.java new file mode 100644 index 000000000..56e7070f6 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/URIBasedContentEntityStore.java @@ -0,0 +1,9 @@ +package it.typesupport.model; + +import org.springframework.content.commons.repository.ContentStore; + +import java.net.URI; + +public interface URIBasedContentEntityStore extends ContentStore { + +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntity.java b/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntity.java new file mode 100644 index 000000000..60756bb58 --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntity.java @@ -0,0 +1,19 @@ +package it.typesupport.model; + +import org.springframework.content.commons.annotations.ContentId; + +import java.util.UUID; + +public class UUIDBasedContentEntity { + + @ContentId + private UUID contentId; + + public UUID getContentId() { + return contentId; + } + + public void setContentId(UUID contentId) { + this.contentId = contentId; + } +} diff --git a/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntityStore.java b/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntityStore.java new file mode 100644 index 000000000..f614111ba --- /dev/null +++ b/spring-content-fs/src/test/java/it/typesupport/model/UUIDBasedContentEntityStore.java @@ -0,0 +1,9 @@ +package it.typesupport.model; + +import org.springframework.content.commons.repository.ContentStore; + +import java.util.UUID; + +public interface UUIDBasedContentEntityStore extends ContentStore { + +}