persistence work
This commit is contained in:
parent
09eaa5b8e1
commit
2b7304c32d
|
@ -1,172 +1,179 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<groupId>org.baeldung</groupId>
|
||||||
<groupId>org.baeldung</groupId>
|
<artifactId>spring-hibernate4</artifactId>
|
||||||
<artifactId>spring-hibernate4</artifactId>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<name>spring-hibernate4</name>
|
<name>spring-hibernate4</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-orm</artifactId>
|
<artifactId>spring-orm</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-core</artifactId>
|
<artifactId>hibernate-core</artifactId>
|
||||||
<version>4.2.1.Final</version>
|
<version>4.2.1.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javassist</groupId>
|
<groupId>org.javassist</groupId>
|
||||||
<artifactId>javassist</artifactId>
|
<artifactId>javassist</artifactId>
|
||||||
<version>3.17.1-GA</version>
|
<version>3.17.1-GA</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<version>5.1.25</version>
|
<version>5.1.25</version>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- utils -->
|
<!-- validation -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>hibernate-validator</artifactId>
|
||||||
<version>14.0.1</version>
|
<version>5.0.1.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- utils -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>3.1</version>
|
<version>14.0.1</version>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<!-- test scoped -->
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>junit-dep</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>3.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hamcrest</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>hamcrest-core</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
<version>${org.hamcrest.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-library</artifactId>
|
|
||||||
<version>${org.hamcrest.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>junit-dep</artifactId>
|
||||||
<version>${mockito.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<build>
|
<dependency>
|
||||||
<finalName>spring-hibernate4</finalName>
|
<groupId>org.mockito</groupId>
|
||||||
<resources>
|
<artifactId>mockito-core</artifactId>
|
||||||
<resource>
|
<version>${mockito.version}</version>
|
||||||
<directory>src/main/resources</directory>
|
<scope>test</scope>
|
||||||
<filtering>true</filtering>
|
</dependency>
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
</dependencies>
|
||||||
|
|
||||||
<plugin>
|
<build>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<finalName>spring-hibernate4</finalName>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<resources>
|
||||||
<configuration>
|
<resource>
|
||||||
<excludes>
|
<directory>src/main/resources</directory>
|
||||||
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
<filtering>true</filtering>
|
||||||
</excludes>
|
</resource>
|
||||||
<systemPropertyVariables>
|
</resources>
|
||||||
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
|
||||||
</systemPropertyVariables>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
<plugins>
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<wait>true</wait>
|
|
||||||
<container>
|
|
||||||
<containerId>jetty8x</containerId>
|
|
||||||
<type>embedded</type>
|
|
||||||
<systemProperties>
|
|
||||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
|
||||||
</systemProperties>
|
|
||||||
</container>
|
|
||||||
<configuration>
|
|
||||||
<properties>
|
|
||||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
|
||||||
</properties>
|
|
||||||
</configuration>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
||||||
|
</excludes>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</build>
|
<plugin>
|
||||||
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<wait>true</wait>
|
||||||
|
<container>
|
||||||
|
<containerId>jetty8x</containerId>
|
||||||
|
<type>embedded</type>
|
||||||
|
<systemProperties>
|
||||||
|
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
||||||
|
</systemProperties>
|
||||||
|
</container>
|
||||||
|
<configuration>
|
||||||
|
<properties>
|
||||||
|
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||||
|
</properties>
|
||||||
|
</configuration>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<properties>
|
</plugins>
|
||||||
<!-- Spring -->
|
|
||||||
<org.springframework.version>3.2.2.RELEASE</org.springframework.version>
|
|
||||||
|
|
||||||
<!-- logging -->
|
</build>
|
||||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
|
||||||
<logback.version>1.0.11</logback.version>
|
|
||||||
|
|
||||||
<!-- testing -->
|
<properties>
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<!-- Spring -->
|
||||||
<junit.version>4.11</junit.version>
|
<org.springframework.version>3.2.2.RELEASE</org.springframework.version>
|
||||||
<mockito.version>1.9.5</mockito.version>
|
|
||||||
|
|
||||||
<httpcore.version>4.2.4</httpcore.version>
|
<!-- logging -->
|
||||||
<httpclient.version>4.2.4</httpclient.version>
|
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||||
|
<logback.version>1.0.11</logback.version>
|
||||||
|
|
||||||
<rest-assured.version>1.8.0</rest-assured.version>
|
<!-- testing -->
|
||||||
<groovy.version>1.8.9</groovy.version>
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.11</junit.version>
|
||||||
|
<mockito.version>1.9.5</mockito.version>
|
||||||
|
|
||||||
</properties>
|
<httpcore.version>4.2.4</httpcore.version>
|
||||||
|
<httpclient.version>4.2.4</httpclient.version>
|
||||||
|
|
||||||
|
<rest-assured.version>1.8.0</rest-assured.version>
|
||||||
|
<groovy.version>1.8.9</groovy.version>
|
||||||
|
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -5,6 +5,7 @@ import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Foo {
|
public class Foo {
|
||||||
|
@ -14,6 +15,7 @@ public class Foo {
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@NotNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public Foo() {
|
public Foo() {
|
||||||
|
|
|
@ -7,6 +7,8 @@ import org.baeldung.spring.persistence.model.Foo;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
@ -30,10 +32,26 @@ public class FooServicePersistenceIntegrationTest {
|
||||||
service.create(new Foo(randomAlphabetic(6)));
|
service.create(new Foo(randomAlphabetic(6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test(expected = DataIntegrityViolationException.class)
|
@Test(expected = DataIntegrityViolationException.class)
|
||||||
@Test
|
|
||||||
public final void whenInvalidEntityIsCreated_thenDataException() {
|
public final void whenInvalidEntityIsCreated_thenDataException() {
|
||||||
service.create(new Foo());
|
service.create(new Foo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = DataIntegrityViolationException.class)
|
||||||
|
public final void whenEntityWithLongNameIsCreated_thenDataException() {
|
||||||
|
service.create(new Foo(randomAlphabetic(2048)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = InvalidDataAccessApiUsageException.class)
|
||||||
|
public final void whenSameEntityIsCreatedTwice_thenDataException() {
|
||||||
|
final Foo entity = new Foo(randomAlphabetic(8));
|
||||||
|
service.create(entity);
|
||||||
|
service.create(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void temp_whenInvalidEntityIsCreated_thenDataException() {
|
||||||
|
service.create(new Foo(randomAlphabetic(2048)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue