From 9aa221afbc66f521085f9eb7e375e800c7c532d6 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Mon, 2 Oct 2017 15:14:27 +0200 Subject: [PATCH 1/5] cleanup spring 5 pom (#2700) * fix spring config * fix spring config * fix spring config * minor fix * fix spring-boot module * fix pom * upgrade jackson * minor fix * java concurrency * cleanup * fix conflict * java 8 * clean up * add core-java-8 to main pom * rename kotlin to core-kotlin * update spring 5 * cleanup pom --- spring-5-mvc/pom.xml | 18 +-------- spring-5/pom.xml | 24 ++---------- ...Spring5URLPatternUsingRouterFunctions.java | 2 +- .../FunctionalSpringBootApplication.java | 25 ++++++------ .../functional/FunctionalWebApplication.java | 25 ++++++------ .../com/baeldung/functional/RootServlet.java | 33 ++++++++-------- .../com/baeldung/jupiter/SpringExtension.java | 38 +++++++++++-------- 7 files changed, 75 insertions(+), 90 deletions(-) diff --git a/spring-5-mvc/pom.xml b/spring-5-mvc/pom.xml index 27dfec6939..7b7ddcba88 100644 --- a/spring-5-mvc/pom.xml +++ b/spring-5-mvc/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M1 + 2.0.0.M3 @@ -165,14 +165,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones @@ -183,14 +175,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones diff --git a/spring-5/pom.xml b/spring-5/pom.xml index b77d89b532..4dfede4dab 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M1 + 2.0.0.M3 @@ -143,14 +143,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones @@ -161,14 +153,6 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - spring-milestones Spring Milestones @@ -183,10 +167,10 @@ UTF-8 UTF-8 1.8 - 1.0.0-M4 - 5.0.0-M4 + 1.0.0 + 5.0.0 2.20 - 5.0.0.RC2 + 5.0.0.RELEASE 1.0.1.RELEASE 1.1.3 1.0 diff --git a/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java b/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java index b7436c5ba7..2a6d04538c 100644 --- a/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java +++ b/spring-5/src/main/java/com/baeldung/functional/ExploreSpring5URLPatternUsingRouterFunctions.java @@ -33,7 +33,7 @@ public class ExploreSpring5URLPatternUsingRouterFunctions { WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); diff --git a/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java b/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java index f181dcb8e8..402b607b19 100644 --- a/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java +++ b/spring-5/src/main/java/com/baeldung/functional/FunctionalSpringBootApplication.java @@ -1,5 +1,17 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; @@ -17,18 +29,9 @@ import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + import reactor.core.publisher.Flux; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; - @SpringBootApplication @ComponentScan(basePackages = { "com.baeldung.functional" }) public class FunctionalSpringBootApplication { @@ -57,7 +60,7 @@ public class FunctionalSpringBootApplication { @Bean public ServletRegistrationBean servletRegistrationBean() throws Exception { HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction())) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/"); registrationBean.setLoadOnStartup(1); diff --git a/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java b/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java index 2a4642c484..5a7d70d3db 100644 --- a/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java +++ b/spring-5/src/main/java/com/baeldung/functional/FunctionalWebApplication.java @@ -1,5 +1,17 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + import org.apache.catalina.Context; import org.apache.catalina.startup.Tomcat; import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; @@ -12,18 +24,9 @@ import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + import reactor.core.publisher.Flux; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; - public class FunctionalWebApplication { private static final Actor BRAD_PITT = new Actor("Brad", "Pitt"); @@ -50,7 +53,7 @@ public class FunctionalWebApplication { WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); diff --git a/spring-5/src/main/java/com/baeldung/functional/RootServlet.java b/spring-5/src/main/java/com/baeldung/functional/RootServlet.java index c0dd54cb4a..8fe24821de 100644 --- a/spring-5/src/main/java/com/baeldung/functional/RootServlet.java +++ b/spring-5/src/main/java/com/baeldung/functional/RootServlet.java @@ -1,5 +1,20 @@ package com.baeldung.functional; +import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; +import static org.springframework.web.reactive.function.BodyExtractors.toFormData; +import static org.springframework.web.reactive.function.BodyInserters.fromObject; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; +import static org.springframework.web.reactive.function.server.RequestPredicates.path; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicLong; + import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; @@ -7,29 +22,17 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicLong; - -import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers; -import static org.springframework.web.reactive.function.BodyExtractors.toFormData; -import static org.springframework.web.reactive.function.BodyInserters.fromObject; -import static org.springframework.web.reactive.function.server.RequestPredicates.*; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; -import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; -import static org.springframework.web.reactive.function.server.ServerResponse.ok; -import org.springframework.web.server.WebHandler; - public class RootServlet extends ServletHttpHandlerAdapter { public RootServlet() { this(WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction())) - .prependFilter(new IndexRewriteFilter()) + .filter(new IndexRewriteFilter()) .build()); } diff --git a/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java b/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java index 0eb7c861f1..7218d984ef 100644 --- a/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java +++ b/spring-5/src/main/java/com/baeldung/jupiter/SpringExtension.java @@ -1,28 +1,36 @@ package com.baeldung.jupiter; -import org.junit.jupiter.api.extension.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.core.annotation.AnnotatedElementUtils; -import org.springframework.test.context.TestContextManager; -import org.springframework.util.Assert; - import java.lang.reflect.Constructor; import java.lang.reflect.Executable; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +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; +import org.junit.jupiter.api.extension.TestInstancePostProcessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.test.context.TestContextManager; +import org.springframework.util.Assert; + public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver { private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class); @Override - public void beforeAll(ContainerExtensionContext context) throws Exception { + public void beforeAll(ExtensionContext context) throws Exception { getTestContextManager(context).beforeTestClass(); } @Override - public void afterAll(ContainerExtensionContext context) throws Exception { + public void afterAll(ExtensionContext context) throws Exception { try { getTestContextManager(context).afterTestClass(); } finally { @@ -38,7 +46,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes } @Override - public void beforeEach(TestExtensionContext context) throws Exception { + public void beforeEach(ExtensionContext context) throws Exception { Object testInstance = context.getTestInstance(); Method testMethod = context.getTestMethod() .get(); @@ -46,24 +54,24 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes } @Override - public void afterEach(TestExtensionContext context) throws Exception { + public void afterEach(ExtensionContext context) throws Exception { Object testInstance = context.getTestInstance(); Method testMethod = context.getTestMethod() .get(); - Throwable testException = context.getTestException() + Throwable testException = context.getExecutionException() .orElse(null); getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException); } @Override - public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); Executable executable = parameter.getDeclaringExecutable(); - return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter); + return ((executable instanceof Constructor) && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter); } @Override - public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); Class testClass = extensionContext.getTestClass() .get(); From dcc7fa65dd314dad2fd68c3fd245ea3f1aecc915 Mon Sep 17 00:00:00 2001 From: Kiran Date: Wed, 4 Oct 2017 02:59:54 +0530 Subject: [PATCH 2/5] Updated for BAEL 971 (#2702) * Updated Lang3Utils.java Added a new test for ConcurrentException and updated other exception with Lambda * LazyInitializer sample files * Updated file with LazyInitializer Unit Test * Updated BuidlerMethods class to include LazyInitializer & BackgroundInitializer * Updated Lang3UtilsTest class * Updated Lang3UtilsTest class --- .../commons/lang3/BuilderMethods.java | 33 +++++++++++++++++++ .../commons/lang3/Lang3UtilsTest.java | 13 ++++++++ 2 files changed, 46 insertions(+) diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java index c64f7e7511..35cae7426d 100644 --- a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java +++ b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java @@ -3,6 +3,8 @@ package com.baeldung.commons.lang3; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.concurrent.ConcurrentException; +import org.apache.commons.lang3.concurrent.BackgroundInitializer; public class BuilderMethods { @@ -56,5 +58,36 @@ public class BuilderMethods { System.out.println(simple1.getName()); System.out.println(simple1.hashCode()); System.out.println(simple1.toString()); + + SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer(); + + try { + sampleLazyInitializer.get(); + } catch (ConcurrentException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer(); + sampleBackgroundInitializer.start(); + + // Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish. + + try { + Object result = sampleBackgroundInitializer.get(); + } catch (ConcurrentException e) { + e.printStackTrace(); + } } } + +class SampleBackgroundInitializer extends BackgroundInitializer{ + + @Override + protected String initialize() throws Exception { + return null; + } + + // Any complex task that takes some time + +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java index af70ccecc7..29bcebeb2b 100644 --- a/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java +++ b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java @@ -3,7 +3,9 @@ package com.baeldung.commons.lang3; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -20,6 +22,7 @@ import org.apache.commons.lang3.ArchUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.arch.Processor; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.apache.commons.lang3.concurrent.ConcurrentException; import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException; import org.apache.commons.lang3.concurrent.ConcurrentUtils; @@ -133,4 +136,14 @@ public class Lang3UtilsTest { assertEquals(sampleObjectOne, sampleObjectTwo); } + @Test + public void testBuildDefaults() { + BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder(); + BasicThreadFactory factory = builder.build(); + assertNull("No naming pattern set Yet", factory.getNamingPattern()); + BasicThreadFactory factory2 = builder.namingPattern("sampleNamingPattern").daemon(true).priority(Thread.MIN_PRIORITY).build(); + assertNotNull("Got a naming pattern", factory2.getNamingPattern()); + assertEquals("sampleNamingPattern", factory2.getNamingPattern()); + + } } From 8a77746e4d6e3424aaa9001e1fd32ff68b702aa5 Mon Sep 17 00:00:00 2001 From: lor6 Date: Wed, 4 Oct 2017 21:00:44 +0300 Subject: [PATCH 3/5] container elements validation ex (#2707) * container elements validation ex * comment custom container validation --- .../src/main/java/org/baeldung/Customer.java | 66 ++++++++++++++ .../main/java/org/baeldung/CustomerMap.java | 19 ++++ .../src/main/java/org/baeldung/Profile.java | 13 +++ .../ProfileValueExtractor.java | 17 ++++ ....validation.valueextraction.ValueExtractor | 1 + .../ContainerValidationIntegrationTest.java | 88 +++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 javaxval/src/main/java/org/baeldung/Customer.java create mode 100644 javaxval/src/main/java/org/baeldung/CustomerMap.java create mode 100644 javaxval/src/main/java/org/baeldung/Profile.java create mode 100644 javaxval/src/main/java/org/baeldung/valueextractors/ProfileValueExtractor.java create mode 100644 javaxval/src/main/resources/META-INF/services/javax.validation.valueextraction.ValueExtractor create mode 100644 javaxval/src/test/java/org/baeldung/ContainerValidationIntegrationTest.java diff --git a/javaxval/src/main/java/org/baeldung/Customer.java b/javaxval/src/main/java/org/baeldung/Customer.java new file mode 100644 index 0000000000..a90fb419de --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/Customer.java @@ -0,0 +1,66 @@ +package org.baeldung; + +import java.util.List; +import java.util.Optional; +import java.util.OptionalInt; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.PositiveOrZero; + +public class Customer { + + @NotBlank(message="Name cannot be empty") + private String name; + + private List<@NotBlank(message="Address must not be blank") String> addresses; + + private Integer age; + + @PositiveOrZero + private OptionalInt numberOfOrders; + + //@NotBlank + private Profile profile; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getAddresses() { + return addresses; + } + + public void setAddresses(List addresses) { + this.addresses = addresses; + } + + public Optional<@Min(18) Integer> getAge() { + return Optional.ofNullable(age); + } + + public void setAge(Integer age) { + this.age = age; + } + + public OptionalInt getNumberOfOrders() { + return numberOfOrders; + } + + public void setNumberOfOrders(OptionalInt numberOfOrders) { + this.numberOfOrders = numberOfOrders; + } + + public Profile getProfile() { + return profile; + } + + public void setProfile(Profile profile) { + this.profile = profile; + } + +} diff --git a/javaxval/src/main/java/org/baeldung/CustomerMap.java b/javaxval/src/main/java/org/baeldung/CustomerMap.java new file mode 100644 index 0000000000..37446cf86e --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/CustomerMap.java @@ -0,0 +1,19 @@ +package org.baeldung; + +import java.util.Map; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotNull; + +public class CustomerMap { + + private Map<@Email(message="Must be a valid email") String, @NotNull Customer> customers; + + public Map getCustomers() { + return customers; + } + + public void setCustomers(Map customers) { + this.customers = customers; + } +} diff --git a/javaxval/src/main/java/org/baeldung/Profile.java b/javaxval/src/main/java/org/baeldung/Profile.java new file mode 100644 index 0000000000..ec73a5c62f --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/Profile.java @@ -0,0 +1,13 @@ +package org.baeldung; + +public class Profile { + private String companyName; + + public String getCompanyName() { + return companyName; + } + + public void setCompanyName(String companyName) { + this.companyName = companyName; + } +} diff --git a/javaxval/src/main/java/org/baeldung/valueextractors/ProfileValueExtractor.java b/javaxval/src/main/java/org/baeldung/valueextractors/ProfileValueExtractor.java new file mode 100644 index 0000000000..f192034261 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/valueextractors/ProfileValueExtractor.java @@ -0,0 +1,17 @@ +package org.baeldung.valueextractors; + +import javax.validation.valueextraction.ExtractedValue; +import javax.validation.valueextraction.UnwrapByDefault; +import javax.validation.valueextraction.ValueExtractor; + +import org.baeldung.Profile; + +@UnwrapByDefault +public class ProfileValueExtractor implements ValueExtractor<@ExtractedValue(type = String.class) Profile> { + + @Override + public void extractValues(Profile originalValue, ValueExtractor.ValueReceiver receiver) { + receiver.value(null, originalValue.getCompanyName()); + } + +} diff --git a/javaxval/src/main/resources/META-INF/services/javax.validation.valueextraction.ValueExtractor b/javaxval/src/main/resources/META-INF/services/javax.validation.valueextraction.ValueExtractor new file mode 100644 index 0000000000..e77a30cfe4 --- /dev/null +++ b/javaxval/src/main/resources/META-INF/services/javax.validation.valueextraction.ValueExtractor @@ -0,0 +1 @@ +org.baeldung.valueextractors.ProfileValueExtractor \ No newline at end of file diff --git a/javaxval/src/test/java/org/baeldung/ContainerValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/ContainerValidationIntegrationTest.java new file mode 100644 index 0000000000..dff02ff13d --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/ContainerValidationIntegrationTest.java @@ -0,0 +1,88 @@ +package org.baeldung; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.OptionalInt; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import org.baeldung.valueextractors.ProfileValueExtractor; + +import org.junit.Before; +import org.junit.Test; + +public class ContainerValidationIntegrationTest { + private Validator validator; + + @Before + public void setup() { + ValidatorFactory factory = Validation.byDefaultProvider().configure() + .addValueExtractor(new ProfileValueExtractor()).buildValidatorFactory(); + validator = factory.getValidator(); + } + + @Test + public void whenEmptyAddress_thenValidationFails() { + Customer customer = new Customer(); + customer.setName("John"); + customer.setAddresses(Collections.singletonList(" ")); + Set> violations = validator.validate(customer); + assertEquals(1, violations.size()); + assertEquals("Address must not be blank", violations.iterator() + .next() + .getMessage()); + } + + @Test + public void whenInvalidEmail_thenValidationFails() { + CustomerMap map = new CustomerMap(); + map.setCustomers(Collections.singletonMap("john", new Customer())); + Set> violations = validator.validate(map); + assertEquals(1, violations.size()); + assertEquals("Must be a valid email", violations.iterator() + .next() + .getMessage()); + } + + @Test + public void whenAgeTooLow_thenValidationFails() { + Customer customer = new Customer(); + customer.setName("John"); + customer.setAge(15); + Set> violations = validator.validate(customer); + assertEquals(1, violations.size()); + } + + @Test + public void whenAgeNull_thenValidationSucceeds() { + Customer customer = new Customer(); + customer.setName("John"); + Set> violations = validator.validate(customer); + assertEquals(0, violations.size()); + } + + @Test + public void whenNumberOrdersValid_thenValidationSucceeds() { + Customer customer = new Customer(); + customer.setName("John"); + customer.setNumberOfOrders(OptionalInt.of(1)); + Set> violations = validator.validate(customer); + assertEquals(0, violations.size()); + } + + //@Test + public void whenProfileCompanyNameBlank_thenValidationFails() { + Customer customer = new Customer(); + customer.setName("John"); + Profile profile = new Profile(); + profile.setCompanyName(" "); + customer.setProfile(profile); + Set> violations = validator.validate(customer); + assertEquals(1, violations.size()); + } + +} From eaba521fdf75fcfe56a6b26934788e0101f6641d Mon Sep 17 00:00:00 2001 From: alincojanu Date: Wed, 4 Oct 2017 21:08:28 +0300 Subject: [PATCH 4/5] runWith (#2691) --- .../java/com/baeldung/junit/Calculator.java | 11 +++++ .../java/com/baeldung/junit/AdditionTest.java | 14 +++++++ .../baeldung/junit/BlockingTestRunner.java | 18 ++++++++ .../com/baeldung/junit/CalculatorTest.java | 17 ++++++++ .../com/baeldung/junit/SubstractionTest.java | 14 +++++++ .../java/com/baeldung/junit/SuiteTest.java | 12 ++++++ .../java/com/baeldung/junit/TestRunner.java | 41 +++++++++++++++++++ 7 files changed, 127 insertions(+) create mode 100644 testing/src/main/java/com/baeldung/junit/Calculator.java create mode 100644 testing/src/test/java/com/baeldung/junit/AdditionTest.java create mode 100644 testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java create mode 100644 testing/src/test/java/com/baeldung/junit/CalculatorTest.java create mode 100644 testing/src/test/java/com/baeldung/junit/SubstractionTest.java create mode 100644 testing/src/test/java/com/baeldung/junit/SuiteTest.java create mode 100644 testing/src/test/java/com/baeldung/junit/TestRunner.java diff --git a/testing/src/main/java/com/baeldung/junit/Calculator.java b/testing/src/main/java/com/baeldung/junit/Calculator.java new file mode 100644 index 0000000000..8ea7b3ed1f --- /dev/null +++ b/testing/src/main/java/com/baeldung/junit/Calculator.java @@ -0,0 +1,11 @@ +package com.baeldung.junit; + +public class Calculator { + public int add(int a, int b) { + return a + b; + } + + public int sub(int a, int b) { + return a - b; + } +} diff --git a/testing/src/test/java/com/baeldung/junit/AdditionTest.java b/testing/src/test/java/com/baeldung/junit/AdditionTest.java new file mode 100644 index 0000000000..0d492f8058 --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/AdditionTest.java @@ -0,0 +1,14 @@ +package com.baeldung.junit; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class AdditionTest { + Calculator calculator = new Calculator(); + + @Test + public void testAddition() { + assertEquals("addition", 8, calculator.add(5, 3)); + } +} diff --git a/testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java b/testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java new file mode 100644 index 0000000000..432d5cda83 --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java @@ -0,0 +1,18 @@ +package com.baeldung.junit; + +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; + +public class BlockingTestRunner extends BlockJUnit4ClassRunner { + public BlockingTestRunner(Class klass) throws InitializationError { + super(klass); + } + + @Override + protected Statement methodInvoker(FrameworkMethod method, Object test) { + System.out.println("invoking: " + method.getName()); + return super.methodInvoker(method, test); + } +} diff --git a/testing/src/test/java/com/baeldung/junit/CalculatorTest.java b/testing/src/test/java/com/baeldung/junit/CalculatorTest.java new file mode 100644 index 0000000000..d1b35d1442 --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/CalculatorTest.java @@ -0,0 +1,17 @@ +package com.baeldung.junit; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static org.junit.Assert.assertEquals; + +@RunWith(JUnit4.class) +public class CalculatorTest { + Calculator calculator = new Calculator(); + + @Test + public void testAddition() { + assertEquals("addition", 8, calculator.add(5, 3)); + } +} diff --git a/testing/src/test/java/com/baeldung/junit/SubstractionTest.java b/testing/src/test/java/com/baeldung/junit/SubstractionTest.java new file mode 100644 index 0000000000..9650d83afe --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/SubstractionTest.java @@ -0,0 +1,14 @@ +package com.baeldung.junit; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SubstractionTest { + Calculator calculator = new Calculator(); + + @Test + public void substraction() { + assertEquals("substraction", 2, calculator.sub(5, 3)); + } +} diff --git a/testing/src/test/java/com/baeldung/junit/SuiteTest.java b/testing/src/test/java/com/baeldung/junit/SuiteTest.java new file mode 100644 index 0000000000..428319e72e --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/SuiteTest.java @@ -0,0 +1,12 @@ +package com.baeldung.junit; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ + AdditionTest.class, + SubstractionTest.class}) +public class SuiteTest { +} diff --git a/testing/src/test/java/com/baeldung/junit/TestRunner.java b/testing/src/test/java/com/baeldung/junit/TestRunner.java new file mode 100644 index 0000000000..9eb4b3141b --- /dev/null +++ b/testing/src/test/java/com/baeldung/junit/TestRunner.java @@ -0,0 +1,41 @@ +package com.baeldung.junit; + +import org.junit.Test; +import org.junit.runner.Description; +import org.junit.runner.Runner; +import org.junit.runner.notification.RunNotifier; + +import java.lang.reflect.Method; + +public class TestRunner extends Runner { + + private Class testClass; + public TestRunner(Class testClass) { + super(); + this.testClass = testClass; + } + + @Override + public Description getDescription() { + return Description.createTestDescription(testClass, "My runner description"); + } + + @Override + public void run(RunNotifier notifier) { + System.out.println("running the tests from MyRunner: " + testClass); + try { + Object testObject = testClass.newInstance(); + for (Method method : testClass.getMethods()) { + if (method.isAnnotationPresent(Test.class)) { + notifier.fireTestStarted(Description + .createTestDescription(testClass, method.getName())); + method.invoke(testObject); + notifier.fireTestFinished(Description + .createTestDescription(testClass, method.getName())); + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} From 75803f13a7dd4c4b5f82cafe19ffdccb97bbd71a Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Thu, 5 Oct 2017 07:23:32 +0100 Subject: [PATCH 5/5] BAEL-1157: Apache Cayenne - Advanced Querying (#2708) * Different types of bean injection in Spring * Difference between two dates in java * Update README.md * Simple clean of difference between dates * Clean my test article * Improve dates diff: for dates and datetimes * Move difference between dates from core-java to libraries * BAEL-890 - Kotlin-Allopen with Spring example * BAEL-1107 - Introduction to Apache Cayenne Orm * BAEL-1107: update formating and version of libs * BAEL-1107: update properties of Author * BAEL-1157: Apache Cayenne - Advanced Querying * BAEL-1157: Fix imports * BAEL-1157: code indentation --- .../CayenneAdvancedOperationTests.java | 256 ++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 apache-cayenne/src/test/java/com/baeldung/apachecayenne/CayenneAdvancedOperationTests.java diff --git a/apache-cayenne/src/test/java/com/baeldung/apachecayenne/CayenneAdvancedOperationTests.java b/apache-cayenne/src/test/java/com/baeldung/apachecayenne/CayenneAdvancedOperationTests.java new file mode 100644 index 0000000000..ddfe9325d3 --- /dev/null +++ b/apache-cayenne/src/test/java/com/baeldung/apachecayenne/CayenneAdvancedOperationTests.java @@ -0,0 +1,256 @@ +package com.baeldung.apachecayenne; + +import com.baeldung.apachecayenne.persistent.Author; +import org.apache.cayenne.ObjectContext; +import org.apache.cayenne.QueryResponse; +import org.apache.cayenne.configuration.server.ServerRuntime; +import org.apache.cayenne.exp.Expression; +import org.apache.cayenne.exp.ExpressionFactory; +import org.apache.cayenne.query.*; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class CayenneAdvancedOperationTests { + private static ObjectContext context = null; + + @BeforeClass + public static void setupTheCayenneContext() { + ServerRuntime cayenneRuntime = ServerRuntime.builder() + .addConfig("cayenne-project.xml") + .build(); + context = cayenneRuntime.newContext(); + } + + @Before + public void saveThreeAuthors() { + Author authorOne = context.newObject(Author.class); + authorOne.setName("Paul Xavier"); + Author authorTwo = context.newObject(Author.class); + authorTwo.setName("pAuL Smith"); + Author authorThree = context.newObject(Author.class); + authorThree.setName("Vicky Sarra"); + context.commitChanges(); + } + + @After + public void deleteAllAuthors() { + SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author"); + context.performGenericQuery(deleteAuthors); + } + + @Test + public void givenAuthors_whenFindAllSQLTmplt_thenWeGetThreeAuthors() { + SQLTemplate select = new SQLTemplate(Author.class, "select * from Author"); + List authors = context.performQuery(select); + + assertEquals(authors.size(), 3); + } + + @Test + public void givenAuthors_whenFindByNameSQLTmplt_thenWeGetOneAuthor() { + SQLTemplate select = new SQLTemplate(Author.class, "select * from Author where name = 'Vicky Sarra'"); + List authors = context.performQuery(select); + Author author = authors.get(0); + + assertEquals(authors.size(), 1); + assertEquals(author.getName(), "Vicky Sarra"); + } + + @Test + public void givenAuthors_whenLikeSltQry_thenWeGetOneAuthor() { + Expression qualifier = ExpressionFactory.likeExp(Author.NAME.getName(), "Paul%"); + SelectQuery query = new SelectQuery(Author.class, qualifier); + List authorsTwo = context.performQuery(query); + + assertEquals(authorsTwo.size(), 1); + } + + @Test + public void givenAuthors_whenCtnsIgnorCaseSltQry_thenWeGetTwoAuthors() { + Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul"); + SelectQuery query = new SelectQuery(Author.class, qualifier); + List authors = context.performQuery(query); + + assertEquals(authors.size(), 2); + } + + @Test + public void givenAuthors_whenCtnsIgnorCaseEndsWSltQry_thenWeGetTwoAuthors() { + Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul") + .andExp(ExpressionFactory.endsWithExp(Author.NAME.getName(), "h")); + SelectQuery query = new SelectQuery(Author.class, qualifier); + List authors = context.performQuery(query); + + Author author = authors.get(0); + + assertEquals(authors.size(), 1); + assertEquals(author.getName(), "pAuL Smith"); + } + + @Test + public void givenAuthors_whenAscOrderingSltQry_thenWeGetOrderedAuthors() { + SelectQuery query = new SelectQuery(Author.class); + query.addOrdering(Author.NAME.asc()); + + List authors = query.select(context); + Author firstAuthor = authors.get(0); + + assertEquals(authors.size(), 3); + assertEquals(firstAuthor.getName(), "Paul Xavier"); + } + + @Test + public void givenAuthors_whenDescOrderingSltQry_thenWeGetOrderedAuthors() { + SelectQuery query = new SelectQuery(Author.class); + query.addOrdering(Author.NAME.desc()); + + List authors = query.select(context); + Author firstAuthor = authors.get(0); + + assertEquals(authors.size(), 3); + assertEquals(firstAuthor.getName(), "pAuL Smith"); + } + + @Test + public void givenAuthors_onContainsObjS_thenWeGetOneRecord() { + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.contains("Paul")) + .select(context); + + assertEquals(authors.size(), 1); + } + + @Test + public void givenAuthors_whenLikeObjS_thenWeGetTwoAuthors() { + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.likeIgnoreCase("Paul%")) + .select(context); + + assertEquals(authors.size(), 2); + } + + @Test + public void givenTwoAuthor_whenEndsWithObjS_thenWeGetOrderedAuthors() { + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.endsWith("Sarra")) + .select(context); + Author firstAuthor = authors.get(0); + + assertEquals(authors.size(), 1); + assertEquals(firstAuthor.getName(), "Vicky Sarra"); + } + + @Test + public void givenTwoAuthor_whenInObjS_thenWeGetAuthors() { + String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"}; + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.in(Arrays.asList(args))) + .select(context); + + assertEquals(authors.size(), 3); + } + + @Test + public void givenTwoAuthor_whenNinObjS_thenWeGetAuthors() { + String [] args = {"Paul Xavier", "pAuL Smith"}; + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.nin(Arrays.asList(args))) + .select(context); + Author author = authors.get(0); + + assertEquals(authors.size(), 1); + assertEquals(author.getName(), "Vicky Sarra"); + } + + @Test + public void givenTwoAuthor_whenIsNotNullObjS_thenWeGetAuthors() { + List authors = ObjectSelect.query(Author.class) + .where(Author.NAME.isNotNull()) + .select(context); + + assertEquals(authors.size(), 3); + } + + @Test + public void givenAuthors_whenFindAllEJBQL_thenWeGetThreeAuthors() { + EJBQLQuery query = new EJBQLQuery("select a FROM Author a"); + List authors = context.performQuery(query); + + assertEquals(authors.size(), 3); + } + + @Test + public void givenAuthors_whenFindByNameEJBQL_thenWeGetOneAuthor() { + EJBQLQuery query = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Sarra'"); + List authors = context.performQuery(query); + Author author = authors.get(0); + + assertEquals(authors.size(), 1); + assertEquals(author.getName(), "Vicky Sarra"); + } + + @Test + public void givenAuthors_whenUpdadingByNameEJBQL_thenWeGetTheUpdatedAuthor() { + EJBQLQuery query = new EJBQLQuery("UPDATE Author AS a SET a.name = 'Vicky Edison' WHERE a.name = 'Vicky Sarra'"); + QueryResponse queryResponse = context.performGenericQuery(query); + + EJBQLQuery queryUpdatedAuthor = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Edison'"); + List authors = context.performQuery(queryUpdatedAuthor); + Author author = authors.get(0); + + assertNotNull(author); + } + + @Test + public void givenAuthors_whenSeletingNamesEJBQL_thenWeGetListWithSizeThree() { + String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"}; + List names = Arrays.asList(args); + EJBQLQuery query = new EJBQLQuery("select a.name FROM Author a"); + List nameList = context.performQuery(query); + + Collections.sort(names); + Collections.sort(nameList); + + assertEquals(names.size(), 3); + assertEquals(nameList.size(), 3); + assertEquals(names, nameList); + } + + @Test + public void givenAuthors_whenDeletingAllWithEJB_thenWeGetNoAuthor() { + EJBQLQuery deleteQuery = new EJBQLQuery("delete FROM Author"); + EJBQLQuery findAllQuery = new EJBQLQuery("select a FROM Author a"); + + context.performQuery(deleteQuery); + List objects = context.performQuery(findAllQuery); + + assertEquals(objects.size(), 0); + } + + @Test + public void givenAuthors_whenInsertingSQLExec_thenWeGetNewAuthor() { + int inserted = SQLExec + .query("INSERT INTO Author (name) VALUES ('Baeldung')") + .update(context); + + assertEquals(inserted, 1); + } + + @Test + public void givenAuthors_whenUpdatingSQLExec_thenItsUpdated() { + int updated = SQLExec + .query("UPDATE Author SET name = 'Baeldung' WHERE name = 'Vicky Sarra'") + .update(context); + + assertEquals(updated, 1); + } +}