Merge remote-tracking branch 'origin/master'

This commit is contained in:
slavisa-baeldung 2016-10-11 17:47:54 +02:00
commit 148928ead1
32 changed files with 993 additions and 296 deletions

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-jaxrs-implementation</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>apache-cxf</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>3.1.7</cxf.version>
<httpclient.version>4.5.2</httpclient.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.cxf.jaxrs.implementation.RestfulServer</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/ServiceTest</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,68 @@
package com.baeldung.cxf.jaxrs.implementation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("baeldung")
@Produces("text/xml")
public class Baeldung {
private Map<Integer, Course> courses = new HashMap<>();
{
Student student1 = new Student();
Student student2 = new Student();
student1.setId(1);
student1.setName("Student A");
student2.setId(2);
student2.setName("Student B");
List<Student> course1Students = new ArrayList<>();
course1Students.add(student1);
course1Students.add(student2);
Course course1 = new Course();
Course course2 = new Course();
course1.setId(1);
course1.setName("REST with Spring");
course1.setStudents(course1Students);
course2.setId(2);
course2.setName("Learn Spring Security");
courses.put(1, course1);
courses.put(2, course2);
}
@GET
@Path("courses/{courseOrder}")
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
return courses.get(courseOrder);
}
@PUT
@Path("courses/{courseOrder}")
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
Course existingCourse = courses.get(courseOrder);
Response response;
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
courses.put(courseOrder, course);
response = Response.ok().build();
} else {
response = Response.notModified().build();
}
return response;
}
@Path("courses/{courseOrder}/students")
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
return courses.get(courseOrder);
}
}

View File

@ -0,0 +1,73 @@
package com.baeldung.cxf.jaxrs.implementation;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Course")
public class Course {
private int id;
private String name;
private List<Student> students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@GET
@Path("{studentOrder}")
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
return students.get(studentOrder);
}
@POST
public Response postStudent(Student student) {
if (students == null) {
students = new ArrayList<Student>();
}
students.add(student);
return Response.ok(student).build();
}
@DELETE
@Path("{studentOrder}")
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
Student student = students.get(studentOrder);
Response response;
if (student != null) {
students.remove(studentOrder);
response = Response.ok().build();
} else {
response = Response.notModified().build();
}
return response;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.cxf.jaxrs.implementation;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
public class RestfulServer {
public static void main(String args[]) throws Exception {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setResourceClasses(Baeldung.class);
factoryBean.setResourceProvider(new SingletonResourceProvider(new Baeldung()));
factoryBean.setAddress("http://localhost:8080/");
Server server = factoryBean.create();
System.out.println("Server ready...");
Thread.sleep(60 * 1000);
System.out.println("Server exiting");
server.destroy();
System.exit(0);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Student")
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,4 @@
<Course>
<id>3</id>
<name>Apache CXF Support for RESTful</name>
</Course>

View File

@ -0,0 +1,5 @@
<Student>
<id>3</id>
<name>Student C</name>
</Student>

View File

@ -0,0 +1,93 @@
package com.baeldung.cxf.jaxrs.implementation;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.bind.JAXB;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class ServiceTest {
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
private static CloseableHttpClient client;
@BeforeClass
public static void createClient() {
client = HttpClients.createDefault();
}
@AfterClass
public static void closeClient() throws IOException {
client.close();
}
@Test
public void whenPutCourse_thenCorrect() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "3");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPut);
assertEquals(200, response.getStatusLine().getStatusCode());
Course course = getCourse(3);
assertEquals(3, course.getId());
assertEquals("Apache CXF Support for RESTful", course.getName());
}
@Test
public void whenPostStudent_thenCorrect() throws IOException {
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
httpPost.setEntity(new InputStreamEntity(resourceStream));
httpPost.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPost);
assertEquals(200, response.getStatusLine().getStatusCode());
Student student = getStudent(2, 0);
assertEquals(3, student.getId());
assertEquals("Student C", student.getName());
}
@Test
public void whenDeleteStudent_thenCorrect() throws IOException {
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
HttpResponse response = client.execute(httpDelete);
assertEquals(200, response.getStatusLine().getStatusCode());
Course course = getCourse(1);
assertEquals(1, course.getStudents().size());
assertEquals(2, course.getStudents().get(0).getId());
assertEquals("Student B", course.getStudents().get(0).getName());
}
private Course getCourse(int courseOrder) throws IOException {
URL url = new URL(BASE_URL + courseOrder);
InputStream input = url.openStream();
Course course = JAXB.unmarshal(new InputStreamReader(input), Course.class);
return course;
}
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
InputStream input = url.openStream();
Student student = JAXB.unmarshal(new InputStreamReader(input), Student.class);
return student;
}
}

View File

@ -8,6 +8,7 @@
<modules> <modules>
<module>cxf-introduction</module> <module>cxf-introduction</module>
<module>cxf-spring</module> <module>cxf-spring</module>
<module>cxf-jaxrs-implementation</module>
</modules> </modules>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -114,6 +114,7 @@ public class FileOperationsTest {
resultStringBuilder.append(line).append("\n"); resultStringBuilder.append(line).append("\n");
} }
} }
return resultStringBuilder.toString(); return resultStringBuilder.toString();
} }
} }

View File

@ -1,47 +1,41 @@
package com.baeldung.util; package com.baeldung.util;
import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.junit.Test; import org.junit.Test;
import java.time.*;
import java.time.temporal.ChronoField;
import static org.junit.Assert.assertEquals;
public class CurrentDateTimeTest { public class CurrentDateTimeTest {
private static final Clock clock = Clock.fixed(Instant.parse("2016-10-09T15:10:30.00Z"), ZoneId.of("UTC"));
@Test @Test
public void shouldReturnCurrentDate() { public void shouldReturnCurrentDate() {
final LocalDate now = LocalDate.now(); final LocalDate now = LocalDate.now(clock);
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals("10-10-2010".length(), now.toString().length()); assertEquals(9, now.get(ChronoField.DAY_OF_MONTH));
assertEquals(calendar.get(Calendar.DATE), now.get(ChronoField.DAY_OF_MONTH)); assertEquals(10, now.get(ChronoField.MONTH_OF_YEAR));
assertEquals(calendar.get(Calendar.MONTH), now.get(ChronoField.MONTH_OF_YEAR) - 1); assertEquals(2016, now.get(ChronoField.YEAR));
assertEquals(calendar.get(Calendar.YEAR), now.get(ChronoField.YEAR));
} }
@Test @Test
public void shouldReturnCurrentTime() { public void shouldReturnCurrentTime() {
final LocalTime now = LocalTime.now(); final LocalTime now = LocalTime.now(clock);
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals(calendar.get(Calendar.HOUR_OF_DAY), now.get(ChronoField.HOUR_OF_DAY)); assertEquals(15, now.get(ChronoField.HOUR_OF_DAY));
assertEquals(calendar.get(Calendar.MINUTE), now.get(ChronoField.MINUTE_OF_HOUR)); assertEquals(10, now.get(ChronoField.MINUTE_OF_HOUR));
assertEquals(calendar.get(Calendar.SECOND), now.get(ChronoField.SECOND_OF_MINUTE)); assertEquals(30, now.get(ChronoField.SECOND_OF_MINUTE));
} }
@Test @Test
public void shouldReturnCurrentTimestamp() { public void shouldReturnCurrentTimestamp() {
final Instant now = Instant.now(); final Instant now = Instant.now(clock);
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals(calendar.getTimeInMillis() / 1000, now.getEpochSecond()); assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond());
} }
} }

View File

@ -0,0 +1,46 @@
package com.baeldung.java.nio.selector;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class EchoClient {
private static SocketChannel client = null;
private static ByteBuffer buffer;
private static EchoClient instance = null;
public static EchoClient start() {
if (instance == null)
instance = new EchoClient();
return instance;
}
private EchoClient() {
try {
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
buffer = ByteBuffer.allocate(256);
} catch (IOException e) {
e.printStackTrace();
}
}
public String sendMessage(String msg) {
buffer = ByteBuffer.wrap(msg.getBytes());
String response = null;
try {
client.write(buffer);
buffer.clear();
client.read(buffer);
response = new String(buffer.array()).trim();
System.out.println("response=" + response);
buffer.clear();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.java.nio.selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.util.Set;
import java.util.Iterator;
import java.net.InetSocketAddress;
public class EchoServer {
public static void main(String[] args)
throws IOException {
Selector selector = Selector.open();
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("localhost", 5454));
serverSocket.configureBlocking(false);
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
ByteBuffer buffer = ByteBuffer.allocate(256);
while (true) {
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iter = selectedKeys.iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
if (key.isAcceptable()) {
SocketChannel client = serverSocket.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
SocketChannel client = (SocketChannel) key.channel();
client.read(buffer);
buffer.flip();
client.write(buffer);
buffer.clear();
}
iter.remove();
}
}
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.java.nio.selector;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EchoTest {
@Test
public void givenClient_whenServerEchosMessage_thenCorrect() {
EchoClient client = EchoClient.start();
String resp1 = client.sendMessage("hello");
String resp2 = client.sendMessage("world");
assertEquals("hello", resp1);
assertEquals("world", resp2);
}
}

View File

@ -1,295 +1,301 @@
<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"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.baeldung</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-all</artifactId> <groupId>com.baeldung</groupId>
<version>0.1-SNAPSHOT</version> <artifactId>spring-all</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-all</name> <name>spring-all</name>
<packaging>war</packaging> <packaging>war</packaging>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version> <version>1.3.6.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId> <artifactId>spring-orm</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
</dependency> </dependency>
<!-- aspectj --> <!-- aspectj -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId> <artifactId>spring-aspects</artifactId>
</dependency> </dependency>
<!-- persistence --> <!-- persistence -->
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> <artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version> <version>${hibernate.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.javassist</groupId> <groupId>org.javassist</groupId>
<artifactId>javassist</artifactId> <artifactId>javassist</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hsqldb</groupId> <groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId> <artifactId>hsqldb</artifactId>
</dependency> </dependency>
<!-- validation --> <!-- validation -->
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId> <artifactId>hibernate-validator</artifactId>
</dependency> </dependency>
<!-- web --> <!-- web -->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId> <artifactId>jstl</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- util --> <!-- util -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<!-- logging --> <!-- logging -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<!-- <scope>runtime</scope> --> <!-- <scope>runtime</scope> -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId> <artifactId>jcl-over-slf4j</artifactId>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl --> <!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</dependency> </dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly --> <dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>3.5.1</version> <version>3.5.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>hamcrest-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.easymock</groupId> <groupId>org.easymock</groupId>
<artifactId>easymock</artifactId> <artifactId>easymock</artifactId>
<version>3.4</version> <version>3.4</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId> <artifactId>spring-framework-bom</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build> <build>
<finalName>spring-all</finalName> <finalName>spring-all</finalName>
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
<configuration> <configuration>
<failOnMissingWebXml>false</failOnMissingWebXml> <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration> <configuration>
<excludes> <excludes>
<!-- <exclude>**/*ProductionTest.java</exclude> --> <!-- <exclude>**/*ProductionTest.java</exclude> -->
</excludes> </excludes>
<systemPropertyVariables> <systemPropertyVariables>
<!-- <provPersistenceTarget>h2</provPersistenceTarget> --> <!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.cargo</groupId> <groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId> <artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version> <version>${cargo-maven2-plugin.version}</version>
<configuration> <configuration>
<wait>true</wait> <wait>true</wait>
<container> <container>
<containerId>jetty8x</containerId> <containerId>jetty8x</containerId>
<type>embedded</type> <type>embedded</type>
<systemProperties> <systemProperties>
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> --> <!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
</systemProperties> </systemProperties>
</container> </container>
<configuration> <configuration>
<properties> <properties>
<cargo.servlet.port>8082</cargo.servlet.port> <cargo.servlet.port>8082</cargo.servlet.port>
</properties> </properties>
</configuration> </configuration>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.3.1.RELEASE</org.springframework.version> <org.springframework.version>4.3.1.RELEASE</org.springframework.version>
<org.springframework.security.version>4.0.4.RELEASE</org.springframework.security.version> <org.springframework.security.version>4.0.4.RELEASE</org.springframework.security.version>
<javassist.version>3.20.0-GA</javassist.version> <javassist.version>3.20.0-GA</javassist.version>
<jstl.version>1.2</jstl.version> <jstl.version>1.2</jstl.version>
<!-- persistence --> <!-- persistence -->
<hibernate.version>4.3.11.Final</hibernate.version> <hibernate.version>4.3.11.Final</hibernate.version>
<mysql-connector-java.version>5.1.38</mysql-connector-java.version> <mysql-connector-java.version>5.1.38</mysql-connector-java.version>
<!-- logging --> <!-- logging -->
<org.slf4j.version>1.7.13</org.slf4j.version> <org.slf4j.version>1.7.13</org.slf4j.version>
<logback.version>1.1.3</logback.version> <logback.version>1.1.3</logback.version>
<!-- various --> <!-- various -->
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version> <hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
<!-- util --> <!-- util -->
<guava.version>19.0</guava.version> <guava.version>19.0</guava.version>
<commons-lang3.version>3.4</commons-lang3.version> <commons-lang3.version>3.4</commons-lang3.version>
<!-- testing --> <!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version> <org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version> <mockito.version>1.10.19</mockito.version>
<httpcore.version>4.4.1</httpcore.version> <httpcore.version>4.4.1</httpcore.version>
<httpclient.version>4.5</httpclient.version> <httpclient.version>4.5</httpclient.version>
<rest-assured.version>2.9.0</rest-assured.version> <rest-assured.version>2.9.0</rest-assured.version>
<!-- maven plugins --> <!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version> <maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version> <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,24 @@
package org.baeldung.ehcache.app;
import org.baeldung.ehcache.calculator.SquaredCalculator;
import org.baeldung.ehcache.config.CacheHelper;
public class App {
public static void main(String[] args) {
SquaredCalculator squaredCalculator = new SquaredCalculator();
CacheHelper cacheHelper = new CacheHelper();
squaredCalculator.setCache(cacheHelper);
calculate(squaredCalculator);
calculate(squaredCalculator);
}
private static void calculate(SquaredCalculator squaredCalculator) {
for (int i = 10; i < 15; i++) {
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
}
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.ehcache.calculator;
import org.baeldung.ehcache.config.CacheHelper;
public class SquaredCalculator {
private CacheHelper cache;
public int getSquareValueOfNumber(int input) {
if (cache.getSquareNumberCache().containsKey(input)) {
return cache.getSquareNumberCache().get(input);
}
System.out.println("Calculating square value of " + input + " and caching result.");
int squaredValue = (int) Math.pow(input, 2);
cache.getSquareNumberCache().put(input, squaredValue);
return squaredValue;
}
public void setCache(CacheHelper cache) {
this.cache = cache;
}
}

View File

@ -0,0 +1,25 @@
package org.baeldung.ehcache.config;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
public class CacheHelper {
private CacheManager cacheManager;
private Cache<Integer, Integer> squareNumberCache;
public CacheHelper() {
cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("squaredNumber", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, Integer.class, ResourcePoolsBuilder.heap(10))).build();
cacheManager.init();
squareNumberCache = cacheManager.getCache("squaredNumber", Integer.class, Integer.class);
}
public Cache<Integer, Integer> getSquareNumberCache() {
return squareNumberCache;
}
}

View File

@ -88,12 +88,12 @@
<dependency> <dependency>
<groupId>org.webjars</groupId> <groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId> <artifactId>bootstrap</artifactId>
<version>3.3.4</version> <version>3.3.7-1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.webjars</groupId> <groupId>org.webjars</groupId>
<artifactId>jquery</artifactId> <artifactId>jquery</artifactId>
<version>2.1.4</version> <version>3.1.1</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,19 +1,19 @@
<html> <html>
<head> <head>
<title>WebJars Demo</title> <title>WebJars Demo</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.4/css/bootstrap.min.css" /> <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
</head> </head>
<body> <body>
<div class="container"><br/> <div class="container"><br/>
<div class="alert alert-success"> <div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> It is working as we expected. <strong>Success!</strong> It is working as we expected.
</div> </div>
</div> </div>
<script src="/webjars/jquery/2.1.4/jquery.min.js"></script> <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.4/js/bootstrap.min.js"></script> <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
</body> </body>
</html> </html>

View File

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Embedded ActiveMQ Broker --> <!-- Embedded ActiveMQ Broker -->
<amq:broker id="broker" useJmx="false" persistent="false"> <amq:broker id="broker" useJmx="false" persistent="false">

View File

@ -285,6 +285,61 @@
</build> </build>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>
</includes>
<systemPropertyVariables>
<webTarget>cargo</webTarget>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.2.5.RELEASE</org.springframework.version> <org.springframework.version>4.2.5.RELEASE</org.springframework.version>

View File

@ -45,7 +45,7 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); credentialsProvider.setCredentials(new AuthScope("localhost", 8082, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build(); final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build();
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client); final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);

View File

@ -33,7 +33,7 @@ public class ClientLiveTest {
@Test @Test
public final void whenSecuredRestApiIsConsumed_then200OK() { public final void whenSecuredRestApiIsConsumed_then200OK() {
final ResponseEntity<Foo> responseEntity = secureRestTemplate.exchange("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1", HttpMethod.GET, null, Foo.class); final ResponseEntity<Foo> responseEntity = secureRestTemplate.exchange("http://localhost:8082/spring-security-rest-basic-auth/api/foos/1", HttpMethod.GET, null, Foo.class);
assertThat(responseEntity.getStatusCode().value(), is(200)); assertThat(responseEntity.getStatusCode().value(), is(200));
} }

View File

@ -30,7 +30,7 @@ import org.springframework.web.client.RestTemplate;
* */ * */
public class RestClientLiveManualTest { public class RestClientLiveManualTest {
final String urlOverHttps = "http://localhost:8080/spring-security-rest-basic-auth/api/bars/1"; final String urlOverHttps = "http://localhost:8082/spring-security-rest-basic-auth/api/bars/1";
// tests // tests

View File

@ -278,6 +278,61 @@
</build> </build>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>
</includes>
<systemPropertyVariables>
<webTarget>cargo</webTarget>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.2.5.RELEASE</org.springframework.version> <org.springframework.version>4.2.5.RELEASE</org.springframework.version>

View File

@ -22,16 +22,16 @@ public class ClientNoSpringLiveTest {
@Test @Test
public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() { public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
final HttpHost host = new HttpHost("localhost", 8080, "http"); final HttpHost host = new HttpHost("localhost", 8082, "http");
final CredentialsProvider credentialsProvider = provider(); final CredentialsProvider credentialsProvider = provider();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build(); final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client); final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
final RestTemplate restTemplate = new RestTemplate(requestFactory); final RestTemplate restTemplate = new RestTemplate(requestFactory);
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); // credentialsProvider.setCredentials(new AuthScope("localhost", 8082, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode()); System.out.println(responseEntity.getStatusCode());
@ -46,7 +46,7 @@ public class ClientNoSpringLiveTest {
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); // credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode()); System.out.println(responseEntity.getStatusCode());

View File

@ -23,7 +23,7 @@ public class ClientWithSpringLiveTest {
@Test @Test
public final void whenSecuredRestApiIsConsumed_then200OK() { public final void whenSecuredRestApiIsConsumed_then200OK() {
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; final String uri = "http://localhost:8082/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode()); System.out.println(responseEntity.getStatusCode());

View File

@ -29,7 +29,7 @@ public class RawClientLiveTest {
final int timeout = 20; // seconds final int timeout = 20; // seconds
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build(); final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
final HttpGet getMethod = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/bars/1"); final HttpGet getMethod = new HttpGet("http://localhost:8082/spring-security-rest-basic-auth/api/bars/1");
getMethod.setConfig(requestConfig); getMethod.setConfig(requestConfig);
final int hardTimeout = 5; // seconds final int hardTimeout = 5; // seconds

View File

@ -257,7 +257,7 @@
<version>${maven-surefire-plugin.version}</version> <version>${maven-surefire-plugin.version}</version>
<configuration> <configuration>
<excludes> <excludes>
<!-- <exclude>**/*ProductionTest.java</exclude> --> <exclude>**/*LiveTest.java</exclude>
</excludes> </excludes>
<systemPropertyVariables> <systemPropertyVariables>
<!-- <provPersistenceTarget>h2</provPersistenceTarget> --> <!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
@ -289,6 +289,62 @@
</build> </build>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>
</includes>
<systemPropertyVariables>
<webTarget>cargo</webTarget>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.2.5.RELEASE</org.springframework.version> <org.springframework.version>4.2.5.RELEASE</org.springframework.version>

View File

@ -17,14 +17,16 @@ import com.jayway.restassured.specification.RequestSpecification;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooLiveTest { public class FooLiveTest {
private static final String URL_PREFIX = "http://localhost:8080/spring-security-rest"; private static final String URL_PREFIX = "http://localhost:8082/spring-security-rest";
// private FormAuthConfig formConfig = new FormAuthConfig(URL_PREFIX + "/login", "temporary", "temporary"); // private FormAuthConfig formConfig = new FormAuthConfig(URL_PREFIX + "/login", "temporary", "temporary");
private String cookie; private String cookie;
private RequestSpecification givenAuth() { private RequestSpecification givenAuth() {
// return RestAssured.given().auth().form("user", "userPass", formConfig); // return RestAssured.given().auth().form("user", "userPass", formConfig);
if (cookie == null) if (cookie == null) {
cookie = RestAssured.given().contentType("application/x-www-form-urlencoded").formParam("password", "userPass").formParam("username", "user").post(URL_PREFIX + "/login").getCookie("JSESSIONID"); cookie = RestAssured.given().contentType("application/x-www-form-urlencoded").formParam("password", "userPass").formParam("username", "user").post(URL_PREFIX + "/login").getCookie("JSESSIONID");
}
return RestAssured.given().cookie("JSESSIONID", cookie); return RestAssured.given().cookie("JSESSIONID", cookie);
} }

View File

@ -8,7 +8,7 @@ import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response; import com.jayway.restassured.response.Response;
public class SwaggerLiveTest { public class SwaggerLiveTest {
private static final String URL_PREFIX = "http://localhost:8080/spring-security-rest/api"; private static final String URL_PREFIX = "http://localhost:8082/spring-security-rest/api";
@Test @Test
public void whenVerifySpringFoxIsWorking_thenOK() { public void whenVerifySpringFoxIsWorking_thenOK() {