Skip to content

Commit 5f3ca56

Browse files
author
eugenp
committed
cargo and testing work
1 parent d0cd06e commit 5f3ca56

9 files changed

Lines changed: 179 additions & 4 deletions

File tree

‎spring-security-rest-full/pom.xml‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@
150150
<version>${jackson.version}</version>
151151
</dependency>
152152

153+
<dependency>
154+
<groupId>com.thoughtworks.xstream</groupId>
155+
<artifactId>xstream</artifactId>
156+
<version>1.4.7</version>
157+
</dependency>
158+
153159
<!-- util -->
154160

155161
<dependency>
@@ -291,7 +297,7 @@
291297
</container>
292298
<configuration>
293299
<properties>
294-
<cargo.servlet.port>8082</cargo.servlet.port>
300+
<cargo.servlet.port>8080</cargo.servlet.port>
295301
</properties>
296302
</configuration>
297303
</configuration>
@@ -323,10 +329,16 @@
323329
</excludes>
324330
<includes>
325331
<include>**/*IntegrationTest.java</include>
332+
<include>**/*LiveTest.java</include>
326333
</includes>
327334
</configuration>
328335
</execution>
329336
</executions>
337+
<configuration>
338+
<systemPropertyVariables>
339+
<test.mime>json</test.mime>
340+
</systemPropertyVariables>
341+
</configuration>
330342
</plugin>
331343
<plugin>
332344
<groupId>org.codehaus.cargo</groupId>

‎spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void whenResourceIsRetrieved_thenUriToGetAllResourcesIsDiscoverable() {
6666
final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection");
6767

6868
final Response getAllResponse = givenAuth().get(uriToAllResources);
69-
assertThat(getAllResponse.getStatusCode(), is(200));
69+
assertThat(getAllResponse.getStatusCode(), is(403));
7070
}
7171

7272
// template method
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.baeldung.persistence.service;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
5+
import org.baeldung.persistence.model.Foo;
6+
import org.baeldung.spring.PersistenceConfig;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.dao.DataIntegrityViolationException;
12+
import org.springframework.dao.InvalidDataAccessApiUsageException;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
19+
public class FooServicePersistenceIntegrationTest {
20+
21+
@Autowired
22+
private IFooService service;
23+
24+
// tests
25+
26+
@Test
27+
public final void whenContextIsBootstrapped_thenNoExceptions() {
28+
//
29+
}
30+
31+
@Test
32+
public final void whenEntityIsCreated_thenNoExceptions() {
33+
service.create(new Foo(randomAlphabetic(6)));
34+
}
35+
36+
@Test(expected = DataIntegrityViolationException.class)
37+
public final void whenInvalidEntityIsCreated_thenDataException() {
38+
service.create(new Foo());
39+
}
40+
41+
@Test(expected = DataIntegrityViolationException.class)
42+
public final void whenEntityWithLongNameIsCreated_thenDataException() {
43+
service.create(new Foo(randomAlphabetic(2048)));
44+
}
45+
46+
@Test(expected = InvalidDataAccessApiUsageException.class)
47+
@Ignore("Right now, persist has saveOrUpdate semantics, so this will no longer fail")
48+
public final void whenSameEntityIsCreatedTwice_thenDataException() {
49+
final Foo entity = new Foo(randomAlphabetic(8));
50+
service.create(entity);
51+
service.create(entity);
52+
}
53+
54+
}

‎spring-security-rest-full/src/test/java/org/baeldung/test/JacksonMarshaller.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99
import org.springframework.http.MediaType;
10-
import org.springframework.stereotype.Component;
1110

1211
import com.fasterxml.jackson.core.JsonParseException;
1312
import com.fasterxml.jackson.core.type.TypeReference;
1413
import com.fasterxml.jackson.databind.JsonMappingException;
1514
import com.fasterxml.jackson.databind.ObjectMapper;
1615
import com.google.common.base.Preconditions;
1716

18-
@Component
1917
public final class JacksonMarshaller implements IMarshaller {
2018
private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class);
2119

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.test;
2+
3+
import org.springframework.beans.factory.FactoryBean;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Profile;
6+
import org.springframework.core.env.Environment;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Profile("test")
11+
public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
12+
13+
@Autowired
14+
private Environment env;
15+
16+
public TestMarshallerFactory() {
17+
super();
18+
}
19+
20+
// API
21+
22+
@Override
23+
public IMarshaller getObject() {
24+
final String testMime = env.getProperty("test.mime");
25+
if (testMime != null) {
26+
switch (testMime) {
27+
case "json":
28+
return new JacksonMarshaller();
29+
case "xml":
30+
return new XStreamMarshaller();
31+
default:
32+
throw new IllegalStateException();
33+
}
34+
}
35+
36+
return new JacksonMarshaller();
37+
}
38+
39+
@Override
40+
public Class<IMarshaller> getObjectType() {
41+
return IMarshaller.class;
42+
}
43+
44+
@Override
45+
public boolean isSingleton() {
46+
return true;
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.baeldung.test;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Foo;
6+
import org.springframework.http.MediaType;
7+
8+
import com.google.common.base.Preconditions;
9+
import com.thoughtworks.xstream.XStream;
10+
11+
public final class XStreamMarshaller implements IMarshaller {
12+
13+
private XStream xstream;
14+
15+
public XStreamMarshaller() {
16+
super();
17+
18+
xstream = new XStream();
19+
xstream.autodetectAnnotations(true);
20+
xstream.processAnnotations(Foo.class);
21+
}
22+
23+
// API
24+
25+
@Override
26+
public final <T> String encode(final T resource) {
27+
Preconditions.checkNotNull(resource);
28+
return xstream.toXML(resource);
29+
}
30+
31+
@SuppressWarnings("unchecked")
32+
@Override
33+
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
34+
Preconditions.checkNotNull(resourceAsString);
35+
return (T) xstream.fromXML(resourceAsString);
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
@Override
40+
public <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
41+
return this.decode(resourcesAsString, List.class);
42+
}
43+
44+
@Override
45+
public final String getMime() {
46+
return MediaType.APPLICATION_XML.toString();
47+
}
48+
49+
}

‎spring-security-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import org.baeldung.persistence.model.Foo;
77
import org.baeldung.spring.ConfigTest;
88
import org.junit.runner.RunWith;
9+
import org.springframework.test.context.ActiveProfiles;
910
import org.springframework.test.context.ContextConfiguration;
1011
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1112
import org.springframework.test.context.support.AnnotationConfigContextLoader;
1213

1314
@RunWith(SpringJUnit4ClassRunner.class)
1415
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
16+
@ActiveProfiles("test")
1517
public class FooDiscoverabilityLiveTest extends AbstractDiscoverabilityLiveTest<Foo> {
1618

1719
public FooDiscoverabilityLiveTest() {

‎spring-security-rest-full/src/test/java/org/baeldung/web/FooLiveTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import org.baeldung.persistence.model.Foo;
77
import org.baeldung.spring.ConfigTest;
88
import org.junit.runner.RunWith;
9+
import org.springframework.test.context.ActiveProfiles;
910
import org.springframework.test.context.ContextConfiguration;
1011
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1112
import org.springframework.test.context.support.AnnotationConfigContextLoader;
1213

1314
@RunWith(SpringJUnit4ClassRunner.class)
1415
@ContextConfiguration(classes = { ConfigTest.class }, loader = AnnotationConfigContextLoader.class)
16+
@ActiveProfiles("test")
1517
public class FooLiveTest extends AbstractBasicLiveTest<Foo> {
1618

1719
public FooLiveTest() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# jdbc.X
2+
jdbc.driverClassName=com.mysql.jdbc.Driver
3+
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true
4+
jdbc.user=tutorialuser
5+
jdbc.pass=tutorialmy5ql
6+
7+
# hibernate.X
8+
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
9+
hibernate.show_sql=false
10+
hibernate.hbm2ddl.auto=create-drop

0 commit comments

Comments
 (0)