From 5f3ca56a525abc1999e8c2e7e521e73f7e18ed8f Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 17 May 2014 00:29:05 +0300 Subject: [PATCH] cargo and testing work --- spring-security-rest-full/pom.xml | 14 ++++- .../web/AbstractDiscoverabilityLiveTest.java | 2 +- .../FooServicePersistenceIntegrationTest.java | 54 +++++++++++++++++++ .../org/baeldung/test/JacksonMarshaller.java | 2 - .../baeldung/test/TestMarshallerFactory.java | 48 +++++++++++++++++ .../org/baeldung/test/XStreamMarshaller.java | 49 +++++++++++++++++ .../web/FooDiscoverabilityLiveTest.java | 2 + .../java/org/baeldung/web/FooLiveTest.java | 2 + .../resources/persistence-mysql.properties | 10 ++++ 9 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java create mode 100644 spring-security-rest-full/src/test/java/org/baeldung/test/TestMarshallerFactory.java create mode 100644 spring-security-rest-full/src/test/java/org/baeldung/test/XStreamMarshaller.java create mode 100644 spring-security-rest-full/src/test/resources/persistence-mysql.properties diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index 7d1d345faf..5dca632807 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -150,6 +150,12 @@ ${jackson.version} + + com.thoughtworks.xstream + xstream + 1.4.7 + + @@ -291,7 +297,7 @@ - 8082 + 8080 @@ -323,10 +329,16 @@ **/*IntegrationTest.java + **/*LiveTest.java + + + json + + org.codehaus.cargo diff --git a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java index 4a7c19b6ce..ca3f56416d 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java @@ -66,7 +66,7 @@ public abstract class AbstractDiscoverabilityLiveTest ex final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection"); final Response getAllResponse = givenAuth().get(uriToAllResources); - assertThat(getAllResponse.getStatusCode(), is(200)); + assertThat(getAllResponse.getStatusCode(), is(403)); } // template method diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java new file mode 100644 index 0000000000..fd035e5615 --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -0,0 +1,54 @@ +package org.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooServicePersistenceIntegrationTest { + + @Autowired + private IFooService service; + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Foo(randomAlphabetic(6))); + } + + @Test(expected = DataIntegrityViolationException.class) + public final void whenInvalidEntityIsCreated_thenDataException() { + service.create(new Foo()); + } + + @Test(expected = DataIntegrityViolationException.class) + public final void whenEntityWithLongNameIsCreated_thenDataException() { + service.create(new Foo(randomAlphabetic(2048))); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + @Ignore("Right now, persist has saveOrUpdate semantics, so this will no longer fail") + public final void whenSameEntityIsCreatedTwice_thenDataException() { + final Foo entity = new Foo(randomAlphabetic(8)); + service.create(entity); + service.create(entity); + } + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/test/JacksonMarshaller.java b/spring-security-rest-full/src/test/java/org/baeldung/test/JacksonMarshaller.java index 99deafaee4..392e101193 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/test/JacksonMarshaller.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/test/JacksonMarshaller.java @@ -7,7 +7,6 @@ import org.baeldung.persistence.model.Foo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; @@ -15,7 +14,6 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Preconditions; -@Component public final class JacksonMarshaller implements IMarshaller { private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class); diff --git a/spring-security-rest-full/src/test/java/org/baeldung/test/TestMarshallerFactory.java b/spring-security-rest-full/src/test/java/org/baeldung/test/TestMarshallerFactory.java new file mode 100644 index 0000000000..11273276cd --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/test/TestMarshallerFactory.java @@ -0,0 +1,48 @@ +package org.baeldung.test; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Profile("test") +public class TestMarshallerFactory implements FactoryBean { + + @Autowired + private Environment env; + + public TestMarshallerFactory() { + super(); + } + + // API + + @Override + public IMarshaller getObject() { + final String testMime = env.getProperty("test.mime"); + if (testMime != null) { + switch (testMime) { + case "json": + return new JacksonMarshaller(); + case "xml": + return new XStreamMarshaller(); + default: + throw new IllegalStateException(); + } + } + + return new JacksonMarshaller(); + } + + @Override + public Class getObjectType() { + return IMarshaller.class; + } + + @Override + public boolean isSingleton() { + return true; + } +} \ No newline at end of file diff --git a/spring-security-rest-full/src/test/java/org/baeldung/test/XStreamMarshaller.java b/spring-security-rest-full/src/test/java/org/baeldung/test/XStreamMarshaller.java new file mode 100644 index 0000000000..d7cf084e34 --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/test/XStreamMarshaller.java @@ -0,0 +1,49 @@ +package org.baeldung.test; + +import java.util.List; + +import org.baeldung.persistence.model.Foo; +import org.springframework.http.MediaType; + +import com.google.common.base.Preconditions; +import com.thoughtworks.xstream.XStream; + +public final class XStreamMarshaller implements IMarshaller { + + private XStream xstream; + + public XStreamMarshaller() { + super(); + + xstream = new XStream(); + xstream.autodetectAnnotations(true); + xstream.processAnnotations(Foo.class); + } + + // API + + @Override + public final String encode(final T resource) { + Preconditions.checkNotNull(resource); + return xstream.toXML(resource); + } + + @SuppressWarnings("unchecked") + @Override + public final T decode(final String resourceAsString, final Class clazz) { + Preconditions.checkNotNull(resourceAsString); + return (T) xstream.fromXML(resourceAsString); + } + + @SuppressWarnings("unchecked") + @Override + public List decodeList(final String resourcesAsString, final Class clazz) { + return this.decode(resourcesAsString, List.class); + } + + @Override + public final String getMime() { + return MediaType.APPLICATION_XML.toString(); + } + +} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java index 28dfcd372c..c0e1f9d04d 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java @@ -6,12 +6,14 @@ import org.baeldung.common.web.AbstractDiscoverabilityLiveTest; import org.baeldung.persistence.model.Foo; import org.baeldung.spring.ConfigTest; import org.junit.runner.RunWith; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) +@ActiveProfiles("test") public class FooDiscoverabilityLiveTest extends AbstractDiscoverabilityLiveTest { public FooDiscoverabilityLiveTest() { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java index 9024ca4f96..5a4f472fe3 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java @@ -6,12 +6,14 @@ import org.baeldung.common.web.AbstractBasicLiveTest; import org.baeldung.persistence.model.Foo; import org.baeldung.spring.ConfigTest; import org.junit.runner.RunWith; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class) +@ActiveProfiles("test") public class FooLiveTest extends AbstractBasicLiveTest { public FooLiveTest() { diff --git a/spring-security-rest-full/src/test/resources/persistence-mysql.properties b/spring-security-rest-full/src/test/resources/persistence-mysql.properties new file mode 100644 index 0000000000..8263b0d9ac --- /dev/null +++ b/spring-security-rest-full/src/test/resources/persistence-mysql.properties @@ -0,0 +1,10 @@ +# jdbc.X +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true +jdbc.user=tutorialuser +jdbc.pass=tutorialmy5ql + +# hibernate.X +hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +hibernate.show_sql=false +hibernate.hbm2ddl.auto=create-drop