Skip to content

Commit

Permalink
test: migrate type support tests from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcwarren committed Jun 27, 2024
1 parent ccf6537 commit 8ea5210
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
139 changes: 139 additions & 0 deletions spring-content-fs/src/test/java/it/typesupport/FsTypeSupportTest.java
Original file line number Diff line number Diff line change
@@ -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 {
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.typesupport.model;

import org.springframework.content.commons.repository.ContentStore;

import java.math.BigInteger;

public interface BigIntegerBasedContentEntityStore extends ContentStore<BigIntegerBasedContentEntity, BigInteger> {

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.typesupport.model;

import org.springframework.content.commons.repository.ContentStore;

public interface LongBasedContentEntityStore extends ContentStore<LongBasedContentEntity, Long> {

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.typesupport.model;

import org.springframework.content.commons.repository.ContentStore;

import java.net.URI;

public interface URIBasedContentEntityStore extends ContentStore<URIBasedContentEntity, URI> {

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.typesupport.model;

import org.springframework.content.commons.repository.ContentStore;

import java.util.UUID;

public interface UUIDBasedContentEntityStore extends ContentStore<UUIDBasedContentEntity, UUID> {

}

0 comments on commit 8ea5210

Please sign in to comment.