Merge branch 'master' into master

This commit is contained in:
Ahmed Tawila 2017-08-11 21:47:26 +02:00 committed by GitHub
commit e83b5c9b79
46 changed files with 1246 additions and 82 deletions

View File

@ -1,4 +1,4 @@
resource.loader=webapp resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path = . webapp.resource.loader.path =
webapp.resource.loader.cache = true webapp.resource.loader.cache = true

View File

@ -6,7 +6,7 @@ public class ServiceMain {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
ProcessHandle thisProcess = ProcessHandle.current(); ProcessHandle thisProcess = ProcessHandle.current();
long pid = thisProcess.getPid(); long pid = thisProcess.pid();
Optional<String[]> opArgs = Optional.ofNullable(args); Optional<String[]> opArgs = Optional.ofNullable(args);
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command")); String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));

View File

@ -0,0 +1,76 @@
package com.baeldung.java9.language;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
public class Java9ObjectsAPIUnitTest {
@Test
public void givenNullObject_whenRequireNonNullElse_thenElse(){
assertThat(Objects.<List>requireNonNullElse(null, Collections.EMPTY_LIST),
is(Collections.EMPTY_LIST));
}
@Test
public void givenObject_whenRequireNonNullElse_thenObject(){
assertThat(Objects.<List>requireNonNullElse(List.of("item1", "item2"),
Collections.EMPTY_LIST), is(List.of("item1", "item2")));
}
@Test(expected = NullPointerException.class)
public void givenNull_whenRequireNonNullElse_thenException(){
Objects.<List>requireNonNullElse(null, null);
}
@Test
public void givenObject_whenRequireNonNullElseGet_thenObject(){
assertThat(Objects.<List>requireNonNullElseGet(null, List::of),
is(List.of()));
}
@Test
public void givenNumber_whenInvokeCheckIndex_thenNumber(){
int length = 5;
assertThat(Objects.checkIndex(4, length), is(4));
}
@Test(expected = IndexOutOfBoundsException.class)
public void givenOutOfRangeNumber_whenInvokeCheckIndex_thenException(){
int length = 5;
Objects.checkIndex(5, length);
}
@Test
public void givenSubRange_whenCheckFromToIndex_thenNumber(){
int length = 6;
assertThat(Objects.checkFromToIndex(2,length,length), is(2));
}
@Test(expected = IndexOutOfBoundsException.class)
public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
int length = 6;
Objects.checkFromToIndex(2,7,length);
}
@Test
public void givenSubRange_whenCheckFromIndexSize_thenNumber(){
int length = 6;
assertThat(Objects.checkFromIndexSize(2,3,length), is(2));
}
@Test(expected = IndexOutOfBoundsException.class)
public void givenInvalidSubRange_whenCheckFromIndexSize_thenException(){
int length = 6;
Objects.checkFromIndexSize(2, 6, length);
}
}

View File

@ -3,7 +3,7 @@
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>com.baeldung</groupId> <!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId>-->
<artifactId>junit5</artifactId> <artifactId>junit5</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
@ -19,9 +19,9 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M5</junit.jupiter.version> <junit.jupiter.version>5.0.0-RC2</junit.jupiter.version>
<junit.platform.version>1.0.0-M5</junit.platform.version> <junit.platform.version>1.0.0-RC2</junit.platform.version>
<junit.vintage.version>4.12.0-M5</junit.vintage.version> <junit.vintage.version>4.12.0-RC2</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version> <log4j2.version>2.8.2</log4j2.version>
<h2.version>1.4.196</h2.version> <h2.version>1.4.196</h2.version>

View File

@ -0,0 +1,50 @@
package com.baeldung.param;
import java.util.Random;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class InvalidPersonParameterResolver implements ParameterResolver {
/**
* The "bad" (invalid) data for testing purposes has to go somewhere, right?
*/
public static Person[] INVALID_PERSONS = {
new Person().setId(1L).setLastName("Ad_ams").setFirstName("Jill,"),
new Person().setId(2L).setLastName(",Baker").setFirstName(""),
new Person().setId(3L).setLastName(null).setFirstName(null),
new Person().setId(4L).setLastName("Daniel&").setFirstName("{Joseph}"),
new Person().setId(5L).setLastName("").setFirstName("English, Jane"),
new Person()/* .setId(6L).setLastName("Fontana").setFirstName("Enrique") */,
// TODO: ADD MORE DATA HERE
};
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Object ret = null;
//
// Return a random, valid Person object if Person.class is the type of Parameter
/// to be resolved. Otherwise return null.
if (parameterContext.getParameter().getType() == Person.class) {
ret = INVALID_PERSONS[new Random().nextInt(INVALID_PERSONS.length)];
}
return ret;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
boolean ret = false;
//
// If the Parameter.type == Person.class, then we support it, otherwise, get outta here!
if (parameterContext.getParameter().getType() == Person.class) {
ret = true;
}
return ret;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.param;
/**
* Very simple Person entity.
* Use the Fluent-style interface to set properties.
*
* @author J Steven Perry
*
*/
public class Person {
private Long id;
private String lastName;
private String firstName;
public Long getId() {
return id;
}
public Person setId(Long id) {
this.id = id;
return this;
}
public String getLastName() {
return lastName;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
public String getFirstName() {
return firstName;
}
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
}

View File

@ -0,0 +1,142 @@
package com.baeldung.param;
import java.util.Arrays;
/**
* Somewhat contrived validation class to illustrate unit test
* concepts.
*
* @author J Steven Perry
*
*/
public class PersonValidator {
/**
* Contrived checked exception to illustrate one possible
* way to handle validation errors (via a checked exception).
*
* @author J Steven Perry
*
*/
public static class ValidationException extends Exception {
/**
*
*/
private static final long serialVersionUID = -134518049431883102L;
// Probably should implement some more constructors, but don't want
/// to tarnish the lesson...
/**
* The one and only way to create this checked exception.
*
* @param message
* The message accompanying the exception. Should be meaningful.
*/
public ValidationException(String message) {
super(message);
}
}
private static final String[] ILLEGAL_NAME_CHARACTERS = {
",",
"_",
"{",
"}",
"!"
};
/**
* Validate the first name of the specified Person object.
*
* @param person
* The Person object to validate.
*
* @return - returns true if the specified Person is valid
*
* @throws ValidationException
* - this Exception is thrown if any kind of validation error occurs.
*/
public static boolean validateFirstName(Person person) throws ValidationException {
boolean ret = true;
// The validation rules go here.
// Naive: use simple ifs
if (person == null) {
throw new ValidationException("Person is null (not allowed)!");
}
if (person.getFirstName() == null) {
throw new ValidationException("Person FirstName is null (not allowed)!");
}
if (person.getFirstName().isEmpty()) {
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
}
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
throw new ValidationException(
"Person FirstName (" + person.getFirstName() + ") may not contain any of the following characters: "
+ Arrays.toString(ILLEGAL_NAME_CHARACTERS)
+ "!");
}
return ret;
}
/**
* Validate the last name of the specified Person object. Looks the same as first
* name? Look closer. Just kidding. It's the same. But real world code can (and will) diverge.
*
* @param person
* The Person object to validate.
*
* @return - returns true if the specified Person is valid
*
* @throws ValidationException
* - this Exception is thrown if any kind of validation error occurs.
*/
public static boolean validateLastName(Person person) throws ValidationException {
boolean ret = true;
// The validation rules go here.
// Naive: use simple ifs
if (person == null) {
throw new ValidationException("Person is null (not allowed)!");
}
if (person.getFirstName() == null) {
throw new ValidationException("Person FirstName is null (not allowed)!");
}
if (person.getFirstName().isEmpty()) {
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
}
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
throw new ValidationException(
"Person LastName (" + person.getLastName() + ") may not contain any of the following characters: "
+ Arrays.toString(ILLEGAL_NAME_CHARACTERS)
+ "!");
}
return ret;
}
/**
* Validates the specified name. If it contains any of the illegalCharacters,
* this method returns false (indicating the name is illegal). Otherwise it returns true.
*
* @param candidate
* The candidate String to validate
*
* @param illegalCharacters
* The characters the String is not allowed to have
*
* @return - boolean - true if the name is valid, false otherwise.
*/
private static boolean isStringValid(String candidate, String[] illegalCharacters) {
boolean ret = true;
for (String illegalChar : illegalCharacters) {
if (candidate.contains(illegalChar)) {
ret = false;
break;
}
}
return ret;
}
}

View File

@ -0,0 +1,102 @@
package com.baeldung.param;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@DisplayName("Testing PersonValidator")
public class PersonValidatorTest {
/**
* Nested class, uses ExtendWith
* {@link com.baeldung.param.ValidPersonParameterResolver ValidPersonParameterResolver}
* to feed Test methods with "good" data.
*/
@Nested
@DisplayName("When using Valid data")
@ExtendWith(ValidPersonParameterResolver.class)
public class ValidData {
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* A valid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are valid")
public void validateFirstName(Person person) {
try {
assertTrue(PersonValidator.validateFirstName(person));
} catch (PersonValidator.ValidationException e) {
fail("Exception not expected: " + e.getLocalizedMessage());
}
}
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* A valid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All last names are valid")
public void validateLastName(Person person) {
try {
assertTrue(PersonValidator.validateLastName(person));
} catch (PersonValidator.ValidationException e) {
fail("Exception not expected: " + e.getLocalizedMessage());
}
}
}
/**
* Nested class, uses ExtendWith
* {@link com.baeldung.param.InvalidPersonParameterResolver InvalidPersonParameterResolver}
* to feed Test methods with "bad" data.
*/
@Nested
@DisplayName("When using Invalid data")
@ExtendWith(InvalidPersonParameterResolver.class)
public class InvalidData {
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* An invalid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are invalid")
public void validateFirstName(Person person) {
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateFirstName(person));
}
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* An invalid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are invalid")
public void validateLastName(Person person) {
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateLastName(person));
}
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.param;
import java.util.Random;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class ValidPersonParameterResolver implements ParameterResolver {
/**
* The "good" (valid) data for testing purposes has to go somewhere, right?
*/
public static Person[] VALID_PERSONS = {
new Person().setId(1L).setLastName("Adams").setFirstName("Jill"),
new Person().setId(2L).setLastName("Baker").setFirstName("James"),
new Person().setId(3L).setLastName("Carter").setFirstName("Samanta"),
new Person().setId(4L).setLastName("Daniels").setFirstName("Joseph"),
new Person().setId(5L).setLastName("English").setFirstName("Jane"),
new Person().setId(6L).setLastName("Fontana").setFirstName("Enrique"),
// TODO: ADD MORE DATA HERE
};
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Object ret = null;
//
// Return a random, valid Person object if Person.class is the type of Parameter
/// to be resolved. Otherwise return null.
if (parameterContext.getParameter().getType() == Person.class) {
ret = VALID_PERSONS[new Random().nextInt(VALID_PERSONS.length)];
}
return ret;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
boolean ret = false;
//
// If the Parameter.type == Person.class, then we support it, otherwise, get outta here!
if (parameterContext.getParameter().getType() == Person.class) {
ret = true;
}
return ret;
}
}

View File

@ -29,6 +29,8 @@
- [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph) - [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph)
- [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue) - [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue)
- [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss) - [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss)
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own. The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.

View File

@ -462,6 +462,11 @@
<artifactId>pcollections</artifactId> <artifactId>pcollections</artifactId>
<version>${pcollections.version}</version> <version>${pcollections.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.machinezoo.noexception</groupId>
<artifactId>noexception</artifactId>
<version>1.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.eclipse.collections</groupId> <groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId> <artifactId>eclipse-collections</artifactId>

View File

@ -0,0 +1,24 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomExceptionHandler extends ExceptionHandler {
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@Override
public boolean handle(Throwable throwable) {
if (throwable.getClass()
.isAssignableFrom(RuntimeException.class)
|| throwable.getClass()
.isAssignableFrom(Error.class)) {
return false;
} else {
logger.error("Caught Exception ", throwable);
return true;
}
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.eclipsecollections;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.tuple.Tuples;
import static org.junit.Assert.assertEquals;
import java.util.Map;
import org.junit.Test;
public class ForEachPatternTest {
@SuppressWarnings("unchecked")
@Test
public void whenInstantiateAndChangeValues_thenCorrect() {
Pair<Integer, String> pair1 = Tuples.pair(1, "One");
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
UnifiedMap<Integer, String> map = UnifiedMap.newMapWith(pair1, pair2, pair3);
for (int i = 0; i < map.size(); i++) {
map.put(i + 1, "New Value");
}
for (int i = 0; i < map.size(); i++) {
assertEquals("New Value", map.get(i + 1));
}
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.eclipsecollections;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.eclipse.collections.impl.factory.Lists;
import org.junit.Test;
public class InjectIntoPatternTest {
@Test
public void whenInjectInto_thenCorrect() {
List<Integer> list = Lists.mutable.of(1, 2, 3, 4);
int result = 5;
for (int i = 0; i < list.size(); i++) {
Integer v = list.get(i);
result = result + v.intValue();
}
assertEquals(15, result);
}
}

View File

@ -31,7 +31,7 @@ import org.springframework.web.client.RestTemplate;
import io.specto.hoverfly.junit.core.SimulationSource; import io.specto.hoverfly.junit.core.SimulationSource;
import io.specto.hoverfly.junit.rule.HoverflyRule; import io.specto.hoverfly.junit.rule.HoverflyRule;
public class HoverflyApiTest { public class HoverflyApiIntegrationTest {
private static final SimulationSource source = dsl( private static final SimulationSource source = dsl(
service("http://www.baeldung.com") service("http://www.baeldung.com")

View File

@ -0,0 +1,62 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.Exceptions;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NoExceptionUnitTest {
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
@Test
public void whenStdExceptionHandling_thenCatchAndLog() {
try {
System.out.println("Result is " + Integer.parseInt("foobar"));
} catch (Throwable exception) {
logger.error("Caught exception:", exception);
}
}
@Test
public void whenDefaultNoException_thenCatchAndLog() {
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
Exceptions.log(logger).run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithMessage() {
Exceptions.log(logger, "Something went wrong:").run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> Integer.parseInt("foobar")).orElse(-1));
}
@Test(expected = Error.class)
public void givenCustomHandler_whenError_thenRethrowError() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwError());
}
@Test
public void givenCustomHandler_whenException_thenCatchAndLog() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwException());
}
private static void throwError() {
throw new Error("This is very bad.");
}
private static void throwException() {
String testString = "foo";
testString.charAt(5);
}
}

View File

@ -1,7 +1,12 @@
package com.baeldung.pcollections; package com.baeldung.pcollections;
import org.junit.Test; import org.junit.Test;
import org.pcollections.*; import org.pcollections.HashPMap;
import org.pcollections.HashTreePMap;
import org.pcollections.HashTreePSet;
import org.pcollections.MapPSet;
import org.pcollections.PVector;
import org.pcollections.TreePVector;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -27,7 +32,7 @@ public class PCollectionsUnitTest {
@Test @Test
public void givenExistingHashMap_whenFrom_thenCreateHashPMap() { public void givenExistingHashMap_whenFrom_thenCreateHashPMap() {
Map map = new HashMap(); Map<String, String> map = new HashMap<>();
map.put("mkey1", "mval1"); map.put("mkey1", "mval1");
map.put("mkey2", "mval2"); map.put("mkey2", "mval2");
@ -41,7 +46,7 @@ public class PCollectionsUnitTest {
HashPMap<String, String> pmap = HashTreePMap.empty(); HashPMap<String, String> pmap = HashTreePMap.empty();
HashPMap<String, String> pmap0 = pmap.plus("key1", "value1"); HashPMap<String, String> pmap0 = pmap.plus("key1", "value1");
Map map = new HashMap(); Map<String, String> map = new HashMap<>();
map.put("key2", "val2"); map.put("key2", "val2");
map.put("key3", "val3"); map.put("key3", "val3");
@ -57,22 +62,24 @@ public class PCollectionsUnitTest {
@Test @Test
public void whenTreePVectorMethods_thenPerformOperations() { public void whenTreePVectorMethods_thenPerformOperations() {
TreePVector pVector = TreePVector.empty(); TreePVector<String> pVector = TreePVector.empty();
TreePVector<String> pV1 = pVector.plus("e1");
TreePVector<String> pV2 = pV1.plusAll(Arrays.asList("e2", "e3", "e4"));
TreePVector pV1 = pVector.plus("e1");
TreePVector pV2 = pV1.plusAll(Arrays.asList("e2", "e3", "e4"));
assertEquals(1, pV1.size()); assertEquals(1, pV1.size());
assertEquals(4, pV2.size()); assertEquals(4, pV2.size());
TreePVector pV3 = pV2.minus("e1"); TreePVector<String> pV3 = pV2.minus("e1");
TreePVector pV4 = pV3.minusAll(Arrays.asList("e2", "e3", "e4")); TreePVector<String> pV4 = pV3.minusAll(Arrays.asList("e2", "e3", "e4"));
assertEquals(pV3.size(), 3); assertEquals(pV3.size(), 3);
assertEquals(pV4.size(), 0); assertEquals(pV4.size(), 0);
TreePVector pSub = pV2.subList(0, 2); TreePVector<String> pSub = pV2.subList(0, 2);
assertTrue(pSub.contains("e1") && pSub.contains("e2")); assertTrue(pSub.contains("e1") && pSub.contains("e2"));
TreePVector pVW = (TreePVector) pV2.with(0, "e10"); PVector<String> pVW = pV2.with(0, "e10");
assertEquals(pVW.get(0), "e10"); assertEquals(pVW.get(0), "e10");
} }

View File

@ -27,9 +27,6 @@ import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/**
* @author aiet
*/
public class AtlasObserverLiveTest { public class AtlasObserverLiveTest {
private final String atlasUri = "http://localhost:7101/api/v1"; private final String atlasUri = "http://localhost:7101/api/v1";

View File

@ -22,10 +22,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** public class MetricAnnotationManualTest extends MetricTestBase {
* @author aiet
*/
public class MetricAnnotationTest extends MetricTestBase {
@Monitor(name = "integerCounter", type = DataSourceType.COUNTER, description = "Total number of update operations.") @Monitor(name = "integerCounter", type = DataSourceType.COUNTER, description = "Total number of update operations.")
private final AtomicInteger updateCount = new AtomicInteger(0); private final AtomicInteger updateCount = new AtomicInteger(0);

View File

@ -13,14 +13,16 @@ import java.util.List;
import static com.netflix.servo.annotations.DataSourceType.GAUGE; import static com.netflix.servo.annotations.DataSourceType.GAUGE;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** public class MetricObserverManualTest extends MetricTestBase {
* @author aiet
*/
public class MetricObserverTest extends MetricTestBase {
@Test @Test
public void givenMetrics_whenRegister_thenMonitored() throws InterruptedException { public void givenMetrics_whenRegister_thenMonitored() throws InterruptedException {

View File

@ -1,20 +1,23 @@
package com.baeldung.metrics.servo; package com.baeldung.metrics.servo;
import com.netflix.servo.Metric; import com.netflix.servo.Metric;
import com.netflix.servo.publish.*; import com.netflix.servo.publish.BasicMetricFilter;
import com.netflix.servo.publish.JvmMetricPoller;
import com.netflix.servo.publish.MemoryMetricObserver;
import com.netflix.servo.publish.PollRunnable;
import com.netflix.servo.publish.PollScheduler;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** public class MetricPollerManualTest {
* @author aiet
*/
public class MetricPollerTest {
@Test @Test
public void givenJvmPoller_whenMonitor_thenDataCollected() throws Exception { public void givenJvmPoller_whenMonitor_thenDataCollected() throws Exception {

View File

@ -1,14 +1,16 @@
package com.baeldung.metrics.servo; package com.baeldung.metrics.servo;
import com.netflix.servo.publish.*; import com.netflix.servo.publish.BasicMetricFilter;
import com.netflix.servo.publish.MemoryMetricObserver;
import com.netflix.servo.publish.MetricFilter;
import com.netflix.servo.publish.MonitorRegistryMetricPoller;
import com.netflix.servo.publish.PollRunnable;
import com.netflix.servo.publish.PollScheduler;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
/**
* @author aiet
*/
abstract class MetricTestBase { abstract class MetricTestBase {
MemoryMetricObserver observer; MemoryMetricObserver observer;

View File

@ -1,6 +1,21 @@
package com.baeldung.metrics.servo; package com.baeldung.metrics.servo;
import com.netflix.servo.monitor.*; import com.netflix.servo.monitor.BasicCounter;
import com.netflix.servo.monitor.BasicGauge;
import com.netflix.servo.monitor.BasicInformational;
import com.netflix.servo.monitor.BasicTimer;
import com.netflix.servo.monitor.BucketConfig;
import com.netflix.servo.monitor.BucketTimer;
import com.netflix.servo.monitor.Counter;
import com.netflix.servo.monitor.Gauge;
import com.netflix.servo.monitor.MaxGauge;
import com.netflix.servo.monitor.Monitor;
import com.netflix.servo.monitor.MonitorConfig;
import com.netflix.servo.monitor.Monitors;
import com.netflix.servo.monitor.PeakRateCounter;
import com.netflix.servo.monitor.StatsTimer;
import com.netflix.servo.monitor.StepCounter;
import com.netflix.servo.monitor.Stopwatch;
import com.netflix.servo.stats.StatsConfig; import com.netflix.servo.stats.StatsConfig;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -9,13 +24,12 @@ import java.util.Map;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/**
* Unit test for simple App.
*/
public class MetricTypeTest { public class MetricTypeTest {
@Test @Test

2
noexception/README.md Normal file
View File

@ -0,0 +1,2 @@
### Relevant Articles:
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)

23
noexception/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<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>
<groupId>com.baeldung</groupId>
<artifactId>noexception</artifactId>
<version>1.0</version>
<name>noexception</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.machinezoo.noexception</groupId>
<artifactId>noexception</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomExceptionHandler extends ExceptionHandler {
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@Override
public boolean handle(Throwable throwable) {
if (throwable.getClass()
.isAssignableFrom(RuntimeException.class)
|| throwable.getClass()
.isAssignableFrom(Error.class)) {
return false;
} else {
logger.error("Caught Exception ", throwable);
return true;
}
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.Exceptions;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NoExceptionUnitTest {
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
@Test
public void whenStdExceptionHandling_thenCatchAndLog() {
try {
System.out.println("Result is " + Integer.parseInt("foobar"));
} catch (Throwable exception) {
logger.error("Caught exception:", exception);
}
}
@Test
public void whenDefaultNoException_thenCatchAndLog() {
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
System.out.println("Result is " + Exceptions.log(logger).get(() -> +Integer.parseInt("foobar")).orElse(-1));
}
@Test
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithClassNameAndMessage() {
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
}
@Test
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
}
@Test(expected = Error.class)
public void givenCustomHandler_whenError_thenRethrowError() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwError());
}
@Test
public void givenCustomHandler_whenException_thenCatchAndLog() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwException());
}
private static void throwError() {
throw new Error("This is very bad.");
}
private static void throwException() {
String testString = "foo";
testString.charAt(5);
}
}

View File

@ -106,6 +106,7 @@
<module>mockito2</module> <module>mockito2</module>
<module>mocks</module> <module>mocks</module>
<module>mustache</module> <module>mustache</module>
<module>noexception</module>
<module>orika</module> <module>orika</module>

View File

@ -3,11 +3,12 @@ package com.example.activitiwithspring;
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines; import org.activiti.engine.ProcessEngines;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test; import org.junit.Test;
public class ProcessEngineCreationTests { import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class ProcessEngineCreationIntegrationTest {
@Test @Test
public void givenXMLConfig_whenGetDefault_thenGotProcessEngine() { public void givenXMLConfig_whenGetDefault_thenGotProcessEngine() {

View File

@ -1,10 +1,6 @@
package com.example.activitiwithspring; package com.example.activitiwithspring;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ActivitiException; import org.activiti.engine.ActivitiException;
import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines; import org.activiti.engine.ProcessEngines;
@ -13,11 +9,16 @@ import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService; import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task; import org.activiti.engine.task.Task;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
public class ProcessExecutionTests { import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class ProcessExecutionIntegrationTest {
@Test @Test
public void givenBPMN_whenDeployProcess_thenDeployed() { public void givenBPMN_whenDeployProcess_thenDeployed() {

View File

@ -0,0 +1,77 @@
/*
* Copyright 2006-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.baeldung.spring_batch_intro.partitioner;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
public class CustomMultiResourcePartitioner implements Partitioner {
private static final String DEFAULT_KEY_NAME = "fileName";
private static final String PARTITION_KEY = "partition";
private Resource[] resources = new Resource[0];
private String keyName = DEFAULT_KEY_NAME;
/**
* The resources to assign to each partition. In Spring configuration you
* can use a pattern to select multiple resources.
* @param resources the resources to use
*/
public void setResources(Resource[] resources) {
this.resources = resources;
}
/**
* The name of the key for the file name in each {@link ExecutionContext}.
* Defaults to "fileName".
* @param keyName the value of the key
*/
public void setKeyName(String keyName) {
this.keyName = keyName;
}
/**
* Assign the filename of each of the injected resources to an
* {@link ExecutionContext}.
*
* @see Partitioner#partition(int)
*/
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Map<String, ExecutionContext> map = new HashMap<String, ExecutionContext>(gridSize);
int i = 0, k = 1;
for (Resource resource : resources) {
ExecutionContext context = new ExecutionContext();
Assert.state(resource.exists(), "Resource does not exist: " + resource);
context.putString(keyName, resource.getFilename());
context.putString("opFileName", "output" + k++ + ".xml");
map.put(PARTITION_KEY + i, context);
i++;
}
return map;
}
}

View File

@ -0,0 +1,169 @@
package org.baeldung.spring_batch_intro.partitioner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import javax.sql.DataSource;
import org.baeldung.spring_batch_intro.model.Transaction;
import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@EnableBatchProcessing
public class SpringbatchPartitionConfig {
@Autowired
ResourcePatternResolver resoursePatternResolver;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean(name = "partitionerJob")
public Job partitionerJob() throws UnexpectedInputException, MalformedURLException, ParseException {
return jobs.get("partitionerJob")
.start(partitionStep())
.build();
}
@Bean
public Step partitionStep() throws UnexpectedInputException, MalformedURLException, ParseException {
return steps.get("partitionStep")
.partitioner("slaveStep", partitioner())
.step(slaveStep())
.taskExecutor(taskExecutor())
.build();
}
@Bean
public Step slaveStep() throws UnexpectedInputException, MalformedURLException, ParseException {
return steps.get("slaveStep")
.<Transaction, Transaction> chunk(1)
.reader(itemReader(null))
.writer(itemWriter(marshaller(), null))
.build();
}
@Bean
public CustomMultiResourcePartitioner partitioner() {
CustomMultiResourcePartitioner partitioner = new CustomMultiResourcePartitioner();
Resource[] resources;
try {
resources = resoursePatternResolver.getResources("file:src/main/resources/input/partitioner/*.csv");
} catch (IOException e) {
throw new RuntimeException("I/O problems when resolving the input file pattern.", e);
}
partitioner.setResources(resources);
return partitioner;
}
@Bean
@StepScope
public FlatFileItemReader<Transaction> itemReader(@Value("#{stepExecutionContext[fileName]}") String filename) throws UnexpectedInputException, ParseException {
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
String[] tokens = { "username", "userid", "transactiondate", "amount" };
tokenizer.setNames(tokens);
reader.setResource(new ClassPathResource("input/partitioner/" + filename));
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
reader.setLinesToSkip(1);
reader.setLineMapper(lineMapper);
return reader;
}
@Bean(destroyMethod = "")
@StepScope
public StaxEventItemWriter<Transaction> itemWriter(Marshaller marshaller, @Value("#{stepExecutionContext[opFileName]}") String filename) throws MalformedURLException {
StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<>();
itemWriter.setMarshaller(marshaller);
itemWriter.setRootTagName("transactionRecord");
itemWriter.setResource(new FileSystemResource("src/main/resources/output/" + filename));
return itemWriter;
}
@Bean
public Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[] { Transaction.class });
return marshaller;
}
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(5);
taskExecutor.setCorePoolSize(5);
taskExecutor.setQueueCapacity(5);
taskExecutor.afterPropertiesSet();
return taskExecutor;
}
private JobRepository getJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource());
factory.setTransactionManager(getTransactionManager());
// JobRepositoryFactoryBean's methods Throws Generic Exception,
// it would have been better to have a specific one
factory.afterPropertiesSet();
return (JobRepository) factory.getObject();
}
private DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
.addScript("classpath:org/springframework/batch/core/schema-h2.sql")
.build();
return db;
}
private PlatformTransactionManager getTransactionManager() {
return new ResourcelessTransactionManager();
}
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
// SimpleJobLauncher's methods Throws Generic Exception,
// it would have been better to have a specific one
jobLauncher.setJobRepository(getJobRepository());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}

View File

@ -0,0 +1,28 @@
package org.baeldung.spring_batch_intro.partitioner;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringbatchPartitionerApp {
public static void main(final String[] args) {
// Spring Java config
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SpringbatchPartitionConfig.class);
context.refresh();
final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
final Job job = (Job) context.getBean("partitionerJob");
System.out.println("Starting the batch job");
try {
final JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Job Status : " + execution.getStatus());
System.out.println("Job succeeded");
} catch (final Exception e) {
e.printStackTrace();
System.out.println("Job failed");
}
}
}

View File

@ -0,0 +1,4 @@
username, user_id, transaction_date, transaction_amount
devendra, 1234, 31/10/2015, 10000
john, 2134, 3/12/2015, 12321
robin, 2134, 2/02/2015, 23411
1 username user_id transaction_date transaction_amount
2 devendra 1234 31/10/2015 10000
3 john 2134 3/12/2015 12321
4 robin 2134 2/02/2015 23411

View File

@ -0,0 +1,4 @@
username, user_id, transaction_date, transaction_amount
devendra, 1234, 31/10/2015, 10000
john, 2134, 3/12/2015, 12321
robin, 2134, 2/02/2015, 23411
1 username user_id transaction_date transaction_amount
2 devendra 1234 31/10/2015 10000
3 john 2134 3/12/2015 12321
4 robin 2134 2/02/2015 23411

View File

@ -0,0 +1,4 @@
username, user_id, transaction_date, transaction_amount
devendra, 1234, 31/10/2015, 10000
john, 2134, 3/12/2015, 12321
robin, 2134, 2/02/2015, 23411
1 username user_id transaction_date transaction_amount
2 devendra 1234 31/10/2015 10000
3 john 2134 3/12/2015 12321
4 robin 2134 2/02/2015 23411

View File

@ -0,0 +1,4 @@
username, user_id, transaction_date, transaction_amount
devendra, 1234, 31/10/2015, 10000
john, 2134, 3/12/2015, 12321
robin, 2134, 2/02/2015, 23411
1 username user_id transaction_date transaction_amount
2 devendra 1234 31/10/2015 10000
3 john 2134 3/12/2015 12321
4 robin 2134 2/02/2015 23411

View File

@ -0,0 +1,4 @@
username, user_id, transaction_date, transaction_amount
devendra, 1234, 31/10/2015, 10000
john, 2134, 3/12/2015, 12321
robin, 2134, 2/02/2015, 23411
1 username user_id transaction_date transaction_amount
2 devendra 1234 31/10/2015 10000
3 john 2134 3/12/2015 12321
4 robin 2134 2/02/2015 23411

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>
</transactionRecord>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>
</transactionRecord>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>
</transactionRecord>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>
</transactionRecord>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>
</transactionRecord>

View File

@ -102,6 +102,7 @@
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <artifactId>servlet-api</artifactId>
<scope>provided</scope>
<version>${javax.servlet.servlet-api.version}</version> <version>${javax.servlet.servlet-api.version}</version>
</dependency> </dependency>

View File

@ -1,11 +1,15 @@
package org.baeldung.web.controller.redirect; package org.baeldung.web.controller.redirect;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView; import org.springframework.web.servlet.view.RedirectView;
@ -49,4 +53,16 @@ public class RedirectController {
model.addAttribute("redirectionAttribute", flashAttribute); model.addAttribute("redirectionAttribute", flashAttribute);
return new ModelAndView("redirection", model); return new ModelAndView("redirection", model);
} }
@RequestMapping(value = "/redirectPostToPost", method = RequestMethod.POST)
public ModelAndView redirectPostToPost(HttpServletRequest request) {
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
return new ModelAndView("redirect:/redirectedPostToPost");
}
@RequestMapping(value = "/redirectedPostToPost", method = RequestMethod.POST)
public ModelAndView redirectedPostToPost() {
return new ModelAndView("redirection");
}
} }