Merge remote-tracking branch 'origin/master'

This commit is contained in:
slavisa-baeldung 2017-05-05 16:23:27 +01:00
commit 1fcdc1314e
16 changed files with 244 additions and 206 deletions

View File

@ -395,7 +395,6 @@
<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>
<testng.version>6.10</testng.version>
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version> <avaitility.version>1.7.0</avaitility.version>

View File

@ -0,0 +1,17 @@
package com.baeldung.java.reflection;
public class Operations {
public double sum(int a, double b) {
return a + b;
}
public static double multiply(float a, long b){
return a * b;
}
private boolean and(boolean a, boolean b) {
return a && b;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.java.reflection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class OperationsUnitTest {
public OperationsUnitTest() {
}
@Test(expected=IllegalAccessException.class)
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
Operations operationsInstance = new Operations();
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
assertFalse(result);
}
@Test
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
andInstanceMethod.setAccessible(true);
Operations operationsInstance = new Operations();
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
assertFalse(result);
}
@Test
public void givenObject_whenInvokePublicMethod_thenCorrect() throws Exception {
Method sumInstanceMethod = Operations.class.getMethod("sum", int.class, double.class);
Operations operationsInstance = new Operations();
Double result = (Double)sumInstanceMethod.invoke(operationsInstance, 1, 3);
assertThat(result, equalTo(4.0));
}
@Test
public void givenObject_whenInvokeStaticMethod_thenCorrect() throws Exception {
Method multiplyStaticMethod = Operations.class.getDeclaredMethod("multiply",float.class, long.class);
Double result = (Double)multiplyStaticMethod.invoke(null, 3.5f, 2);
assertThat(result, equalTo(7.0));
}
}

View File

@ -111,7 +111,7 @@
<module>selenium-junit-testng</module> <module>selenium-junit-testng</module>
<module>solr</module> <module>solr</module>
<module>spark-java</module> <module>spark-java</module>
<!-- <module>spring-5</module> TODO: CI problems--> <module>spring-5</module>
<module>spring-akka</module> <module>spring-akka</module>
<module>spring-amqp</module> <module>spring-amqp</module>
<module>spring-all</module> <module>spring-all</module>

3
spring-5/.gitignore vendored
View File

@ -1,6 +1,5 @@
*.class
#folders# #folders#
.idea
/target /target
/neoDb* /neoDb*
/data /data

View File

@ -18,33 +18,24 @@ public class FormHandler {
return request return request
.body(toFormData()) .body(toFormData())
.map(MultiValueMap::toSingleValueMap) .map(MultiValueMap::toSingleValueMap)
.map(formData -> { .filter(formData -> "baeldung".equals(formData.get("user")))
System.out.println("form data: " + formData.toString()); .filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) { .flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
return ok() .or(ServerResponse.badRequest().build());
.body(Mono.just("welcome back!"), String.class)
.block();
}
return ServerResponse
.badRequest()
.build()
.block();
});
} }
Mono<ServerResponse> handleUpload(ServerRequest request) { Mono<ServerResponse> handleUpload(ServerRequest request) {
return request return request
.body(toDataBuffers()) .body(toDataBuffers())
.collectList() .collectList()
.map(dataBuffers -> { .flatMap(dataBuffers -> {
AtomicLong atomicLong = new AtomicLong(0); AtomicLong atomicLong = new AtomicLong(0);
dataBuffers.forEach(d -> atomicLong.addAndGet(d dataBuffers.forEach(d -> atomicLong.addAndGet(d
.asByteBuffer() .asByteBuffer()
.array().length)); .array().length));
System.out.println("data length:" + atomicLong.get());
return ok() return ok()
.body(fromObject(atomicLong.toString())) .body(fromObject(atomicLong.toString()));
.block();
}); });
} }
} }

View File

@ -1,26 +1,15 @@
package com.baeldung.web; package com.baeldung.web;
import java.util.List; import com.baeldung.persistence.FooRepository;
import java.util.Optional;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.persistence.FooRepository; import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
@RestController("/foos") @RestController("/foos")
public class FooController { public class FooController {
@ -30,30 +19,29 @@ public class FooController {
// API - read // API - read
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") @GetMapping("/foos/{id}")
@ResponseBody @ResponseBody
@Validated @Validated
public Foo findById(@PathVariable @Min(0) final long id) { public Foo findById(@PathVariable @Min(0) final long id) {
return repo.findOne(id).orElse(null); return repo.findById(id).orElse(null);
} }
@RequestMapping(method = RequestMethod.GET) @GetMapping
@ResponseBody @ResponseBody
public List<Foo> findAll() { public List<Foo> findAll() {
return repo.findAll(); return repo.findAll();
} }
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) @GetMapping(params = { "page", "size" })
@ResponseBody @ResponseBody
@Validated @Validated
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) { public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
final Page<Foo> resultPage = repo.findAll(new PageRequest(page, size)); return repo.findAll(PageRequest.of(page, size)).getContent();
return resultPage.getContent();
} }
// API - write // API - write
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") @PutMapping("/foos/{id}")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ResponseBody @ResponseBody
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) { public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class IntegrationTestExample1 { public class Example1IntegrationTest {
@Test @Test
public void test1a() { public void test1a() {

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class IntegrationTestExample2 { public class Example2IntegrationTest {
@Test @Test
public void test1a() { public void test1a() {

View File

@ -5,18 +5,18 @@ import org.junit.experimental.ParallelComputer;
import org.junit.runner.Computer; import org.junit.runner.Computer;
import org.junit.runner.JUnitCore; import org.junit.runner.JUnitCore;
public class ParallelTestExample { public class ParallelIntegrationTest {
@Test @Test
public void runTests() { public void runTests() {
final Class<?>[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
JUnitCore.runClasses(new Computer(), classes); JUnitCore.runClasses(new Computer(), classes);
} }
@Test @Test
public void runTestsInParallel() { public void runTestsInParallel() {
final Class<?>[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
JUnitCore.runClasses(new ParallelComputer(true, true), classes); JUnitCore.runClasses(new ParallelComputer(true, true), classes);
} }

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class Spring5ApplicationTests { public class Spring5ApplicationIntegrationTest {
@Test @Test
public void contextLoads() { public void contextLoads() {

View File

@ -4,152 +4,139 @@ import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.core.io.ClassPathResource; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
//import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
// TODO The class does not compile, WebTestClient cannot be resolved. Missing dependency?
public class FunctionalWebApplicationIntegrationTest { public class FunctionalWebApplicationIntegrationTest {
// private static WebTestClient client; private static WebTestClient client;
// private static WebServer server; private static WebServer server;
//
// @BeforeClass @BeforeClass
// public static void setup() throws Exception { public static void setup() throws Exception {
// server = new FunctionalWebApplication().start(); server = new FunctionalWebApplication().start();
// client = WebTestClient client = WebTestClient
// .bindToServer() .bindToServer()
// .baseUrl("http://localhost:" + server.getPort()) .baseUrl("http://localhost:" + server.getPort())
// .build(); .build();
// } }
//
// @AfterClass @AfterClass
// public static void destroy() { public static void destroy() {
// server.stop(); server.stop();
// } }
//
// @Test @Test
// public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception { public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
// client client
// .get() .get()
// .uri("/test") .uri("/test")
// .exchange() .exchange()
// .expectStatus() .expectStatus()
// .isOk() .isOk()
// .expectBody(String.class) .expectBody(String.class)
// .value() .isEqualTo("helloworld");
// .isEqualTo("helloworld"); }
// }
// @Test
// @Test public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
// public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception { client
// client .get()
// .get() .uri("/")
// .uri("/") .exchange()
// .exchange() .expectStatus()
// .expectStatus() .isOk()
// .isOk() .expectBody(String.class)
// .expectBody(String.class) .isEqualTo("helloworld");
// .value() }
// .isEqualTo("helloworld");
// } /* @Test
// public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
// @Test MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
// public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception { formData.add("user", "baeldung");
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1); formData.add("token", "you_know_what_to_do");
// formData.add("user", "baeldung");
// formData.add("token", "you_know_what_to_do"); client
// .post()
// client .uri("/login")
// .post() .contentType(MediaType.APPLICATION_FORM_URLENCODED)
// .uri("/login") .exchange(BodyInserters.fromFormData(formData))
// .contentType(MediaType.APPLICATION_FORM_URLENCODED) .expectStatus()
// .exchange(BodyInserters.fromFormData(formData)) .isOk()
// .expectStatus() .expectBody(String.class)
// .isOk() .value()
// .expectBody(String.class) .isEqualTo("welcome back!");
// .value() }*/
// .isEqualTo("welcome back!");
// } /* @Test
// public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
// @Test MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
// public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception { formData.add("user", "baeldung");
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2); formData.add("token", "try_again");
// formData.add("user", "baeldung");
// formData.add("token", "try_again"); client
// .post()
// client .uri("/login")
// .post() .contentType(MediaType.APPLICATION_FORM_URLENCODED)
// .uri("/login") .exchange(BodyInserters.fromFormData(formData))
// .contentType(MediaType.APPLICATION_FORM_URLENCODED) .expectStatus()
// .exchange(BodyInserters.fromFormData(formData)) .isBadRequest();
// .expectStatus() }
// .isBadRequest(); */
// } /* @Test
// public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
// @Test Resource resource = new ClassPathResource("/baeldung-weekly.png");
// public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception { client
// Resource resource = new ClassPathResource("/baeldung-weekly.png"); .post()
// client .uri("/upload")
// .post() .contentType(MediaType.MULTIPART_FORM_DATA)
// .uri("/upload") .exchange(fromResource(resource))
// .contentType(MediaType.MULTIPART_FORM_DATA) .expectStatus()
// .exchange(fromResource(resource)) .isOk()
// .expectStatus() .expectBody(String.class)
// .isOk() .value()
// .expectBody(String.class) .isEqualTo(String.valueOf(resource.contentLength()));
// .value() }
// .isEqualTo(String.valueOf(resource.contentLength())); */
// } /* @Test
// public void givenActors_whenAddActor_thenAdded() throws Exception {
// @Test client
// public void givenActors_whenAddActor_thenAdded() throws Exception { .get()
// client .uri("/actor")
// .get() .exchange()
// .uri("/actor") .expectStatus()
// .exchange() .isOk()
// .expectStatus() .expectBody(Actor.class)
// .isOk() .list()
// .expectBody(Actor.class) .hasSize(2);
// .list()
// .hasSize(2); client
// .post()
// client .uri("/actor")
// .post() .exchange(fromObject(new Actor("Clint", "Eastwood")))
// .uri("/actor") .expectStatus()
// .exchange(fromObject(new Actor("Clint", "Eastwood"))) .isOk();
// .expectStatus()
// .isOk(); client
// .get()
// client .uri("/actor")
// .get() .exchange()
// .uri("/actor") .expectStatus()
// .exchange() .isOk()
// .expectStatus() .expectBody(Actor.class)
// .isOk() .list()
// .expectBody(Actor.class) .hasSize(3);
// .list() }*/
// .hasSize(3);
// } @Test
// public void givenResources_whenAccess_thenGot() throws Exception {
// @Test client
// public void givenResources_whenAccess_thenGot() throws Exception { .get()
// client .uri("/files/hello.txt")
// .get() .exchange()
// .uri("/files/hello.txt") .expectStatus()
// .exchange() .isOk()
// .expectStatus() .expectBody(String.class)
// .isOk() .isEqualTo("hello");
// .expectBody(String.class) }
// .value()
// .isEqualTo("hello");
// }
} }

View File

@ -1,7 +1,7 @@
package com.baeldung.jupiter; package com.baeldung.jupiter;
import com.baeldung.IntegrationTestExample1; import com.baeldung.Example1IntegrationTest;
import com.baeldung.IntegrationTestExample2; import com.baeldung.Example2IntegrationTest;
import org.junit.experimental.ParallelComputer; import org.junit.experimental.ParallelComputer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.Computer; import org.junit.runner.Computer;
@ -12,7 +12,7 @@ public class Spring5JUnit5ParallelTest {
@Test @Test
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() { public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() {
final Class<?>[] classes = { final Class<?>[] classes = {
IntegrationTestExample1.class, IntegrationTestExample2.class Example1IntegrationTest.class, Example2IntegrationTest.class
}; };
JUnitCore.runClasses(new ParallelComputer(true, true), classes); JUnitCore.runClasses(new ParallelComputer(true, true), classes);
@ -21,7 +21,7 @@ public class Spring5JUnit5ParallelTest {
@Test @Test
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() { public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() {
final Class<?>[] classes = { final Class<?>[] classes = {
IntegrationTestExample1.class, IntegrationTestExample2.class Example1IntegrationTest.class, Example2IntegrationTest.class
}; };
JUnitCore.runClasses(new Computer(), classes); JUnitCore.runClasses(new Computer(), classes);

View File

@ -6,12 +6,12 @@ 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.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
public class SpringBootJPAIntegrationTest { public class SpringBootJPAIntegrationTest {
@Autowired @Autowired

View File

@ -1,8 +1,5 @@
package org.baeldung; package org.baeldung;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.baeldung.config.H2TestProfileJPAConfig; import org.baeldung.config.H2TestProfileJPAConfig;
import org.baeldung.domain.GenericEntity; import org.baeldung.domain.GenericEntity;
import org.baeldung.repository.GenericEntityRepository; import org.baeldung.repository.GenericEntityRepository;
@ -11,9 +8,12 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringJUnit4ClassRunner.class) import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class, H2TestProfileJPAConfig.class }) @SpringBootTest(classes = { Application.class, H2TestProfileJPAConfig.class })
@ActiveProfiles("test") @ActiveProfiles("test")
public class SpringBootProfileIntegrationTest { public class SpringBootProfileIntegrationTest {

View File

@ -5,9 +5,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
@TestPropertySource("classpath:exception.properties") @TestPropertySource("classpath:exception.properties")
public class ApplicationIntegrationTest { public class ApplicationIntegrationTest {