From 4331871e09966b5521600c9076617c7ad465b4b9 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Tue, 2 Jan 2018 15:21:09 +0100 Subject: [PATCH 001/102] BAEL-592 Guide to Hibernate 5 with Spring --- .../hibernate/bootstrap/BarHibernateDAO.java | 39 ++++++++++++ .../hibernate/bootstrap/HibernateConf.java | 61 +++++++++++++++++++ .../hibernate/bootstrap/HibernateXMLConf.java | 24 ++++++++ .../hibernate/bootstrap/model/TestEntity.java | 29 +++++++++ .../resources/hibernate5Configuration.xml | 30 +++++++++ .../main/resources/persistence-h2.properties | 1 + .../HibernateBootstrapIntegrationTest.java | 38 ++++++++++++ .../HibernateXMLBootstrapIntegrationTest.java | 36 +++++++++++ 8 files changed, 258 insertions(+) create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/BarHibernateDAO.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateConf.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateXMLConf.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/model/TestEntity.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/resources/hibernate5Configuration.xml create mode 100644 persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java create mode 100644 persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateXMLBootstrapIntegrationTest.java diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/BarHibernateDAO.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/BarHibernateDAO.java new file mode 100644 index 0000000000..5fc932b256 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/BarHibernateDAO.java @@ -0,0 +1,39 @@ +package com.baeldung.hibernate.bootstrap; + +import com.baeldung.hibernate.bootstrap.model.TestEntity; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public abstract class BarHibernateDAO { + + @Autowired + private SessionFactory sessionFactory; + + public TestEntity findEntity(int id) { + + return getCurrentSession().find(TestEntity.class, 1); + } + + public void createEntity(TestEntity entity) { + + getCurrentSession().save(entity); + } + + public void createEntity(int id, String newDescription) { + + TestEntity entity = findEntity(id); + entity.setDescription(newDescription); + getCurrentSession().save(entity); + } + + public void deleteEntity(int id) { + + TestEntity entity = findEntity(id); + getCurrentSession().delete(entity); + } + + protected Session getCurrentSession() { + return sessionFactory.getCurrentSession(); + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateConf.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateConf.java new file mode 100644 index 0000000000..150e3778af --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateConf.java @@ -0,0 +1,61 @@ +package com.baeldung.hibernate.bootstrap; + +import com.google.common.base.Preconditions; +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.util.Properties; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-h2.properties" }) +public class HibernateConf { + + @Autowired + private Environment env; + + @Bean + public LocalSessionFactoryBean sessionFactory() { + final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); + sessionFactory.setDataSource(dataSource()); + sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.bootstrap.model" }); + sessionFactory.setHibernateProperties(hibernateProperties()); + + return sessionFactory; + } + + @Bean + public DataSource dataSource() { + final BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); + dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); + + return dataSource; + } + + @Bean + public PlatformTransactionManager hibernateTransactionManager() { + final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); + transactionManager.setSessionFactory(sessionFactory().getObject()); + return transactionManager; + } + + private final Properties hibernateProperties() { + final Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + + return hibernateProperties; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateXMLConf.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateXMLConf.java new file mode 100644 index 0000000000..b3e979478f --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/HibernateXMLConf.java @@ -0,0 +1,24 @@ +package com.baeldung.hibernate.bootstrap; + +import com.google.common.base.Preconditions; +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.util.Properties; + +@Configuration +@EnableTransactionManagement +@ImportResource({ "classpath:hibernate5Configuration.xml" }) +public class HibernateXMLConf { + +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/model/TestEntity.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/model/TestEntity.java new file mode 100644 index 0000000000..cae41db831 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/bootstrap/model/TestEntity.java @@ -0,0 +1,29 @@ +package com.baeldung.hibernate.bootstrap.model; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class TestEntity { + + private int id; + + private String description; + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/resources/hibernate5Configuration.xml b/persistence-modules/spring-hibernate-5/src/main/resources/hibernate5Configuration.xml new file mode 100644 index 0000000000..cb6cf0aa5c --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/resources/hibernate5Configuration.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + ${hibernate.hbm2ddl.auto} + ${hibernate.dialect} + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-hibernate-5/src/main/resources/persistence-h2.properties b/persistence-modules/spring-hibernate-5/src/main/resources/persistence-h2.properties index 915bc4317b..0325174b67 100644 --- a/persistence-modules/spring-hibernate-5/src/main/resources/persistence-h2.properties +++ b/persistence-modules/spring-hibernate-5/src/main/resources/persistence-h2.properties @@ -2,6 +2,7 @@ jdbc.driverClassName=org.h2.Driver jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 jdbc.eventGeneratedId=sa +jdbc.user=sa jdbc.pass=sa # hibernate.X diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java new file mode 100644 index 0000000000..ffe82b7ced --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.hibernate.bootstrap; + +import com.baeldung.hibernate.bootstrap.model.TestEntity; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { HibernateConf.class }) +@Transactional +public class HibernateBootstrapIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + @Test + public void whenBootstrapHibernateSession_thenNoException() { + + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + } + +} diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateXMLBootstrapIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateXMLBootstrapIntegrationTest.java new file mode 100644 index 0000000000..5b811ad576 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateXMLBootstrapIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.hibernate.bootstrap; + +import com.baeldung.hibernate.bootstrap.model.TestEntity; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { HibernateXMLConf.class }) +@Transactional +public class HibernateXMLBootstrapIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + @Test + public void whenBootstrapHibernateSession_thenNoException() { + + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + } + +} From 02b0c1d2933ee636241ae1446773af770f68dd13 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Thu, 4 Jan 2018 10:26:48 +0100 Subject: [PATCH 002/102] BAEL-592: Upgrade to Spring 5 --- persistence-modules/spring-hibernate-5/pom.xml | 2 +- .../com/baeldung/manytomany/spring/PersistenceConfig.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/persistence-modules/spring-hibernate-5/pom.xml b/persistence-modules/spring-hibernate-5/pom.xml index 88db38b2fc..86e952c0e4 100644 --- a/persistence-modules/spring-hibernate-5/pom.xml +++ b/persistence-modules/spring-hibernate-5/pom.xml @@ -179,7 +179,7 @@ - 4.3.10.RELEASE + 5.0.2.RELEASE 1.10.6.RELEASE diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/manytomany/spring/PersistenceConfig.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/manytomany/spring/PersistenceConfig.java index 6f359054b6..5dace1f742 100644 --- a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/manytomany/spring/PersistenceConfig.java +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/manytomany/spring/PersistenceConfig.java @@ -10,8 +10,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.orm.hibernate4.HibernateTransactionManager; -import org.springframework.orm.hibernate4.LocalSessionFactoryBean; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; From 369ec4e181bee2df90e96b4edac5b26a6c6cc04b Mon Sep 17 00:00:00 2001 From: Adi Date: Fri, 19 Jan 2018 00:06:57 +0200 Subject: [PATCH 003/102] BAEL-1267: programmatically create, configure and run a tomcat server --- libraries/pom.xml | 33 ++++++++++ .../java/com/baeldung/tomcat/MyFilter.java | 31 +++++++++ .../java/com/baeldung/tomcat/MyServlet.java | 25 ++++++++ .../baeldung/tomcat/ProgrammaticTomcat.java | 63 +++++++++++++++++++ .../tomcat/ProgrammaticTomcatTest.java | 62 ++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/tomcat/MyFilter.java create mode 100644 libraries/src/main/java/com/baeldung/tomcat/MyServlet.java create mode 100644 libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java create mode 100644 libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 09c8cb8335..404c8fa397 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -705,6 +705,38 @@ test test + + + + org.apache.tomcat + tomcat-catalina + ${tomcat.version} + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + + + org.apache.tomcat.embed + tomcat-embed-jasper + ${tomcat.version} + + + org.apache.tomcat + tomcat-jasper + ${tomcat.version} + + + org.apache.tomcat + tomcat-jasper-el + ${tomcat.version} + + + org.apache.tomcat + tomcat-jsp-api + ${tomcat.version} + @@ -786,5 +818,6 @@ v4-rev493-1.21.0 1.0.0 3.0.14 + 8.5.24 \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java b/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java new file mode 100644 index 0000000000..9cf4a0ed95 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/MyFilter.java @@ -0,0 +1,31 @@ +package com.baeldung.tomcat; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by adi on 1/14/18. + */ +@WebFilter(urlPatterns = "/my-servlet/*") +public class MyFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + System.out.println("Filtering stuff..."); + HttpServletResponse httpResponse = (HttpServletResponse) response; + httpResponse.addHeader("myHeader", "myHeaderValue"); + chain.doFilter(request, httpResponse); + } + + @Override + public void destroy() { + + } +} diff --git a/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java b/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java new file mode 100644 index 0000000000..4bbf3c03a7 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/MyServlet.java @@ -0,0 +1,25 @@ +package com.baeldung.tomcat; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by adi on 1/10/18. + */ +@WebServlet( + name = "com.baeldung.tomcat.programmatic.MyServlet", + urlPatterns = {"/my-servlet"} +) +public class MyServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + resp.setStatus(HttpServletResponse.SC_OK); + resp.getWriter().write("test"); + resp.getWriter().flush(); + resp.getWriter().close(); + } +} diff --git a/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java b/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java new file mode 100644 index 0000000000..b84b6b5c6d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/tomcat/ProgrammaticTomcat.java @@ -0,0 +1,63 @@ +package com.baeldung.tomcat; + +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.startup.Tomcat; +import org.apache.tomcat.util.descriptor.web.FilterDef; +import org.apache.tomcat.util.descriptor.web.FilterMap; + +import java.io.File; + +/** + * Created by adi on 1/10/18. + */ +public class ProgrammaticTomcat { + + private Tomcat tomcat = null; + + //uncomment for live test + // public static void main(String[] args) throws LifecycleException, ServletException, URISyntaxException, IOException { + // startTomcat(); + // } + + public void startTomcat() throws LifecycleException { + tomcat = new Tomcat(); + tomcat.setPort(8080); + tomcat.setHostname("localhost"); + String appBase = "."; + tomcat + .getHost() + .setAppBase(appBase); + + File docBase = new File(System.getProperty("java.io.tmpdir")); + Context context = tomcat.addContext("", docBase.getAbsolutePath()); + + //add a servlet + Class servletClass = MyServlet.class; + Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName()); + context.addServletMappingDecoded("/my-servlet/*", servletClass.getSimpleName()); + + //add a filter and filterMapping + Class filterClass = MyFilter.class; + FilterDef myFilterDef = new FilterDef(); + myFilterDef.setFilterClass(filterClass.getName()); + myFilterDef.setFilterName(filterClass.getSimpleName()); + context.addFilterDef(myFilterDef); + + FilterMap myFilterMap = new FilterMap(); + myFilterMap.setFilterName(filterClass.getSimpleName()); + myFilterMap.addURLPattern("/my-servlet/*"); + context.addFilterMap(myFilterMap); + + tomcat.start(); + //uncomment for live test + // tomcat + // .getServer() + // .await(); + } + + public void stopTomcat() throws LifecycleException { + tomcat.stop(); + tomcat.destroy(); + } +} diff --git a/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatTest.java b/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatTest.java new file mode 100644 index 0000000000..d559c3d408 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/tomcat/ProgrammaticTomcatTest.java @@ -0,0 +1,62 @@ +package com.baeldung.tomcat; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Created by adi on 1/14/18. + */ +@RunWith(BlockJUnit4ClassRunner.class) +public class ProgrammaticTomcatTest { + + private ProgrammaticTomcat tomcat = new ProgrammaticTomcat(); + + @Before + public void setUp() throws Exception { + tomcat.startTomcat(); + } + + @After + public void tearDown() throws Exception { + tomcat.stopTomcat(); + } + + @Test + public void givenTomcatStarted_whenAccessServlet_responseIsTestAndResponseHeaderIsSet() throws Exception { + CloseableHttpClient httpClient = HttpClientBuilder + .create() + .build(); + HttpGet getServlet = new HttpGet("http://localhost:8080/my-servlet"); + + HttpResponse response = httpClient.execute(getServlet); + assertEquals(HttpStatus.SC_OK, response + .getStatusLine() + .getStatusCode()); + + String myHeaderValue = response + .getFirstHeader("myHeader") + .getValue(); + assertEquals("myHeaderValue", myHeaderValue); + + HttpEntity responseEntity = response.getEntity(); + assertNotNull(responseEntity); + + String responseString = EntityUtils.toString(responseEntity, "UTF-8"); + assertEquals("test", responseString); + } + +} From 3f3204e14f9e13dcf174c12894ae53d34ae1dac1 Mon Sep 17 00:00:00 2001 From: Adi Date: Fri, 26 Jan 2018 21:18:17 +0200 Subject: [PATCH 004/102] BAEL-1267: programmatically create, configure and run a tomcat server - remove unused dependencies --- libraries/pom.xml | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/libraries/pom.xml b/libraries/pom.xml index 404c8fa397..6c0e6c2577 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -712,31 +712,6 @@ tomcat-catalina ${tomcat.version} - - org.apache.tomcat.embed - tomcat-embed-core - ${tomcat.version} - - - org.apache.tomcat.embed - tomcat-embed-jasper - ${tomcat.version} - - - org.apache.tomcat - tomcat-jasper - ${tomcat.version} - - - org.apache.tomcat - tomcat-jasper-el - ${tomcat.version} - - - org.apache.tomcat - tomcat-jsp-api - ${tomcat.version} - From 955a2e7e18c895d1b2580fcc9ef3d955c5d435a1 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 27 Jan 2018 06:52:34 +0100 Subject: [PATCH 005/102] Reactive exception handling --- .../websocket/ReactiveWebSocketHandler.java | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java index 669c212fd3..2e93c0c0dc 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java @@ -1,48 +1,41 @@ package com.baeldung.reactive.websocket; -import org.springframework.web.reactive.socket.WebSocketSession; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; - import org.springframework.stereotype.Component; import org.springframework.web.reactive.socket.WebSocketHandler; import org.springframework.web.reactive.socket.WebSocketMessage; - +import org.springframework.web.reactive.socket.WebSocketSession; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.time.Duration; -import java.time.LocalDateTime; -import java.util.UUID; + +import static java.time.LocalDateTime.now; +import static java.util.UUID.randomUUID; @Component public class ReactiveWebSocketHandler implements WebSocketHandler { - private Flux eventFlux = Flux.generate(e -> { - Event event = new Event(UUID.randomUUID().toString(), LocalDateTime.now().toString()); - e.next(event); + private static final ObjectMapper json = new ObjectMapper(); + + private Flux eventFlux = Flux.generate(sink -> { + Event event = new Event(randomUUID().toString(), now().toString()); + try { + sink.next(json.writeValueAsString(event)); + } catch (JsonProcessingException e) { + sink.error(e); + } }); - private Flux intervalFlux = Flux.interval(Duration.ofMillis(1000L)).zipWith(eventFlux, (time, event) -> event); - - private ObjectMapper json = new ObjectMapper(); + private Flux intervalFlux = Flux.interval(Duration.ofMillis(1000L)) + .zipWith(eventFlux, (time, event) -> event); @Override public Mono handle(WebSocketSession webSocketSession) { - - return webSocketSession.send(intervalFlux.map(event -> { - try { - String jsonEvent = json.writeValueAsString(event); - System.out.println(jsonEvent); - return jsonEvent; - } catch (JsonProcessingException e) { - e.printStackTrace(); - return ""; - } - }).map(webSocketSession::textMessage)) - - .and(webSocketSession.receive().map(WebSocketMessage::getPayloadAsText).log()); + return webSocketSession.send(intervalFlux + .map(webSocketSession::textMessage)) + .and(webSocketSession.receive() + .map(WebSocketMessage::getPayloadAsText).log()); } - } From 81e8da29a402f0cb8fd1c362ce249271b1e94f5d Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Sat, 27 Jan 2018 11:14:14 +0100 Subject: [PATCH 006/102] BAEL-1335 --- javaxval/pom.xml | 15 +- .../MethodValidationConfig.java | 38 ++++ .../ConsistentDateParameterValidator.java | 32 +++ .../constraints/ConsistentDateParameters.java | 23 ++ .../constraints/ValidReservation.java | 24 +++ .../ValidReservationValidator.java | 40 ++++ .../methodvalidation/model/Customer.java | 41 ++++ .../methodvalidation/model/Reservation.java | 60 ++++++ .../model/ReservationManagement.java | 50 +++++ .../ContainerValidationIntegrationTest.java | 86 ++++++++ .../ValidationIntegrationTest.java | 199 ++++++++++++++++++ 11 files changed, 606 insertions(+), 2 deletions(-) create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java create mode 100644 javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java create mode 100644 javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java create mode 100644 javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 6a83a25f01..0891fe6959 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -6,10 +6,11 @@ 0.1-SNAPSHOT - 2.0.0.Final - 6.0.2.Final + 2.0.1.Final + 6.0.7.Final 3.0.0 2.2.6 + 5.0.2.RELEASE @@ -50,6 +51,16 @@ javax.el ${javax.el.version} + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-test + ${org.springframework.version} + diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java new file mode 100644 index 0000000000..206a145337 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/MethodValidationConfig.java @@ -0,0 +1,38 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; + +import java.time.LocalDate; + +@Configuration +@ComponentScan({ "org.baeldung.javaxval.methodvalidation.model" }) +public class MethodValidationConfig { + + @Bean + public MethodValidationPostProcessor methodValidationPostProcessor() { + return new MethodValidationPostProcessor(); + } + + @Bean("customer") + @Scope(BeanDefinition.SCOPE_PROTOTYPE) + public Customer customer(String firstName, String lastName) { + + Customer customer = new Customer(firstName, lastName); + return customer; + } + + @Bean("reservation") + @Scope(BeanDefinition.SCOPE_PROTOTYPE) + public Reservation reservation(LocalDate begin, LocalDate end, Customer customer, int room) { + + Reservation reservation = new Reservation(begin, end, customer, room); + return reservation; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java new file mode 100644 index 0000000000..b28abcba45 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java @@ -0,0 +1,32 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.constraintvalidation.SupportedValidationTarget; +import javax.validation.constraintvalidation.ValidationTarget; +import java.time.LocalDate; + +@SupportedValidationTarget(ValidationTarget.PARAMETERS) +public class ConsistentDateParameterValidator implements ConstraintValidator { + + @Override + public void initialize(ConsistentDateParameters constraintAnnotation) { + } + + @Override + public boolean isValid(Object[] value, ConstraintValidatorContext context) { + if (value.length != 4 && value.length != 3) { + throw new IllegalArgumentException("Illegal method signature"); + } + + if (value[0] == null || value[1] == null) { + return false; + } + + if (!(value[0] instanceof LocalDate) || !(value[1] instanceof LocalDate)) { + throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate."); + } + + return ((LocalDate) value[0]).isBefore((LocalDate) value[1]); + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java new file mode 100644 index 0000000000..6b321f545c --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameters.java @@ -0,0 +1,23 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Constraint(validatedBy = ConsistentDateParameterValidator.class) +@Target({ METHOD, CONSTRUCTOR }) +@Retention(RUNTIME) +@Documented +public @interface ConsistentDateParameters { + + String message() default "End date must be after begin date and both must be in the future"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java new file mode 100644 index 0000000000..f9cdea1483 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservation.java @@ -0,0 +1,24 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Constraint(validatedBy = ValidReservationValidator.class) +@Target({ METHOD, CONSTRUCTOR }) +@Retention(RUNTIME) +@Documented +public @interface ValidReservation { + + String message() default "End date must be after begin date and both must be in the future, room number must be bigger than 0"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java new file mode 100644 index 0000000000..1da257044b --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java @@ -0,0 +1,40 @@ +package org.baeldung.javaxval.methodvalidation.constraints; + +import org.baeldung.javaxval.methodvalidation.model.Reservation; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.time.LocalDate; + +public class ValidReservationValidator implements ConstraintValidator { + + @Override + public void initialize(ValidReservation constraintAnnotation) { + } + + @Override + public boolean isValid(Reservation reservation, ConstraintValidatorContext context) { + + if (reservation == null) { + return true; + } + + if (!(reservation instanceof Reservation)) { + throw new IllegalArgumentException("Illegal method signature, expected parameter of type Reservation."); + } + + if (reservation.getBegin() == null || reservation.getEnd() == null || reservation.getCustomer() == null) { + return false; + } + + if (reservation.getBegin() + .isAfter(LocalDate.now()) + && reservation.getBegin() + .isBefore(reservation.getEnd()) + && reservation.getRoom() > 0) { + + return true; + } + return false; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java new file mode 100644 index 0000000000..529cf436da --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java @@ -0,0 +1,41 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +@Validated +public class Customer { + + @Size(min = 5, max = 200) + private String firstName; + + @Size(min = 5, max = 200) + private String lastName; + + public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public Customer() { + + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java new file mode 100644 index 0000000000..89f13e9e37 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Reservation.java @@ -0,0 +1,60 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters; +import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation; +import org.springframework.validation.annotation.Validated; + +import java.time.LocalDate; + +@Validated +public class Reservation { + + private LocalDate begin; + + private LocalDate end; + + private Customer customer; + + private int room; + + @ConsistentDateParameters + @ValidReservation + public Reservation(LocalDate begin, LocalDate end, Customer customer, int room) { + this.begin = begin; + this.end = end; + this.customer = customer; + this.room = room; + } + + public LocalDate getBegin() { + return begin; + } + + public void setBegin(LocalDate begin) { + this.begin = begin; + } + + public LocalDate getEnd() { + return end; + } + + public void setEnd(LocalDate end) { + this.end = end; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public int getRoom() { + return room; + } + + public void setRoom(int room) { + this.room = room; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java new file mode 100644 index 0000000000..0aeeb52e1f --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/ReservationManagement.java @@ -0,0 +1,50 @@ +package org.baeldung.javaxval.methodvalidation.model; + +import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters; +import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.validation.annotation.Validated; + +import javax.validation.Valid; +import javax.validation.constraints.*; +import java.time.LocalDate; +import java.util.List; + +@Controller +@Validated +public class ReservationManagement { + + @Autowired + private ApplicationContext applicationContext; + + @ConsistentDateParameters + public void createReservation(LocalDate begin, LocalDate end, @NotNull Customer customer) { + + // ... + } + + public void createReservation(@NotNull @Future LocalDate begin, @Min(1) int duration, @NotNull Customer customer) { + + // ... + } + + @NotNull + @Size(min = 1) + public List<@NotNull Customer> getAllCustomers() { + + return null; + } + + public void createNewCustomer(@Valid Customer customer) { + + // ... + } + + @Valid + public Customer getCustomerById() { + + return null; + } +} diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java new file mode 100644 index 0000000000..53133edd48 --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java @@ -0,0 +1,86 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import javax.validation.ConstraintViolationException; +import java.time.LocalDate; +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { MethodValidationConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ContainerValidationIntegrationTest { + + @Autowired + ReservationManagement reservationManagement; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void whenValidationWithInvalidMethodParameters_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + reservationManagement.createReservation(LocalDate.now(), 0, null); + } + + @Test + public void whenValidationWithValidMethodParameters_thenNoException() { + + reservationManagement.createReservation(LocalDate.now() + .plusDays(1), 1, new Customer("William", "Smith")); + } + + @Test + public void whenCrossParameterValidationWithInvalidParameters_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + reservationManagement.createReservation(LocalDate.now(), LocalDate.now(), null); + } + + @Test + public void whenCrossParameterValidationWithValidParameters_thenNoException() { + + reservationManagement.createReservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith")); + } + + @Test + public void whenValidationWithInvalidReturnValue_thenConstraintViolationException() { + + exception.expect(ConstraintViolationException.class); + List list = reservationManagement.getAllCustomers(); + } + + @Test + public void whenValidationWithInvalidCascadedValue_thenConstraintViolationException() { + + Customer customer = new Customer(); + customer.setFirstName("John"); + customer.setLastName("Doe"); + + exception.expect(ConstraintViolationException.class); + reservationManagement.createNewCustomer(customer); + } + + @Test + public void whenValidationWithValidCascadedValue_thenCNoException() { + + Customer customer = new Customer(); + customer.setFirstName("William"); + customer.setLastName("Smith"); + + reservationManagement.createNewCustomer(customer); + } +} diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java new file mode 100644 index 0000000000..6a750698c6 --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java @@ -0,0 +1,199 @@ +package org.baeldung.javaxval.methodvalidation; + +import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; +import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.executable.ExecutableValidator; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.time.LocalDate; +import java.util.Collections; +import java.util.Set; + +public class ValidationIntegrationTest { + + private ExecutableValidator executableValidator; + + @Before + public void getExecutableValidator() { + + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + this.executableValidator = factory.getValidator() + .forExecutables(); + } + + @Test + public void whenValidationWithInvalidMethodParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, int.class, Customer.class); + Object[] parameterValues = { LocalDate.now(), 0, null }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(3, violations.size()); + } + + @Test + public void whenValidationWithValidMethodParameters_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, int.class, Customer.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), 1, new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithInvalidParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, LocalDate.class, Customer.class); + Object[] parameterValues = { LocalDate.now(), LocalDate.now(), new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(1, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithValidParameters_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createReservation", LocalDate.class, LocalDate.class, Customer.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("John", "Doe") }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidConstructorParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Customer.class.getConstructor(String.class, String.class); + Object[] parameterValues = { "John", "Doe" }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(2, violations.size()); + } + + @Test + public void whenValidationWithValidConstructorParameters_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Customer.class.getConstructor(String.class, String.class); + Object[] parameterValues = { "William", "Smith" }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithInvalidConstructorParameters_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Object[] parameterValues = { LocalDate.now(), LocalDate.now(), new Customer("William", "Smith"), 1 }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(1, violations.size()); + } + + @Test + public void whenCrossParameterValidationWithValidConstructorParameters_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Object[] parameterValues = { LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith"), 1 }; + Set> violations = executableValidator.validateConstructorParameters(constructor, parameterValues); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidReturnValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("getAllCustomers"); + Object returnValue = Collections. emptyList(); + Set> violations = executableValidator.validateReturnValue(object, method, returnValue); + + assertEquals(1, violations.size()); + } + + @Test + public void whenValidationWithValidReturnValue_thenZeroVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("getAllCustomers"); + Object returnValue = Collections.singletonList(new Customer("William", "Smith")); + Set> violations = executableValidator.validateReturnValue(object, method, returnValue); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidConstructorReturnValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Reservation createdObject = new Reservation(LocalDate.now(), LocalDate.now(), new Customer("William", "Smith"), 0); + Set> violations = executableValidator.validateConstructorReturnValue(constructor, createdObject); + + assertEquals(1, violations.size()); + } + + @Test + public void whenValidationWithValidConstructorReturnValue_thenZeroVoilations() throws NoSuchMethodException { + + Constructor constructor = Reservation.class.getConstructor(LocalDate.class, LocalDate.class, Customer.class, int.class); + Reservation createdObject = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + new Customer("William", "Smith"), 1); + Set> violations = executableValidator.validateConstructorReturnValue(constructor, createdObject); + + assertEquals(0, violations.size()); + } + + @Test + public void whenValidationWithInvalidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); + Customer customer = new Customer(); + customer.setFirstName("John"); + customer.setLastName("Doe"); + Object[] parameterValues = { customer }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(2, violations.size()); + } + + @Test + public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { + + ReservationManagement object = new ReservationManagement(); + Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); + Customer customer = new Customer(); + customer.setFirstName("William"); + customer.setLastName("Smith"); + Object[] parameterValues = { customer }; + Set> violations = executableValidator.validateParameters(object, method, parameterValues); + + assertEquals(0, violations.size()); + } + +} From 0d85d1ad01015f719a7f7b5f65ab731925af4886 Mon Sep 17 00:00:00 2001 From: Adam InTae Gerard Date: Sun, 28 Jan 2018 05:11:10 -0800 Subject: [PATCH 007/102] BAEL-1175 - corrected directory cmd (#3534) --- spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh | 2 +- spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh b/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh index 577a25dd6e..f1c6bfcf3f 100644 --- a/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh +++ b/spring-cloud/spring-cloud-stream-starters/hdfs/hdfs.sh @@ -6,6 +6,6 @@ git clone https://github.com/spring-cloud-stream-app-starters/hdfs.git ./mvnw clean install -PgenerateApps # Run it -cd apps +cd target # Optionally inject application.properties prior to build java -jar hdfs-sink.jar --fsUri=hdfs://127.0.0.1:50010/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh b/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh index 4c76fe637b..967cb54dfe 100644 --- a/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh +++ b/spring-cloud/spring-cloud-stream-starters/twitter/twitter.sh @@ -6,7 +6,7 @@ git clone https://github.com/spring-cloud-stream-app-starters/twitter.git ./mvnw clean install -PgenerateApps # Run it -cd apps +cd target # Optionally inject application.properties prior to build java -jar twitter_stream_source.jar --consumerKey= --consumerSecret= \ --accessToken= --accessTokenSecret= \ No newline at end of file From 4d8f0a48ae285bd681758f5da5ab785fa91b6fa1 Mon Sep 17 00:00:00 2001 From: k0l0ssus Date: Sun, 28 Jan 2018 18:06:11 -0500 Subject: [PATCH 008/102] Add Java vs Vavr Stream sample --- .../samples/java/vavr/VavrSampler.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java diff --git a/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java new file mode 100644 index 0000000000..ae06c97e2e --- /dev/null +++ b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java @@ -0,0 +1,99 @@ +package com.baeldung.samples.java.vavr; + +import io.vavr.collection.Stream; +import java.util.ArrayList; +import java.util.List; + + +/** + * + * @author baeldung + */ +public class VavrSampler { + + static int[] intArray = new int[]{1, 2, 4}; + static List intList = new ArrayList(); + static int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + + public static void main(String[] args) { + vavrStreamElementAccess(); + System.out.println("===================================="); + vavrParallelStreamAccess(); + System.out.println("===================================="); + vavrFlatMapping(); + System.out.println("===================================="); + vavrStreamManipulation(); + System.out.println("===================================="); + vavrStreamDistinct(); + } + + public static void vavrStreamElementAccess() { + System.out.println("Vavr Element Access"); + System.out.println("===================================="); + Stream vavredStream = Stream.ofAll(intArray); + System.out.println("Vavr index access: " + vavredStream.get(2)); + System.out.println("Vavr head element access: " + vavredStream.get()); + + Stream vavredStringStream = Stream.of("foo", "bar", "baz"); + System.out.println("Find foo " + vavredStringStream.indexOf("foo")); + } + + public static void vavrParallelStreamAccess() { + + System.out.println("Vavr Stream Concurrent Modification"); + System.out.println("===================================="); + Stream vavrStream = Stream.ofAll(intList); + //intList.add(5); + vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i)); + +// Stream wrapped = Stream.ofAll(intArray); +// intArray[2] = 5; +// wrapped.forEach(i -> System.out.println("Vavr looped " + i)); + } + + public static void jdkFlatMapping() { + System.out.println("Java flatMapping"); + System.out.println("===================================="); + java.util.stream.Stream.of(42).flatMap(i -> java.util.stream.Stream.generate(() -> { + System.out.println("nested call"); + return 42; + })).findAny(); + } + + public static void vavrFlatMapping() { + System.out.println("Vavr flatMapping"); + System.out.println("===================================="); + Stream.of(42) + .flatMap(i -> Stream.continually(() -> { + System.out.println("nested call"); + return 42; + })) + .get(0); + } + + public static void vavrStreamManipulation() { + System.out.println("Vavr Stream Manipulation"); + System.out.println("===================================="); + List stringList = new ArrayList<>(); + stringList.add("foo"); + stringList.add("bar"); + stringList.add("baz"); + Stream vavredStream = Stream.ofAll(stringList); + vavredStream.forEach(item -> System.out.println("Vavr Stream item: " + item)); + Stream vavredStream2 = vavredStream.insert(2, "buzz"); + vavredStream2.forEach(item -> System.out.println("Vavr Stream item after stream addition: " + item)); + stringList.forEach(item -> System.out.println("List item after stream addition: " + item)); + Stream deletionStream = vavredStream.remove("bar"); + deletionStream.forEach(item -> System.out.println("Vavr Stream item after stream item deletion: " + item)); + + } + + public static void vavrStreamDistinct() { + Stream vavredStream = Stream.of("foo", "bar", "baz", "buxx", "bar", "bar", "foo"); + Stream distinctVavrStream = vavredStream.distinctBy((y, z) -> { + return y.compareTo(z); + }); + distinctVavrStream.forEach(item -> System.out.println("Vavr Stream item after distinct query " + item)); + + } +} From f888a3f78accaa6588f36528b8eee5a8c27772bc Mon Sep 17 00:00:00 2001 From: Bogdan Stoean <4540392+BogdanStoean@users.noreply.github.com> Date: Mon, 29 Jan 2018 07:44:40 +0200 Subject: [PATCH 009/102] BAEL-1410 - refactor tests (#3525) * initial setup with spring boot/ spring data jpa/ flyway * BAEL-1315 - added flyway test extensions for spring * BAEL-1315 - added flyway test extensions for spring * BAEL-1315 - created multiple migration scripts and locations * BAEL-1315 - test insert after schema creation * cleanup * BAEL-1315 - test data changes by a migration * [BAEL-1410] Spring Boot Security Auto-Configuration * [BAEL-1410] Added some tests for incorrect credentials use case * [BAEL-1410] Added readme and some code improvements * [BAEL-1410] removed form based auth config because is redundant added oauth2 server auto-configuration sample with test * [BAEL-1410] added custom Authorization Server Config * [BAEL-1410] update README * [BAEL-1410]refactor tests * [BAEL-1410]oauth2 resource server * [BAEL-1410]oauth2 sso sample with facebook * [BAEL-1410]remove spring-flyway * [BAEL-1410]refactor tests * [BAEL-1410] refactor tests * [BAEL-1410] update --- ...BasicAuthConfigurationIntegrationTest.java | 2 + ...figAuthorizationServerIntegrationTest.java | 55 ++++++------------- ...figAuthorizationServerIntegrationTest.java | 26 +++------ .../OAuth2IntegrationTestSupport.java | 34 ++++++++++++ 4 files changed, 59 insertions(+), 58 deletions(-) create mode 100644 spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java index 4e4244abb7..32c3fbdef4 100644 --- a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java @@ -37,6 +37,7 @@ public class BasicAuthConfigurationIntegrationTest { @Test public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException { ResponseEntity response = restTemplate.getForEntity(base.toString(), String.class); + assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response .getBody() @@ -47,6 +48,7 @@ public class BasicAuthConfigurationIntegrationTest { public void whenUserWithWrongCredentialsRequestsHomePage_ThenUnauthorizedPage() throws IllegalStateException, IOException { restTemplate = new TestRestTemplate("user", "wrongpassword"); ResponseEntity response = restTemplate.getForEntity(base.toString(), String.class); + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertTrue(response .getBody() diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java index 09df9ce645..cc953eef5a 100644 --- a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java @@ -2,10 +2,7 @@ package com.baeldung.springbootsecurity.oauth2server; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException; import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; @@ -13,7 +10,6 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import static java.lang.String.format; import static java.util.Collections.singletonList; import static org.junit.Assert.assertNotNull; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @@ -21,54 +17,35 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class) @ActiveProfiles("authz") -public class CustomConfigAuthorizationServerIntegrationTest { - - @Value("${local.server.port}") protected int port; +public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport { @Test - public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { - ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails(); - resourceDetails.setClientId("baeldung"); - resourceDetails.setClientSecret("baeldung"); - resourceDetails.setScope(singletonList("read")); - DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); - OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); - restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); + public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + assertNotNull(accessToken); } @Test(expected = OAuth2AccessDeniedException.class) - public void whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() { - ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails(); - resourceDetails.setClientId("baeldung"); - resourceDetails.setClientSecret("baeldung"); - resourceDetails.setScope(singletonList("write")); - DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); - OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); - restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); + public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + restTemplate.getAccessToken(); } @Test - public void whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() { - ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails(); - resourceDetails.setClientId("baeldung-admin"); - resourceDetails.setClientSecret("baeldung"); - resourceDetails.setScope(singletonList("write")); - DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); - OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); - restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); - OAuth2AccessToken accessToken = restTemplate.getAccessToken(); - assertNotNull(accessToken); - } + public void givenOAuth2Context_whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung-admin", singletonList("write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); - private ClientCredentialsResourceDetails getClientCredentialsResourceDetails() { - ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails(); - resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port)); - resourceDetails.setGrantType("client_credentials"); - return resourceDetails; + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + + assertNotNull(accessToken); } } diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java index c7b1b4ef6c..4d7b449380 100644 --- a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java @@ -2,40 +2,28 @@ package com.baeldung.springbootsecurity.oauth2server; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.test.context.junit4.SpringRunner; -import static java.lang.String.format; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.junit.Assert.assertNotNull; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class, - properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=secret" }) -public class DefaultConfigAuthorizationServerIntegrationTest { - - @Value("${local.server.port}") protected int port; + properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=baeldung" }) +public class DefaultConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport { @Test - public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { - ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails(); - resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port)); - resourceDetails.setClientId("client"); - resourceDetails.setClientSecret("secret"); - resourceDetails.setGrantType("client_credentials"); - resourceDetails.setScope(asList("read", "write")); - DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); - OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); - restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); + public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() { + ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("client", asList("read", "write")); + OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails); + OAuth2AccessToken accessToken = restTemplate.getAccessToken(); + assertNotNull(accessToken); } diff --git a/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java new file mode 100644 index 0000000000..3eef206c7d --- /dev/null +++ b/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/OAuth2IntegrationTestSupport.java @@ -0,0 +1,34 @@ +package com.baeldung.springbootsecurity.oauth2server; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; + +import java.util.List; + +import static java.lang.String.format; +import static java.util.Collections.singletonList; + +public class OAuth2IntegrationTestSupport { + + @Value("${local.server.port}") protected int port; + + protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List scopes) { + ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails(); + resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port)); + resourceDetails.setClientId(clientId); + resourceDetails.setClientSecret("baeldung"); + resourceDetails.setScope(scopes); + resourceDetails.setGrantType("client_credentials"); + return resourceDetails; + } + + protected OAuth2RestTemplate getOAuth2RestTemplate(final ClientCredentialsResourceDetails resourceDetails) { + DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext(); + OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext); + restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter())); + return restTemplate; + } +} From b070f3ddaa207a66101a5ea1e51ad4e52854ae97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20Gr=C3=A4f?= Date: Mon, 29 Jan 2018 07:37:17 +0100 Subject: [PATCH 010/102] fixed formatting --- .../samples/java/vavr/VavrSampler.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java index ae06c97e2e..f4e0728f32 100644 --- a/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java +++ b/vavr/src/main/java/com/baeldung/samples/java/vavr/VavrSampler.java @@ -1,9 +1,9 @@ package com.baeldung.samples.java.vavr; -import io.vavr.collection.Stream; import java.util.ArrayList; import java.util.List; +import io.vavr.collection.Stream; /** * @@ -11,12 +11,12 @@ import java.util.List; */ public class VavrSampler { - static int[] intArray = new int[]{1, 2, 4}; - static List intList = new ArrayList(); - static int[][] intOfInts = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + static int[] intArray = new int[] { 1, 2, 4 }; + static List intList = new ArrayList<>(); + static int[][] intOfInts = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; public static void main(String[] args) { - vavrStreamElementAccess(); + vavrStreamElementAccess(); System.out.println("===================================="); vavrParallelStreamAccess(); System.out.println("===================================="); @@ -43,12 +43,12 @@ public class VavrSampler { System.out.println("Vavr Stream Concurrent Modification"); System.out.println("===================================="); Stream vavrStream = Stream.ofAll(intList); - //intList.add(5); + // intList.add(5); vavrStream.forEach(i -> System.out.println("in a Vavr Stream: " + i)); -// Stream wrapped = Stream.ofAll(intArray); -// intArray[2] = 5; -// wrapped.forEach(i -> System.out.println("Vavr looped " + i)); + // Stream wrapped = Stream.ofAll(intArray); + // intArray[2] = 5; + // wrapped.forEach(i -> System.out.println("Vavr looped " + i)); } public static void jdkFlatMapping() { @@ -65,9 +65,9 @@ public class VavrSampler { System.out.println("===================================="); Stream.of(42) .flatMap(i -> Stream.continually(() -> { - System.out.println("nested call"); - return 42; - })) + System.out.println("nested call"); + return 42; + })) .get(0); } From d1a4848cb49bbf4ec6fe720fea109f715be53779 Mon Sep 17 00:00:00 2001 From: Aprian Diaz Novandi Date: Mon, 29 Jan 2018 09:26:15 +0100 Subject: [PATCH 011/102] BAEL-1464 Guava Memoizer (#3420) * BAEL-1464 Guava Memoizer * Update codes based on code review and discussion in JIRA --- .../guava/memoizer/CostlySupplier.java | 17 ++++ .../baeldung/guava/memoizer/Factorial.java | 22 +++++ .../guava/memoizer/FibonacciSequence.java | 26 +++++ .../baeldung/guava/GuavaMemoizerUnitTest.java | 98 +++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java create mode 100644 guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java create mode 100644 guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java create mode 100644 guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java b/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java new file mode 100644 index 0000000000..63b3fbd438 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/CostlySupplier.java @@ -0,0 +1,17 @@ +package org.baeldung.guava.memoizer; + +import java.math.BigInteger; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +public class CostlySupplier { + + public static BigInteger generateBigNumber() { + try { + TimeUnit.SECONDS.sleep(2); + } catch (InterruptedException e) { + } + return new BigInteger("12345"); + } + +} diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java b/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java new file mode 100644 index 0000000000..74fcbdcc14 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/Factorial.java @@ -0,0 +1,22 @@ +package org.baeldung.guava.memoizer; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.math.BigInteger; + +public class Factorial { + + private static LoadingCache memo = CacheBuilder.newBuilder() + .build(CacheLoader.from(Factorial::getFactorial)); + + public static BigInteger getFactorial(int n) { + if (n == 0) { + return BigInteger.ONE; + } else { + return BigInteger.valueOf(n).multiply(memo.getUnchecked(n - 1)); + } + } + +} diff --git a/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java b/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java new file mode 100644 index 0000000000..0c70f08c23 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/memoizer/FibonacciSequence.java @@ -0,0 +1,26 @@ +package org.baeldung.guava.memoizer; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.math.BigInteger; +import java.util.concurrent.TimeUnit; + +public class FibonacciSequence { + + private static LoadingCache memo = CacheBuilder.newBuilder() + .maximumSize(100) + .build(CacheLoader.from(FibonacciSequence::getFibonacciNumber)); + + public static BigInteger getFibonacciNumber(int n) { + if (n == 0) { + return BigInteger.ZERO; + } else if (n == 1) { + return BigInteger.ONE; + } else { + return memo.getUnchecked(n - 1).add(memo.getUnchecked(n - 2)); + } + } + +} diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java new file mode 100644 index 0000000000..0ae1f438e3 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java @@ -0,0 +1,98 @@ +package org.baeldung.guava; + +import com.google.common.base.Suppliers; +import org.baeldung.guava.memoizer.CostlySupplier; +import org.baeldung.guava.memoizer.Factorial; +import org.baeldung.guava.memoizer.FibonacciSequence; +import org.junit.Test; + +import java.math.BigInteger; +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.number.IsCloseTo.closeTo; +import static org.junit.Assert.assertThat; + +public class GuavaMemoizerUnitTest { + + @Test + public void givenInteger_whenGetFibonacciNumber_thenShouldCalculateFibonacciNumber() { + // given + int n = 95; + + // when + BigInteger fibonacciNumber = FibonacciSequence.getFibonacciNumber(n); + + // then + BigInteger expectedFibonacciNumber = new BigInteger("31940434634990099905"); + assertThat(fibonacciNumber, is(equalTo(expectedFibonacciNumber))); + } + + @Test + public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() { + // given + int n = 95; + + // when + BigInteger factorial = new Factorial().getFactorial(n); + + // then + BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000"); + assertThat(factorial, is(equalTo(expectedFactorial))); + } + + @Test + public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() { + Supplier memoizedSupplier; + memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber); + + Instant start = Instant.now(); + BigInteger bigNumber = memoizedSupplier.get(); + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + + start = Instant.now(); + bigNumber = memoizedSupplier.get().add(BigInteger.ONE); + durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + + start = Instant.now(); + bigNumber = memoizedSupplier.get().add(BigInteger.TEN); + durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + } + + @Test + public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException { + Supplier memoizedSupplier; + memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS); + + Instant start = Instant.now(); + BigInteger bigNumber = memoizedSupplier.get(); + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + + start = Instant.now(); + bigNumber = memoizedSupplier.get().add(BigInteger.ONE); + durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + + TimeUnit.SECONDS.sleep(1); + + start = Instant.now(); + bigNumber = memoizedSupplier.get().add(BigInteger.TEN); + durationInMs = Duration.between(start, Instant.now()).toMillis(); + assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + } + +} From be90e9870c12864b651bc2093a8cfea01563805f Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Mon, 29 Jan 2018 17:02:48 +0700 Subject: [PATCH 012/102] Initial commit for BAEL-1500 --- .../main/java/com/baeldung/javac/Data.java | 28 +++++++++++++++++++ core-java/src/main/java/javac-args/arguments | 2 ++ core-java/src/main/java/javac-args/options | 2 ++ core-java/src/main/java/javac-args/types | 1 + core-java/src/main/java/javac-args/xlint-ops | 3 ++ 5 files changed, 36 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/javac/Data.java create mode 100644 core-java/src/main/java/javac-args/arguments create mode 100644 core-java/src/main/java/javac-args/options create mode 100644 core-java/src/main/java/javac-args/types create mode 100644 core-java/src/main/java/javac-args/xlint-ops diff --git a/core-java/src/main/java/com/baeldung/javac/Data.java b/core-java/src/main/java/com/baeldung/javac/Data.java new file mode 100644 index 0000000000..f6912fbd14 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/javac/Data.java @@ -0,0 +1,28 @@ +package com.baeldung.javac; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +public class Data implements Serializable { + static List textList = new ArrayList(); + + private static void addText() { + textList.add("baeldung"); + textList.add("."); + textList.add("com"); + } + + public List getTextList() { + this.addText(); + List result = new ArrayList(); + String firstElement = (String) textList.get(0); + switch (firstElement) { + case "baeldung": + result.add("baeldung"); + case "com": + result.add("com"); + } + return result; + } +} diff --git a/core-java/src/main/java/javac-args/arguments b/core-java/src/main/java/javac-args/arguments new file mode 100644 index 0000000000..51639800a7 --- /dev/null +++ b/core-java/src/main/java/javac-args/arguments @@ -0,0 +1,2 @@ +-d javac-target -verbose +com/baeldung/javac/Data.java \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/options b/core-java/src/main/java/javac-args/options new file mode 100644 index 0000000000..f02f2344ff --- /dev/null +++ b/core-java/src/main/java/javac-args/options @@ -0,0 +1,2 @@ +-d javac-target +-verbose \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/types b/core-java/src/main/java/javac-args/types new file mode 100644 index 0000000000..ef2d861f84 --- /dev/null +++ b/core-java/src/main/java/javac-args/types @@ -0,0 +1 @@ +com/baeldung/javac/Data.java \ No newline at end of file diff --git a/core-java/src/main/java/javac-args/xlint-ops b/core-java/src/main/java/javac-args/xlint-ops new file mode 100644 index 0000000000..cdccbc0cce --- /dev/null +++ b/core-java/src/main/java/javac-args/xlint-ops @@ -0,0 +1,3 @@ +-d javac-target +-Xlint:rawtypes,unchecked,static,cast,serial,fallthrough +com/baeldung/javac/Data.java \ No newline at end of file From bdae4fe99bd201f37a187e08f93fa8ca063ddd3a Mon Sep 17 00:00:00 2001 From: abialas Date: Tue, 30 Jan 2018 03:32:09 +0100 Subject: [PATCH 013/102] BAEL-21 new Java9 HTTP API overview (#3536) * BAEL-1412 add java 8 spring data features * BAEL-21 new HTTP API overview --- .../java9/httpclient/HttpClientTest.java | 215 ++++++++++++++++++ .../java9/httpclient/HttpRequestTest.java | 171 ++++++++++++++ .../java9/httpclient/HttpResponseTest.java | 55 +++++ 3 files changed, 441 insertions(+) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java new file mode 100644 index 0000000000..a4c6ac0d7d --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java @@ -0,0 +1,215 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.IOException; +import java.net.*; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpClientTest { + + @Test + public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) + .build(); + + HttpResponse response = HttpClient.newBuilder() + .proxy(ProxySelector.getDefault()) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample body")); + } + + @Test + public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); + assertThat(response.body(), containsString("https://stackoverflow.com/")); + } + + @Test + public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.finalRequest() + .uri() + .toString(), equalTo("https://stackoverflow.com/")); + } + + @Test + public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/basic-auth")) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("postman", "password".toCharArray()); + } + }) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) + .build(); + CompletableFuture> response = HttpClient.newBuilder() + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + CompletableFuture> response1 = HttpClient.newBuilder() + .executor(Executors.newFixedThreadPool(2)) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture> response2 = HttpClient.newBuilder() + .executor(Executors.newFixedThreadPool(2)) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture> response3 = HttpClient.newBuilder() + .executor(Executors.newFixedThreadPool(2)) + .build() + .sendAsync(request, HttpResponse.BodyHandler.asString()); + + CompletableFuture.allOf(response1, response2, response3) + .join(); + + assertThat(response1.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response2.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response3.get() + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpClient httpClient = HttpClient.newBuilder() + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE)) + .build(); + + httpClient.send(request, HttpResponse.BodyHandler.asString()); + + assertThat(httpClient.cookieManager() + .get() + .getCookieStore() + .getCookies(), empty()); + } + + @Test + public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpClient httpClient = HttpClient.newBuilder() + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) + .build(); + + httpClient.send(request, HttpResponse.BodyHandler.asString()); + + assertThat(httpClient.cookieManager() + .get() + .getCookieStore() + .getCookies(), not(empty())); + } + + @Test + public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException { + List targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2")); + + HttpClient client = HttpClient.newHttpClient(); + + List> futures = targets.stream() + .map(target -> client.sendAsync(HttpRequest.newBuilder(target) + .GET() + .build(), HttpResponse.BodyHandler.asString()) + .thenApply(response -> response.body())) + .collect(Collectors.toList()); + + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .join(); + + if (futures.get(0) + .get() + .contains("foo1")) { + assertThat(futures.get(0) + .get(), containsString("bar1")); + assertThat(futures.get(1) + .get(), containsString("bar2")); + } else { + assertThat(futures.get(1) + .get(), containsString("bar2")); + assertThat(futures.get(1) + .get(), containsString("bar1")); + } + + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java new file mode 100644 index 0000000000..7c0e9a90e0 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java @@ -0,0 +1,171 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.time.Duration; + +import static java.time.temporal.ChronoUnit.SECONDS; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpRequestTest { + + @Test + public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://stackoverflow.com")) + .version(HttpClient.Version.HTTP_2) + .GET() + .build(); + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2)); + } + + @Test + public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .version(HttpClient.Version.HTTP_2) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1)); + } + + @Test + public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .headers("key1", "value1", "key2", "value2") + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .timeout(Duration.of(10, SECONDS)) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .POST(HttpRequest.noBody()) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromString("Sample request body")) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException { + byte[] sampleData = "Sample request body".getBytes(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData))) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException { + byte[] sampleData = "Sample request body".getBytes(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromByteArray(sampleData)) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample request body")); + } + + @Test + public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/post")) + .headers("Content-Type", "text/plain;charset=UTF-8") + .POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt"))) + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), containsString("Sample file content")); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java new file mode 100644 index 0000000000..80295ff34c --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java @@ -0,0 +1,55 @@ +package com.baeldung.java9.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; +import org.junit.Test; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.isEmptyString; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; + +/** + * Created by adam. + */ +public class HttpResponseTest { + + @Test + public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://postman-echo.com/get")) + .GET() + .build(); + + HttpResponse response = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); + assertThat(response.body(), not(isEmptyString())); + } + + @Test + public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://stackoverflow.com")) + .version(HttpClient.Version.HTTP_1_1) + .GET() + .build(); + HttpResponse response = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandler.asString()); + + assertThat(request.uri() + .toString(), equalTo("http://stackoverflow.com")); + assertThat(response.uri() + .toString(), equalTo("https://stackoverflow.com/")); + } + +} From 32e309ae4d62f3b38334ad60977660cb55c1deff Mon Sep 17 00:00:00 2001 From: Ganesh Date: Tue, 30 Jan 2018 20:25:08 +0530 Subject: [PATCH 014/102] BAEL-1456 - How to implement task prioritization in Java (#3366) * priority based job execution in java * minor fixes * updated to use java 8 features --- .../concurrent/prioritytaskexecution/Job.java | 24 ++++++++ .../prioritytaskexecution/JobPriority.java | 7 +++ .../PriorityJobScheduler.java | 56 +++++++++++++++++++ .../PriorityJobSchedulerUnitTest.java | 38 +++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java create mode 100644 core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java new file mode 100644 index 0000000000..a70041ed7d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -0,0 +1,24 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +public class Job implements Runnable { + private String jobName; + private JobPriority jobPriority; + + public Job(String jobName, JobPriority jobPriority) { + this.jobName = jobName; + this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM; + } + + public JobPriority getJobPriority() { + return jobPriority; + } + + @Override + public void run() { + try { + System.out.println("Job:" + jobName + " Priority:" + jobPriority); + Thread.sleep(1000); + } catch (InterruptedException ignored) { + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java new file mode 100644 index 0000000000..d8092a9ce7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/JobPriority.java @@ -0,0 +1,7 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +public enum JobPriority { + HIGH, + MEDIUM, + LOW +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java new file mode 100644 index 0000000000..70fd1710c0 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java @@ -0,0 +1,56 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +import java.util.Comparator; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.TimeUnit; + +public class PriorityJobScheduler { + + private ExecutorService priorityJobPoolExecutor; + private ExecutorService priorityJobScheduler; + private PriorityBlockingQueue priorityQueue; + + public PriorityJobScheduler(Integer poolSize, Integer queueSize) { + priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize); + Comparator jobComparator = Comparator.comparing(Job::getJobPriority); + priorityQueue = new PriorityBlockingQueue(queueSize, + (Comparator) jobComparator); + + priorityJobScheduler = Executors.newSingleThreadExecutor(); + priorityJobScheduler.execute(()->{ + while (true) { + try { + priorityJobPoolExecutor.execute(priorityQueue.take()); + } catch (InterruptedException e) { + break; + } + } + }); + } + + public void scheduleJob(Job job) { + priorityQueue.add(job); + } + + public int getQueuedTaskCount() { + return priorityQueue.size(); + } + + protected void close(ExecutorService scheduler) { + scheduler.shutdown(); + try { + if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) { + scheduler.shutdownNow(); + } + } catch (InterruptedException e) { + scheduler.shutdownNow(); + } + } + + public void closeScheduler() { + close(priorityJobPoolExecutor); + close(priorityJobScheduler); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java new file mode 100644 index 0000000000..902bada3a2 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.concurrent.prioritytaskexecution; + +import org.junit.Test; + +public class PriorityJobSchedulerUnitTest { + private static int POOL_SIZE = 1; + private static int QUEUE_SIZE = 10; + + @Test + public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() { + Job job1 = new Job("Job1", JobPriority.LOW); + Job job2 = new Job("Job2", JobPriority.MEDIUM); + Job job3 = new Job("Job3", JobPriority.HIGH); + Job job4 = new Job("Job4", JobPriority.MEDIUM); + Job job5 = new Job("Job5", JobPriority.LOW); + Job job6 = new Job("Job6", JobPriority.HIGH); + + PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE); + + pjs.scheduleJob(job1); + pjs.scheduleJob(job2); + pjs.scheduleJob(job3); + pjs.scheduleJob(job4); + pjs.scheduleJob(job5); + pjs.scheduleJob(job6); + + // ensure no tasks is pending before closing the scheduler + while (pjs.getQueuedTaskCount() != 0); + + // delay to avoid job sleep (added for demo) being interrupted + try { + Thread.sleep(2000); + } catch (InterruptedException ignored) { + } + + pjs.closeScheduler(); + } +} From f6cfff3f9d2be2018af280b2647bb3a01081d715 Mon Sep 17 00:00:00 2001 From: iaforek Date: Tue, 30 Jan 2018 18:13:53 +0000 Subject: [PATCH 015/102] BAEL-1298 - How to create a Sudoku solver (#3197) * Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention * BAEL-821 Added more test and updated round methods. * BAEL-837 - initial commit. A few examples of adding doubles. * BAEL-837 - Couple of smaller changes * BAEL-837 - Added jUnit test. * BAEL-579 Updated Spring Cloud Version I was getting error: java.lang.NoSuchMethodError: org.springframework.cloud.config.environment.Environment After version update, all is okay. * BAEL-579 Added actuator to Cloud Config Client. * BAEL-579 Enabled cloud bus and updated dependencies. * BAEL-579 Config Client using Spring Cloud Bus. * BAEL-579 Recreated Basic Config Server. * BAEL-579 Recreated Config Client. * BAEL-579 Removed test Git URL. * BAEL-579 Added Actuator to Config Client * BAEL-579 Added Spring Cloud Bus to Client. * BAEL-579 Server changes for Spring Cloud Bus Added dependencies and removed git.clone-on-start as this was causing server to throw errors after git properties change. * BAEL-579 Removed Git URL. * Revert "BAEL-579 Updated Spring Cloud Version" This reverts commit f775bf91e53a1ecfb9b70596688d7c8202bf495f. * Revert "BAEL-579 Config Client using Spring Cloud Bus." This reverts commit 1d96bc5761994a33af9a7a9aa5ab68604a5b44dc. * Revert "BAEL-579 Enabled cloud bus and updated dependencies." This reverts commit 7845da922d89d53506dd0fff387ea13694c50bc1. * Revert "BAEL-579 Added actuator to Cloud Config Client." This reverts commit 076657a26a57e0aa676989a4d97966a3b9d53e1c. * BAEL-579 Added missing dependency versions. * BAEL-579 Added missing dependency versions. * Updated gitignore * BAEL-1065 Simple performance check StringBuffer vs StringBuilder. * BAEL-1065 Added JMH benchmarks * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Dancing Links Algorithm. Smaller changes to Backtracking * BAEL-1298 Resolve conflict - use most up-to-date POM * Updated code - mostly with CONSTANTS. Extracted methods. * Removed pointless Java8 code. Renamed constant --- .../sudoku/BacktrackingAlgorithm.java | 103 ++++++++++++++ .../algorithms/sudoku/ColumnNode.java | 33 +++++ .../algorithms/sudoku/DancingLinks.java | 134 ++++++++++++++++++ .../sudoku/DancingLinksAlgorithm.java | 110 ++++++++++++++ .../algorithms/sudoku/DancingNode.java | 50 +++++++ 5 files changed, 430 insertions(+) create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java new file mode 100644 index 0000000000..127e78900c --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -0,0 +1,103 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.stream.IntStream; + +public class BacktrackingAlgorithm { + + private static int BOARD_SIZE = 9; + private static int SUBSECTION_SIZE = 3; + private static int BOARD_START_INDEX = 0; + + private static int NO_VALUE = 0; + private static int MIN_VALUE = 1; + private static int MAX_VALUE = 9; + + public static int[][] board = { + { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, + { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, + { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, + { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, + { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, + { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, + { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, + { 0, 9, 0, 0, 0, 0, 4, 0, 0 } + }; + + public static void main(String[] args) { + BacktrackingAlgorithm solver = new BacktrackingAlgorithm(); + solver.solve(board); + solver.printBoard(); + } + + public void printBoard() { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + System.out.print(board[row][column] + " "); + } + System.out.println(); + } + } + + public boolean solve(int[][] board) { + for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) { + for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) { + if (board[r][c] == NO_VALUE) { + for (int k = MIN_VALUE; k <= MAX_VALUE; k++) { + board[r][c] = k; + if (isValid(board, r, c) && solve(board)) { + return true; + } else { + board[r][c] = NO_VALUE; + } + } + return false; + } + } + } + return true; + } + + public boolean isValid(int[][] board, int r, int c) { + return (rowConstraint(board, r) && + columnConstraint(board, c) && + subsectionConstraint(board, r, c)); + } + + private boolean subsectionConstraint(int[][] board, int r, int c) { + boolean[] constraint = new boolean[BOARD_SIZE]; + for (int i = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; i < (r / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; i++) { + for (int j = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; j < (c / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; j++) { + if (!checkConstraint(board, i, constraint, j)) return false; + } + } + return true; + } + + private boolean columnConstraint(int[][] board, int c) { + boolean[] constraint = new boolean[BOARD_SIZE]; + for (int i = BOARD_START_INDEX; i < BOARD_SIZE; i++) { + if (!checkConstraint(board, i, constraint, c)) return false; + } + return true; + } + + private boolean rowConstraint(int[][] board, int r) { + boolean[] constraint = new boolean[BOARD_SIZE]; + for (int i = BOARD_START_INDEX; i < BOARD_SIZE; i++) { + if (!checkConstraint(board, r, constraint, i)) return false; + } + return true; + } + + private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) { + if (board[r][c] >= MIN_VALUE && board[r][c] <= MAX_VALUE) { + if (constraint[board[r][c] - 1] == false) { + constraint[board[r][c] - 1] = true; + } else { + return false; + } + } + return true; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java new file mode 100644 index 0000000000..48538344b6 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java @@ -0,0 +1,33 @@ +package com.baeldung.algorithms.sudoku; + +class ColumnNode extends DancingNode { + int size; + String name; + + public ColumnNode(String n) { + super(); + size = 0; + name = n; + C = this; + } + + void cover() { + unlinkLR(); + for (DancingNode i = this.D; i != this; i = i.D) { + for (DancingNode j = i.R; j != i; j = j.R) { + j.unlinkUD(); + j.C.size--; + } + } + } + + void uncover() { + for (DancingNode i = this.U; i != this; i = i.U) { + for (DancingNode j = i.L; j != i; j = j.L) { + j.C.size++; + j.relinkUD(); + } + } + relinkLR(); + } +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java new file mode 100644 index 0000000000..a30f8ecab5 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java @@ -0,0 +1,134 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class DancingLinks { + + private ColumnNode header; + private List answer; + + private void search(int k) { + if (header.R == header) { + handleSolution(answer); + } else { + ColumnNode c = selectColumnNodeHeuristic(); + c.cover(); + + for (DancingNode r = c.D; r != c; r = r.D) { + answer.add(r); + + for (DancingNode j = r.R; j != r; j = j.R) { + j.C.cover(); + } + + search(k + 1); + + r = answer.remove(answer.size() - 1); + c = r.C; + + for (DancingNode j = r.L; j != r; j = j.L) { + j.C.uncover(); + } + } + c.uncover(); + } + } + + private ColumnNode selectColumnNodeHeuristic() { + int min = Integer.MAX_VALUE; + ColumnNode ret = null; + for (ColumnNode c = (ColumnNode) header.R; c != header; c = (ColumnNode) c.R) { + if (c.size < min) { + min = c.size; + ret = c; + } + } + return ret; + } + + private ColumnNode makeDLXBoard(boolean[][] grid) { + final int COLS = grid[0].length; + final int ROWS = grid.length; + + ColumnNode headerNode = new ColumnNode("header"); + ArrayList columnNodes = new ArrayList(); + + for (int i = 0; i < COLS; i++) { + ColumnNode n = new ColumnNode(Integer.toString(i)); + columnNodes.add(n); + headerNode = (ColumnNode) headerNode.hookRight(n); + } + headerNode = headerNode.R.C; + + for (int i = 0; i < ROWS; i++) { + DancingNode prev = null; + for (int j = 0; j < COLS; j++) { + if (grid[i][j] == true) { + ColumnNode col = columnNodes.get(j); + DancingNode newNode = new DancingNode(col); + if (prev == null) + prev = newNode; + col.U.hookDown(newNode); + prev = prev.hookRight(newNode); + col.size++; + } + } + } + + headerNode.size = COLS; + + return headerNode; + } + + public DancingLinks(boolean[][] cover) { + header = makeDLXBoard(cover); + } + + public void runSolver() { + answer = new LinkedList(); + search(0); + } + + public void handleSolution(List answer) { + int[][] result = parseBoard(answer); + printSolution(result); + } + + int size = 9; + + private int[][] parseBoard(List answer) { + int[][] result = new int[size][size]; + for (DancingNode n : answer) { + DancingNode rcNode = n; + int min = Integer.parseInt(rcNode.C.name); + for (DancingNode tmp = n.R; tmp != n; tmp = tmp.R) { + int val = Integer.parseInt(tmp.C.name); + if (val < min) { + min = val; + rcNode = tmp; + } + } + int ans1 = Integer.parseInt(rcNode.C.name); + int ans2 = Integer.parseInt(rcNode.R.C.name); + int r = ans1 / size; + int c = ans1 % size; + int num = (ans2 % size) + 1; + result[r][c] = num; + } + return result; + } + + public static void printSolution(int[][] result) { + int N = result.length; + for (int i = 0; i < N; i++) { + String ret = ""; + for (int j = 0; j < N; j++) { + ret += result[i][j] + " "; + } + System.out.println(ret); + } + System.out.println(); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java new file mode 100644 index 0000000000..057a15c594 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -0,0 +1,110 @@ +package com.baeldung.algorithms.sudoku; + +import java.util.*; + +public class DancingLinksAlgorithm { + private static int BOARD_SIZE = 9; + private static int SUBSECTION_SIZE = 3; + private static int NO_VALUE = 0; + private static int CONSTRAINTS = 4; + private static int MIN_VALUE = 1; + private static int MAX_VALUE = 9; + private static int COVER_START_INDEX = 1; + + public static int[][] board = { + { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, + { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, + { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, + { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, + { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, + { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, + { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, + { 0, 9, 0, 0, 0, 0, 4, 0, 0 } + }; + + public static void main(String[] args) { + DancingLinksAlgorithm solver = new DancingLinksAlgorithm(); + solver.solve(board); + } + + public boolean solve(int[][] board) { + boolean[][] cover = initializeExactCoverBoard(board); + DancingLinks dlx = new DancingLinks(cover); + dlx.runSolver(); + + return true; + } + + private int getIndex(int row, int col, int num) { + return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1); + } + + private boolean[][] createExactCoverBoard() { + boolean[][] R = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS]; + + int hBase = 0; + + // Cell constraint. + for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { + int index = getIndex(r, c, n); + R[index][hBase] = true; + } + } + } + + // Row constrain. + for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) { + int index = getIndex(r, c1, n); + R[index][hBase] = true; + } + } + } + + // Column constraint. + for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) { + int index = getIndex(r1, c, n); + R[index][hBase] = true; + } + } + } + + // Subsection constraint + for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { + for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) { + for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) { + int index = getIndex(br + rDelta, bc + cDelta, n); + R[index][hBase] = true; + } + } + } + } + } + return R; + } + + private boolean[][] initializeExactCoverBoard(int[][] board) { + boolean[][] R = createExactCoverBoard(); + for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) { + for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) { + int n = board[i - 1][j - 1]; + if (n != NO_VALUE) { + for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { + if (num != n) { + Arrays.fill(R[getIndex(i, j, num)], false); + } + } + } + } + } + return R; + } +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java new file mode 100644 index 0000000000..13dc3f2b57 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -0,0 +1,50 @@ +package com.baeldung.algorithms.sudoku; + +class DancingNode { + DancingNode L, R, U, D; + ColumnNode C; + + DancingNode hookDown(DancingNode n1) { + assert (this.C == n1.C); + n1.D = this.D; + n1.D.U = n1; + n1.U = this; + this.D = n1; + return n1; + } + + DancingNode hookRight(DancingNode n1) { + n1.R = this.R; + n1.R.L = n1; + n1.L = this; + this.R = n1; + return n1; + } + + void unlinkLR() { + this.L.R = this.R; + this.R.L = this.L; + } + + void relinkLR() { + this.L.R = this.R.L = this; + } + + void unlinkUD() { + this.U.D = this.D; + this.D.U = this.U; + } + + void relinkUD() { + this.U.D = this.D.U = this; + } + + public DancingNode() { + L = R = U = D = this; + } + + public DancingNode(ColumnNode c) { + this(); + C = c; + } +} \ No newline at end of file From a48e0625987442bd490766828d10187afc1ebbce Mon Sep 17 00:00:00 2001 From: mkuligowski Date: Tue, 30 Jan 2018 22:49:54 +0100 Subject: [PATCH 016/102] BAEL-1204 (#3508) * Initialize smooks subproject * Add Smooks dependency * Delete files form badly created submodule * Add domain classes * Create class responsible for converting Orders * Create class responsible for validating messages * Add configuration file * Add integration tests for Smooks converters and validators * ADd en_US locale and fix date format * Fix number format in expected messages * Delete unused mapping * Remove unused conversion to JSON * Add assertion for ruleName in givenIncorrectOrderXML_whenValidate_thenExpectValidationErrors --- libraries/pom.xml | 6 ++ .../smooks/converter/OrderConverter.java | 45 ++++++++++++ .../smooks/converter/OrderValidator.java | 27 +++++++ .../java/com/baeldung/smooks/model/Item.java | 71 +++++++++++++++++++ .../java/com/baeldung/smooks/model/Order.java | 52 ++++++++++++++ .../com/baeldung/smooks/model/Status.java | 5 ++ .../com/baeldung/smooks/model/Supplier.java | 49 +++++++++++++ libraries/src/main/resources/smooks/email.ftl | 8 +++ .../src/main/resources/smooks/item-rules.csv | 1 + libraries/src/main/resources/smooks/order.ftl | 7 ++ .../src/main/resources/smooks/order.json | 21 ++++++ libraries/src/main/resources/smooks/order.xml | 20 ++++++ .../main/resources/smooks/smooks-mapping.xml | 29 ++++++++ .../resources/smooks/smooks-transform-edi.xml | 11 +++ .../smooks/smooks-transform-email.xml | 12 ++++ .../resources/smooks/smooks-validation.xml | 17 +++++ .../main/resources/smooks/supplier.properties | 2 + .../converter/SmooksIntegrationTest.java | 70 ++++++++++++++++++ 18 files changed, 453 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java create mode 100644 libraries/src/main/java/com/baeldung/smooks/converter/OrderValidator.java create mode 100644 libraries/src/main/java/com/baeldung/smooks/model/Item.java create mode 100644 libraries/src/main/java/com/baeldung/smooks/model/Order.java create mode 100644 libraries/src/main/java/com/baeldung/smooks/model/Status.java create mode 100644 libraries/src/main/java/com/baeldung/smooks/model/Supplier.java create mode 100644 libraries/src/main/resources/smooks/email.ftl create mode 100644 libraries/src/main/resources/smooks/item-rules.csv create mode 100644 libraries/src/main/resources/smooks/order.ftl create mode 100644 libraries/src/main/resources/smooks/order.json create mode 100644 libraries/src/main/resources/smooks/order.xml create mode 100644 libraries/src/main/resources/smooks/smooks-mapping.xml create mode 100644 libraries/src/main/resources/smooks/smooks-transform-edi.xml create mode 100644 libraries/src/main/resources/smooks/smooks-transform-email.xml create mode 100644 libraries/src/main/resources/smooks/smooks-validation.xml create mode 100644 libraries/src/main/resources/smooks/supplier.properties create mode 100644 libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 3e802e76c8..a330494bb3 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -710,6 +710,11 @@ test test + + org.milyn + milyn-smooks-all + ${smooks.version} + @@ -789,6 +794,7 @@ 1.23.0 v4-rev493-1.21.0 1.0.0 + 1.7.0 3.0.14 \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java b/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java new file mode 100644 index 0000000000..d11f5a29b2 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/converter/OrderConverter.java @@ -0,0 +1,45 @@ +package com.baeldung.smooks.converter; + +import com.baeldung.smooks.model.Order; +import org.milyn.Smooks; +import org.milyn.payload.JavaResult; +import org.milyn.payload.StringResult; +import org.xml.sax.SAXException; + +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; + +public class OrderConverter { + + public Order convertOrderXMLToOrderObject(String path) throws IOException, SAXException { + Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping.xml")); + try { + JavaResult javaResult = new JavaResult(); + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult); + return (Order) javaResult.getBean("order"); + } finally { + smooks.close(); + } + } + + + public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException { + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml"); + } + + public String convertOrderXMLtoEmailMessage(String path) throws IOException, SAXException { + return convertDocumentWithTempalte(path, "/smooks/smooks-transform-email.xml"); + } + + private String convertDocumentWithTempalte(String path, String config) throws IOException, SAXException { + Smooks smooks = new Smooks(config); + + try { + StringResult stringResult = new StringResult(); + smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), stringResult); + return stringResult.toString(); + } finally { + smooks.close(); + } + } +} diff --git a/libraries/src/main/java/com/baeldung/smooks/converter/OrderValidator.java b/libraries/src/main/java/com/baeldung/smooks/converter/OrderValidator.java new file mode 100644 index 0000000000..3975921da0 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/converter/OrderValidator.java @@ -0,0 +1,27 @@ +package com.baeldung.smooks.converter; + +import org.milyn.Smooks; +import org.milyn.payload.JavaResult; +import org.milyn.payload.StringResult; +import org.milyn.validation.ValidationResult; +import org.xml.sax.SAXException; + +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; + +public class OrderValidator { + + public ValidationResult validate(String path) throws IOException, SAXException { + Smooks smooks = new Smooks(OrderValidator.class.getResourceAsStream("/smooks/smooks-validation.xml")); + + try { + StringResult xmlResult = new StringResult(); + JavaResult javaResult = new JavaResult(); + ValidationResult validationResult = new ValidationResult(); + smooks.filterSource(new StreamSource(OrderValidator.class.getResourceAsStream(path)), xmlResult, javaResult, validationResult); + return validationResult; + } finally { + smooks.close(); + } + } +} diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Item.java b/libraries/src/main/java/com/baeldung/smooks/model/Item.java new file mode 100644 index 0000000000..a7f7783b3f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/model/Item.java @@ -0,0 +1,71 @@ +package com.baeldung.smooks.model; + +public class Item { + + public Item() { + } + + public Item(String code, Double price, Integer quantity) { + this.code = code; + this.price = price; + this.quantity = quantity; + } + + private String code; + private Double price; + private Integer quantity; + + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Item item = (Item) o; + + if (code != null ? !code.equals(item.code) : item.code != null) return false; + if (price != null ? !price.equals(item.price) : item.price != null) return false; + return quantity != null ? quantity.equals(item.quantity) : item.quantity == null; + } + + @Override + public int hashCode() { + int result = code != null ? code.hashCode() : 0; + result = 31 * result + (price != null ? price.hashCode() : 0); + result = 31 * result + (quantity != null ? quantity.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Item{" + + "code='" + code + '\'' + + ", price=" + price + + ", quantity=" + quantity + + '}'; + } +} diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Order.java b/libraries/src/main/java/com/baeldung/smooks/model/Order.java new file mode 100644 index 0000000000..047e1fe8a3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/model/Order.java @@ -0,0 +1,52 @@ +package com.baeldung.smooks.model; + +import java.util.Date; +import java.util.List; + +public class Order { + private Date creationDate; + private Long number; + private Status status; + private Supplier supplier; + private List items; + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public Supplier getSupplier() { + return supplier; + } + + public void setSupplier(Supplier supplier) { + this.supplier = supplier; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Status.java b/libraries/src/main/java/com/baeldung/smooks/model/Status.java new file mode 100644 index 0000000000..53c50bdf46 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/model/Status.java @@ -0,0 +1,5 @@ +package com.baeldung.smooks.model; + +public enum Status { + NEW, IN_PROGRESS, FINISHED +} diff --git a/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java b/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java new file mode 100644 index 0000000000..31a9e1f43f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/smooks/model/Supplier.java @@ -0,0 +1,49 @@ +package com.baeldung.smooks.model; + +public class Supplier { + + private String name; + private String phoneNumber; + + public Supplier() { + } + + public Supplier(String name, String phoneNumber) { + this.name = name; + this.phoneNumber = phoneNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Supplier supplier = (Supplier) o; + + if (name != null ? !name.equals(supplier.name) : supplier.name != null) return false; + return phoneNumber != null ? phoneNumber.equals(supplier.phoneNumber) : supplier.phoneNumber == null; + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0); + return result; + } +} diff --git a/libraries/src/main/resources/smooks/email.ftl b/libraries/src/main/resources/smooks/email.ftl new file mode 100644 index 0000000000..8413046508 --- /dev/null +++ b/libraries/src/main/resources/smooks/email.ftl @@ -0,0 +1,8 @@ +<#setting locale="en_US"> +Hi, +Order number #${order.number} created on ${order.creationDate?string["yyyy-MM-dd"]} is currently in ${order.status} status. +Consider contact supplier "${supplier.name}" with phone number: "${supplier.phoneNumber}". +Order items: +<#list items as item> +${item.quantity} X ${item.code} (total price ${item.price * item.quantity}) + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/item-rules.csv b/libraries/src/main/resources/smooks/item-rules.csv new file mode 100644 index 0000000000..c93c453f25 --- /dev/null +++ b/libraries/src/main/resources/smooks/item-rules.csv @@ -0,0 +1 @@ +"max_total","item.quantity * item.price < 300.00" diff --git a/libraries/src/main/resources/smooks/order.ftl b/libraries/src/main/resources/smooks/order.ftl new file mode 100644 index 0000000000..9d40eb55c7 --- /dev/null +++ b/libraries/src/main/resources/smooks/order.ftl @@ -0,0 +1,7 @@ +<#setting locale="en_US"> +UNA:+.? ' +UNH+${order.number}+${order.status}+${order.creationDate?string["yyyy-MM-dd"]}' +CTA+${supplier.name}+${supplier.phoneNumber}' +<#list items as item> +LIN+${item.quantity}+${item.code}+${item.price}' + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/order.json b/libraries/src/main/resources/smooks/order.json new file mode 100644 index 0000000000..bf6bc5fe93 --- /dev/null +++ b/libraries/src/main/resources/smooks/order.json @@ -0,0 +1,21 @@ +{ + "creationDate":"2018-01-14", + "orderNumber":771, + "orderStatus":"IN_PROGRESS", + "supplier":{ + "name":"CompanyX", + "phone":"1234567" + }, + "orderItems":[ + { + "quantity":1, + "code":"PX1234", + "price":9.99 + }, + { + "quantity":2, + "code":"RX1990", + "price":120.32 + } + ] +} \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/order.xml b/libraries/src/main/resources/smooks/order.xml new file mode 100644 index 0000000000..343c5cab38 --- /dev/null +++ b/libraries/src/main/resources/smooks/order.xml @@ -0,0 +1,20 @@ + + 771 + IN_PROGRESS + + CompanyX + 1234567 + + + + 1 + PX1234 + 9.99 + + + 2 + RX990 + 120.32 + + + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/smooks-mapping.xml b/libraries/src/main/resources/smooks/smooks-mapping.xml new file mode 100644 index 0000000000..7996834e38 --- /dev/null +++ b/libraries/src/main/resources/smooks/smooks-mapping.xml @@ -0,0 +1,29 @@ + + + + + + + + yyyy-MM-dd + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/smooks-transform-edi.xml b/libraries/src/main/resources/smooks/smooks-transform-edi.xml new file mode 100644 index 0000000000..1dae4055a8 --- /dev/null +++ b/libraries/src/main/resources/smooks/smooks-transform-edi.xml @@ -0,0 +1,11 @@ + + + + + + + /smooks/order.ftl + + + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/smooks-transform-email.xml b/libraries/src/main/resources/smooks/smooks-transform-email.xml new file mode 100644 index 0000000000..101aa67f0d --- /dev/null +++ b/libraries/src/main/resources/smooks/smooks-transform-email.xml @@ -0,0 +1,12 @@ + + + + + + + + /smooks/email.ftl + + + \ No newline at end of file diff --git a/libraries/src/main/resources/smooks/smooks-validation.xml b/libraries/src/main/resources/smooks/smooks-validation.xml new file mode 100644 index 0000000000..b66722ffc5 --- /dev/null +++ b/libraries/src/main/resources/smooks/smooks-validation.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + diff --git a/libraries/src/main/resources/smooks/supplier.properties b/libraries/src/main/resources/smooks/supplier.properties new file mode 100644 index 0000000000..cc17e45eb4 --- /dev/null +++ b/libraries/src/main/resources/smooks/supplier.properties @@ -0,0 +1,2 @@ +supplierName=[A-Za-z0-9]* +supplierPhone=^[0-9\\-\\+]{9,15}$ diff --git a/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java b/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java new file mode 100644 index 0000000000..4d2cb71329 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/smooks/converter/SmooksIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.smooks.converter; + +import com.baeldung.smooks.model.Item; +import com.baeldung.smooks.model.Order; +import com.baeldung.smooks.model.Status; +import com.baeldung.smooks.model.Supplier; +import org.junit.Test; +import org.milyn.validation.ValidationResult; +import java.text.SimpleDateFormat; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + + +public class SmooksIntegrationTest { + + private static final String EDIFACT_MESSAGE = + "UNA:+.? '\r\n" + + "UNH+771+IN_PROGRESS+2018-01-14'\r\n" + + "CTA+CompanyX+1234567'\r\n" + + "LIN+1+PX1234+9.99'\r\n" + + "LIN+2+RX990+120.32'\r\n"; + private static final String EMAIL_MESSAGE = + "Hi,\r\n" + + "Order number #771 created on 2018-01-14 is currently in IN_PROGRESS status.\r\n" + + "Consider contact supplier \"CompanyX\" with phone number: \"1234567\".\r\n" + + "Order items:\r\n" + + "1 X PX1234 (total price 9.99)\r\n" + + "2 X RX990 (total price 240.64)\r\n"; + + @Test + public void givenOrderXML_whenConvert_thenPOJOsConstructedCorrectly() throws Exception { + + OrderConverter xmlToJavaOrderConverter = new OrderConverter(); + Order order = xmlToJavaOrderConverter.convertOrderXMLToOrderObject("/smooks/order.xml"); + + assertThat(order.getNumber(),is(771L)); + assertThat(order.getStatus(),is(Status.IN_PROGRESS)); + assertThat(order.getCreationDate(),is(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-14"))); + assertThat(order.getSupplier(),is(new Supplier("CompanyX","1234567"))); + assertThat(order.getItems(),containsInAnyOrder( + new Item("PX1234",9.99,1), + new Item("RX990",120.32,2)) + ); + + } + + @Test + public void givenIncorrectOrderXML_whenValidate_thenExpectValidationErrors() throws Exception { + OrderValidator orderValidator = new OrderValidator(); + ValidationResult validationResult = orderValidator.validate("/smooks/order.xml"); + + assertThat(validationResult.getErrors(), hasSize(1)); + // 1234567 didn't match ^[0-9\\-\\+]{9,15}$ + assertThat(validationResult.getErrors().get(0).getFailRuleResult().getRuleName(),is("supplierPhone")); + } + + @Test + public void givenOrderXML_whenApplyEDITemplate_thenConvertedToEDIFACT() throws Exception { + OrderConverter orderConverter = new OrderConverter(); + String edifact = orderConverter.convertOrderXMLtoEDIFACT("/smooks/order.xml"); + assertThat(edifact,is(EDIFACT_MESSAGE)); + } + + @Test + public void givenOrderXML_whenApplyEmailTemplate_thenConvertedToEmailMessage() throws Exception { + OrderConverter orderConverter = new OrderConverter(); + String emailMessage = orderConverter.convertOrderXMLtoEmailMessage("/smooks/order.xml"); + assertThat(emailMessage,is(EMAIL_MESSAGE)); + } +} \ No newline at end of file From eb99ad575377f17e81e0cb480bcd4d48e234204c Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Wed, 31 Jan 2018 16:32:21 +0700 Subject: [PATCH 017/102] Initial commit for BAEL-1515 --- testing-modules/testing/pom.xml | 12 ++++++ .../testing/assertj/custom/Assertions.java | 11 ++++++ .../baeldung/testing/assertj/custom/Car.java | 22 +++++++++++ .../testing/assertj/custom/CarAssert.java | 30 +++++++++++++++ .../testing/assertj/custom/Person.java | 32 ++++++++++++++++ .../testing/assertj/custom/PersonAssert.java | 38 +++++++++++++++++++ .../custom/AssertJCarAssertUnitTest.java | 20 ++++++++++ .../AssertJCustomAssertionsUnitTest.java | 29 ++++++++++++++ .../custom/AssertJPersonAssertUnitTest.java | 26 +++++++++++++ 9 files changed, 220 insertions(+) create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 1fd6357b87..6f185d3b4c 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -154,6 +154,17 @@ + + org.assertj + assertj-assertions-generator-maven-plugin + ${assertj-generator.version} + + + com.baeldung.testing.assertj.custom.Person + com.baeldung.testing.assertj.custom.Car + + + @@ -164,6 +175,7 @@ 21.0 3.1.0 3.6.1 + 2.1.0 0.32 1.1.0 0.12 diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java new file mode 100644 index 0000000000..5c72eb6d05 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java @@ -0,0 +1,11 @@ +package com.baeldung.testing.assertj.custom; + +public class Assertions { + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + public static CarAssert assertThat(Car actual) { + return new CarAssert(actual); + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java new file mode 100644 index 0000000000..e52ffee8e5 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java @@ -0,0 +1,22 @@ +package com.baeldung.testing.assertj.custom; + +public class Car { + private String type; + private Person owner; + + public Car(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public Person getOwner() { + return owner; + } + + public void setOwner(Person owner) { + this.owner = owner; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java new file mode 100644 index 0000000000..413c2d3e12 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java @@ -0,0 +1,30 @@ +package com.baeldung.testing.assertj.custom; + +import org.assertj.core.api.AbstractAssert; + +public class CarAssert extends AbstractAssert { + + public CarAssert(Car actual) { + super(actual, CarAssert.class); + } + + public static CarAssert assertThat(Car actual) { + return new CarAssert(actual); + } + + public CarAssert hasType(String type) { + isNotNull(); + if (!actual.getType().equals(type)) { + failWithMessage("Expected type %s but was %s", type, actual.getType()); + } + return this; + } + + public CarAssert isUsed() { + isNotNull(); + if (actual.getOwner() == null) { + failWithMessage("Expected old but was new"); + } + return this; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java new file mode 100644 index 0000000000..34afc480e4 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.testing.assertj.custom; + +import java.util.ArrayList; +import java.util.List; + +public class Person { + private String fullName; + private int age; + private List nicknames; + + public Person(String fullName, int age) { + this.fullName = fullName; + this.age = age; + this.nicknames = new ArrayList<>(); + } + + public void addNickname(String nickname) { + nicknames.add(nickname); + } + + public String getFullName() { + return fullName; + } + + public int getAge() { + return age; + } + + public List getNicknames() { + return nicknames; + } +} diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java new file mode 100644 index 0000000000..4c071660f3 --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java @@ -0,0 +1,38 @@ +package com.baeldung.testing.assertj.custom; + +import org.assertj.core.api.AbstractAssert; + +public class PersonAssert extends AbstractAssert { + + public PersonAssert(Person actual) { + super(actual, PersonAssert.class); + } + + public static PersonAssert assertThat(Person actual) { + return new PersonAssert(actual); + } + + public PersonAssert hasFullName(String fullName) { + isNotNull(); + if (!actual.getFullName().equals(fullName)) { + failWithMessage("Expected full name %s but was %s", fullName, actual.getFullName()); + } + return this; + } + + public PersonAssert isAdult() { + isNotNull(); + if (actual.getAge() < 18) { + failWithMessage("Expected adult but was juvenile"); + } + return this; + } + + public PersonAssert hasNickname(String nickName) { + isNotNull(); + if (!actual.getNicknames().contains(nickName)) { + failWithMessage("Expected nickname %s but did not have", nickName); + } + return this; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java new file mode 100644 index 0000000000..d438cc42f6 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.CarAssert.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJCarAssertUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenCarTypeDoesNotMatch_thenIncorrect() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected type SUV but was Sedan"); + Car car = new Car("Sedan"); + assertThat(car).hasType("SUV"); + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java new file mode 100644 index 0000000000..8b800de3db --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJCustomAssertionsUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected nickname John but did not have"); + Person person = new Person("John Doe", 20); + person.addNickname("Nick"); + assertThat(person).hasNickname("John"); + } + + @Test + public void whenCarIsUsed_thenCorrect() { + Person person = new Person("Jane Roe", 16); + Car car = new Car("SUV"); + car.setOwner(person); + assertThat(car).isUsed(); + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java new file mode 100644 index 0000000000..ab421915af --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.testing.assertj.custom; + +import static com.baeldung.testing.assertj.custom.PersonAssert.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AssertJPersonAssertUnitTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenPersonNameMatches_thenCorrect() { + Person person = new Person("John Doe", 20); + assertThat(person).hasFullName("John Doe"); + } + + @Test + public void whenPersonAgeLessThanEighteen_thenNotAdult() { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected adult but was juvenile"); + Person person = new Person("Jane Roe", 16); + assertThat(person).isAdult(); + } +} From 950434a87341b4a6f1085b8feb8bb33eb95447f2 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Wed, 31 Jan 2018 16:46:19 +0700 Subject: [PATCH 018/102] Fix the scope of AssertJ core --- testing-modules/testing/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 6f185d3b4c..2804a94244 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -34,7 +34,6 @@ org.assertj assertj-core ${assertj-core.version} - test From 5f87ceb1ffcdaf64f9d4ecf8ee468511c22a9f92 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Wed, 31 Jan 2018 18:34:09 +0700 Subject: [PATCH 019/102] Relocate custom assertion classes --- testing-modules/testing/pom.xml | 1 + .../java/com/baeldung/testing/assertj/custom/Assertions.java | 0 .../java/com/baeldung/testing/assertj/custom/CarAssert.java | 0 .../java/com/baeldung/testing/assertj/custom/PersonAssert.java | 0 4 files changed, 1 insertion(+) rename testing-modules/testing/src/{main => test}/java/com/baeldung/testing/assertj/custom/Assertions.java (100%) rename testing-modules/testing/src/{main => test}/java/com/baeldung/testing/assertj/custom/CarAssert.java (100%) rename testing-modules/testing/src/{main => test}/java/com/baeldung/testing/assertj/custom/PersonAssert.java (100%) diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 2804a94244..6f185d3b4c 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -34,6 +34,7 @@ org.assertj assertj-core ${assertj-core.version} + test diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Assertions.java rename to testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/CarAssert.java rename to testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/PersonAssert.java rename to testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java From bd9a87c137477b26047f9e033e5fa6f049a534ba Mon Sep 17 00:00:00 2001 From: Aprian Diaz Novandi Date: Wed, 31 Jan 2018 15:58:52 +0100 Subject: [PATCH 020/102] Simplify unit test logic (#3548) --- .../baeldung/guava/GuavaMemoizerUnitTest.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java index 0ae1f438e3..1f934347b4 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java @@ -38,7 +38,7 @@ public class GuavaMemoizerUnitTest { int n = 95; // when - BigInteger factorial = new Factorial().getFactorial(n); + BigInteger factorial = Factorial.getFactorial(n); // then BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000"); @@ -47,52 +47,46 @@ public class GuavaMemoizerUnitTest { @Test public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() { + // given Supplier memoizedSupplier; memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber); - Instant start = Instant.now(); - BigInteger bigNumber = memoizedSupplier.get(); - Long durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.ONE); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); - - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.TEN); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); } @Test public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException { + // given Supplier memoizedSupplier; memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS); - Instant start = Instant.now(); - BigInteger bigNumber = memoizedSupplier.get(); - Long durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); - - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.ONE); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + // add one more second until memoized Supplier is evicted from memory TimeUnit.SECONDS.sleep(1); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + } - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.TEN); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + private void assertSupplierGetExecutionResultAndDuration(Supplier supplier, + T expectedValue, + double expectedDurationInMs) { + Instant start = Instant.now(); + T value = supplier.get(); + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); + double marginOfErrorInMs = 100D; + + assertThat(value, is(equalTo(expectedValue))); + assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs))); } } From 43a4d08c363242fd565f2f5cc3c03456b8a1b95d Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 31 Jan 2018 18:50:37 +0100 Subject: [PATCH 021/102] Sudoku refactor (#3556) * BacktrackingAlgorithm refactor * DancingLinks refactor --- .../sudoku/BacktrackingAlgorithm.java | 79 ++++++++++--------- .../algorithms/sudoku/ColumnNode.java | 2 +- .../algorithms/sudoku/DancingLinks.java | 23 +++--- .../sudoku/DancingLinksAlgorithm.java | 46 ++++++----- .../algorithms/sudoku/DancingNode.java | 4 +- 5 files changed, 76 insertions(+), 78 deletions(-) diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java index 127e78900c..dc2a324c12 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -3,25 +3,25 @@ package com.baeldung.algorithms.sudoku; import java.util.stream.IntStream; public class BacktrackingAlgorithm { - - private static int BOARD_SIZE = 9; - private static int SUBSECTION_SIZE = 3; - private static int BOARD_START_INDEX = 0; - - private static int NO_VALUE = 0; - private static int MIN_VALUE = 1; - private static int MAX_VALUE = 9; - public static int[][] board = { - { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, - { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, - { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, - { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, - { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, - { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, - { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, - { 0, 9, 0, 0, 0, 0, 4, 0, 0 } + private static final int BOARD_SIZE = 9; + private static final int SUBSECTION_SIZE = 3; + private static final int BOARD_START_INDEX = 0; + + private static final int NO_VALUE = 0; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 9; + + private static int[][] board = { + {8, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 3, 6, 0, 0, 0, 0, 0}, + {0, 7, 0, 0, 9, 0, 2, 0, 0}, + {0, 5, 0, 0, 0, 7, 0, 0, 0}, + {0, 0, 0, 0, 4, 5, 7, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 3, 0}, + {0, 0, 1, 0, 0, 0, 0, 6, 8}, + {0, 0, 8, 5, 0, 0, 0, 1, 0}, + {0, 9, 0, 0, 0, 0, 4, 0, 0} }; public static void main(String[] args) { @@ -30,7 +30,7 @@ public class BacktrackingAlgorithm { solver.printBoard(); } - public void printBoard() { + private void printBoard() { for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { System.out.print(board[row][column] + " "); @@ -39,7 +39,7 @@ public class BacktrackingAlgorithm { } } - public boolean solve(int[][] board) { + private boolean solve(int[][] board) { for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) { for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) { if (board[r][c] == NO_VALUE) { @@ -47,9 +47,8 @@ public class BacktrackingAlgorithm { board[r][c] = k; if (isValid(board, r, c) && solve(board)) { return true; - } else { - board[r][c] = NO_VALUE; } + board[r][c] = NO_VALUE; } return false; } @@ -58,16 +57,22 @@ public class BacktrackingAlgorithm { return true; } - public boolean isValid(int[][] board, int r, int c) { - return (rowConstraint(board, r) && - columnConstraint(board, c) && - subsectionConstraint(board, r, c)); + private boolean isValid(int[][] board, int r, int c) { + return rowConstraint(board, r) && + columnConstraint(board, c) && + subsectionConstraint(board, r, c); } private boolean subsectionConstraint(int[][] board, int r, int c) { boolean[] constraint = new boolean[BOARD_SIZE]; - for (int i = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; i < (r / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; i++) { - for (int j = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; j < (c / SUBSECTION_SIZE) * SUBSECTION_SIZE + SUBSECTION_SIZE; j++) { + int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE; + + int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE; + + for (int i = subsectionRowStart; i < subsectionRowEnd; i++) { + for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) { if (!checkConstraint(board, i, constraint, j)) return false; } } @@ -76,23 +81,19 @@ public class BacktrackingAlgorithm { private boolean columnConstraint(int[][] board, int c) { boolean[] constraint = new boolean[BOARD_SIZE]; - for (int i = BOARD_START_INDEX; i < BOARD_SIZE; i++) { - if (!checkConstraint(board, i, constraint, c)) return false; - } - return true; + return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) + .allMatch(i -> checkConstraint(board, i, constraint, c)); } private boolean rowConstraint(int[][] board, int r) { boolean[] constraint = new boolean[BOARD_SIZE]; - for (int i = BOARD_START_INDEX; i < BOARD_SIZE; i++) { - if (!checkConstraint(board, r, constraint, i)) return false; - } - return true; + return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) + .allMatch(i -> checkConstraint(board, r, constraint, i)); } private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) { - if (board[r][c] >= MIN_VALUE && board[r][c] <= MAX_VALUE) { - if (constraint[board[r][c] - 1] == false) { + if (board[r][c] != NO_VALUE) { + if (!constraint[board[r][c] - 1]) { constraint[board[r][c] - 1] = true; } else { return false; @@ -100,4 +101,4 @@ public class BacktrackingAlgorithm { } return true; } -} +} \ No newline at end of file diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java index 48538344b6..46995ca42f 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java @@ -4,7 +4,7 @@ class ColumnNode extends DancingNode { int size; String name; - public ColumnNode(String n) { + ColumnNode(String n) { super(); size = 0; name = n; diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java index a30f8ecab5..e5a02b7c91 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java @@ -50,10 +50,9 @@ public class DancingLinks { private ColumnNode makeDLXBoard(boolean[][] grid) { final int COLS = grid[0].length; - final int ROWS = grid.length; ColumnNode headerNode = new ColumnNode("header"); - ArrayList columnNodes = new ArrayList(); + List columnNodes = new ArrayList<>(); for (int i = 0; i < COLS; i++) { ColumnNode n = new ColumnNode(Integer.toString(i)); @@ -62,10 +61,10 @@ public class DancingLinks { } headerNode = headerNode.R.C; - for (int i = 0; i < ROWS; i++) { + for (boolean[] aGrid : grid) { DancingNode prev = null; for (int j = 0; j < COLS; j++) { - if (grid[i][j] == true) { + if (aGrid[j]) { ColumnNode col = columnNodes.get(j); DancingNode newNode = new DancingNode(col); if (prev == null) @@ -82,21 +81,21 @@ public class DancingLinks { return headerNode; } - public DancingLinks(boolean[][] cover) { + DancingLinks(boolean[][] cover) { header = makeDLXBoard(cover); } public void runSolver() { - answer = new LinkedList(); + answer = new LinkedList<>(); search(0); } - public void handleSolution(List answer) { + private void handleSolution(List answer) { int[][] result = parseBoard(answer); printSolution(result); } - int size = 9; + private int size = 9; private int[][] parseBoard(List answer) { int[][] result = new int[size][size]; @@ -120,12 +119,12 @@ public class DancingLinks { return result; } - public static void printSolution(int[][] result) { + private static void printSolution(int[][] result) { int N = result.length; - for (int i = 0; i < N; i++) { - String ret = ""; + for (int[] aResult : result) { + StringBuilder ret = new StringBuilder(); for (int j = 0; j < N; j++) { - ret += result[i][j] + " "; + ret.append(aResult[j]).append(" "); } System.out.println(ret); } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java index 057a15c594..6b0f57a075 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -1,39 +1,37 @@ package com.baeldung.algorithms.sudoku; -import java.util.*; +import java.util.Arrays; public class DancingLinksAlgorithm { - private static int BOARD_SIZE = 9; - private static int SUBSECTION_SIZE = 3; - private static int NO_VALUE = 0; - private static int CONSTRAINTS = 4; - private static int MIN_VALUE = 1; - private static int MAX_VALUE = 9; - private static int COVER_START_INDEX = 1; + private static final int BOARD_SIZE = 9; + private static final int SUBSECTION_SIZE = 3; + private static final int NO_VALUE = 0; + private static final int CONSTRAINTS = 4; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 9; + private static final int COVER_START_INDEX = 1; - public static int[][] board = { - { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, - { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, - { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, - { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, - { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, - { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, - { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, - { 0, 9, 0, 0, 0, 0, 4, 0, 0 } - }; + private static int[][] board = { + {8, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 3, 6, 0, 0, 0, 0, 0}, + {0, 7, 0, 0, 9, 0, 2, 0, 0}, + {0, 5, 0, 0, 0, 7, 0, 0, 0}, + {0, 0, 0, 0, 4, 5, 7, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 3, 0}, + {0, 0, 1, 0, 0, 0, 0, 6, 8}, + {0, 0, 8, 5, 0, 0, 0, 1, 0}, + {0, 9, 0, 0, 0, 0, 4, 0, 0} + }; public static void main(String[] args) { DancingLinksAlgorithm solver = new DancingLinksAlgorithm(); solver.solve(board); } - public boolean solve(int[][] board) { + private void solve(int[][] board) { boolean[][] cover = initializeExactCoverBoard(board); DancingLinks dlx = new DancingLinks(cover); dlx.runSolver(); - - return true; } private int getIndex(int row, int col, int num) { @@ -54,7 +52,7 @@ public class DancingLinksAlgorithm { } } } - + // Row constrain. for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { @@ -74,7 +72,7 @@ public class DancingLinksAlgorithm { } } } - + // Subsection constraint for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java index 13dc3f2b57..b494eba9ef 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -39,11 +39,11 @@ class DancingNode { this.U.D = this.D.U = this; } - public DancingNode() { + DancingNode() { L = R = U = D = this; } - public DancingNode(ColumnNode c) { + DancingNode(ColumnNode c) { this(); C = c; } From 6e23e8f4fdc8bf1b1e37570c6d69fad80cef781d Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Thu, 1 Feb 2018 03:16:53 +0700 Subject: [PATCH 022/102] Initial commit for BAEL-1516 --- .../com/baeldung/testing/assertj/Member.java | 19 +++++ .../assertj/AssertJConditionUnitTest.java | 72 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java new file mode 100644 index 0000000000..a0b3d0daac --- /dev/null +++ b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java @@ -0,0 +1,19 @@ +package com.baeldung.testing.assertj; + +public class Member { + private String name; + private int age; + + public Member(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java new file mode 100644 index 0000000000..153af828f1 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java @@ -0,0 +1,72 @@ +package com.baeldung.testing.assertj; + +import static org.assertj.core.api.Assertions.allOf; +import static org.assertj.core.api.Assertions.anyOf; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.not; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.assertj.core.api.Condition; +import org.junit.Test; + +public class AssertJConditionUnitTest { + private Condition senior = new Condition<>(m -> m.getAge() >= 60, "senior"); + private Condition nameJohn = new Condition<>(m -> m.getName().equalsIgnoreCase("John"), "name John"); + + @Test + public void whenUsingMemberAgeCondition_thenCorrect() { + Member member = new Member("John", 65); + assertThat(member).is(senior); + + try { + assertThat(member).isNot(senior); + fail(); + } catch (AssertionError e) { + assertThat(e).hasMessageContaining("not to be "); + } + } + + @Test + public void whenUsingMemberNameCondition_thenCorrect() { + Member member = new Member("Jane", 60); + assertThat(member).doesNotHave(nameJohn); + + try { + assertThat(member).has(nameJohn); + fail(); + } catch (AssertionError e) { + assertThat(e).hasMessageContaining("to have:\n "); + } + } + + @Test + public void whenCollectionConditionsAreSatisfied_thenCorrect() { + List members = new ArrayList<>(); + members.add(new Member("Alice", 50)); + members.add(new Member("Bob", 60)); + + assertThat(members).haveExactly(1, senior); + assertThat(members).doNotHave(nameJohn); + } + + @Test + public void whenCombiningAllOfConditions_thenCorrect() { + Member john = new Member("John", 60); + Member jane = new Member("Jane", 50); + + assertThat(john).is(allOf(senior, nameJohn)); + assertThat(jane).is(allOf(not(nameJohn), not(senior))); + } + + @Test + public void whenCombiningAnyOfConditions_thenCorrect() { + Member john = new Member("John", 50); + Member jane = new Member("Jane", 60); + + assertThat(john).is(anyOf(senior, nameJohn)); + assertThat(jane).is(anyOf(nameJohn, senior)); + } +} From 3e4b6df194059c0fc95c22e7fe2bcf07ea6c1224 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Thu, 1 Feb 2018 02:36:16 +0530 Subject: [PATCH 023/102] Interrupted thread before logging (#3563) --- .../main/java/com/baeldung/concurrent/waitandnotify/Data.java | 4 ++-- .../java/com/baeldung/concurrent/waitandnotify/Receiver.java | 1 + .../java/com/baeldung/concurrent/waitandnotify/Sender.java | 1 + .../concurrent/waitandnotify/NetworkIntegrationTest.java | 3 ++- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java index d9e7434e0c..9d76c1fcd1 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java @@ -1,7 +1,5 @@ package com.baeldung.concurrent.waitandnotify; -import org.slf4j.Logger; - public class Data { private String packet; @@ -14,6 +12,7 @@ public class Data { try { wait(); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); System.out.println("Thread Interrupted"); } } @@ -28,6 +27,7 @@ public class Data { try { wait(); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); System.out.println("Thread Interrupted"); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java index 724e908a99..21ba822bfd 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java @@ -20,6 +20,7 @@ public class Receiver implements Runnable { try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); System.out.println("Thread Interrupted"); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java index b4945b7b73..c365294cdd 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java @@ -25,6 +25,7 @@ public class Sender implements Runnable { try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); System.out.println("Thread Interrupted"); } } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java index 8ecc92236e..e2bc328df3 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java @@ -57,7 +57,8 @@ public class NetworkIntegrationTest { sender.join(); receiver.join(); } catch (InterruptedException e) { - e.printStackTrace(); + Thread.currentThread().interrupt(); + System.out.println("Thread Interrupted"); } assertEquals(expected, outContent.toString()); From 63b8aa334f89ab042e4203f99d032b03a1dd8b60 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Thu, 1 Feb 2018 11:00:51 +0700 Subject: [PATCH 024/102] Refactor BAEL-1515 --- testing-modules/testing/pom.xml | 1 - .../custom/AssertJCarAssertUnitTest.java | 20 ----------- .../AssertJCustomAssertionsUnitTest.java | 35 +++++++++++++------ .../custom/AssertJPersonAssertUnitTest.java | 26 -------------- .../testing/assertj/custom/Assertions.java | 4 +-- .../testing/assertj/custom/CarAssert.java | 30 ---------------- 6 files changed, 26 insertions(+), 90 deletions(-) delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java delete mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index 6f185d3b4c..c76045380b 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -161,7 +161,6 @@ com.baeldung.testing.assertj.custom.Person - com.baeldung.testing.assertj.custom.Car diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java deleted file mode 100644 index d438cc42f6..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCarAssertUnitTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.testing.assertj.custom; - -import static com.baeldung.testing.assertj.custom.CarAssert.assertThat; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class AssertJCarAssertUnitTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void whenCarTypeDoesNotMatch_thenIncorrect() { - thrown.expect(AssertionError.class); - thrown.expectMessage("Expected type SUV but was Sedan"); - Car car = new Car("Sedan"); - assertThat(car).hasType("SUV"); - } -} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java index 8b800de3db..f9b5bad039 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.testing.assertj.custom; import static com.baeldung.testing.assertj.custom.Assertions.assertThat; +import static org.junit.Assert.fail; import org.junit.Rule; import org.junit.Test; @@ -9,21 +10,35 @@ import org.junit.rules.ExpectedException; public class AssertJCustomAssertionsUnitTest { @Rule public ExpectedException thrown = ExpectedException.none(); - + @Test - public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { - thrown.expect(AssertionError.class); - thrown.expectMessage("Expected nickname John but did not have"); + public void whenPersonNameMatches_thenCorrect() { Person person = new Person("John Doe", 20); - person.addNickname("Nick"); - assertThat(person).hasNickname("John"); + assertThat(person).hasFullName("John Doe"); } @Test - public void whenCarIsUsed_thenCorrect() { + public void whenPersonAgeLessThanEighteen_thenNotAdult() { Person person = new Person("Jane Roe", 16); - Car car = new Car("SUV"); - car.setOwner(person); - assertThat(car).isUsed(); + + try { + assertThat(person).isAdult(); + fail(); + } catch (AssertionError e) { + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected adult but was juvenile"); + } + } + + @Test + public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { + Person person = new Person("John Doe", 20); + person.addNickname("Nick"); + + try { + assertThat(person).hasNickname("John"); + fail(); + } catch (AssertionError e) { + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected nickname John but did not have"); + } } } diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java deleted file mode 100644 index ab421915af..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJPersonAssertUnitTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.testing.assertj.custom; - -import static com.baeldung.testing.assertj.custom.PersonAssert.assertThat; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class AssertJPersonAssertUnitTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void whenPersonNameMatches_thenCorrect() { - Person person = new Person("John Doe", 20); - assertThat(person).hasFullName("John Doe"); - } - - @Test - public void whenPersonAgeLessThanEighteen_thenNotAdult() { - thrown.expect(AssertionError.class); - thrown.expectMessage("Expected adult but was juvenile"); - Person person = new Person("Jane Roe", 16); - assertThat(person).isAdult(); - } -} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java index 5c72eb6d05..fcffb8fc6c 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java @@ -5,7 +5,5 @@ public class Assertions { return new PersonAssert(actual); } - public static CarAssert assertThat(Car actual) { - return new CarAssert(actual); - } + // static factory methods of other assertion classes } diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java deleted file mode 100644 index 413c2d3e12..0000000000 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/CarAssert.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.testing.assertj.custom; - -import org.assertj.core.api.AbstractAssert; - -public class CarAssert extends AbstractAssert { - - public CarAssert(Car actual) { - super(actual, CarAssert.class); - } - - public static CarAssert assertThat(Car actual) { - return new CarAssert(actual); - } - - public CarAssert hasType(String type) { - isNotNull(); - if (!actual.getType().equals(type)) { - failWithMessage("Expected type %s but was %s", type, actual.getType()); - } - return this; - } - - public CarAssert isUsed() { - isNotNull(); - if (actual.getOwner() == null) { - failWithMessage("Expected old but was new"); - } - return this; - } -} From 15f18bbb83a7472328225851f527db78f938c771 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Thu, 1 Feb 2018 15:27:48 +0530 Subject: [PATCH 025/102] BAEL 1269 Intro to JSON-JAVA (#3493) * Final commit * Made changes as per last review * Moved from core-java to json module --- json/pom.xml | 5 ++ .../java/com/baeldung/jsonjava/CDLDemo.java | 59 +++++++++++++++++++ .../com/baeldung/jsonjava/CookieDemo.java | 30 ++++++++++ .../java/com/baeldung/jsonjava/DemoBean.java | 26 ++++++++ .../java/com/baeldung/jsonjava/HTTPDemo.java | 27 +++++++++ .../com/baeldung/jsonjava/JSONArrayDemo.java | 52 ++++++++++++++++ .../com/baeldung/jsonjava/JSONObjectDemo.java | 59 +++++++++++++++++++ .../baeldung/jsonjava/JSONTokenerDemo.java | 13 ++++ .../baeldung/jsonjava/CDLIntegrationTest.java | 52 ++++++++++++++++ .../jsonjava/CookieIntegrationTest.java | 29 +++++++++ .../jsonjava/HTTPIntegrationTest.java | 25 ++++++++ .../jsonjava/JSONArrayIntegrationTest.java | 47 +++++++++++++++ .../jsonjava/JSONObjectIntegrationTest.java | 53 +++++++++++++++++ .../jsonjava/JSONTokenerIntegrationTest.java | 21 +++++++ 14 files changed, 498 insertions(+) create mode 100644 json/src/main/java/com/baeldung/jsonjava/CDLDemo.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/CookieDemo.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/DemoBean.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java create mode 100644 json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java create mode 100644 json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java diff --git a/json/pom.xml b/json/pom.xml index 958dd32f53..7c74d425af 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -31,6 +31,11 @@ ${fastjson.version} + + org.json + json + 20171018 + diff --git a/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java b/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java new file mode 100644 index 0000000000..f5fee0c4a9 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/CDLDemo.java @@ -0,0 +1,59 @@ +package com.baeldung.jsonjava; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONTokener; + +public class CDLDemo { + public static void main(String[] args) { + System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: "); + jsonArrayFromCDT(); + + System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: "); + cDTfromJSONArray(); + + System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: "); + jaOfJOFromCDT2(); + + System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: "); + jaOfJOFromCDT2(); + } + + public static void jsonArrayFromCDT() { + JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada")); + System.out.println(ja); + } + + public static void cDTfromJSONArray() { + JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]"); + String cdt = CDL.rowToString(ja); + System.out.println(cdt); + } + + public static void jaOfJOFromCDT() { + String string = + "name, city, age \n" + + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(string); + System.out.println(result.toString()); + } + + public static void jaOfJOFromCDT2() { + JSONArray ja = new JSONArray(); + ja.put("name"); + ja.put("city"); + ja.put("age"); + + String string = + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(ja, string); + System.out.println(result.toString()); + } + +} diff --git a/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java b/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java new file mode 100644 index 0000000000..bc39d13642 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/CookieDemo.java @@ -0,0 +1,30 @@ +package com.baeldung.jsonjava; + +import org.json.Cookie; +import org.json.JSONObject; + +public class CookieDemo { + public static void main(String[] args) { + System.out.println("8.1. Converting a Cookie String into a JSONObject"); + cookieStringToJSONObject(); + + System.out.println("\n8.2. Converting a JSONObject into Cookie String"); + jSONObjectToCookieString(); + } + + public static void cookieStringToJSONObject() { + String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"; + JSONObject cookieJO = Cookie.toJSONObject(cookie); + System.out.println(cookieJO); + } + + public static void jSONObjectToCookieString() { + JSONObject cookieJO = new JSONObject(); + cookieJO.put("name", "username"); + cookieJO.put("value", "John Doe"); + cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC"); + cookieJO.put("path", "/"); + String cookie = Cookie.toString(cookieJO); + System.out.println(cookie); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/DemoBean.java b/json/src/main/java/com/baeldung/jsonjava/DemoBean.java new file mode 100644 index 0000000000..6d27b329c2 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/DemoBean.java @@ -0,0 +1,26 @@ +package com.baeldung.jsonjava; + +public class DemoBean { + private int id; + private String name; + private boolean active; + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public boolean isActive() { + return active; + } + public void setActive(boolean active) { + this.active = active; + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java b/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java new file mode 100644 index 0000000000..800018005c --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/HTTPDemo.java @@ -0,0 +1,27 @@ +package com.baeldung.jsonjava; + +import org.json.HTTP; +import org.json.JSONObject; + +public class HTTPDemo { + public static void main(String[] args) { + System.out.println("9.1. Converting JSONObject to HTTP Header: "); + jSONObjectToHTTPHeader(); + + System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: "); + hTTPHeaderToJSONObject(); + } + + public static void jSONObjectToHTTPHeader() { + JSONObject jo = new JSONObject(); + jo.put("Method", "POST"); + jo.put("Request-URI", "http://www.example.com/"); + jo.put("HTTP-Version", "HTTP/1.1"); + System.out.println(HTTP.toString(jo)); + } + + public static void hTTPHeaderToJSONObject() { + JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1"); + System.out.println(obj); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java new file mode 100644 index 0000000000..2a4fab2eab --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONArrayDemo.java @@ -0,0 +1,52 @@ +package com.baeldung.jsonjava; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class JSONArrayDemo { + public static void main(String[] args) { + System.out.println("5.1. Creating JSON Array: "); + creatingJSONArray(); + + System.out.println("\n5.2. Creating JSON Array from JSON string: "); + jsonArrayFromJSONString(); + + System.out.println("\n5.3. Creating JSON Array from Collection Object: "); + jsonArrayFromCollectionObj(); + } + + public static void creatingJSONArray() { + JSONArray ja = new JSONArray(); + ja.put(Boolean.TRUE); + ja.put("lorem ipsum"); + + // We can also put a JSONObject in JSONArray + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + ja.put(jo); + + System.out.println(ja.toString()); + } + + public static void jsonArrayFromJSONString() { + JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]"); + System.out.println(ja); + } + + public static void jsonArrayFromCollectionObj() { + List list = new ArrayList<>(); + list.add("California"); + list.add("Texas"); + list.add("Hawaii"); + list.add("Alaska"); + + JSONArray ja = new JSONArray(list); + System.out.println(ja); + } +} \ No newline at end of file diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java new file mode 100644 index 0000000000..cfe8467c30 --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONObjectDemo.java @@ -0,0 +1,59 @@ +package com.baeldung.jsonjava; + +import java.util.HashMap; +import java.util.Map; + +import org.json.JSONObject; + +public class JSONObjectDemo { + public static void main(String[] args) { + System.out.println("4.1. Creating JSONObject: "); + jsonFromJSONObject(); + + System.out.println("\n4.2. Creating JSONObject from Map: "); + jsonFromMap(); + + System.out.println("\n4.3. Creating JSONObject from JSON string: "); + jsonFromJSONString(); + + System.out.println("\n4.4. Creating JSONObject from Java Bean: "); + jsonFromDemoBean(); + } + + public static void jsonFromJSONObject() { + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + System.out.println(jo.toString()); + } + + public static void jsonFromMap() { + Map map = new HashMap<>(); + map.put("name", "jon doe"); + map.put("age", "22"); + map.put("city", "chicago"); + JSONObject jo = new JSONObject(map); + + System.out.println(jo.toString()); + } + + public static void jsonFromJSONString() { + JSONObject jo = new JSONObject( + "{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}" + ); + + System.out.println(jo.toString()); + } + + public static void jsonFromDemoBean() { + DemoBean demo = new DemoBean(); + demo.setId(1); + demo.setName("lorem ipsum"); + demo.setActive(true); + + JSONObject jo = new JSONObject(demo); + System.out.println(jo); + } +} diff --git a/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java b/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java new file mode 100644 index 0000000000..fb9fea959c --- /dev/null +++ b/json/src/main/java/com/baeldung/jsonjava/JSONTokenerDemo.java @@ -0,0 +1,13 @@ +package com.baeldung.jsonjava; + +import org.json.JSONTokener; + +public class JSONTokenerDemo { + public static void main(String[] args) { + JSONTokener jt = new JSONTokener("Sample String"); + + while(jt.more()) { + System.out.println(jt.next()); + } + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java new file mode 100644 index 0000000000..441c71e78e --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/CDLIntegrationTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONTokener; +import org.junit.Test; + +public class CDLIntegrationTest { + @Test + public void givenCommaDelimitedText_thenConvertToJSONArray() { + JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada")); + assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString()); + } + + @Test + public void givenJSONArray_thenConvertToCommaDelimitedText() { + JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]"); + String cdt = CDL.rowToString(ja); + assertEquals("England,USA,Canada", cdt.toString().trim()); + } + + @Test + public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() { + String string = + "name, city, age \n" + + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(string); + assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString()); + } + + @Test + public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() { + JSONArray ja = new JSONArray(); + ja.put("name"); + ja.put("city"); + ja.put("age"); + + String string = + "john, chicago, 22 \n" + + "gary, florida, 35 \n" + + "sal, vegas, 18"; + + JSONArray result = CDL.toJSONArray(ja, string); + assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString()); + } + +} diff --git a/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java new file mode 100644 index 0000000000..c1a3505bbc --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java @@ -0,0 +1,29 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.Cookie; +import org.json.JSONObject; +import org.junit.Test; + +public class CookieIntegrationTest { + @Test + public void givenCookieString_thenConvertToJSONObject() { + String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"; + JSONObject cookieJO = Cookie.toJSONObject(cookie); + + assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString()); + } + + @Test + public void givenJSONObject_thenConvertToCookieString() { + JSONObject cookieJO = new JSONObject(); + cookieJO.put("name", "username"); + cookieJO.put("value", "John Doe"); + cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC"); + cookieJO.put("path", "/"); + String cookie = Cookie.toString(cookieJO); + + assertEquals("username=John Doe;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/", cookie.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java new file mode 100644 index 0000000000..1aa0427c7e --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/HTTPIntegrationTest.java @@ -0,0 +1,25 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; +import org.json.HTTP; +import org.json.JSONObject; +import org.junit.Test; + +public class HTTPIntegrationTest { + @Test + public void givenJSONObject_thenConvertToHTTPHeader() { + JSONObject jo = new JSONObject(); + jo.put("Method", "POST"); + jo.put("Request-URI", "http://www.example.com/"); + jo.put("HTTP-Version", "HTTP/1.1"); + + assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo)); + } + + @Test + public void givenHTTPHeader_thenConvertToJSONObject() { + JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1"); + + assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java new file mode 100644 index 0000000000..c956232abe --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONArrayIntegrationTest.java @@ -0,0 +1,47 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; + +public class JSONArrayIntegrationTest { + @Test + public void givenJSONJava_thenCreateNewJSONArrayFromScratch() { + JSONArray ja = new JSONArray(); + ja.put(Boolean.TRUE); + ja.put("lorem ipsum"); + + // We can also put a JSONObject in JSONArray + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + ja.put(jo); + + assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString()); + } + + @Test + public void givenJsonString_thenCreateNewJSONArray() { + JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]"); + assertEquals("[true,\"lorem ipsum\",215]", ja.toString()); + } + + @Test + public void givenListObject_thenConvertItToJSONArray() { + List list = new ArrayList<>(); + list.add("California"); + list.add("Texas"); + list.add("Hawaii"); + list.add("Alaska"); + + JSONArray ja = new JSONArray(list); + assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString()); + } +} \ No newline at end of file diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java new file mode 100644 index 0000000000..70f7921797 --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONObjectIntegrationTest.java @@ -0,0 +1,53 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.json.JSONObject; +import org.junit.Test; + +public class JSONObjectIntegrationTest { + @Test + public void givenJSONJava_thenCreateNewJSONObject() { + JSONObject jo = new JSONObject(); + jo.put("name", "jon doe"); + jo.put("age", "22"); + jo.put("city", "chicago"); + + assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString()); + + } + + @Test + public void givenMapObject_thenCreateJSONObject() { + Map map = new HashMap<>(); + map.put("name", "jon doe"); + map.put("age", "22"); + map.put("city", "chicago"); + JSONObject jo = new JSONObject(map); + + assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString()); + } + + @Test + public void givenJsonString_thenCreateJSONObject() { + JSONObject jo = new JSONObject( + "{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}" + ); + + assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString()); + } + + @Test + public void givenDemoBean_thenCreateJSONObject() { + DemoBean demo = new DemoBean(); + demo.setId(1); + demo.setName("lorem ipsum"); + demo.setActive(true); + + JSONObject jo = new JSONObject(demo); + assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString()); + } +} diff --git a/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java new file mode 100644 index 0000000000..4fe8f27231 --- /dev/null +++ b/json/src/test/java/com/baeldung/jsonjava/JSONTokenerIntegrationTest.java @@ -0,0 +1,21 @@ +package com.baeldung.jsonjava; + +import static org.junit.Assert.assertEquals; + +import org.json.JSONTokener; +import org.junit.Test; + +public class JSONTokenerIntegrationTest { + @Test + public void givenString_convertItToJSONTokens() { + String str = "Sample String"; + JSONTokener jt = new JSONTokener(str); + + char[] expectedTokens = str.toCharArray(); + int index = 0; + + while(jt.more()) { + assertEquals(expectedTokens[index++], jt.next()); + } + } +} From 72c9fea7e23c479468464b9ff7e4470d11aaf6eb Mon Sep 17 00:00:00 2001 From: tamasradu Date: Thu, 1 Feb 2018 15:56:36 +0200 Subject: [PATCH 026/102] Radutamas/bael 1487 (#3569) * Code for test article: Different Types of Bean Injection in Spring * Adding jUnits for test article: Different Types of Bean Injection in Spring * BAEL-1265: Adding jUnit for article * BAEL-1265: Closing ExecutorService in jUnit * BAEL-1487: Adding test for AsyncHtpClient tutorial --- libraries/pom.xml | 9 +- .../AsyncHttpClientTestCase.java | 219 ++++++++++++++++++ 2 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java diff --git a/libraries/pom.xml b/libraries/pom.xml index a330494bb3..e8f7c470a3 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -116,6 +116,12 @@ + + + org.asynchttpclient + async-http-client + ${async.http.client.version} + org.beykery @@ -766,7 +772,7 @@ 1.1.3-rc.5 1.4.0 1.1.0 - 4.1.15.Final + 4.1.20.Final 4.1 4.12 0.10 @@ -796,5 +802,6 @@ 1.0.0 1.7.0 3.0.14 + 2.2.0 \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java new file mode 100644 index 0000000000..7f9c2699fe --- /dev/null +++ b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java @@ -0,0 +1,219 @@ +package com.baeldung.asynchttpclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.asynchttpclient.AsyncCompletionHandler; +import org.asynchttpclient.AsyncHandler; +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.AsyncHttpClientConfig; +import org.asynchttpclient.BoundRequestBuilder; +import org.asynchttpclient.Dsl; +import org.asynchttpclient.HttpResponseBodyPart; +import org.asynchttpclient.HttpResponseStatus; +import org.asynchttpclient.ListenableFuture; +import org.asynchttpclient.Request; +import org.asynchttpclient.Response; +import org.asynchttpclient.ws.WebSocket; +import org.asynchttpclient.ws.WebSocketListener; +import org.asynchttpclient.ws.WebSocketUpgradeHandler; +import org.junit.Before; +import org.junit.Test; + +import io.netty.handler.codec.http.HttpHeaders; + +public class AsyncHttpClientTestCase { + + private static AsyncHttpClient HTTP_CLIENT; + + @Before + public void setup() { + + AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); + HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); + } + + @Test + public void givenHttpClient_executeSyncGetRequest() { + + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + + Future responseFuture = boundGetRequest.execute(); + try { + Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); + assertNotNull(response); + assertEquals(200, response.getStatusCode()); + + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequest() { + + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) throws Exception { + + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); + + // execute a bound GET request + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + + boundGetRequest.execute(new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) throws Exception { + + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { + + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { + + int responseStatusCode = -1; + + @Override + public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception { + responseStatusCode = responseStatus.getStatusCode(); + return State.CONTINUE; + } + + @Override + public State onHeadersReceived(HttpHeaders headers) throws Exception { + return State.CONTINUE; + } + + @Override + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { + return State.CONTINUE; + } + + @Override + public void onThrowable(Throwable t) { + + } + + @Override + public Integer onCompleted() throws Exception { + assertEquals(200, responseStatusCode); + return responseStatusCode; + } + }); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + + ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); + listenableFuture.addListener(() -> { + Response response; + try { + response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + + }, Executors.newCachedThreadPool()); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void givenWebSocketClient_tryToConnect() { + + WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); + WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { + @Override + public void onOpen(WebSocket websocket) { + // WebSocket connection opened + } + + @Override + public void onClose(WebSocket websocket, int code, String reason) { + // WebSocket connection closed + } + + @Override + public void onError(Throwable t) { + // WebSocket connection error + assertTrue(t.getMessage().contains("Request timeout")); + } + }).build(); + + WebSocket WEBSOCKET_CLIENT = null; + try { + WEBSOCKET_CLIENT = Dsl.asyncHttpClient() + .prepareGet("ws://localhost:5590/websocket") + .addHeader("header_name", "header_value") + .addQueryParam("key", "value") + .setRequestTimeout(5000) + .execute(wsHandler).get(); + + if (WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendPingFrame(); + WEBSOCKET_CLIENT.sendTextFrame("test message"); + WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' }); + } + + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } finally { + if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); + } + } + } +} From 310e31e2bd5c1ca5a4f021268f09e6b0808807e5 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Thu, 1 Feb 2018 20:30:54 +0100 Subject: [PATCH 027/102] Code for BAEL-68 (#3559) --- .../HibernateBootstrapIntegrationTest.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java index ffe82b7ced..c41423643a 100644 --- a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/bootstrap/HibernateBootstrapIntegrationTest.java @@ -8,11 +8,16 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.Commit; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.test.context.transaction.TestTransaction; import org.springframework.transaction.annotation.Transactional; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { HibernateConf.class }) @Transactional @@ -35,4 +40,146 @@ public class HibernateBootstrapIntegrationTest { Assert.assertNotNull(searchEntity); } + @Test + public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { + assertTrue(TestTransaction.isActive()); + + //Save an entity and commit. + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + assertTrue(TestTransaction.isFlaggedForRollback()); + + TestTransaction.flagForCommit(); + TestTransaction.end(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it, but don't commit. + TestTransaction.start(); + + assertTrue(TestTransaction.isFlaggedForRollback()); + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it and commit. + TestTransaction.start(); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + assertTrue(TestTransaction.isActive()); + + TestTransaction.flagForCommit(); + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is no longer there in a new transaction. + TestTransaction.start(); + + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNull(searchEntity); + } + + @Test + @Commit + public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase() { + assertTrue(TestTransaction.isActive()); + + //Save an entity and commit. + Session session = sessionFactory.getCurrentSession(); + + TestEntity newEntity = new TestEntity(); + newEntity.setId(1); + session.save(newEntity); + + TestEntity searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + assertFalse(TestTransaction.isFlaggedForRollback()); + + TestTransaction.end(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it, but don't commit. + TestTransaction.start(); + + assertFalse(TestTransaction.isFlaggedForRollback()); + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + TestTransaction.flagForRollback(); + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is still there in a new transaction, + //then delete it and commit. + TestTransaction.start(); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNotNull(searchEntity); + + session.delete(searchEntity); + session.flush(); + + assertTrue(TestTransaction.isActive()); + + TestTransaction.end(); + + assertFalse(TestTransaction.isActive()); + + //Check that the entity is no longer there in a new transaction. + TestTransaction.start(); + + assertTrue(TestTransaction.isActive()); + + session = sessionFactory.getCurrentSession(); + searchEntity = session.find(TestEntity.class, 1); + + Assert.assertNull(searchEntity); + } + } From 55660e770254f96e274c98801a0ad9a60e0489f0 Mon Sep 17 00:00:00 2001 From: myluckagain Date: Fri, 2 Feb 2018 03:17:53 +0500 Subject: [PATCH 028/102] BAEL-1497 (#3568) --- .../designpatterns/observer/Channel.java | 5 ++ .../designpatterns/observer/NewsAgency.java | 24 ++++++++++ .../designpatterns/observer/NewsChannel.java | 20 ++++++++ .../designpatterns/observer/ONewsAgency.java | 13 +++++ .../designpatterns/observer/ONewsChannel.java | 22 +++++++++ .../observer/PCLNewsAgency.java | 28 +++++++++++ .../observer/PCLNewsChannel.java | 21 +++++++++ .../observer/ObserverIntegrationTest.java | 47 +++++++++++++++++++ 8 files changed, 180 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java create mode 100644 core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java new file mode 100644 index 0000000000..9ca2edac38 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/Channel.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.observer; + +public interface Channel { + public void update(Object o); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java new file mode 100644 index 0000000000..0330fbdcdc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsAgency.java @@ -0,0 +1,24 @@ +package com.baeldung.designpatterns.observer; + +import java.util.ArrayList; +import java.util.List; + +public class NewsAgency { + private String news; + private List channels = new ArrayList<>(); + + public void addObserver(Channel channel) { + this.channels.add(channel); + } + + public void removeObserver(Channel channel) { + this.channels.remove(channel); + } + + public void setNews(String news) { + this.news = news; + for (Channel channel : this.channels) { + channel.update(this.news); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java new file mode 100644 index 0000000000..09c22e0ad8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/NewsChannel.java @@ -0,0 +1,20 @@ +package com.baeldung.designpatterns.observer; + +public class NewsChannel implements Channel { + + private String news; + + @Override + public void update(Object news) { + this.setNews((String) news); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } + +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java new file mode 100644 index 0000000000..2849820663 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsAgency.java @@ -0,0 +1,13 @@ +package com.baeldung.designpatterns.observer; + +import java.util.Observable; + +public class ONewsAgency extends Observable { + private String news; + + public void setNews(String news) { + this.news = news; + setChanged(); + notifyObservers(news); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java new file mode 100644 index 0000000000..3989fe0286 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/ONewsChannel.java @@ -0,0 +1,22 @@ +package com.baeldung.designpatterns.observer; + +import java.util.Observable; +import java.util.Observer; + +public class ONewsChannel implements Observer { + + private String news; + + @Override + public void update(Observable o, Object news) { + this.setNews((String) news); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java new file mode 100644 index 0000000000..b05b97ab0b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsAgency.java @@ -0,0 +1,28 @@ +package com.baeldung.designpatterns.observer; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; + +public class PCLNewsAgency { + private String news; + + private PropertyChangeSupport support; + + public PCLNewsAgency() { + support = new PropertyChangeSupport(this); + } + + public void addPropertyChangeListener(PropertyChangeListener pcl) { + support.addPropertyChangeListener(pcl); + } + + public void removePropertyChangeListener(PropertyChangeListener pcl) { + support.removePropertyChangeListener(pcl); + } + + public void setNews(String value) { + support.firePropertyChange("news", this.news, value); + this.news = value; + + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java new file mode 100644 index 0000000000..ff8d35463c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/observer/PCLNewsChannel.java @@ -0,0 +1,21 @@ +package com.baeldung.designpatterns.observer; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +public class PCLNewsChannel implements PropertyChangeListener { + + private String news; + + public void propertyChange(PropertyChangeEvent evt) { + this.setNews((String) evt.getNewValue()); + } + + public String getNews() { + return news; + } + + public void setNews(String news) { + this.news = news; + } +} diff --git a/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java new file mode 100644 index 0000000000..a8a0e29990 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/designpatterns/observer/ObserverIntegrationTest.java @@ -0,0 +1,47 @@ +package com.baeldung.designpatterns.observer; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.baeldung.designpatterns.observer.NewsAgency; +import com.baeldung.designpatterns.observer.NewsChannel; + +public class ObserverIntegrationTest { + + @Test + public void whenChangingNewsAgencyState_thenNewsChannelNotified() { + + NewsAgency observable = new NewsAgency(); + NewsChannel observer = new NewsChannel(); + + observable.addObserver(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } + + @Test + public void whenChangingONewsAgencyState_thenONewsChannelNotified() { + + ONewsAgency observable = new ONewsAgency(); + ONewsChannel observer = new ONewsChannel(); + + observable.addObserver(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } + + @Test + public void whenChangingPCLNewsAgencyState_thenONewsChannelNotified() { + + PCLNewsAgency observable = new PCLNewsAgency(); + PCLNewsChannel observer = new PCLNewsChannel(); + + observable.addPropertyChangeListener(observer); + + observable.setNews("news"); + assertEquals(observer.getNews(), "news"); + } +} From c057808c6e9b86a97e1fbf5be8af050ed82a8f60 Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Fri, 2 Feb 2018 11:59:17 +0700 Subject: [PATCH 029/102] Modifies error messages --- .../assertj/custom/AssertJCustomAssertionsUnitTest.java | 4 ++-- .../com/baeldung/testing/assertj/custom/PersonAssert.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java index f9b5bad039..4c09311bac 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -25,7 +25,7 @@ public class AssertJCustomAssertionsUnitTest { assertThat(person).isAdult(); fail(); } catch (AssertionError e) { - org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected adult but was juvenile"); + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to be adult"); } } @@ -38,7 +38,7 @@ public class AssertJCustomAssertionsUnitTest { assertThat(person).hasNickname("John"); fail(); } catch (AssertionError e) { - org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected nickname John but did not have"); + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to have nickname John"); } } } diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java index 4c071660f3..d6cc585e96 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java @@ -15,7 +15,7 @@ public class PersonAssert extends AbstractAssert { public PersonAssert hasFullName(String fullName) { isNotNull(); if (!actual.getFullName().equals(fullName)) { - failWithMessage("Expected full name %s but was %s", fullName, actual.getFullName()); + failWithMessage("Expected person to have full name %s but was %s", fullName, actual.getFullName()); } return this; } @@ -23,7 +23,7 @@ public class PersonAssert extends AbstractAssert { public PersonAssert isAdult() { isNotNull(); if (actual.getAge() < 18) { - failWithMessage("Expected adult but was juvenile"); + failWithMessage("Expected person to be adult"); } return this; } @@ -31,7 +31,7 @@ public class PersonAssert extends AbstractAssert { public PersonAssert hasNickname(String nickName) { isNotNull(); if (!actual.getNicknames().contains(nickName)) { - failWithMessage("Expected nickname %s but did not have", nickName); + failWithMessage("Expected person to have nickname %s", nickName); } return this; } From 2169be43014347905e0ff6a35e25020953c3bfc8 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 2 Feb 2018 15:40:03 +0100 Subject: [PATCH 030/102] Async refactor (#3570) --- .../AsyncHttpClientTestCase.java | 318 +++++++++--------- 1 file changed, 154 insertions(+), 164 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java index 7f9c2699fe..1398c2ba41 100644 --- a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java +++ b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java @@ -1,15 +1,6 @@ package com.baeldung.asynchttpclient; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - +import io.netty.handler.codec.http.HttpHeaders; import org.asynchttpclient.AsyncCompletionHandler; import org.asynchttpclient.AsyncHandler; import org.asynchttpclient.AsyncHttpClient; @@ -27,193 +18,192 @@ import org.asynchttpclient.ws.WebSocketUpgradeHandler; import org.junit.Before; import org.junit.Test; -import io.netty.handler.codec.http.HttpHeaders; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class AsyncHttpClientTestCase { - private static AsyncHttpClient HTTP_CLIENT; + private static AsyncHttpClient HTTP_CLIENT; - @Before - public void setup() { + @Before + public void setup() { + AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); + HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); + } - AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); - HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); - } - - @Test - public void givenHttpClient_executeSyncGetRequest() { + @Test + public void givenHttpClient_executeSyncGetRequest() { - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); - Future responseFuture = boundGetRequest.execute(); - try { - Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); - assertNotNull(response); - assertEquals(200, response.getStatusCode()); + Future responseFuture = boundGetRequest.execute(); + try { + Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); + assertNotNull(response); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (TimeoutException e) { - e.printStackTrace(); - } - - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - @Test - public void givenHttpClient_executeAsyncGetRequest() { + @Test + public void givenHttpClient_executeAsyncGetRequest() { - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { - @Override - public Integer onCompleted(Response response) throws Exception { + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { - int resposeStatusCode = response.getStatusCode(); - assertEquals(200, resposeStatusCode); - return resposeStatusCode; - } - }); + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); - // execute a bound GET request - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + // execute a bound GET request + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); - boundGetRequest.execute(new AsyncCompletionHandler() { - @Override - public Integer onCompleted(Response response) throws Exception { + boundGetRequest.execute(new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); - int resposeStatusCode = response.getStatusCode(); - assertEquals(200, resposeStatusCode); - return resposeStatusCode; - } - }); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + @Test + public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { - @Test - public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { + int responseStatusCode = -1; - int responseStatusCode = -1; + @Override + public State onStatusReceived(HttpResponseStatus responseStatus) { + responseStatusCode = responseStatus.getStatusCode(); + return State.CONTINUE; + } - @Override - public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception { - responseStatusCode = responseStatus.getStatusCode(); - return State.CONTINUE; - } + @Override + public State onHeadersReceived(HttpHeaders headers) { + return State.CONTINUE; + } - @Override - public State onHeadersReceived(HttpHeaders headers) throws Exception { - return State.CONTINUE; - } + @Override + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) { + return State.CONTINUE; + } - @Override - public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { - return State.CONTINUE; - } + @Override + public void onThrowable(Throwable t) { - @Override - public void onThrowable(Throwable t) { + } - } + @Override + public Integer onCompleted() { + assertEquals(200, responseStatusCode); + return responseStatusCode; + } + }); - @Override - public Integer onCompleted() throws Exception { - assertEquals(200, responseStatusCode); - return responseStatusCode; - } - }); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + @Test + public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - @Test - public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); + listenableFuture.addListener(() -> { + Response response; + try { + response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + }, Executors.newCachedThreadPool()); - ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); - listenableFuture.addListener(() -> { - Response response; - try { - response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); - assertEquals(200, response.getStatusCode()); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - e.printStackTrace(); - } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - }, Executors.newCachedThreadPool()); + @Test + public void givenWebSocketClient_tryToConnect() { - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); + WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { + @Override + public void onOpen(WebSocket websocket) { + // WebSocket connection opened + } - @Test - public void givenWebSocketClient_tryToConnect() { + @Override + public void onClose(WebSocket websocket, int code, String reason) { + // WebSocket connection closed + } - WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); - WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { - @Override - public void onOpen(WebSocket websocket) { - // WebSocket connection opened - } + @Override + public void onError(Throwable t) { + // WebSocket connection error + assertTrue(t.getMessage().contains("Request timeout")); + } + }).build(); - @Override - public void onClose(WebSocket websocket, int code, String reason) { - // WebSocket connection closed - } + WebSocket WEBSOCKET_CLIENT = null; + try { + WEBSOCKET_CLIENT = Dsl.asyncHttpClient() + .prepareGet("ws://localhost:5590/websocket") + .addHeader("header_name", "header_value") + .addQueryParam("key", "value") + .setRequestTimeout(5000) + .execute(wsHandler).get(); - @Override - public void onError(Throwable t) { - // WebSocket connection error - assertTrue(t.getMessage().contains("Request timeout")); - } - }).build(); - - WebSocket WEBSOCKET_CLIENT = null; - try { - WEBSOCKET_CLIENT = Dsl.asyncHttpClient() - .prepareGet("ws://localhost:5590/websocket") - .addHeader("header_name", "header_value") - .addQueryParam("key", "value") - .setRequestTimeout(5000) - .execute(wsHandler).get(); - - if (WEBSOCKET_CLIENT.isOpen()) { - WEBSOCKET_CLIENT.sendPingFrame(); - WEBSOCKET_CLIENT.sendTextFrame("test message"); - WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' }); - } - - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } finally { - if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { - WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); - } - } - } + if (WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendPingFrame(); + WEBSOCKET_CLIENT.sendTextFrame("test message"); + WEBSOCKET_CLIENT.sendBinaryFrame(new byte[]{'t', 'e', 's', 't'}); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } finally { + if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); + } + } + } } From fa906a2b59420fd29fca9fa93ae291771c84a8d4 Mon Sep 17 00:00:00 2001 From: Tom Hombergs Date: Fri, 2 Feb 2018 21:01:22 +0100 Subject: [PATCH 031/102] removed Car class --- .../baeldung/testing/assertj/custom/Car.java | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java b/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java deleted file mode 100644 index e52ffee8e5..0000000000 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Car.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.testing.assertj.custom; - -public class Car { - private String type; - private Person owner; - - public Car(String type) { - this.type = type; - } - - public String getType() { - return type; - } - - public Person getOwner() { - return owner; - } - - public void setOwner(Person owner) { - this.owner = owner; - } -} From ff76cbc1fe12379b6e28ff63fb4ae80daa8a678e Mon Sep 17 00:00:00 2001 From: Jose Carvajal Date: Sat, 3 Feb 2018 05:00:27 +0100 Subject: [PATCH 032/102] Bael 113 (#3484) * BAEL-399: A Guide to Multitenancy in Hibernate 5 * Removed unused properties in profile 2 * Changes after code review * BAEL-113 * Changes after code review * Added main method in spring boot application * Removed extra files --- pom.xml | 1 + spring-jinq/pom.xml | 83 +++++++++++++++++++ .../baeldung/spring/jinq/JinqApplication.java | 12 +++ .../config/JinqProviderConfiguration.java | 18 ++++ .../baeldung/spring/jinq/entities/Car.java | 58 +++++++++++++ .../spring/jinq/entities/Manufacturer.java | 42 ++++++++++ .../repositories/BaseJinqRepositoryImpl.java | 26 ++++++ .../jinq/repositories/CarRepository.java | 27 ++++++ .../jinq/repositories/CarRepositoryImpl.java | 72 ++++++++++++++++ .../src/main/resources/application.properties | 7 ++ .../CarRepositoryIntegrationTest.java | 42 ++++++++++ 11 files changed, 388 insertions(+) create mode 100644 spring-jinq/pom.xml create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java create mode 100644 spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java create mode 100644 spring-jinq/src/main/resources/application.properties create mode 100644 spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java diff --git a/pom.xml b/pom.xml index 4a25459fcb..582ee6696e 100644 --- a/pom.xml +++ b/pom.xml @@ -246,6 +246,7 @@ spring-zuul spring-reactor spring-vertx + spring-jinq spring-rest-embedded-tomcat diff --git a/spring-jinq/pom.xml b/spring-jinq/pom.xml new file mode 100644 index 0000000000..a895ae8dd4 --- /dev/null +++ b/spring-jinq/pom.xml @@ -0,0 +1,83 @@ + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + 4.0.0 + spring-jinq + 0.1-SNAPSHOT + + spring-jinq + + jar + + + UTF-8 + 1.8 + + 1.8.22 + + + + + + org.springframework.boot + spring-boot-dependencies + 1.5.9.RELEASE + pom + import + + + + + + + org.jinq + jinq-jpa + ${jinq.version} + + + + + com.h2database + h2 + + + + org.hibernate + hibernate-entitymanager + + + + + org.springframework + spring-orm + + + + + org.springframework.boot + spring-boot-starter-test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + false + + + + + + diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java new file mode 100644 index 0000000000..d53b585d36 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/JinqApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.jinq; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JinqApplication { + + public static void main(String[] args) { + SpringApplication.run(JinqApplication.class, args); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java new file mode 100644 index 0000000000..6d921045b7 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/config/JinqProviderConfiguration.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.jinq.config; + +import javax.persistence.EntityManagerFactory; + +import org.jinq.jpa.JinqJPAStreamProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JinqProviderConfiguration { + + @Bean + @Autowired + JinqJPAStreamProvider jinqProvider(EntityManagerFactory emf) { + return new JinqJPAStreamProvider(emf); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java new file mode 100644 index 0000000000..263e6c7622 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Car.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.jinq.entities; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +@Entity(name = "CAR") +public class Car { + private String model; + private String description; + private int year; + private String engine; + private Manufacturer manufacturer; + + @Id + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public String getEngine() { + return engine; + } + + public void setEngine(String engine) { + this.engine = engine; + } + + @OneToOne + @JoinColumn(name = "name") + public Manufacturer getManufacturer() { + return manufacturer; + } + + public void setManufacturer(Manufacturer manufacturer) { + this.manufacturer = manufacturer; + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java new file mode 100644 index 0000000000..f6e5fd23de --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/entities/Manufacturer.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.jinq.entities; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity(name = "MANUFACTURER") +public class Manufacturer { + + private String name; + private String city; + private List cars; + + @Id + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + @OneToMany(mappedBy = "model") + public List getCars() { + return cars; + } + + public void setCars(List cars) { + this.cars = cars; + } + +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java new file mode 100644 index 0000000000..42b81ecc59 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/BaseJinqRepositoryImpl.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.jinq.repositories; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.jinq.jpa.JPAJinqStream; +import org.jinq.jpa.JinqJPAStreamProvider; +import org.springframework.beans.factory.annotation.Autowired; + +public abstract class BaseJinqRepositoryImpl { + @Autowired + private JinqJPAStreamProvider jinqDataProvider; + + @PersistenceContext + private EntityManager entityManager; + + protected abstract Class entityType(); + + public JPAJinqStream stream() { + return streamOf(entityType()); + } + + protected JPAJinqStream streamOf(Class clazz) { + return jinqDataProvider.streamAll(entityManager, clazz); + } +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java new file mode 100644 index 0000000000..56f6106e08 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepository.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.jinq.repositories; + +import java.util.List; +import java.util.Optional; + +import org.jinq.tuples.Pair; +import org.jinq.tuples.Tuple3; + +import com.baeldung.spring.jinq.entities.Car; +import com.baeldung.spring.jinq.entities.Manufacturer; + +public interface CarRepository { + + Optional findByModel(String model); + + List findByModelAndDescription(String model, String desc); + + List> findWithModelYearAndEngine(); + + Optional findManufacturerByModel(String model); + + List> findCarsPerManufacturer(); + + long countCarsByModel(String model); + + List findAll(int skip, int limit); +} diff --git a/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java new file mode 100644 index 0000000000..bf16c87461 --- /dev/null +++ b/spring-jinq/src/main/java/com/baeldung/spring/jinq/repositories/CarRepositoryImpl.java @@ -0,0 +1,72 @@ +package com.baeldung.spring.jinq.repositories; + +import java.util.List; +import java.util.Optional; + +import org.jinq.orm.stream.JinqStream; +import org.jinq.tuples.Pair; +import org.jinq.tuples.Tuple3; +import org.springframework.stereotype.Repository; + +import com.baeldung.spring.jinq.entities.Car; +import com.baeldung.spring.jinq.entities.Manufacturer; + +@Repository +public class CarRepositoryImpl extends BaseJinqRepositoryImpl implements CarRepository { + + @Override + public Optional findByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .findFirst(); + } + + @Override + public List findByModelAndDescription(String model, String desc) { + return stream().where(c -> c.getModel() + .equals(model) + && c.getDescription() + .contains(desc)) + .toList(); + } + + @Override + public List> findWithModelYearAndEngine() { + return stream().select(c -> new Tuple3<>(c.getModel(), c.getYear(), c.getEngine())) + .toList(); + } + + @Override + public Optional findManufacturerByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .select(c -> c.getManufacturer()) + .findFirst(); + } + + @Override + public List> findCarsPerManufacturer() { + return streamOf(Manufacturer.class).join(m -> JinqStream.from(m.getCars())) + .toList(); + } + + @Override + public long countCarsByModel(String model) { + return stream().where(c -> c.getModel() + .equals(model)) + .count(); + } + + @Override + public List findAll(int skip, int limit) { + return stream().skip(skip) + .limit(limit) + .toList(); + } + + @Override + protected Class entityType() { + return Car.class; + } + +} diff --git a/spring-jinq/src/main/resources/application.properties b/spring-jinq/src/main/resources/application.properties new file mode 100644 index 0000000000..dc73bed0c5 --- /dev/null +++ b/spring-jinq/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.datasource.url=jdbc:h2:~/jinq +spring.datasource.username=sa +spring.datasource.password= + +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file diff --git a/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java b/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java new file mode 100644 index 0000000000..9cb126cbaa --- /dev/null +++ b/spring-jinq/src/test/java/com/baeldung/spring/jinq/repositories/CarRepositoryIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.jinq.repositories; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.jinq.JinqApplication; + +@ContextConfiguration(classes = JinqApplication.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class CarRepositoryIntegrationTest { + + @Autowired + private CarRepository repository; + + @Test + public void givenACar_whenFilter_thenShouldBeFound() { + assertThat(repository.findByModel("model1") + .isPresent()).isFalse(); + } + + @Test + public void givenACar_whenMultipleFilters_thenShouldBeFound() { + assertThat(repository.findByModelAndDescription("model1", "desc") + .isEmpty()).isTrue(); + } + + @Test + public void whenUseASelectClause() { + assertThat(repository.findWithModelYearAndEngine() + .isEmpty()).isTrue(); + } + + @Test + public void whenUsingOneToOneRelationship() { + assertThat(repository.findManufacturerByModel("model1")).isNotNull(); + } +} From 710c25fb010068882767bccaaa8c73a70922adcd Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 3 Feb 2018 12:37:28 +0100 Subject: [PATCH 033/102] Ocheja fix (#3572) * Define beans for handling different message types in a lean chat app * Add class based spring beans configuration * Define spring configuration in XML for constructor based bean injection * Refactor package structure to separate constructor based bean injection code set from setter based bean injection code set * Define configuration and classes specific to setter-based bean injection. * Implement tests for constructor-based and setter-based bean injections * develop codes for explaining type erasure * Write unit tests for type erasure examples * Remove evaluation article code * Modify type erasure examples and unit tests * Modify type erasure examples and unit tests * Add expected exception in TypeErasureUnitTest * Correct grammar in class name * Implement File Manager app to demonstrate Polymorphism. Develop unit tests for Polymorphism article code * Add examples for static polymorphism * Change sysout statments to slf4j log info statements * Add assertions and expected errors check on Test * Add assertions and expected errors check on Test * Correct compile time error of symbol not found * Removed commented out non-compiling test. * Replace string concatenations with String.format * Replace string concatenations with String.format * Remove verbose file info descriptor and replace with simpler one * Add example codes for Hibernate Interceptors article Write tests for session-scoped and sessionFactory-scoped interceptors * Implement serializable on customInterceptorImpl * Implement examples for spring data with spring security integration * Remove webapp example implementations; too extensive --- .../interceptors/CustomInterceptorImpl.java | 2 +- spring-data-spring-security/README.md | 14 +++ spring-data-spring-security/pom.xml | 67 ++++++++++++ .../src/main/java/com/baeldung/AppConfig.java | 64 +++++++++++ .../com/baeldung/SpringSecurityConfig.java | 89 ++++++++++++++++ .../data/repositories/TweetRepository.java | 14 +++ .../data/repositories/UserRepository.java | 27 +++++ .../java/com/baeldung/models/AppUser.java | 83 +++++++++++++++ .../main/java/com/baeldung/models/Tweet.java | 63 +++++++++++ .../baeldung/security/AppUserPrincipal.java | 67 ++++++++++++ .../AuthenticationSuccessHandlerImpl.java | 28 +++++ .../security/CustomUserDetailsService.java | 40 +++++++ .../com/baeldung/util/DummyContentUtil.java | 63 +++++++++++ .../src/main/resources/application.properties | 0 .../main/resources/persistence-h2.properties | 8 ++ .../SpringDataWithSecurityTest.java | 100 ++++++++++++++++++ 16 files changed, 728 insertions(+), 1 deletion(-) create mode 100644 spring-data-spring-security/README.md create mode 100644 spring-data-spring-security/pom.xml create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java create mode 100644 spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java create mode 100644 spring-data-spring-security/src/main/resources/application.properties create mode 100644 spring-data-spring-security/src/main/resources/persistence-h2.properties create mode 100644 spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java b/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java index a84a981f7f..6736b39b64 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/interceptors/CustomInterceptorImpl.java @@ -9,7 +9,7 @@ import org.hibernate.Interceptor; import org.hibernate.Transaction; import org.hibernate.type.Type; -public class CustomInterceptorImpl implements Interceptor { +public class CustomInterceptorImpl implements Interceptor, Serializable { @Override public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException { diff --git a/spring-data-spring-security/README.md b/spring-data-spring-security/README.md new file mode 100644 index 0000000000..15b4b50870 --- /dev/null +++ b/spring-data-spring-security/README.md @@ -0,0 +1,14 @@ +# About this project +This project contains examples from the [Spring Data with Spring Security](http://www.baeldung.com/spring-data-with-spring-security) article from Baeldung. + +# Running the project +The application uses [Spring Boot](http://projects.spring.io/spring-boot/), so it is easy to run. You can start it any of a few ways: +* Run the `main` method from `SpringDataRestApplication` +* Use the Maven Spring Boot plugin: `mvn spring-boot:run` +* Package the application as a JAR and run it using `java -jar spring-data-spring-security.jar` + +# Viewing the running application +To view the running application, visit [http://localhost:8080](http://localhost:8080) in your browser + +###Relevant Articles: +- [Spring Data with Spring Security](http://www.baeldung.com/spring-data-with-spring-security) diff --git a/spring-data-spring-security/pom.xml b/spring-data-spring-security/pom.xml new file mode 100644 index 0000000000..d6b671ee57 --- /dev/null +++ b/spring-data-spring-security/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.baeldung + spring-data-spring-security + 1.0 + jar + + intro-spring-data-spring-security + Spring Data with Spring Security + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.security + spring-security-data + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-test + test + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + + com.h2database + h2 + + + javax.servlet + jstl + + + + + ${project.artifactId} + + + + diff --git a/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java b/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java new file mode 100644 index 0000000000..16bbe8b326 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/AppConfig.java @@ -0,0 +1,64 @@ +package com.baeldung; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@SpringBootApplication +@PropertySource("classpath:persistence-h2.properties") +@EnableJpaRepositories(basePackages = { "com.baeldung.data.repositories" }) +@EnableWebMvc +@Import(SpringSecurityConfig.class) +public class AppConfig extends WebMvcConfigurerAdapter { + + @Autowired + private Environment env; + + @Bean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("driverClassName")); + dataSource.setUrl(env.getProperty("url")); + dataSource.setUsername(env.getProperty("user")); + dataSource.setPassword(env.getProperty("password")); + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "com.baeldung.models" }); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + em.setJpaProperties(additionalProperties()); + return em; + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + if (env.getProperty("hibernate.hbm2ddl.auto") != null) { + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + } + if (env.getProperty("hibernate.dialect") != null) { + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + } + if (env.getProperty("hibernate.show_sql") != null) { + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); + } + return hibernateProperties; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java b/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java new file mode 100644 index 0000000000..ee13678a24 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/SpringSecurityConfig.java @@ -0,0 +1,89 @@ +package com.baeldung; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.security.AuthenticationSuccessHandlerImpl; +import com.baeldung.security.CustomUserDetailsService; + +@Configuration +@EnableWebSecurity +@ComponentScan("com.baeldung.security") +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private WebApplicationContext applicationContext; + private CustomUserDetailsService userDetailsService; + @Autowired + private AuthenticationSuccessHandlerImpl successHandler; + @Autowired + private DataSource dataSource; + + @PostConstruct + public void completeSetup() { + userDetailsService = applicationContext.getBean(CustomUserDetailsService.class); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService) + .passwordEncoder(encoder()) + .and() + .authenticationProvider(authenticationProvider()) + .jdbcAuthentication() + .dataSource(dataSource); + } + + @Override + public void configure(WebSecurity web) throws Exception { + web.ignoring() + .antMatchers("/resources/**"); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/login") + .permitAll() + .and() + .formLogin() + .permitAll() + .successHandler(successHandler) + .and() + .csrf() + .disable(); + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() { + final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(userDetailsService); + authProvider.setPasswordEncoder(encoder()); + return authProvider; + } + + @Bean + public PasswordEncoder encoder() { + return new BCryptPasswordEncoder(11); + } + + @Bean + public SecurityEvaluationContextExtension securityEvaluationContextExtension() { + return new SecurityEvaluationContextExtension(); + } +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java new file mode 100644 index 0000000000..7d6446ed0d --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/TweetRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.data.repositories; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.PagingAndSortingRepository; + +import com.baeldung.models.Tweet; + +public interface TweetRepository extends PagingAndSortingRepository { + + @Query("select twt from Tweet twt JOIN twt.likes as lk where lk = ?#{ principal?.username } or twt.owner = ?#{ principal?.username }") + Page getMyTweetsAndTheOnesILiked(Pageable pageable); +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java new file mode 100644 index 0000000000..9f13c3197e --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/data/repositories/UserRepository.java @@ -0,0 +1,27 @@ +package com.baeldung.data.repositories; + +import java.util.Date; +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; + +public interface UserRepository extends CrudRepository { + AppUser findByUsername(String username); + + List findByName(String name); + + @Query("UPDATE AppUser u SET u.lastLogin=:lastLogin WHERE u.username = ?#{ principal?.username }") + @Modifying + @Transactional + public void updateLastLogin(@Param("lastLogin") Date lastLogin); +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java b/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java new file mode 100644 index 0000000000..e48233f90a --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/models/AppUser.java @@ -0,0 +1,83 @@ +package com.baeldung.models; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class AppUser { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + private String name; + @Column(unique = true) + private String username; + private String password; + private boolean enabled = true; + private Date lastLogin; + + private AppUser() { + } + + public AppUser(String name, String email, String password) { + this.username = email; + this.name = name; + this.password = password; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Date getLastLogin() { + return lastLogin; + } + + public void setLastLogin(Date lastLogin) { + this.lastLogin = lastLogin; + } +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java b/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java new file mode 100644 index 0000000000..b2e45009f6 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/models/Tweet.java @@ -0,0 +1,63 @@ +package com.baeldung.models; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Tweet { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + private String tweet; + private String owner; + @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER) + private Set likes = new HashSet(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + private Tweet() { + } + + public Tweet(String tweet, String owner) { + this.tweet = tweet; + this.owner = owner; + } + + public String getTweet() { + return tweet; + } + + public void setTweet(String tweet) { + this.tweet = tweet; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Set getLikes() { + return likes; + } + + public void setLikes(Set likes) { + this.likes = likes; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java b/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java new file mode 100644 index 0000000000..195f9f7bf6 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/AppUserPrincipal.java @@ -0,0 +1,67 @@ +package com.baeldung.security; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import com.baeldung.models.AppUser; + +public class AppUserPrincipal implements UserDetails { + + private final AppUser user; + + // + + public AppUserPrincipal(AppUser user) { + this.user = user; + } + + // + + @Override + public String getUsername() { + return user.getUsername(); + } + + @Override + public String getPassword() { + return user.getPassword(); + } + + @Override + public Collection getAuthorities() { + final List authorities = Collections.singletonList(new SimpleGrantedAuthority("User")); + return authorities; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + // + + public AppUser getAppUser() { + return user; + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java b/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java new file mode 100644 index 0000000000..3fc2bc6559 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/AuthenticationSuccessHandlerImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.security; + +import java.io.IOException; +import java.util.Date; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + +import com.baeldung.data.repositories.UserRepository; + +@Component +public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler { + + @Autowired + private UserRepository userRepository; + + @Override + public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication arg2) throws IOException, ServletException { + userRepository.updateLastLogin(new Date()); + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java b/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java new file mode 100644 index 0000000000..016f4f7fa9 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/security/CustomUserDetailsService.java @@ -0,0 +1,40 @@ +package com.baeldung.security; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.data.repositories.UserRepository; +import com.baeldung.models.AppUser; + +@Service +public class CustomUserDetailsService implements UserDetailsService { + + @Autowired + private WebApplicationContext applicationContext; + private UserRepository userRepository; + + public CustomUserDetailsService() { + super(); + } + + @PostConstruct + public void completeSetup() { + userRepository = applicationContext.getBean(UserRepository.class); + } + + @Override + public UserDetails loadUserByUsername(final String username) { + final AppUser appUser = userRepository.findByUsername(username); + if (appUser == null) { + throw new UsernameNotFoundException(username); + } + return new AppUserPrincipal(appUser); + } + +} diff --git a/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java b/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java new file mode 100644 index 0000000000..f1640264d2 --- /dev/null +++ b/spring-data-spring-security/src/main/java/com/baeldung/util/DummyContentUtil.java @@ -0,0 +1,63 @@ +package com.baeldung.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; + +public class DummyContentUtil { + + public static final List generateDummyUsers() { + List appUsers = new ArrayList<>(); + BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + appUsers.add(new AppUser("Lionel Messi", "lionel@messi.com", passwordEncoder.encode("li1234"))); + appUsers.add(new AppUser("Cristiano Ronaldo", "cristiano@ronaldo.com", passwordEncoder.encode("c1234"))); + appUsers.add(new AppUser("Neymar Dos Santos", "neymar@neymar.com", passwordEncoder.encode("n1234"))); + appUsers.add(new AppUser("Luiz Suarez", "luiz@suarez.com", passwordEncoder.encode("lu1234"))); + appUsers.add(new AppUser("Andres Iniesta", "andres@iniesta.com", passwordEncoder.encode("a1234"))); + appUsers.add(new AppUser("Ivan Rakitic", "ivan@rakitic.com", passwordEncoder.encode("i1234"))); + appUsers.add(new AppUser("Ousman Dembele", "ousman@dembele.com", passwordEncoder.encode("o1234"))); + appUsers.add(new AppUser("Sergio Busquet", "sergio@busquet.com", passwordEncoder.encode("s1234"))); + appUsers.add(new AppUser("Gerard Pique", "gerard@pique.com", passwordEncoder.encode("g1234"))); + appUsers.add(new AppUser("Ter Stergen", "ter@stergen.com", passwordEncoder.encode("t1234"))); + return appUsers; + } + + public static final List generateDummyTweets(List users) { + List tweets = new ArrayList<>(); + Random random = new Random(); + IntStream.range(0, 9) + .sequential() + .forEach(i -> { + Tweet twt = new Tweet(String.format("Tweet %d", i), users.get(random.nextInt(users.size())) + .getUsername()); + twt.getLikes() + .addAll(users.subList(0, random.nextInt(users.size())) + .stream() + .map(AppUser::getUsername) + .collect(Collectors.toSet())); + tweets.add(twt); + }); + return tweets; + } + + public static Collection getAuthorities() { + Collection grantedAuthorities = new ArrayList(); + GrantedAuthority grantedAuthority = new GrantedAuthority() { + public String getAuthority() { + return "ROLE_USER"; + } + }; + grantedAuthorities.add(grantedAuthority); + return grantedAuthorities; + } + +} diff --git a/spring-data-spring-security/src/main/resources/application.properties b/spring-data-spring-security/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-data-spring-security/src/main/resources/persistence-h2.properties b/spring-data-spring-security/src/main/resources/persistence-h2.properties new file mode 100644 index 0000000000..a4b2af6361 --- /dev/null +++ b/spring-data-spring-security/src/main/resources/persistence-h2.properties @@ -0,0 +1,8 @@ +driverClassName=org.h2.Driver +url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +username=sa +password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=false +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java b/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java new file mode 100644 index 0000000000..dbbfe7e85e --- /dev/null +++ b/spring-data-spring-security/src/test/java/com/baeldung/relationships/SpringDataWithSecurityTest.java @@ -0,0 +1,100 @@ +package com.baeldung.relationships; + +import static org.springframework.util.Assert.isTrue; + +import java.util.Date; +import java.util.List; + +import javax.servlet.ServletContext; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import com.baeldung.AppConfig; +import com.baeldung.data.repositories.TweetRepository; +import com.baeldung.data.repositories.UserRepository; +import com.baeldung.models.AppUser; +import com.baeldung.models.Tweet; +import com.baeldung.security.AppUserPrincipal; +import com.baeldung.util.DummyContentUtil; + +@RunWith(SpringRunner.class) +@WebAppConfiguration +@ContextConfiguration +@DirtiesContext +public class SpringDataWithSecurityTest { + AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); + @Autowired + private ServletContext servletContext; + private static UserRepository userRepository; + private static TweetRepository tweetRepository; + + @Before + public void testInit() { + ctx.register(AppConfig.class); + ctx.setServletContext(servletContext); + ctx.refresh(); + userRepository = ctx.getBean(UserRepository.class); + tweetRepository = ctx.getBean(TweetRepository.class); + List appUsers = (List) userRepository.save(DummyContentUtil.generateDummyUsers()); + tweetRepository.save(DummyContentUtil.generateDummyTweets(appUsers)); + } + + @AfterClass + public static void tearDown() { + tweetRepository.deleteAll(); + userRepository.deleteAll(); + } + + @Test + public void givenAppUser_whenLoginSuccessful_shouldUpdateLastLogin() { + AppUser appUser = userRepository.findByUsername("lionel@messi.com"); + Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities()); + SecurityContextHolder.getContext() + .setAuthentication(auth); + userRepository.updateLastLogin(new Date()); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + public void givenNoAppUserInSecurityContext_whenUpdateLastLoginAttempted_shouldFail() { + userRepository.updateLastLogin(new Date()); + } + + @Test + public void givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets() { + AppUser appUser = userRepository.findByUsername("lionel@messi.com"); + Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities()); + SecurityContextHolder.getContext() + .setAuthentication(auth); + Page page = null; + do { + page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5)); + for (Tweet twt : page.getContent()) { + isTrue((twt.getOwner() == appUser.getUsername()) || (twt.getLikes() + .contains(appUser.getUsername())), "I do not have any Tweets"); + } + } while (page.hasNext()); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + public void givenNoAppUser_whenPaginatedResultsRetrievalAttempted_shouldFail() { + Page page = null; + do { + page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5)); + } while (page != null && page.hasNext()); + } +} From b9ac0dc78b8e51e0bda526b420746227ab53eff3 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Sat, 3 Feb 2018 12:51:59 +0100 Subject: [PATCH 034/102] Updates after editor feedback --- .../ConsistentDateParameterValidator.java | 5 +---- .../constraints/ValidReservationValidator.java | 2 +- .../methodvalidation/model/Reservation.java | 4 ++++ .../model/ReservationManagement.java | 12 ++++++------ .../ContainerValidationIntegrationTest.java | 15 +++++++++++++-- .../ValidationIntegrationTest.java | 18 ++++++++++++++---- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java index b28abcba45..0ee1b6eda1 100644 --- a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java @@ -15,9 +15,6 @@ public class ConsistentDateParameterValidator implements ConstraintValidator getAllCustomers() { @@ -37,13 +42,8 @@ public class ReservationManagement { return null; } - public void createNewCustomer(@Valid Customer customer) { - - // ... - } - @Valid - public Customer getCustomerById() { + public Reservation getReservationById(int id) { return null; } diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java index 53133edd48..2363bf8f5d 100644 --- a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ContainerValidationIntegrationTest.java @@ -1,6 +1,7 @@ package org.baeldung.javaxval.methodvalidation; import org.baeldung.javaxval.methodvalidation.model.Customer; +import org.baeldung.javaxval.methodvalidation.model.Reservation; import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; import org.junit.Rule; import org.junit.Test; @@ -69,9 +70,14 @@ public class ContainerValidationIntegrationTest { Customer customer = new Customer(); customer.setFirstName("John"); customer.setLastName("Doe"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); exception.expect(ConstraintViolationException.class); - reservationManagement.createNewCustomer(customer); + reservationManagement.createReservation(reservation); } @Test @@ -80,7 +86,12 @@ public class ContainerValidationIntegrationTest { Customer customer = new Customer(); customer.setFirstName("William"); customer.setLastName("Smith"); + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); - reservationManagement.createNewCustomer(customer); + reservationManagement.createReservation(reservation); } } diff --git a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java index 6a750698c6..6b53d3a107 100644 --- a/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java +++ b/javaxval/src/test/java/org/baeldung/javaxval/methodvalidation/ValidationIntegrationTest.java @@ -172,11 +172,16 @@ public class ValidationIntegrationTest { public void whenValidationWithInvalidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { ReservationManagement object = new ReservationManagement(); - Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); + Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class); Customer customer = new Customer(); customer.setFirstName("John"); customer.setLastName("Doe"); - Object[] parameterValues = { customer }; + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + Object[] parameterValues = { reservation }; Set> violations = executableValidator.validateParameters(object, method, parameterValues); assertEquals(2, violations.size()); @@ -186,11 +191,16 @@ public class ValidationIntegrationTest { public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { ReservationManagement object = new ReservationManagement(); - Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); + Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class); Customer customer = new Customer(); customer.setFirstName("William"); customer.setLastName("Smith"); - Object[] parameterValues = { customer }; + Reservation reservation = new Reservation(LocalDate.now() + .plusDays(1), + LocalDate.now() + .plusDays(2), + customer, 1); + Object[] parameterValues = { reservation }; Set> violations = executableValidator.validateParameters(object, method, parameterValues); assertEquals(0, violations.size()); From e15f2fc7dd347b42dbf414747cac8aa11e3c0b86 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Sat, 3 Feb 2018 13:10:31 +0100 Subject: [PATCH 035/102] Updates after editor feedback --- .../constraints/ValidReservationValidator.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java index a1cba9a5f6..fc225999be 100644 --- a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java @@ -27,14 +27,10 @@ public class ValidReservationValidator implements ConstraintValidator 0) { - - return true; - } - return false; + && reservation.getRoom() > 0); } } From 5a31640528ac3e7fce5314b06e194077b0cccd4b Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Sat, 3 Feb 2018 13:30:52 +0100 Subject: [PATCH 036/102] Updates after editor feedback --- .../org/baeldung/javaxval/methodvalidation/model/Customer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java index 529cf436da..fe9ad7080e 100644 --- a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/model/Customer.java @@ -14,7 +14,7 @@ public class Customer { @Size(min = 5, max = 200) private String lastName; - public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) String lastName) { + public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) @NotNull String lastName) { this.firstName = firstName; this.lastName = lastName; } From 53bb9276100b3714cdf08cc49de91e17dc172a7e Mon Sep 17 00:00:00 2001 From: Nam Thai Nguyen Date: Sat, 3 Feb 2018 20:21:46 +0700 Subject: [PATCH 037/102] Modifies the Data class --- .../main/java/com/baeldung/javac/Data.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/javac/Data.java b/core-java/src/main/java/com/baeldung/javac/Data.java index f6912fbd14..feef0c4f1e 100644 --- a/core-java/src/main/java/com/baeldung/javac/Data.java +++ b/core-java/src/main/java/com/baeldung/javac/Data.java @@ -1,28 +1,16 @@ package com.baeldung.javac; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class Data implements Serializable { - static List textList = new ArrayList(); +public class Data { + List textList = new ArrayList(); - private static void addText() { - textList.add("baeldung"); - textList.add("."); - textList.add("com"); + public void addText(String text) { + textList.add(text); } public List getTextList() { - this.addText(); - List result = new ArrayList(); - String firstElement = (String) textList.get(0); - switch (firstElement) { - case "baeldung": - result.add("baeldung"); - case "com": - result.add("com"); - } - return result; + return this.textList; } -} +} \ No newline at end of file From ecf1cae3f7802bfb52e60777c95e9312ff7a5a53 Mon Sep 17 00:00:00 2001 From: Marcos Date: Sat, 3 Feb 2018 19:01:20 +0100 Subject: [PATCH 038/102] JPA Attribute Converters --- .../com/baeldung/hibernate/HibernateUtil.java | 1 + .../converters/PersonNameConverter.java | 43 +++++++++++ .../com/baeldung/hibernate/pojo/Person.java | 32 ++++++++ .../baeldung/hibernate/pojo/PersonName.java | 32 ++++++++ .../converter/PersonNameConverterTest.java | 73 +++++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java index 25fc0d7b02..130edec8cc 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -81,6 +81,7 @@ public class HibernateUtil { metadataSources.addAnnotatedClass(Bag.class); metadataSources.addAnnotatedClass(PointEntity.class); metadataSources.addAnnotatedClass(PolygonEntity.class); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class); Metadata metadata = metadataSources.buildMetadata(); return metadata.getSessionFactoryBuilder() diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java b/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java new file mode 100644 index 0000000000..c8b3397b09 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/converters/PersonNameConverter.java @@ -0,0 +1,43 @@ +package com.baeldung.hibernate.converters; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +import com.baeldung.hibernate.pojo.PersonName; + +@Converter +public class PersonNameConverter implements AttributeConverter { + + private static final String SEPARATOR = ", "; + + @Override + public String convertToDatabaseColumn(PersonName person) { + StringBuilder sb = new StringBuilder(); + if (person.getSurname() != null) { + sb.append(person.getSurname()); + sb.append(SEPARATOR); + } + + if (person.getName() != null) { + sb.append(person.getName()); + } + + return sb.toString(); + } + + @Override + public PersonName convertToEntityAttribute(String dbPerson) { + String[] pieces = dbPerson.split(SEPARATOR); + + if (pieces == null || pieces.length != 2) { + return null; + } + + PersonName personName = new PersonName(); + personName.setSurname(pieces[0]); + personName.setName(pieces[1]); + + return personName; + } + +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java new file mode 100644 index 0000000000..390a5954ed --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Person.java @@ -0,0 +1,32 @@ +package com.baeldung.hibernate.pojo; + +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import com.baeldung.hibernate.converters.PersonNameConverter; + +@Entity(name = "PersonTable") +public class Person { + + @Id + @GeneratedValue + private Long id; + + @Convert(converter = PersonNameConverter.class) + private PersonName personName; + + public PersonName getPersonName() { + return personName; + } + + public void setPersonName(PersonName personName) { + this.personName = personName; + } + + public Long getId() { + return id; + } + +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java new file mode 100644 index 0000000000..689afd463a --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java @@ -0,0 +1,32 @@ +package com.baeldung.hibernate.pojo; + +import java.io.Serializable; + +import javax.persistence.Entity; + +@Entity +public class PersonName implements Serializable { + + private static final long serialVersionUID = 7883094644631050150L; + + private String name; + + private String surname; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java new file mode 100644 index 0000000000..6eb89713f5 --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java @@ -0,0 +1,73 @@ +package com.baeldung.hibernate.converter; + +import java.io.IOException; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.hibernate.HibernateUtil; +import com.baeldung.hibernate.pojo.Person; +import com.baeldung.hibernate.pojo.PersonName; +import com.vividsolutions.jts.util.Assert; + +public class PersonNameConverterTest { + + private Session session; + private Transaction transaction; + + @Before + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory() + .openSession(); + transaction = session.beginTransaction(); + + session.createNativeQuery("delete from personTable") + .executeUpdate(); + + transaction.commit(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + transaction.rollback(); + session.close(); + } + + @Test + public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() { + final String name = "name"; + final String surname = "surname"; + + PersonName personName = new PersonName(); + personName.setName(name); + personName.setSurname(surname); + + Person person = new Person(); + person.setPersonName(personName); + + Long id = (Long) session.save(person); + + session.flush(); + session.clear(); + + String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id") + .setParameter("id", id) + .getSingleResult(); + + Assert.equals(surname + ", " + name, dbPersonName); + + Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) + .setParameter("id", id) + .getSingleResult(); + + Assert.equals(dbPerson.getPersonName() + .getName(), name); + Assert.equals(dbPerson.getPersonName() + .getSurname(), surname); + } + +} From 0bf31021884f5c40668fab1ccd3a3fbbdc62ead9 Mon Sep 17 00:00:00 2001 From: Marcos Date: Sat, 3 Feb 2018 19:25:48 +0100 Subject: [PATCH 039/102] code cleanup --- .../src/main/java/com/baeldung/hibernate/pojo/PersonName.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java index 689afd463a..335fe73f75 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pojo/PersonName.java @@ -2,9 +2,6 @@ package com.baeldung.hibernate.pojo; import java.io.Serializable; -import javax.persistence.Entity; - -@Entity public class PersonName implements Serializable { private static final long serialVersionUID = 7883094644631050150L; From bf5b3045a1f6fe710a1ae63e935504a6139f05ba Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Sat, 3 Feb 2018 22:49:11 -0500 Subject: [PATCH 040/102] BAEL-1486 - sample code for JGroups. Fixed a typo in InfluxDB. (#3578) --- influxdb/README.md | 1 - .../influxdb/InfluxDBConnectionLiveTest.java | 3 +- jgroups/README.md | 15 ++ jgroups/pom.xml | 36 +++ .../baeldung/jgroups/JGroupsMessenger.java | 222 ++++++++++++++++++ jgroups/src/main/resources/udp.xml | 48 ++++ pom.xml | 1 + 7 files changed, 323 insertions(+), 3 deletions(-) create mode 100644 jgroups/README.md create mode 100644 jgroups/pom.xml create mode 100644 jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java create mode 100644 jgroups/src/main/resources/udp.xml diff --git a/influxdb/README.md b/influxdb/README.md index 7d1684688d..f2c421580e 100644 --- a/influxdb/README.md +++ b/influxdb/README.md @@ -2,7 +2,6 @@ ### Relevant Article: - [Introduction to using InfluxDB with Java](http://www.baeldung.com/using-influxdb-with-java/) -- [Using InfluxDB with Java](http://www.baeldung.com/java-influxdb) ### Overview This Maven project contains the Java code for the article linked above. diff --git a/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java b/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java index 6c7a03cb70..858903a676 100644 --- a/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java +++ b/influxdb/src/test/java/com/baeldung/influxdb/InfluxDBConnectionLiveTest.java @@ -8,7 +8,6 @@ import org.influxdb.dto.*; import org.influxdb.impl.InfluxDBResultMapper; import org.junit.Test; -import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; @@ -103,7 +102,7 @@ public class InfluxDBConnectionLiveTest { // another brief pause. Thread.sleep(10); - List memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); + List memoryPointList = getPoints(connection, "Select * from memory", "baeldung"); assertEquals(10, memoryPointList.size()); diff --git a/jgroups/README.md b/jgroups/README.md new file mode 100644 index 0000000000..bb2813c3d6 --- /dev/null +++ b/jgroups/README.md @@ -0,0 +1,15 @@ +## Reliable Messaging with JGroups Tutorial Project + +### Relevant Article: +- [Reliable Messaging with JGroups](http://www.baeldung.com/reliable-messaging-with-jgroups/) + +### Overview +This Maven project contains the Java code for the article linked above. + +### Package Organization +Java classes for the intro tutorial are in the org.baeldung.jgroups package. + + +### Running the tests + +``` diff --git a/jgroups/pom.xml b/jgroups/pom.xml new file mode 100644 index 0000000000..0e5971875e --- /dev/null +++ b/jgroups/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + jgroups + 0.1-SNAPSHOT + jar + jgroups + Reliable Messaging with JGroups Tutorial + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.jgroups + jgroups + 4.0.10.Final + + + + commons-cli + commons-cli + 1.4 + + + + + 1.8 + UTF-8 + + + diff --git a/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java new file mode 100644 index 0000000000..2a8df8a9ab --- /dev/null +++ b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java @@ -0,0 +1,222 @@ +package com.baeldung.jgroups; + +import org.apache.commons.cli.*; +import org.jgroups.*; +import org.jgroups.util.Util; + +import java.io.*; +import java.util.List; +import java.util.Optional; + +public class JGroupsMessenger extends ReceiverAdapter { + + private JChannel channel; + private String userName; + private String clusterName; + private View lastView; + private boolean running = true; + + // Our shared state + private Integer messageCount = 0; + + /** + * Connect to a JGroups cluster using command line options + * @param args command line arguments + * @throws Exception + */ + private void start(String[] args) throws Exception { + processCommandline(args); + + // Create the channel + // This file could be moved, or made a command line option. + channel = new JChannel("src/main/resources/udp.xml"); + + // Set a name + channel.name(userName); + + // Register for callbacks + channel.setReceiver(this); + + // Ignore our messages + channel.setDiscardOwnMessages(true); + + // Connect + channel.connect(clusterName); + + // Start state transfer + channel.getState(null, 0); + + // Do the things + processInput(); + + // Clean up + channel.close(); + + } + + /** + * Quick and dirty implementaton of commons cli for command line args + * @param args the command line args + * @throws ParseException + */ + private void processCommandline(String[] args) throws ParseException { + + // Options, parser, friendly help + Options options = new Options(); + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + + options.addOption("u", "user", true, "User name") + .addOption("c", "cluster", true, "Cluster name"); + + CommandLine line = parser.parse(options, args); + + if (line.hasOption("user")) { + userName = line.getOptionValue("user"); + } else { + formatter.printHelp("JGroupsMessenger: need a user name.\n", options); + System.exit(-1); + } + + if (line.hasOption("cluster")) { + clusterName = line.getOptionValue("cluster"); + } else { + formatter.printHelp("JGroupsMessenger: need a cluster name.\n", options); + System.exit(-1); + } + } + + // Start it up + public static void main(String[] args) throws Exception { + new JGroupsMessenger().start(args); + } + + + @Override + public void viewAccepted(View newView) { + + // Save view if this is the first + if (lastView == null) { + System.out.println("Received initial view:"); + newView.forEach(System.out::println); + } else { + + // Compare to last view + System.out.println("Received new view."); + + List
newMembers = View.newMembers(lastView, newView); + if (newMembers.size() > 0) { + System.out.println("New members: "); + newMembers.forEach(System.out::println); + } + + List
exMembers = View.leftMembers(lastView, newView); + if (exMembers.size() > 0) { + System.out.println("Exited members:"); + exMembers.forEach(System.out::println); + } + } + lastView = newView; + } + + /** + * Loop on console input until we see 'x' to exit + */ + private void processInput() { + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + while (running) { + try { + + // Get a destination, means broadcast + Address destination = null; + System.out.print("Enter a destination: "); + System.out.flush(); + String destinationName = in.readLine().toLowerCase(); + + if (destinationName.equals("x")) { + running = false; + continue; + } else if (!destinationName.isEmpty()) { + Optional
optDestination = getAddress(destinationName); + if (optDestination.isPresent()) { + destination = optDestination.get(); + } else { + System.out.println("Destination not found, try again."); + continue; + } + } + + // Accept a string to send + System.out.print("Enter a message: "); + System.out.flush(); + String line = in.readLine().toLowerCase(); + sendMessage(destination, line); + } catch (IOException ioe) { + running = false; + } + } + System.out.println("Exiting."); + } + + /** + * Send message from here + * @param destination the destination + * @param messageString the message + */ + private void sendMessage(Address destination, String messageString) { + try { + System.out.println("Sending " + messageString + " to " + destination); + Message message = new Message(destination, messageString); + channel.send(message); + } catch (Exception exception) { + System.err.println("Exception sending message: " + exception.getMessage()); + running = false; + } + } + + @Override + public void receive(Message message) { + // Print source and dest with message + String line = "Message received from: " + message.getSrc() + " to: " + message.getDest() + " -> " + message.getObject(); + + // Only track the count of broadcast messages + // Tracking direct message would make for a pointless state + if (message.getDest() == null) { + messageCount++; + System.out.println("Message count: " + messageCount); + } + + System.out.println(line); + } + + @Override + public void getState(OutputStream output) throws Exception { + // Serialize into the stream + Util.objectToStream(messageCount, new DataOutputStream(output)); + } + + @Override + public void setState(InputStream input) { + + // NOTE: since we know that incrementing the count and transferring the state + // is done inside the JChannel's thread, we don't have to worry about synchronizing + // messageCount. For production code it should be synchronized! + try { + // Deserialize + messageCount = Util.objectFromStream(new DataInputStream(input)); + } catch (Exception e) { + System.out.println("Error deserialing state!"); + } + System.out.println(messageCount + " is the current messagecount."); + } + + + private Optional
getAddress(String name) { + View view = channel.view(); + return view.getMembers().stream().filter(address -> name.equals(address.toString())).findFirst(); + } + +} + + diff --git a/jgroups/src/main/resources/udp.xml b/jgroups/src/main/resources/udp.xml new file mode 100644 index 0000000000..5a9bfd0851 --- /dev/null +++ b/jgroups/src/main/resources/udp.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 582ee6696e..ca6d4afe82 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,7 @@ javax-servlets javaxval jaxb + jgroups jee-7 jjwt From 581e8592ae52591525054054e906f8d9df7cf9a3 Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Sun, 4 Feb 2018 18:22:17 +0530 Subject: [PATCH 041/102] BAEL-1525: Article code completed. --- .../java/com/baeldung/string/Palindrome.java | 14 +++++++ .../string/WhenCheckingPalindrome.java | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/string/Palindrome.java create mode 100644 core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java new file mode 100644 index 0000000000..e339d92447 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -0,0 +1,14 @@ +package com.baeldung.string; + +public class Palindrome { + + public boolean isPalindrome(String text) { + text = text.replaceAll("\\s+", "").toLowerCase(); + int length = text.length(); + int forward = 0; + int backward = length - 1; + boolean palindrome = true; + while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false); + return palindrome; + } +} diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java new file mode 100644 index 0000000000..691d74c751 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -0,0 +1,37 @@ +package com.baeldung.string; + +import org.junit.Assert; +import org.junit.Test; + +public class WhenCheckingPalindrome { + + private String[] words = { + "Anna", + "Civic", + "Kayak", + "Level", + "Madam", + }; + + private String[] sentences = { + "Sore was I ere I saw Eros", + "Euston saw I was not Sue", + "Too hot to hoot", + "No mists or frost Simon", + "Stella won no wallets" + }; + + private Palindrome palindrome = new Palindrome(); + + @Test + public void whenWord_shouldBePalindrome() { + for(String word:words) + Assert.assertTrue(palindrome.isPalindrome(word)); + } + + @Test + public void whenSentence_shouldBePalindrome() { + for(String sentence:sentences) + Assert.assertTrue(palindrome.isPalindrome(sentence)); + } +} From 796277bdaa4faf45cf549ee58b18dee56e872564 Mon Sep 17 00:00:00 2001 From: Marcos Date: Sun, 4 Feb 2018 14:06:11 +0100 Subject: [PATCH 042/102] fix assert --- .../hibernate/converter/PersonNameConverterTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java index 6eb89713f5..aec2311294 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/converter/PersonNameConverterTest.java @@ -11,7 +11,8 @@ import org.junit.Test; import com.baeldung.hibernate.HibernateUtil; import com.baeldung.hibernate.pojo.Person; import com.baeldung.hibernate.pojo.PersonName; -import com.vividsolutions.jts.util.Assert; + +import static org.junit.Assert.assertEquals; public class PersonNameConverterTest { @@ -58,15 +59,15 @@ public class PersonNameConverterTest { .setParameter("id", id) .getSingleResult(); - Assert.equals(surname + ", " + name, dbPersonName); + assertEquals(surname + ", " + name, dbPersonName); Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class) .setParameter("id", id) .getSingleResult(); - Assert.equals(dbPerson.getPersonName() + assertEquals(dbPerson.getPersonName() .getName(), name); - Assert.equals(dbPerson.getPersonName() + assertEquals(dbPerson.getPersonName() .getSurname(), surname); } From 84782d054e429e5d71a4bda0d18029edd1f61ac3 Mon Sep 17 00:00:00 2001 From: abialas Date: Sun, 4 Feb 2018 23:44:22 +0100 Subject: [PATCH 043/102] BAEL-21 (#3587) * BAEL-1412 add java 8 spring data features * BAEL-21 new HTTP API overview * BAEL-21 fix executor --- .../com/baeldung/java9/httpclient/HttpClientTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java index a4c6ac0d7d..5cf3b9098f 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @@ -116,18 +117,20 @@ public class HttpClientTest { .GET() .build(); + ExecutorService executorService = Executors.newFixedThreadPool(2); + CompletableFuture> response1 = HttpClient.newBuilder() - .executor(Executors.newFixedThreadPool(2)) + .executor(executorService) .build() .sendAsync(request, HttpResponse.BodyHandler.asString()); CompletableFuture> response2 = HttpClient.newBuilder() - .executor(Executors.newFixedThreadPool(2)) + .executor(executorService) .build() .sendAsync(request, HttpResponse.BodyHandler.asString()); CompletableFuture> response3 = HttpClient.newBuilder() - .executor(Executors.newFixedThreadPool(2)) + .executor(executorService) .build() .sendAsync(request, HttpResponse.BodyHandler.asString()); From c03e0919e1195bdc06139a930220568dfefa72b1 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Mon, 5 Feb 2018 10:21:36 +0100 Subject: [PATCH 044/102] Updates after editor feedback --- javaxval/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 0891fe6959..a05ee43aaf 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -22,12 +22,6 @@ - - javax.validation - validation-api - ${validation-api.version} - - org.hibernate hibernate-validator From 7ecceaf5e25ba9d1b549bf2bfad60f592a8677c2 Mon Sep 17 00:00:00 2001 From: Markus Gulden Date: Mon, 5 Feb 2018 11:17:37 +0100 Subject: [PATCH 045/102] Updates after editor feedback --- .../constraints/ConsistentDateParameterValidator.java | 6 +----- .../constraints/ValidReservationValidator.java | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java index 0ee1b6eda1..f1c97760d7 100644 --- a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ConsistentDateParameterValidator.java @@ -9,15 +9,11 @@ import java.time.LocalDate; @SupportedValidationTarget(ValidationTarget.PARAMETERS) public class ConsistentDateParameterValidator implements ConstraintValidator { - @Override - public void initialize(ConsistentDateParameters constraintAnnotation) { - } - @Override public boolean isValid(Object[] value, ConstraintValidatorContext context) { if (value[0] == null || value[1] == null) { - return false; + return true; } if (!(value[0] instanceof LocalDate) || !(value[1] instanceof LocalDate)) { diff --git a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java index fc225999be..7b730480ed 100644 --- a/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java +++ b/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation/constraints/ValidReservationValidator.java @@ -8,15 +8,11 @@ import java.time.LocalDate; public class ValidReservationValidator implements ConstraintValidator { - @Override - public void initialize(ValidReservation constraintAnnotation) { - } - @Override public boolean isValid(Reservation reservation, ConstraintValidatorContext context) { if (reservation == null) { - return false; + return true; } if (!(reservation instanceof Reservation)) { From a6a291ce22607fc8e7d50f4a90bb1c1f1e649d06 Mon Sep 17 00:00:00 2001 From: daoire Date: Mon, 5 Feb 2018 12:18:04 +0000 Subject: [PATCH 046/102] Update README --- libraries/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/README.md b/libraries/README.md index d001f13698..fbf2b4e876 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -59,6 +59,7 @@ - [Intro to JDO Queries 2/2](http://www.baeldung.com/jdo-queries) - [Guide to google-http-client](http://www.baeldung.com/google-http-client) - [Interact with Google Sheets from Java](http://www.baeldung.com/google-sheets-java-client) +- [Programatically Create, Configure, and Run a Tomcat Server] (http://www.baeldung.com/tomcat-programmatic-setup) From 34e8c290fd57bbe98646ff50a423fd4f863b7f2d Mon Sep 17 00:00:00 2001 From: daoire Date: Mon, 5 Feb 2018 12:30:23 +0000 Subject: [PATCH 047/102] Update README.md --- logging-modules/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/logging-modules/README.md b/logging-modules/README.md index 8ae7316047..6de71adb43 100644 --- a/logging-modules/README.md +++ b/logging-modules/README.md @@ -4,3 +4,4 @@ ### Relevant Articles: - [Creating a Custom Logback Appender](http://www.baeldung.com/custom-logback-appender) +- [Get Log Output in JSON Format](http://www.baeldung.com/java-log-json-output) From fcb114b50c1cf920085d55c2c1acd2785d12c0ed Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Mon, 5 Feb 2018 20:55:45 +0200 Subject: [PATCH 048/102] Update README.md --- mesos-marathon/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mesos-marathon/README.md b/mesos-marathon/README.md index 3223eb2478..164f0db5d3 100644 --- a/mesos-marathon/README.md +++ b/mesos-marathon/README.md @@ -1,3 +1,5 @@ ### Relevant articles - [Simple Jenkins Pipeline with Marathon and Mesos](http://www.baeldung.com/jenkins-pipeline-with-marathon-mesos) + +To run the pipeline, please modify the dockerise.sh file with your own useranema and password for docker login. From 43d0f4c15c6b8813a86305ba9c218331458f6d92 Mon Sep 17 00:00:00 2001 From: Tom Hombergs Date: Mon, 5 Feb 2018 20:10:44 +0100 Subject: [PATCH 049/102] added link to article --- testing-modules/testing/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing-modules/testing/README.md b/testing-modules/testing/README.md index 3511bb1bb9..2894da3496 100644 --- a/testing-modules/testing/README.md +++ b/testing-modules/testing/README.md @@ -17,3 +17,4 @@ - [Introduction to Jukito](http://www.baeldung.com/jukito) - [Custom JUnit 4 Test Runners](http://www.baeldung.com/junit-4-custom-runners) - [Guide to JSpec](http://www.baeldung.com/jspec) +- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) From 1011edf288140b87a68341f05b2774e153ffbac3 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Mon, 5 Feb 2018 21:16:09 +0200 Subject: [PATCH 050/102] Update README.md --- mesos-marathon/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesos-marathon/README.md b/mesos-marathon/README.md index 164f0db5d3..1d4d4995a8 100644 --- a/mesos-marathon/README.md +++ b/mesos-marathon/README.md @@ -2,4 +2,4 @@ - [Simple Jenkins Pipeline with Marathon and Mesos](http://www.baeldung.com/jenkins-pipeline-with-marathon-mesos) -To run the pipeline, please modify the dockerise.sh file with your own useranema and password for docker login. + To run the pipeline, please modify the dockerise.sh file with your own useranema and password for docker login. From 372cba10bb23e5e1fc50631c63dfa5ad4da6db20 Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Mon, 5 Feb 2018 19:47:17 -0500 Subject: [PATCH 051/102] BAEL-1486 - small changes to JGroups (#3590) * BAEL-1486 - sample code for JGroups. Fixed a typo in InfluxDB. * BAEL-1486 - updates for JGroups. --- .../baeldung/jgroups/JGroupsMessenger.java | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java index 2a8df8a9ab..70eacabf1e 100644 --- a/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java +++ b/jgroups/src/main/java/com/baeldung/jgroups/JGroupsMessenger.java @@ -100,21 +100,16 @@ public class JGroupsMessenger extends ReceiverAdapter { System.out.println("Received initial view:"); newView.forEach(System.out::println); } else { - // Compare to last view System.out.println("Received new view."); List
newMembers = View.newMembers(lastView, newView); - if (newMembers.size() > 0) { - System.out.println("New members: "); - newMembers.forEach(System.out::println); - } + System.out.println("New members: "); + newMembers.forEach(System.out::println); List
exMembers = View.leftMembers(lastView, newView); - if (exMembers.size() > 0) { - System.out.println("Exited members:"); - exMembers.forEach(System.out::println); - } + System.out.println("Exited members:"); + exMembers.forEach(System.out::println); } lastView = newView; } @@ -122,7 +117,7 @@ public class JGroupsMessenger extends ReceiverAdapter { /** * Loop on console input until we see 'x' to exit */ - private void processInput() { + private void processInput() throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (running) { @@ -138,13 +133,8 @@ public class JGroupsMessenger extends ReceiverAdapter { running = false; continue; } else if (!destinationName.isEmpty()) { - Optional
optDestination = getAddress(destinationName); - if (optDestination.isPresent()) { - destination = optDestination.get(); - } else { - System.out.println("Destination not found, try again."); - continue; - } + destination = getAddress(destinationName) + . orElseThrow(() -> new Exception("Destination not found")); } // Accept a string to send From 56316fd029ff941e39e5bdf7dc3f5cca170946b0 Mon Sep 17 00:00:00 2001 From: linhvovn Date: Wed, 7 Feb 2018 03:43:44 +0800 Subject: [PATCH 052/102] [tlinh2110-BAEL1382] Add Security in SI (#3593) * [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger --- spring-integration/pom.xml | 41 ++++++++-- .../baeldung/si/security/MessageConsumer.java | 45 ++++++++++ .../si/security/SecuredDirectChannel.java | 50 +++++++++++ .../baeldung/si/security/SecurityConfig.java | 46 +++++++++++ .../si/security/SecurityPubSubChannel.java | 82 +++++++++++++++++++ .../security/UsernameAccessDecisionVoter.java | 45 ++++++++++ .../si/TestSpringIntegrationSecurity.java | 81 ++++++++++++++++++ ...TestSpringIntegrationSecurityExecutor.java | 68 +++++++++++++++ 8 files changed, 452 insertions(+), 6 deletions(-) create mode 100644 spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java create mode 100644 spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java create mode 100644 spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java create mode 100644 spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java create mode 100644 spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java create mode 100644 spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java create mode 100644 spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java diff --git a/spring-integration/pom.xml b/spring-integration/pom.xml index 4e210122b0..27cc7381e3 100644 --- a/spring-integration/pom.xml +++ b/spring-integration/pom.xml @@ -18,7 +18,7 @@ UTF-8 - 4.3.5.RELEASE + 5.0.1.RELEASE 1.1.4.RELEASE 1.4.7 1.1.1 @@ -68,7 +68,7 @@ org.springframework.integration spring-integration-core - ${spring.integration.version} + ${spring.version} javax.activation @@ -84,17 +84,17 @@ org.springframework.integration spring-integration-twitter - ${spring.integration.version} + ${spring.version} org.springframework.integration spring-integration-mail - ${spring.integration.version} + ${spring.version} org.springframework.integration spring-integration-ftp - ${spring.integration.version} + ${spring.version} org.springframework.social @@ -104,7 +104,36 @@ org.springframework.integration spring-integration-file - ${spring.integration.version} + ${spring.version} + + + + org.springframework.security + spring-security-core + ${spring.version} + + + org.springframework.security + spring-security-config + ${spring.version} + + + org.springframework.integration + spring-integration-security + ${spring.version} + + + + org.springframework.security + spring-security-test + ${spring.version} + test + + + org.springframework + spring-test + ${spring.version} + test junit diff --git a/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java b/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java new file mode 100644 index 0000000000..af925c63a7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/MessageConsumer.java @@ -0,0 +1,45 @@ +package com.baeldung.si.security; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.messaging.Message; +import org.springframework.stereotype.Service; + +@Service +public class MessageConsumer { + + private String messageContent; + + private Map messagePSContent = new ConcurrentHashMap<>(); + + public String getMessageContent() { + return messageContent; + } + + public void setMessageContent(String messageContent) { + this.messageContent = messageContent; + } + + public Map getMessagePSContent() { + return messagePSContent; + } + + public void setMessagePSContent(Map messagePSContent) { + this.messagePSContent = messagePSContent; + } + + @ServiceActivator(inputChannel = "endDirectChannel") + public void endDirectFlow(Message message) { + setMessageContent(message.getPayload().toString()); + } + + @ServiceActivator(inputChannel = "finalPSResult") + public void endPSFlow(Message message) { + Logger.getAnonymousLogger().info(Thread.currentThread().getName() + " has completed ---------------------------"); + messagePSContent.put(Thread.currentThread().getName(), (String) message.getPayload()); + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java b/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java new file mode 100644 index 0000000000..964a07d7d7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecuredDirectChannel.java @@ -0,0 +1,50 @@ +package com.baeldung.si.security; + +import java.util.logging.Logger; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.security.channel.ChannelSecurityInterceptor; +import org.springframework.integration.security.channel.SecuredChannel; +import org.springframework.messaging.Message; +import org.springframework.security.access.AccessDecisionManager; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.authentication.AuthenticationManager; + +@Configuration +@EnableIntegration +public class SecuredDirectChannel { + + @Bean(name = "startDirectChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_VIEWER", "jane" }) + public DirectChannel startDirectChannel() { + return new DirectChannel(); + } + + @ServiceActivator(inputChannel = "startDirectChannel", outputChannel = "endDirectChannel") + @PreAuthorize("hasRole('ROLE_LOGGER')") + public Message logMessage(Message message) { + Logger.getAnonymousLogger().info(message.toString()); + return message; + } + + @Bean(name = "endDirectChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = { "ROLE_EDITOR" }) + public DirectChannel endDirectChannel() { + return new DirectChannel(); + } + + @Autowired + @Bean + public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) { + ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor(); + channelSecurityInterceptor.setAuthenticationManager(authenticationManager); + channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager); + return channelSecurityInterceptor; + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java b/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java new file mode 100644 index 0000000000..9c5b38b909 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecurityConfig.java @@ -0,0 +1,46 @@ +package com.baeldung.si.security; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.security.channel.ChannelSecurityInterceptor; +import org.springframework.security.access.AccessDecisionManager; +import org.springframework.security.access.AccessDecisionVoter; +import org.springframework.security.access.vote.AffirmativeBased; +import org.springframework.security.access.vote.RoleVoter; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; + +@Configuration +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class SecurityConfig extends GlobalMethodSecurityConfiguration { + + @Override + @Bean + public AuthenticationManager authenticationManager() throws Exception { + return super.authenticationManager(); + } + + @Bean + public AccessDecisionManager customAccessDecisionManager() { + List> decisionVoters = new ArrayList<>(); + decisionVoters.add(new RoleVoter()); + decisionVoters.add(new UsernameAccessDecisionVoter()); + AccessDecisionManager accessDecisionManager = new AffirmativeBased(decisionVoters); + return accessDecisionManager; + } + + @Autowired + @Bean + public ChannelSecurityInterceptor channelSecurityInterceptor(AuthenticationManager authenticationManager, AccessDecisionManager customAccessDecisionManager) { + ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor(); + channelSecurityInterceptor.setAuthenticationManager(authenticationManager); + channelSecurityInterceptor.setAccessDecisionManager(customAccessDecisionManager); + return channelSecurityInterceptor; + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java b/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java new file mode 100644 index 0000000000..11409bb89b --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/SecurityPubSubChannel.java @@ -0,0 +1,82 @@ +package com.baeldung.si.security; + +import java.util.stream.Collectors; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.channel.PublishSubscribeChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.config.GlobalChannelInterceptor; +import org.springframework.integration.security.channel.SecuredChannel; +import org.springframework.integration.security.channel.SecurityContextPropagationChannelInterceptor; +import org.springframework.integration.support.DefaultMessageBuilderFactory; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.ChannelInterceptor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +@Configuration +@EnableIntegration +public class SecurityPubSubChannel { + + @Bean(name = "startPSChannel") + @SecuredChannel(interceptor = "channelSecurityInterceptor", sendAccess = "ROLE_VIEWER") + public PublishSubscribeChannel startChannel() { + return new PublishSubscribeChannel(executor()); + } + + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") + @PreAuthorize("hasRole('ROLE_LOGGER')") + public Message changeMessageToRole(Message message) { + return buildNewMessage(getRoles(), message); + } + + @ServiceActivator(inputChannel = "startPSChannel", outputChannel = "finalPSResult") + @PreAuthorize("hasRole('ROLE_VIEWER')") + public Message changeMessageToUserName(Message message) { + return buildNewMessage(getUsername(), message); + } + + @Bean(name = "finalPSResult") + public DirectChannel finalPSResult() { + return new DirectChannel(); + } + + @Bean + @GlobalChannelInterceptor(patterns = { "startPSChannel", "endDirectChannel" }) + public ChannelInterceptor securityContextPropagationInterceptor() { + return new SecurityContextPropagationChannelInterceptor(); + } + + @Bean + public ThreadPoolTaskExecutor executor() { + ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); + pool.setCorePoolSize(10); + pool.setMaxPoolSize(10); + pool.setWaitForTasksToCompleteOnShutdown(true); + return pool; + } + + public String getRoles() { + SecurityContext securityContext = SecurityContextHolder.getContext(); + return securityContext.getAuthentication().getAuthorities().stream().map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); + } + + public String getUsername() { + SecurityContext securityContext = SecurityContextHolder.getContext(); + return securityContext.getAuthentication().getName(); + } + + public Message buildNewMessage(String content, Message message) { + DefaultMessageBuilderFactory builderFactory = new DefaultMessageBuilderFactory(); + MessageBuilder messageBuilder = builderFactory.withPayload(content); + messageBuilder.copyHeaders(message.getHeaders()); + return messageBuilder.build(); + } + +} diff --git a/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java b/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java new file mode 100644 index 0000000000..052f79ddf7 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/si/security/UsernameAccessDecisionVoter.java @@ -0,0 +1,45 @@ +package com.baeldung.si.security; + +import java.util.Collection; + +import org.springframework.security.access.AccessDecisionVoter; +import org.springframework.security.access.ConfigAttribute; +import org.springframework.security.core.Authentication; + +public class UsernameAccessDecisionVoter implements AccessDecisionVoter { + private String rolePrefix = "ROLE_"; + + @Override + public boolean supports(ConfigAttribute attribute) { + if ((attribute.getAttribute() != null) + && !attribute.getAttribute().startsWith(rolePrefix)) { + return true; + }else { + return false; + } + } + + @Override + public boolean supports(Class clazz) { + return true; + } + + @Override + public int vote(Authentication authentication, Object object, Collection attributes) { + if (authentication == null) { + return ACCESS_DENIED; + } + String name = authentication.getName(); + int result = ACCESS_ABSTAIN; + for (ConfigAttribute attribute : attributes) { + if (this.supports(attribute)) { + result = ACCESS_DENIED; + if (attribute.getAttribute().equals(name)) { + return ACCESS_GRANTED; + } + } + } + return result; + } + +} diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java new file mode 100644 index 0000000000..45dcb1e836 --- /dev/null +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java @@ -0,0 +1,81 @@ +package com.baeldung.si; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.SubscribableChannel; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.si.security.MessageConsumer; +import com.baeldung.si.security.SecuredDirectChannel; +import com.baeldung.si.security.SecurityConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class }) +public class TestSpringIntegrationSecurity { + + @Autowired + SubscribableChannel startDirectChannel; + + @Autowired + MessageConsumer messageConsumer; + + final String DIRECT_CHANNEL_MESSAGE = "Direct channel message"; + + @Test(expected = AuthenticationCredentialsNotFoundException.class) + public void givenNoUser_whenSendToDirectChannel_thenCredentialNotFound() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } + + @Test(expected = AccessDeniedException.class) + @WithMockUser(username = "jane", roles = { "LOGGER" }) + public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { + try { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } catch (Exception e) { + throw e.getCause(); + } + } + + @Test(expected = AccessDeniedException.class) + @WithMockUser(username = "jane") + public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { + try { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } catch (Exception e) { + throw e.getCause(); + } + } + + @Test(expected = AccessDeniedException.class) + @WithMockUser(roles = { "VIEWER" }) + public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() throws Throwable { + try { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + } catch (Exception e) { + throw e.getCause(); + } + } + + @Test + @WithMockUser(roles = { "LOGGER", "VIEWER", "EDITOR" }) + public void givenRoleLoggerAndUser_whenSendMessageToDirectChannel_thenFlowCompletedSuccessfully() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); + } + + @Test + @WithMockUser(username = "jane", roles = { "LOGGER", "EDITOR" }) + public void givenJaneLoggerEditor_whenSendToDirectChannel_thenFlowCompleted() { + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); + } + +} diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java new file mode 100644 index 0000000000..b06136a7ca --- /dev/null +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurityExecutor.java @@ -0,0 +1,68 @@ +package com.baeldung.si; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.SubscribableChannel; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.si.security.MessageConsumer; +import com.baeldung.si.security.SecurityConfig; +import com.baeldung.si.security.SecurityPubSubChannel; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SecurityPubSubChannel.class, MessageConsumer.class, SecurityConfig.class }) +public class TestSpringIntegrationSecurityExecutor { + + @Autowired + SubscribableChannel startPSChannel; + + @Autowired + MessageConsumer messageConsumer; + + @Autowired + ThreadPoolTaskExecutor executor; + + final String DIRECT_CHANNEL_MESSAGE = "Direct channel message"; + + @Before + public void clearData() { + messageConsumer.setMessagePSContent(new ConcurrentHashMap<>()); + executor.setWaitForTasksToCompleteOnShutdown(true); + } + + @Test + @WithMockUser(username = "user", roles = { "VIEWER" }) + public void givenRoleUser_whenSendMessageToPSChannel_thenNoMessageArrived() throws IllegalStateException, InterruptedException { + startPSChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + + executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS); + + assertEquals(1, messageConsumer.getMessagePSContent().size()); + assertTrue(messageConsumer.getMessagePSContent().values().contains("user")); + } + + @Test + @WithMockUser(username = "user", roles = { "LOGGER", "VIEWER" }) + public void givenRoleUserAndLogger_whenSendMessageToPSChannel_then2GetMessages() throws IllegalStateException, InterruptedException { + startPSChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); + + executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS); + + assertEquals(2, messageConsumer.getMessagePSContent().size()); + assertTrue(messageConsumer.getMessagePSContent().values().contains("user")); + assertTrue(messageConsumer.getMessagePSContent().values().contains("ROLE_LOGGER,ROLE_VIEWER")); + } + +} From 5a34de448edd30514b3454dddcb30f9ce5ea749e Mon Sep 17 00:00:00 2001 From: Orry Date: Tue, 6 Feb 2018 21:47:26 +0200 Subject: [PATCH 053/102] AssertJ Exception Assertions (#3600) * Add XML, JavaConfig and Autowired examples. * BAEL-1517: Added Java7 style assertions * BAEL-1517: Upgrade AssertJ to 3.9.0; add Java 8 style assertion tests * Revert "Add XML, JavaConfig and Autowired examples." This reverts commit 8f4df6b903866dac1725832d06ee7382fc89d0ce. --- testing-modules/testing/pom.xml | 2 +- .../exceptions/Java7StyleAssertions.java | 24 +++++++++++ .../exceptions/Java8StyleAssertions.java | 42 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java create mode 100644 testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml index c76045380b..91792a4681 100644 --- a/testing-modules/testing/pom.xml +++ b/testing-modules/testing/pom.xml @@ -173,7 +173,7 @@ 0.7.7.201606060606 21.0 3.1.0 - 3.6.1 + 3.9.0 2.1.0 0.32 1.1.0 diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java new file mode 100644 index 0000000000..07a5be1118 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java @@ -0,0 +1,24 @@ +package com.baeldung.testing.assertj.exceptions; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; + +import org.junit.Test; + +public class Java7StyleAssertions { + + @Test + public void whenDividingByZero_thenArithmeticException() { + try { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + fail("ArithmeticException expected because dividing by zero yields an ArithmeticException."); + failBecauseExceptionWasNotThrown(ArithmeticException.class); + } catch (Exception e) { + assertThat(e).hasMessage("/ by zero"); + assertThat(e).isInstanceOf(ArithmeticException.class); + } + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java new file mode 100644 index 0000000000..53f192bb2f --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java @@ -0,0 +1,42 @@ +package com.baeldung.testing.assertj.exceptions; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.catchThrowable; + +import org.junit.Test; + +public class Java8StyleAssertions { + + @Test + public void whenDividingByZero_thenArithmeticException() { + assertThatThrownBy(() -> { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }).isInstanceOf(ArithmeticException.class) + .hasMessageContaining("/ by zero"); + + assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }) + .withMessageContaining("/ by zero"); + + // BDD style: + + // when + Throwable thrown = catchThrowable(() -> { + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }); + + // then + assertThat(thrown).isInstanceOf(ArithmeticException.class) + .hasMessageContaining("/ by zero"); + + } +} From efb66e300108383c5c72b304dc81429dba1a7acf Mon Sep 17 00:00:00 2001 From: deep20jain Date: Wed, 7 Feb 2018 01:37:38 +0530 Subject: [PATCH 054/102] Bael 1299 - Maze Solver - deep20jain@gmail.com (#3537) * Maze solver using DFS * Adding BFS maze solver * Fixing formatting --- .../algorithms/maze/solver/BFSMazeSolver.java | 52 +++++++ .../algorithms/maze/solver/Coordinate.java | 31 ++++ .../algorithms/maze/solver/DFSMazeSolver.java | 48 ++++++ .../baeldung/algorithms/maze/solver/Maze.java | 141 ++++++++++++++++++ .../algorithms/maze/solver/MazeDriver.java | 34 +++++ algorithms/src/main/resources/maze/maze1.txt | 12 ++ algorithms/src/main/resources/maze/maze2.txt | 22 +++ 7 files changed, 340 insertions(+) create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java create mode 100644 algorithms/src/main/resources/maze/maze1.txt create mode 100644 algorithms/src/main/resources/maze/maze2.txt diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java new file mode 100644 index 0000000000..08972251b8 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java @@ -0,0 +1,52 @@ +package com.baeldung.algorithms.maze.solver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class BFSMazeSolver { + private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; + + public List solve(Maze maze) { + LinkedList nextToVisit = new LinkedList<>(); + Coordinate start = maze.getEntry(); + nextToVisit.add(start); + + while (!nextToVisit.isEmpty()) { + Coordinate cur = nextToVisit.remove(); + + if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) { + continue; + } + + if (maze.isWall(cur.getX(), cur.getY())) { + maze.setVisited(cur.getX(), cur.getY(), true); + continue; + } + + if (maze.isExit(cur.getX(), cur.getY())) { + return backtrackPath(cur); + } + + for (int[] direction : DIRECTIONS) { + Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur); + nextToVisit.add(coordinate); + maze.setVisited(cur.getX(), cur.getY(), true); + } + } + return Collections.emptyList(); + } + + private List backtrackPath(Coordinate cur) { + List path = new ArrayList<>(); + Coordinate iter = cur; + + while (iter != null) { + path.add(iter); + iter = iter.parent; + } + + return path; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java new file mode 100644 index 0000000000..09b2ced5e6 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java @@ -0,0 +1,31 @@ +package com.baeldung.algorithms.maze.solver; + +public class Coordinate { + int x; + int y; + Coordinate parent; + + public Coordinate(int x, int y) { + this.x = x; + this.y = y; + this.parent = null; + } + + public Coordinate(int x, int y, Coordinate parent) { + this.x = x; + this.y = y; + this.parent = parent; + } + + int getX() { + return x; + } + + int getY() { + return y; + } + + Coordinate getParent() { + return parent; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java new file mode 100644 index 0000000000..f9640066b9 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java @@ -0,0 +1,48 @@ +package com.baeldung.algorithms.maze.solver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class DFSMazeSolver { + private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; + + public List solve(Maze maze) { + List path = new ArrayList<>(); + if (explore(maze, maze.getEntry() + .getX(), + maze.getEntry() + .getY(), + path)) { + return path; + } + return Collections.emptyList(); + } + + private boolean explore(Maze maze, int row, int col, List path) { + if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) { + return false; + } + + path.add(new Coordinate(row, col)); + maze.setVisited(row, col, true); + + if (maze.isExit(row, col)) { + return true; + } + + for (int[] direction : DIRECTIONS) { + Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]); + if (explore(maze, coordinate.getX(), coordinate.getY(), path)) { + return true; + } + } + + path.remove(path.size() - 1); + return false; + } + + private Coordinate getNextCoordinate(int row, int col, int i, int j) { + return new Coordinate(row + i, col + j); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java new file mode 100644 index 0000000000..8aaa44d9b1 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java @@ -0,0 +1,141 @@ +package com.baeldung.algorithms.maze.solver; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class Maze { + private static final int ROAD = 0; + private static final int WALL = 1; + private static final int START = 2; + private static final int EXIT = 3; + private static final int PATH = 4; + + private int[][] maze; + private boolean[][] visited; + private Coordinate start; + private Coordinate end; + + public Maze(File maze) throws FileNotFoundException { + String fileText = ""; + try (Scanner input = new Scanner(maze)) { + while (input.hasNextLine()) { + fileText += input.nextLine() + "\n"; + } + } + initializeMaze(fileText); + } + + private void initializeMaze(String text) { + if (text == null || (text = text.trim()).length() == 0) { + throw new IllegalArgumentException("empty lines data"); + } + + String[] lines = text.split("[\r]?\n"); + maze = new int[lines.length][lines[0].length()]; + visited = new boolean[lines.length][lines[0].length()]; + + for (int row = 0; row < getHeight(); row++) { + if (lines[row].length() != getWidth()) { + throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")"); + } + + for (int col = 0; col < getWidth(); col++) { + if (lines[row].charAt(col) == '#') + maze[row][col] = WALL; + else if (lines[row].charAt(col) == 'S') { + maze[row][col] = START; + start = new Coordinate(row, col); + } else if (lines[row].charAt(col) == 'E') { + maze[row][col] = EXIT; + end = new Coordinate(row, col); + } else + maze[row][col] = ROAD; + } + } + } + + public int getHeight() { + return maze.length; + } + + public int getWidth() { + return maze[0].length; + } + + public Coordinate getEntry() { + return start; + } + + public Coordinate getExit() { + return end; + } + + public boolean isExit(int x, int y) { + return x == end.getX() && y == end.getY(); + } + + public boolean isStart(int x, int y) { + return x == start.getX() && y == start.getY(); + } + + public boolean isExplored(int row, int col) { + return visited[row][col]; + } + + public boolean isWall(int row, int col) { + return maze[row][col] == WALL; + } + + public void setVisited(int row, int col, boolean value) { + visited[row][col] = value; + } + + public boolean isValidLocation(int row, int col) { + if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) { + return false; + } + return true; + } + + public void printPath(List path) { + int[][] tempMaze = Arrays.stream(maze) + .map(int[]::clone) + .toArray(int[][]::new); + for (Coordinate coordinate : path) { + if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) { + continue; + } + tempMaze[coordinate.getX()][coordinate.getY()] = PATH; + } + System.out.println(toString(tempMaze)); + } + + public String toString(int[][] maze) { + StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1)); + for (int row = 0; row < getHeight(); row++) { + for (int col = 0; col < getWidth(); col++) { + if (maze[row][col] == ROAD) { + result.append(' '); + } else if (maze[row][col] == WALL) { + result.append('#'); + } else if (maze[row][col] == START) { + result.append('S'); + } else if (maze[row][col] == EXIT) { + result.append('E'); + } else { + result.append('.'); + } + } + result.append('\n'); + } + return result.toString(); + } + + public void reset() { + for (int i = 0; i < visited.length; i++) + Arrays.fill(visited[i], false); + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java new file mode 100644 index 0000000000..60263deba3 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java @@ -0,0 +1,34 @@ +package com.baeldung.algorithms.maze.solver; + +import java.io.File; +import java.util.List; + +public class MazeDriver { + public static void main(String[] args) throws Exception { + File maze1 = new File("src/main/resources/maze/maze1.txt"); + File maze2 = new File("src/main/resources/maze/maze2.txt"); + + execute(maze1); + execute(maze2); + } + + private static void execute(File file) throws Exception { + Maze maze = new Maze(file); + dfs(maze); + bfs(maze); + } + + private static void bfs(Maze maze) { + BFSMazeSolver bfs = new BFSMazeSolver(); + List path = bfs.solve(maze); + maze.printPath(path); + maze.reset(); + } + + private static void dfs(Maze maze) { + DFSMazeSolver dfs = new DFSMazeSolver(); + List path = dfs.solve(maze); + maze.printPath(path); + maze.reset(); + } +} diff --git a/algorithms/src/main/resources/maze/maze1.txt b/algorithms/src/main/resources/maze/maze1.txt new file mode 100644 index 0000000000..0a6309d25b --- /dev/null +++ b/algorithms/src/main/resources/maze/maze1.txt @@ -0,0 +1,12 @@ +S ######## +# # +# ### ## # +# # # # +# # # # # +# ## ##### +# # # +# # # # # +##### #### +# # E +# # # # +########## \ No newline at end of file diff --git a/algorithms/src/main/resources/maze/maze2.txt b/algorithms/src/main/resources/maze/maze2.txt new file mode 100644 index 0000000000..22e6d0382a --- /dev/null +++ b/algorithms/src/main/resources/maze/maze2.txt @@ -0,0 +1,22 @@ +S ########################## +# # # # +# # #### ############### # +# # # # # # +# # #### # # ############### +# # # # # # # +# # # #### ### ########### # +# # # # # # +# ################## # +######### # # # # # +# # #### # ####### # # +# # ### ### # # # # # +# # ## # ##### # # +##### ####### # # # # # +# # ## ## #### # # +# ##### ####### # # +# # ############ +####### ######### # # +# # ######## # +# ####### ###### ## # E +# # # ## # +############################ \ No newline at end of file From 596efe3042c68eb91cc048356ab71a9ae95ce1d7 Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Wed, 7 Feb 2018 19:45:04 +0530 Subject: [PATCH 055/102] Changes for BAEL-1472 --- persistence-modules/spring-data-redis/pom.xml | 12 ++++- .../spring/data/redis/config/RedisConfig.java | 9 ++-- .../spring/data/redis/model/Student.java | 3 ++ .../data/redis/repo/StudentRepository.java | 19 ++----- .../redis/repo/StudentRepositoryImpl.java | 49 ------------------- .../StudentRepositoryIntegrationTest.java | 40 ++++++++------- 6 files changed, 46 insertions(+), 86 deletions(-) delete mode 100644 persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java diff --git a/persistence-modules/spring-data-redis/pom.xml b/persistence-modules/spring-data-redis/pom.xml index 6cb49f11cf..0dc51e790e 100644 --- a/persistence-modules/spring-data-redis/pom.xml +++ b/persistence-modules/spring-data-redis/pom.xml @@ -16,11 +16,13 @@ UTF-8 - 4.3.7.RELEASE - 1.8.1.RELEASE + 5.0.3.RELEASE + 2.0.3.RELEASE 3.2.4 2.9.0 0.10.0 + 2.0.3.RELEASE + @@ -73,6 +75,12 @@ nosqlunit-redis ${nosqlunit.version} + + + org.springframework.data + spring-data-commons + 2.0.3.RELEASE + diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java index 4fd83a2bb6..4ea8bb4bc0 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -1,8 +1,5 @@ package com.baeldung.spring.data.redis.config; -import com.baeldung.spring.data.redis.queue.MessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; -import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -11,10 +8,16 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.GenericToStringSerializer; +import com.baeldung.spring.data.redis.queue.MessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; + @Configuration @ComponentScan("com.baeldung.spring.data.redis") +@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo") public class RedisConfig { @Bean diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java index 10ba0f5ef4..b97ed23387 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java @@ -2,6 +2,9 @@ package com.baeldung.spring.data.redis.model; import java.io.Serializable; +import org.springframework.data.redis.core.RedisHash; + +@RedisHash("Student") public class Student implements Serializable { public enum Gender { diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java index 250c227f00..39f13bb6a7 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java @@ -1,18 +1,9 @@ package com.baeldung.spring.data.redis.repo; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + import com.baeldung.spring.data.redis.model.Student; -import java.util.Map; - -public interface StudentRepository { - - void saveStudent(Student person); - - void updateStudent(Student student); - - Student findStudent(String id); - - Map findAllStudents(); - - void deleteStudent(String id); -} +@Repository +public interface StudentRepository extends CrudRepository {} diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java deleted file mode 100644 index f13bef0f54..0000000000 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.baeldung.spring.data.redis.repo; - -import com.baeldung.spring.data.redis.model.Student; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Repository; - -import javax.annotation.PostConstruct; -import java.util.Map; - -@Repository -public class StudentRepositoryImpl implements StudentRepository { - - private static final String KEY = "Student"; - - private RedisTemplate redisTemplate; - private HashOperations hashOperations; - - @Autowired - public StudentRepositoryImpl(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - @PostConstruct - private void init() { - hashOperations = redisTemplate.opsForHash(); - } - - public void saveStudent(final Student student) { - hashOperations.put(KEY, student.getId(), student); - } - - public void updateStudent(final Student student) { - hashOperations.put(KEY, student.getId(), student); - } - - public Student findStudent(final String id) { - return (Student) hashOperations.get(KEY, id); - } - - public Map findAllStudents() { - return hashOperations.entries(KEY); - } - - public void deleteStudent(final String id) { - hashOperations.delete(KEY, id); - } -} diff --git a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java index 1028c0bc24..66ef3c21b2 100644 --- a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryIntegrationTest.java @@ -1,17 +1,20 @@ package com.baeldung.spring.data.redis.repo; -import com.baeldung.spring.data.redis.config.RedisConfig; -import com.baeldung.spring.data.redis.model.Student; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import com.baeldung.spring.data.redis.config.RedisConfig; +import com.baeldung.spring.data.redis.model.Student; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = RedisConfig.class) @@ -23,18 +26,18 @@ public class StudentRepositoryIntegrationTest { @Test public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + final Student retrievedStudent = studentRepository.findById(student.getId()).get(); assertEquals(student.getId(), retrievedStudent.getId()); } @Test public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); + studentRepository.save(student); student.setName("Richard Watson"); - studentRepository.saveStudent(student); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + final Student retrievedStudent = studentRepository.findById(student.getId()).get(); assertEquals(student.getName(), retrievedStudent.getName()); } @@ -42,18 +45,19 @@ public class StudentRepositoryIntegrationTest { public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); - studentRepository.saveStudent(engStudent); - studentRepository.saveStudent(medStudent); - final Map retrievedStudent = studentRepository.findAllStudents(); - assertEquals(retrievedStudent.size(), 2); + studentRepository.save(engStudent); + studentRepository.save(medStudent); + List students = new ArrayList<>(); + studentRepository.findAll().forEach(students::add); + assertEquals(students.size(), 2); } @Test public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); - studentRepository.saveStudent(student); - studentRepository.deleteStudent(student.getId()); - final Student retrievedStudent = studentRepository.findStudent(student.getId()); + studentRepository.save(student); + studentRepository.deleteById(student.getId()); + final Student retrievedStudent = studentRepository.findById(student.getId()).orElse(null); assertNull(retrievedStudent); } } \ No newline at end of file From b2372b50061766aa27e203d6a1f84a2e04fb4198 Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Wed, 7 Feb 2018 19:46:42 +0530 Subject: [PATCH 056/102] Changes for BAEL-1472 --- persistence-modules/spring-data-redis/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-redis/pom.xml b/persistence-modules/spring-data-redis/pom.xml index 0dc51e790e..0b9075147d 100644 --- a/persistence-modules/spring-data-redis/pom.xml +++ b/persistence-modules/spring-data-redis/pom.xml @@ -79,7 +79,7 @@ org.springframework.data spring-data-commons - 2.0.3.RELEASE + ${spring-data-commons.version} From 6887bc10d1a3aa1d8b5778917c77c9047ef97dee Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Wed, 7 Feb 2018 20:29:35 +0530 Subject: [PATCH 057/102] BAEL-1525: More changes. --- .../java/com/baeldung/string/Palindrome.java | 28 ++++++++++++++- .../string/WhenCheckingPalindrome.java | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index e339d92447..37284a5424 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -8,7 +8,33 @@ public class Palindrome { int forward = 0; int backward = length - 1; boolean palindrome = true; - while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false); + while (backward > forward) { + char forwardChar = text.charAt(forward++); + char backwardChar = text.charAt(backward--); + if (forwardChar != backwardChar) + return false; + } return palindrome; } + + public boolean isPalindromeReverseTheString(String text) { + String reverse = ""; + text = text.toLowerCase(); + char[] plain = text.toCharArray(); + for (int i = plain.length - 1; i >= 0; i--) + reverse += plain[i]; + return reverse.equals(text); + } + + public boolean isPalindromeUsingStringBuilder(String text) { + StringBuilder plain = new StringBuilder(text); + StringBuilder reverse = plain.reverse(); + return reverse.equals(plain); + } + + public boolean isPalindromeUsingStringBuffer(String text) { + StringBuffer plain = new StringBuffer(text); + StringBuffer reverse = plain.reverse(); + return reverse.equals(plain); + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java index 691d74c751..eeaed09fff 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -34,4 +34,40 @@ public class WhenCheckingPalindrome { for(String sentence:sentences) Assert.assertTrue(palindrome.isPalindrome(sentence)); } + + @Test + public void whenReverseWord_shouldBePalindrome() { + for(String word:words) + Assert.assertTrue(palindrome.isPalindromeReverseTheString(word)); + } + + @Test + public void whenReverseSentence_shouldBePalindrome() { + for(String sentence:sentences) + Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence)); + } + + @Test + public void whenStringBuilderWord_shouldBePalindrome() { + for(String word:words) + Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); + } + + @Test + public void whenStringBuilderSentence_shouldBePalindrome() { + for(String sentence:sentences) + Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); + } + + @Test + public void whenStringBufferWord_shouldBePalindrome() { + for(String word:words) + Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); + } + + @Test + public void whenStringBufferSentence_shouldBePalindrome() { + for(String sentence:sentences) + Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); + } } From 0743dec07c59c0fecb8ac3ad0052457d5e6feaf6 Mon Sep 17 00:00:00 2001 From: iaforek Date: Thu, 8 Feb 2018 06:26:42 +0000 Subject: [PATCH 058/102] Extracted 'constraints' methods and renamed variables (#3580) * Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention * BAEL-821 Added more test and updated round methods. * BAEL-837 - initial commit. A few examples of adding doubles. * BAEL-837 - Couple of smaller changes * BAEL-837 - Added jUnit test. * BAEL-579 Updated Spring Cloud Version I was getting error: java.lang.NoSuchMethodError: org.springframework.cloud.config.environment.Environment After version update, all is okay. * BAEL-579 Added actuator to Cloud Config Client. * BAEL-579 Enabled cloud bus and updated dependencies. * BAEL-579 Config Client using Spring Cloud Bus. * BAEL-579 Recreated Basic Config Server. * BAEL-579 Recreated Config Client. * BAEL-579 Removed test Git URL. * BAEL-579 Added Actuator to Config Client * BAEL-579 Added Spring Cloud Bus to Client. * BAEL-579 Server changes for Spring Cloud Bus Added dependencies and removed git.clone-on-start as this was causing server to throw errors after git properties change. * BAEL-579 Removed Git URL. * Revert "BAEL-579 Updated Spring Cloud Version" This reverts commit f775bf91e53a1ecfb9b70596688d7c8202bf495f. * Revert "BAEL-579 Config Client using Spring Cloud Bus." This reverts commit 1d96bc5761994a33af9a7a9aa5ab68604a5b44dc. * Revert "BAEL-579 Enabled cloud bus and updated dependencies." This reverts commit 7845da922d89d53506dd0fff387ea13694c50bc1. * Revert "BAEL-579 Added actuator to Cloud Config Client." This reverts commit 076657a26a57e0aa676989a4d97966a3b9d53e1c. * BAEL-579 Added missing dependency versions. * BAEL-579 Added missing dependency versions. * Updated gitignore * BAEL-1065 Simple performance check StringBuffer vs StringBuilder. * BAEL-1065 Added JMH benchmarks * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Dancing Links Algorithm. Smaller changes to Backtracking * BAEL-1298 Resolve conflict - use most up-to-date POM * Updated code - mostly with CONSTANTS. Extracted methods. * Removed pointless Java8 code. Renamed constant * Extracted 'constraints' methods and renamed coverBoard variable * Extracted 'constraints' methods and renamed coverBoard variable --- .../sudoku/BacktrackingAlgorithm.java | 2 +- .../algorithms/sudoku/DancingLinks.java | 4 +- .../sudoku/DancingLinksAlgorithm.java | 87 +++++++++++-------- 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java index dc2a324c12..ff426cbe68 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -101,4 +101,4 @@ public class BacktrackingAlgorithm { } return true; } -} \ No newline at end of file +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java index e5a02b7c91..d3cbb2bd02 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java @@ -120,10 +120,10 @@ public class DancingLinks { } private static void printSolution(int[][] result) { - int N = result.length; + int size = result.length; for (int[] aResult : result) { StringBuilder ret = new StringBuilder(); - for (int j = 0; j < N; j++) { + for (int j = 0; j < size; j++) { ret.append(aResult[j]).append(" "); } System.out.println(ret); diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java index 6b0f57a075..76b686afa6 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -39,70 +39,83 @@ public class DancingLinksAlgorithm { } private boolean[][] createExactCoverBoard() { - boolean[][] R = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS]; + boolean[][] coverBoard = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS]; int hBase = 0; + hBase = checkCellConstraint(coverBoard, hBase); + hBase = checkRowConstraint(coverBoard, hBase); + hBase = checkColumnConstraint(coverBoard, hBase); + checkSubsectionConstraint(coverBoard, hBase); + + return coverBoard; + } - // Cell constraint. - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { - for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { - int index = getIndex(r, c, n); - R[index][hBase] = true; - } - } - } - - // Row constrain. - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { - for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) { - int index = getIndex(r, c1, n); - R[index][hBase] = true; - } - } - } - - // Column constraint. - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { - for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) { - int index = getIndex(r1, c, n); - R[index][hBase] = true; - } - } - } - - // Subsection constraint + private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) { for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) { for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) { int index = getIndex(br + rDelta, bc + cDelta, n); - R[index][hBase] = true; + coverBoard[index][hBase] = true; } } } } } - return R; + return hBase; + } + + private int checkColumnConstraint(boolean[][] coverBoard, int hBase) { + for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) { + int index = getIndex(r1, c, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; + } + + private int checkRowConstraint(boolean[][] coverBoard, int hBase) { + for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { + for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) { + int index = getIndex(r, c1, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; + } + + private int checkCellConstraint(boolean[][] coverBoard, int hBase) { + for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { + for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { + int index = getIndex(r, c, n); + coverBoard[index][hBase] = true; + } + } + } + return hBase; } private boolean[][] initializeExactCoverBoard(int[][] board) { - boolean[][] R = createExactCoverBoard(); + boolean[][] coverBoard = createExactCoverBoard(); for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) { for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) { int n = board[i - 1][j - 1]; if (n != NO_VALUE) { for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { if (num != n) { - Arrays.fill(R[getIndex(i, j, num)], false); + Arrays.fill(coverBoard[getIndex(i, j, num)], false); } } } } } - return R; + return coverBoard; } } \ No newline at end of file From 2b2b78cbf28072ed643c1b37cfd5929bada148b3 Mon Sep 17 00:00:00 2001 From: felipeazv Date: Thu, 8 Feb 2018 10:24:21 +0100 Subject: [PATCH 059/102] [BAEL-1449]-Combining Publishers (Project Reactor) --- .../reactor/core/CombiningPublishersTest.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java diff --git a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java new file mode 100644 index 0000000000..f33f911274 --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java @@ -0,0 +1,107 @@ +package com.baeldung.reactor.core; + +import java.time.Duration; + +import org.junit.Test; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +public class CombiningPublishersTest { + + private static Integer min = 1; + private static Integer max = 5; + + private static Flux evenNumbers = Flux.range(min, max).filter(x -> x % 2 == 0); + private static Flux oddNumbers = Flux.range(min, max).filter(x -> x % 2 > 0); + + + @Test + public void testMerge() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testMergeWithDelayedElements() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + @Test + public void testConcat() { + Flux fluxOfIntegers = Flux.concat( + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testConcatWith() { + Flux fluxOfIntegers = evenNumbers + .concatWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testCombineLatest() { + Flux fluxOfIntegers = Flux.combineLatest( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(5) + .expectNext(7) + .expectNext(9) + .expectComplete() + .verify(); + + } + + @Test + public void testCombineLatest1() { + StepVerifier.create(Flux.combineLatest(obj -> (int) obj[1], evenNumbers, oddNumbers)) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .verifyComplete(); + } + +} From 19ef04d07cb25d48f7c6b8ab6e8f0ea24556d46d Mon Sep 17 00:00:00 2001 From: felipeazv Date: Thu, 8 Feb 2018 10:30:46 +0100 Subject: [PATCH 060/102] [BAEL-1449]-Combining Publishers (Project Reactor) --- reactor-core/pom.xml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 12f481c96f..d387471d56 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -26,11 +26,17 @@ ${assertj.version} test - + + + io.projectreactor + reactor-test + ${reactor-core.version} + test + - 3.0.5.RELEASE + 3.1.3.RELEASE 3.6.1 From 5ce6ad7d8f42af90c131f2efe107be303b143f2c Mon Sep 17 00:00:00 2001 From: felipeazv Date: Thu, 8 Feb 2018 11:39:09 +0100 Subject: [PATCH 061/102] [BAEL-1449]-Combining Publishers (Project Reactor) --- .../reactor/core/CombiningPublishersTest.java | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java index f33f911274..9d5d094875 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java @@ -103,5 +103,80 @@ public class CombiningPublishersTest { .expectNext(5) .verifyComplete(); } - + + @Test + public void testMergeSequential() { + Flux fluxOfIntegers = Flux.mergeSequential( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + + @Test + public void testMergeDelayError() { + Flux fluxOfIntegers = Flux.mergeDelayError(1, + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + @Test + public void testMergeWith() { + Flux fluxOfIntegers = evenNumbers.mergeWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testZip() { + Flux fluxOfIntegers = Flux.zip( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(3) + .expectNext(7) + .expectComplete() + .verify(); + } + + @Test + public void testZipWith() { + Flux fluxOfIntegers = evenNumbers + .zipWith(oddNumbers, + (a, b) -> a * b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(12) + .expectComplete() + .verify(); + } + + } From 9d9045f130e647ccc48f78c11a3b7980880c2a72 Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Thu, 8 Feb 2018 21:04:46 +0530 Subject: [PATCH 062/102] BAEL-1525: New changes added. --- .../java/com/baeldung/string/Palindrome.java | 17 +++++++- .../string/WhenCheckingPalindrome.java | 41 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index 37284a5424..03b65d1a84 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -3,7 +3,8 @@ package com.baeldung.string; public class Palindrome { public boolean isPalindrome(String text) { - text = text.replaceAll("\\s+", "").toLowerCase(); + text = text.replaceAll("\\s+", "") + .toLowerCase(); int length = text.length(); int forward = 0; int backward = length - 1; @@ -19,7 +20,7 @@ public class Palindrome { public boolean isPalindromeReverseTheString(String text) { String reverse = ""; - text = text.toLowerCase(); + text = text.replaceAll("\\s+", "").toLowerCase(); char[] plain = text.toCharArray(); for (int i = plain.length - 1; i >= 0; i--) reverse += plain[i]; @@ -37,4 +38,16 @@ public class Palindrome { StringBuffer reverse = plain.reverse(); return reverse.equals(plain); } + + public boolean isPalindromeRecursive(String text, int forward, int backward) { + if (forward == backward) + return true; + if ((text.charAt(forward)) != (text.charAt(backward))) + return false; + if (forward < backward + 1) { + return isPalindromeRecursive(text, forward + 1, backward - 1); + } + + return true; + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java index eeaed09fff..e3655a3140 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -1,5 +1,7 @@ package com.baeldung.string; +import java.util.Arrays; + import org.junit.Assert; import org.junit.Test; @@ -68,6 +70,45 @@ public class WhenCheckingPalindrome { @Test public void whenStringBufferSentence_shouldBePalindrome() { for(String sentence:sentences) + Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); } + + @Test + public void whenPalindromeRecursive_wordShouldBePalindrome() { + for(String word:words) { + word = word.replaceAll("\\s+", "").toLowerCase(); + int backward = word.length()-1; + + Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); + } + } + + @Test + public void whenPalindromeRecursive_sentenceShouldBePalindrome() { + for(String sentence:sentences) { + sentence = sentence.replaceAll("\\s+", "").toLowerCase(); + int backward = sentence.length()-1; + + Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); + } + } + + @Test + public void whenPalindromeStreams_wordShouldBePalindrome() { + String[] expected = Arrays.stream(words) + .filter(palindrome::isPalindrome) + .toArray(String[]::new); + + Assert.assertArrayEquals(expected, words); + } + + @Test + public void whenPalindromeStreams_sentenceShouldBePalindrome() { + String[] expected = Arrays.stream(sentences) + .filter(palindrome::isPalindrome) + .toArray(String[]::new); + + Assert.assertArrayEquals(expected, sentences); + } } From 8b279673c493f43ea821c90ab9fe3f4995276b49 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Thu, 8 Feb 2018 23:05:13 +0100 Subject: [PATCH 063/102] Code for BAEL-894 - regular expressions in Kotlin (#3618) --- .../com/baeldung/kotlin/stdlib/RegexTest.kt | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt new file mode 100644 index 0000000000..eeb587ee22 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt @@ -0,0 +1,128 @@ +package com.baeldung.kotlin.stdlib + +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import kotlin.test.* +import kotlin.text.RegexOption.* + +class RegexTest { + + @Test + fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() { + val pattern = """a([bc]+)d?\\""" + + assertEquals(Regex.fromLiteral(pattern).pattern, pattern) + assertEquals(pattern, Regex(pattern).pattern) + assertEquals(pattern, pattern.toRegex().pattern) + } + + @Test + fun whenRegexMatches_thenResultIsTrue() { + val regex = """a([bc]+)d?""".toRegex() + + assertTrue(regex.containsMatchIn("xabcdy")) + assertTrue(regex.matches("abcd")) + assertFalse(regex matches "xabcdy") + } + + @Test + fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """a([bc]+)d?""".toRegex() + + assertNull(regex.matchEntire("xabcdy")) + + val matchResult = regex.matchEntire("abbccbbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + assertEquals("bbccbb", matchResult.destructured.component1()) + assertNull(matchResult.next()) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() { + val regex = """a([bc]+)d?""".toRegex() + var matchResult = regex.find("abcb abbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals("abcb", matchResult.value) + assertEquals(IntRange(0, 3), matchResult.range) + assertEquals(listOf("abcb", "bcb"), matchResult.groupValues) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + + matchResult = matchResult.next() + + assertNotNull(matchResult) + assertEquals("abbd", matchResult!!.value) + assertEquals("bb", matchResult.groupValues[1]) + + matchResult = matchResult.next() + + assertNull(matchResult) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """([\w\s]+) is (\d+) years old""".toRegex() + val matchResult = regex.find("Mickey Mouse is 95 years old") + val (name, age) = matchResult!!.destructured + + assertEquals("Mickey Mouse", name) + assertEquals("95", age) + } + + @Test + fun givenNonMatchingRegex_whenFindCalled_thenNull() { + val regex = """a([bc]+)d?""".toRegex() + val matchResult = regex.find("foo") + + assertNull(matchResult) + } + + @Test + fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() { + val regex = """a([bc]+)d?""".toRegex() + val matchResults = regex.findAll("foo") + + assertNotNull(matchResults) + assertTrue(matchResults.none()) + } + + @Test + fun whenReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val grim = regex.replace(beautiful, "dark") + val shiny = regex.replaceFirst(beautiful, "rainbow") + + assertEquals("Roses are dark, Violets are dark", grim) + assertEquals("Roses are rainbow, Violets are blue", shiny) + } + + @Test + fun whenComplexReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val reallyBeautiful = regex.replace(beautiful) { + matchResult -> matchResult.value.toUpperCase() + "!" + } + + assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful) + } + + @Test + fun whenSplit_thenList() { + val regex = """\W+""".toRegex() + val beautiful = "Roses are red, Violets are blue" + + assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful)) + assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4)) + assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful)) + } + +} \ No newline at end of file From 65267041c31b198960404408a45d872f9476656c Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Fri, 9 Feb 2018 12:11:19 +0000 Subject: [PATCH 064/102] Examples for Infix Functions article (#3606) --- .../com/baeldung/kotlin/InfixFunctionsTest.kt | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt new file mode 100644 index 0000000000..fc4286460a --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt @@ -0,0 +1,51 @@ +package com.baeldung.kotlin + +import org.junit.Assert +import org.junit.Test + +class InfixFunctionsTest { + @Test + fun testColours() { + val color = 0x123456 + val red = (color and 0xff0000) shr 16 + val green = (color and 0x00ff00) shr 8 + val blue = (color and 0x0000ff) shr 0 + + Assert.assertEquals(0x12, red) + Assert.assertEquals(0x34, green) + Assert.assertEquals(0x56, blue) + } + + @Test + fun testNewAssertions() { + class Assertion(private val target: T) { + infix fun isEqualTo(other: T) { + Assert.assertEquals(other, target) + } + + infix fun isDifferentFrom(other: T) { + Assert.assertNotEquals(other, target) + } + } + + val result = Assertion(5) + + result isEqualTo 5 + + // The following two lines are expected to fail + // result isEqualTo 6 + // result isDifferentFrom 5 + } + + @Test + fun testNewStringMethod() { + infix fun String.substringMatches(r: Regex) : List { + return r.findAll(this) + .map { it.value } + .toList() + } + + val matches = "a bc def" substringMatches ".*? ".toRegex() + Assert.assertEquals(listOf("a ", "bc "), matches) + } +} From d4522c7c16acc1c0f0c0e4eddd4e4e6165857b5c Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Fri, 9 Feb 2018 22:25:13 +0100 Subject: [PATCH 065/102] BAEL-1285: Update Jackson articles (#3623) * BAEL-1216: improve tests * BAEL-1448: Update Spring 5 articles to use the release version * Setting up the Maven Wrapper on a maven project * Add Maven Wrapper on spring-boot module * simple add * BAEL-976: Update spring version * BAEL-1273: Display RSS feed with spring mvc (AbstractRssFeedView) * Move RSS feed with Spring MVC from spring-boot to spring-mvc-simple * BAEL-1285: Update Jackson articles * BAEL-1273: implement both MVC and Rest approach to serve RSS content --- .gitignore | 1 + jackson/pom.xml | 2 +- spring-mvc-java/pom.xml | 2 +- .../ApplicationConfiguration.java | 25 ++++++++-- .../controller/rss/ArticleRssController.java | 49 ++++++++++++++++++- .../rss/ArticleRssFeedViewResolver.java | 15 ++++++ 6 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java diff --git a/.gitignore b/.gitignore index 08f570ad06..693a35c176 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties *.springBeans +20171220-JMeter.csv diff --git a/jackson/pom.xml b/jackson/pom.xml index 001fde5021..2587e61ac1 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -129,7 +129,7 @@ - 2.9.2 + 2.9.4 19.0 diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index b939f0496d..9d90ba2dbf 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -297,7 +297,7 @@ 4.3.4.RELEASE 4.2.0.RELEASE 2.1.5.RELEASE - 2.8.5 + 2.9.4 5.2.5.Final diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index b9a8336bf2..69c45d90b3 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,14 +1,21 @@ package com.baeldung.spring.configuration; +import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.accept.ContentNegotiationManager; + +import java.util.List; +import java.util.ArrayList; @Configuration @EnableWebMvc @@ -21,11 +28,19 @@ class ApplicationConfiguration implements WebMvcConfigurer { } @Bean - public InternalResourceViewResolver jspViewResolver() { - InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setPrefix("/WEB-INF/views/"); - bean.setSuffix(".jsp"); - return bean; + public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) { + ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver(); + cnvResolver.setContentNegotiationManager(cnManager); + List resolvers = new ArrayList<>(); + + InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp"); + ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver(); + + resolvers.add(bean); + resolvers.add(articleRssFeedViewResolver); + + cnvResolver.setViewResolvers(resolvers); + return cnvResolver; } @Bean diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java index 1f51b238ac..8f23076e8e 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -1,14 +1,59 @@ package com.baeldung.spring.controller.rss; +import com.rometools.rome.feed.synd.*; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.SyndFeedOutput; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; @Controller public class ArticleRssController { - @GetMapping(value = "/rss", produces = "application/*") - public String articleFeed() { + @GetMapping(value = "/rssMvc") + public String articleMvcFeed() { return "articleFeedView"; } + @GetMapping(value = "/rssRest", produces = "application/rss+xml") + @ResponseBody + public String articleRestFeed() throws FeedException { + SyndFeed feed = new SyndFeedImpl(); + feed.setFeedType("rss_2.0"); + feed.setLink("http://localhost:8080/spring-mvc-simple/rss"); + feed.setTitle("Article Feed"); + feed.setDescription("Article Feed Description"); + feed.setPublishedDate(new Date()); + + List list = new ArrayList(); + + SyndEntry item1 = new SyndEntryImpl(); + item1.setLink("http://www.baeldung.com/netty-exception-handling"); + item1.setTitle("Exceptions in Netty"); + SyndContent description1 = new SyndContentImpl(); + description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); + item1.setDescription(description1); + item1.setPublishedDate(new Date()); + item1.setAuthor("Carlos"); + + SyndEntry item2 = new SyndEntryImpl(); + item2.setLink("http://www.baeldung.com/cockroachdb-java"); + item2.setTitle("Guide to CockroachDB in Java"); + SyndContent description2 = new SyndContentImpl(); + description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); + item2.setDescription(description2); + item2.setPublishedDate(new Date()); + item2.setAuthor("Baeldung"); + + list.add(item1); + list.add(item2); + feed.setEntries(list); + + return new SyndFeedOutput().outputString(feed); + } + } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java new file mode 100644 index 0000000000..6be06c4812 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.controller.rss; + +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.ViewResolver; + +import java.util.Locale; + +public class ArticleRssFeedViewResolver implements ViewResolver { + + @Override + public View resolveViewName(String s, Locale locale) throws Exception { + ArticleFeedView articleFeedView = new ArticleFeedView(); + return articleFeedView; + } +} From c67eca5faa952fa1aaa33fc92ac4a1915efc36bc Mon Sep 17 00:00:00 2001 From: Adam InTae Gerard Date: Fri, 9 Feb 2018 22:55:34 -0800 Subject: [PATCH 066/102] Issue #3628 - Closed unclosed input stream --- .../controller/MultipartController.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java index 1cc8261f7c..a693bf039f 100644 --- a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java +++ b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java @@ -25,14 +25,20 @@ public class MultipartController { try { InputStream in = file.getInputStream(); String path = new File(".").getAbsolutePath(); - FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ "/uploads/" + file.getOriginalFilename()); - int ch; - while ((ch = in.read()) != -1) { - f.write(ch); + FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + "/uploads/" + file.getOriginalFilename()); + try { + int ch; + while ((ch = in.read()) != -1) { + f.write(ch); + } + modelAndView.getModel().put("message", "File uploaded successfully!"); + } catch (Exception e) { + System.out.println("Exception uploading multipart: " + e); + } finally { + f.flush(); + f.close(); + in.close(); } - f.flush(); - f.close(); - modelAndView.getModel().put("message", "File uploaded successfully!"); } catch (Exception e) { System.out.println("Exception uploading multipart: " + e); } From 50deec5b5cb8c1ae7d732ffd5726933c1e98f956 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 17:42:52 +0200 Subject: [PATCH 067/102] refactor streams ex --- .../java/com/baeldung/string/Palindrome.java | 8 ++++ ...ingPalindrome.java => PalindromeTest.java} | 40 +++++++++---------- 2 files changed, 26 insertions(+), 22 deletions(-) rename core-java/src/test/java/com/baeldung/string/{WhenCheckingPalindrome.java => PalindromeTest.java} (63%) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index 03b65d1a84..d70a89525a 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -1,5 +1,7 @@ package com.baeldung.string; +import java.util.stream.IntStream; + public class Palindrome { public boolean isPalindrome(String text) { @@ -50,4 +52,10 @@ public class Palindrome { return true; } + + public boolean isPalindromeUsingIntStream(String text) { + String temp = text.replaceAll("\\s+", "").toLowerCase(); + return IntStream.range(0, temp.length() / 2) + .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java similarity index 63% rename from core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java rename to core-java/src/test/java/com/baeldung/string/PalindromeTest.java index e3655a3140..ef78a2d840 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -2,10 +2,10 @@ package com.baeldung.string; import java.util.Arrays; -import org.junit.Assert; +import static org.junit.Assert.*; import org.junit.Test; -public class WhenCheckingPalindrome { +public class PalindromeTest { private String[] words = { "Anna", @@ -28,50 +28,50 @@ public class WhenCheckingPalindrome { @Test public void whenWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindrome(word)); + assertTrue(palindrome.isPalindrome(word)); } @Test public void whenSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindrome(sentence)); + assertTrue(palindrome.isPalindrome(sentence)); } @Test public void whenReverseWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeReverseTheString(word)); + assertTrue(palindrome.isPalindromeReverseTheString(word)); } @Test public void whenReverseSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence)); + assertTrue(palindrome.isPalindromeReverseTheString(sentence)); } @Test public void whenStringBuilderWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); + assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); } @Test public void whenStringBuilderSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); + assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); } @Test public void whenStringBufferWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); + assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); } @Test public void whenStringBufferSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); + assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); } @Test @@ -80,7 +80,7 @@ public class WhenCheckingPalindrome { word = word.replaceAll("\\s+", "").toLowerCase(); int backward = word.length()-1; - Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); + assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); } } @@ -90,25 +90,21 @@ public class WhenCheckingPalindrome { sentence = sentence.replaceAll("\\s+", "").toLowerCase(); int backward = sentence.length()-1; - Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); + assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); } } @Test public void whenPalindromeStreams_wordShouldBePalindrome() { - String[] expected = Arrays.stream(words) - .filter(palindrome::isPalindrome) - .toArray(String[]::new); - - Assert.assertArrayEquals(expected, words); + for(String word:words) { + assertTrue(palindrome.isPalindromeUsingIntStream(word)); + } } @Test public void whenPalindromeStreams_sentenceShouldBePalindrome() { - String[] expected = Arrays.stream(sentences) - .filter(palindrome::isPalindrome) - .toArray(String[]::new); - - Assert.assertArrayEquals(expected, sentences); + for(String sentence:sentences) { + assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); + } } } From 4c044fcaeec33a3b68637f751672a8f48de74c83 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 17:45:01 +0200 Subject: [PATCH 068/102] remove imports --- core-java/src/test/java/com/baeldung/string/PalindromeTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java index ef78a2d840..d1a05b6617 100644 --- a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -1,7 +1,5 @@ package com.baeldung.string; -import java.util.Arrays; - import static org.junit.Assert.*; import org.junit.Test; From 7c6a4b0b4dc5250faf6b17cac8a69fece05f58d1 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 18:48:41 +0200 Subject: [PATCH 069/102] Update Palindrome.java --- core-java/src/main/java/com/baeldung/string/Palindrome.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index d70a89525a..0759787249 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -58,4 +58,5 @@ public class Palindrome { return IntStream.range(0, temp.length() / 2) .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); } + } From 3c371bea4587ed754e2e28b42873fead3af75695 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 19:04:31 +0200 Subject: [PATCH 070/102] rename projects (#3604) --- .../{oauth2client => auth-client}/pom.xml | 4 ++-- .../src/main/java/com/baeldung/CloudSite.java | 0 .../com/baeldung/config/SiteSecurityConfigurer.java | 0 .../baeldung/controller/CloudSiteController.java | 0 .../java/com/baeldung/filters/SimpleFilter.java | 0 .../src/main/resources/application.properties | 0 .../src/main/resources/application.yml | 0 .../src/main/resources/templates/personinfo.html | 0 .../springoath2/Springoath2ApplicationTests.java | 0 .../{personresource => auth-resource}/pom.xml | 4 ++-- .../src/main/java/com/baeldung/Application.java | 0 .../com/baeldung/config/ResourceConfigurer.java | 0 .../baeldung/controller/PersonInfoController.java | 0 .../src/main/java/com/baeldung/model/Person.java | 0 .../src/main/resources/application.yml | 0 .../PersonserviceApplicationTests.java | 0 .../{authserver => auth-server}/pom.xml | 2 +- .../src/main/java/com/baeldung/AuthServer.java | 0 .../com/baeldung/config/AuthServerConfigurer.java | 0 .../baeldung/config/ResourceServerConfigurer.java | 0 .../java/com/baeldung/config/WebMvcConfigurer.java | 0 .../com/baeldung/config/WebSecurityConfigurer.java | 0 .../com/baeldung/controller/ResourceController.java | 0 .../src/main/resources/application.yml | 0 .../src/main/resources/certificate/mykeystore.jks | Bin .../src/main/resources/templates/login.html | 0 26 files changed, 5 insertions(+), 5 deletions(-) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/pom.xml (97%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/java/com/baeldung/CloudSite.java (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/java/com/baeldung/controller/CloudSiteController.java (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/java/com/baeldung/filters/SimpleFilter.java (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/resources/application.properties (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/resources/application.yml (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/main/resources/templates/personinfo.html (100%) rename spring-cloud/spring-cloud-security/{oauth2client => auth-client}/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/pom.xml (96%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/main/java/com/baeldung/Application.java (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/main/java/com/baeldung/config/ResourceConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/main/java/com/baeldung/controller/PersonInfoController.java (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/main/java/com/baeldung/model/Person.java (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/main/resources/application.yml (100%) rename spring-cloud/spring-cloud-security/{personresource => auth-resource}/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/pom.xml (96%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/AuthServer.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/config/AuthServerConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/config/ResourceServerConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/config/WebMvcConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/config/WebSecurityConfigurer.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/java/com/baeldung/controller/ResourceController.java (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/resources/application.yml (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/resources/certificate/mykeystore.jks (100%) rename spring-cloud/spring-cloud-security/{authserver => auth-server}/src/main/resources/templates/login.html (100%) diff --git a/spring-cloud/spring-cloud-security/oauth2client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml similarity index 97% rename from spring-cloud/spring-cloud-security/oauth2client/pom.xml rename to spring-cloud/spring-cloud-security/auth-client/pom.xml index fd1c6964e1..5213b93f9a 100644 --- a/spring-cloud/spring-cloud-security/oauth2client/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -3,11 +3,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung - oauth2client + auth-client 0.0.1-SNAPSHOT jar - oauth2client + auth-client Demo project for Spring Boot diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/CloudSite.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/CloudSite.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/controller/CloudSiteController.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/controller/CloudSiteController.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/filters/SimpleFilter.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/filters/SimpleFilter.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.properties b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.properties rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/templates/personinfo.html b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/templates/personinfo.html rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java diff --git a/spring-cloud/spring-cloud-security/personresource/pom.xml b/spring-cloud/spring-cloud-security/auth-resource/pom.xml similarity index 96% rename from spring-cloud/spring-cloud-security/personresource/pom.xml rename to spring-cloud/spring-cloud-security/auth-resource/pom.xml index ca1ff82515..2c54d24e7d 100644 --- a/spring-cloud/spring-cloud-security/personresource/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -4,11 +4,11 @@ 4.0.0 com.baeldung - personresource + auth-resource 0.0.1-SNAPSHOT jar - personresource + auth-resource Demo project for Spring Boot diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/Application.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/Application.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/config/ResourceConfigurer.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/config/ResourceConfigurer.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/controller/PersonInfoController.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/controller/PersonInfoController.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/model/Person.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/model/Person.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/personresource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java diff --git a/spring-cloud/spring-cloud-security/authserver/pom.xml b/spring-cloud/spring-cloud-security/auth-server/pom.xml similarity index 96% rename from spring-cloud/spring-cloud-security/authserver/pom.xml rename to spring-cloud/spring-cloud-security/auth-server/pom.xml index ed88ac046b..ab30f3f2ec 100644 --- a/spring-cloud/spring-cloud-security/authserver/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.baeldung - authserver + auth-server 0.0.1-SNAPSHOT diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/AuthServer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/AuthServer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/AuthServerConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/AuthServerConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/ResourceServerConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/ResourceServerConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/ResourceServerConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/ResourceServerConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebMvcConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebMvcConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebSecurityConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/controller/ResourceController.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/controller/ResourceController.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/controller/ResourceController.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/controller/ResourceController.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/certificate/mykeystore.jks b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/certificate/mykeystore.jks rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/templates/login.html b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/templates/login.html rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html From ef8276dd1a62e528a35696f6e334285ed0c3c5e8 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 19:11:06 +0200 Subject: [PATCH 071/102] remove project from build --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index ca6d4afe82..fc0c8f8ba7 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,9 @@ core-java core-java-io core-java-8 + couchbase deltaspike From d488e693017a7eba978c5cf9ce4cf81e12122f39 Mon Sep 17 00:00:00 2001 From: linhvovn Date: Sun, 11 Feb 2018 01:28:00 +0800 Subject: [PATCH 072/102] [BAEL 1382] Update Unit Test (#3630) * [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger * [tlinh2110-BAEL-1382] Update Unit Test --- .../si/TestSpringIntegrationSecurity.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java index 45dcb1e836..9ae82af2dc 100644 --- a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java @@ -1,8 +1,11 @@ package com.baeldung.si; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import org.hamcrest.core.IsInstanceOf; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.SubscribableChannel; @@ -21,6 +24,9 @@ import com.baeldung.si.security.SecurityConfig; @ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class }) public class TestSpringIntegrationSecurity { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Autowired SubscribableChannel startDirectChannel; @@ -34,34 +40,28 @@ public class TestSpringIntegrationSecurity { startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(username = "jane", roles = { "LOGGER" }) - public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(username = "jane") - public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(roles = { "VIEWER" }) - public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } @Test @@ -78,4 +78,4 @@ public class TestSpringIntegrationSecurity { assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); } -} +} \ No newline at end of file From d935985ec1b541558864f146c4dd530571f09df4 Mon Sep 17 00:00:00 2001 From: Ganesh Date: Sun, 11 Feb 2018 13:29:30 +0530 Subject: [PATCH 073/102] Priorityjobscheduler (#3583) * priority based job execution in java * minor fixes * updated to use java 8 features * fix need for type inference * handle exception * indentation * handling exception generated during terminal of normal flow * handling exception generated during terminal of normal flow * added comment --- .../concurrent/prioritytaskexecution/Job.java | 3 ++- .../prioritytaskexecution/PriorityJobScheduler.java | 12 ++++++------ .../PriorityJobSchedulerUnitTest.java | 4 +++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java index a70041ed7d..9900d1c63d 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -16,7 +16,8 @@ public class Job implements Runnable { @Override public void run() { try { - System.out.println("Job:" + jobName + " Priority:" + jobPriority); + System.out.println("Job:" + jobName + + " Priority:" + jobPriority); Thread.sleep(1000); } catch (InterruptedException ignored) { } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java index 70fd1710c0..ba55696d5a 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java @@ -9,21 +9,21 @@ import java.util.concurrent.TimeUnit; public class PriorityJobScheduler { private ExecutorService priorityJobPoolExecutor; - private ExecutorService priorityJobScheduler; - private PriorityBlockingQueue priorityQueue; + private ExecutorService priorityJobScheduler = + Executors.newSingleThreadExecutor(); + private PriorityBlockingQueue priorityQueue; public PriorityJobScheduler(Integer poolSize, Integer queueSize) { priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize); - Comparator jobComparator = Comparator.comparing(Job::getJobPriority); - priorityQueue = new PriorityBlockingQueue(queueSize, - (Comparator) jobComparator); + priorityQueue = new PriorityBlockingQueue(queueSize, + Comparator.comparing(Job::getJobPriority)); - priorityJobScheduler = Executors.newSingleThreadExecutor(); priorityJobScheduler.execute(()->{ while (true) { try { priorityJobPoolExecutor.execute(priorityQueue.take()); } catch (InterruptedException e) { + // exception needs special handling break; } } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java index 902bada3a2..1e67fe45c1 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -30,7 +30,9 @@ public class PriorityJobSchedulerUnitTest { // delay to avoid job sleep (added for demo) being interrupted try { Thread.sleep(2000); - } catch (InterruptedException ignored) { + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); } pjs.closeScheduler(); From 1c043dc20287524d91562e81aab5a64bcbb3d2f5 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 11 Feb 2018 12:34:49 +0200 Subject: [PATCH 074/102] custom dsl ex (#3577) * custom dsl ex * Update application.properties --- .../baeldung/dsl/ClientErrorLoggingDsl.java | 32 +++++++++++ .../dsl/ClientErrorLoggingFilter.java | 54 +++++++++++++++++++ .../baeldung/dsl/CustomDslApplication.java | 13 +++++ .../java/com/baeldung/dsl/MyController.java | 14 +++++ .../java/com/baeldung/dsl/SecurityConfig.java | 50 +++++++++++++++++ .../src/main/resources/application.properties | 4 +- 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java create mode 100644 spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java create mode 100644 spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java create mode 100644 spring-5-security/src/main/java/com/baeldung/dsl/MyController.java create mode 100644 spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java new file mode 100644 index 0000000000..6c7c0d2717 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java @@ -0,0 +1,32 @@ +package com.baeldung.dsl; + +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; + +public class ClientErrorLoggingDsl extends AbstractHttpConfigurer { + + private List errorCodes; + + public ClientErrorLoggingDsl(List errorCodes) { + this.errorCodes = errorCodes; + } + + public ClientErrorLoggingDsl() { + + } + + @Override + public void init(HttpSecurity http) throws Exception { + // initialization code + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http.addFilterAfter(new ClientErrorLoggingFilter(errorCodes), FilterSecurityInterceptor.class); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java new file mode 100644 index 0000000000..56f7cea3b8 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java @@ -0,0 +1,54 @@ +package com.baeldung.dsl; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.GenericFilterBean; + +public class ClientErrorLoggingFilter extends GenericFilterBean { + + private static final Logger logger = LogManager.getLogger(ClientErrorLoggingFilter.class); + + private List errorCodes; + + public ClientErrorLoggingFilter(List errorCodes) { + this.errorCodes = errorCodes; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + + if (auth != null) { + int status = ((HttpServletResponse) response).getStatus(); + if (status >= 400 && status < 500) { + if (errorCodes == null) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } else { + if (errorCodes.stream() + .filter(s -> s.value() == status) + .findFirst() + .isPresent()) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } + } + } + } + + chain.doFilter(request, response); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java b/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java new file mode 100644 index 0000000000..3e58bccaf4 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.dsl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CustomDslApplication { + + public static void main(String[] args) { + SpringApplication.run(CustomDslApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java new file mode 100644 index 0000000000..c69046afb5 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java @@ -0,0 +1,14 @@ +package com.baeldung.dsl; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MyController { + + @GetMapping("/admin") + public String getAdminPage() { + return "Hello Admin"; + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java new file mode 100644 index 0000000000..4494aaa131 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java @@ -0,0 +1,50 @@ +package com.baeldung.dsl; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/admin*") + .hasAnyRole("ADMIN") + .anyRequest() + .authenticated() + .and() + .formLogin() + .and() + .apply(clientErrorLogging()); + } + + @Bean + public ClientErrorLoggingDsl clientErrorLogging() { + return new ClientErrorLoggingDsl(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .passwordEncoder(passwordEncoder()) + .withUser("user1") + .password(passwordEncoder().encode("user")) + .roles("USER") + .and() + .withUser("admin") + .password(passwordEncoder().encode("admin")) + .roles("ADMIN"); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + +} diff --git a/spring-5-security/src/main/resources/application.properties b/spring-5-security/src/main/resources/application.properties index ccec014c2b..781ee76826 100644 --- a/spring-5-security/src/main/resources/application.properties +++ b/spring-5-security/src/main/resources/application.properties @@ -1,3 +1,5 @@ server.port=8081 -logging.level.root=INFO \ No newline at end of file +logging.level.root=INFO + +logging.level.com.baeldung.dsl.ClientErrorLoggingFilter=DEBUG From 6eeff13c459a3661c9151086ce2cf70e0faf31f1 Mon Sep 17 00:00:00 2001 From: Allan Vital Date: Sun, 11 Feb 2018 11:36:33 -0200 Subject: [PATCH 075/102] BAEL-1368: infinispan article (#3633) --- libraries/pom.xml | 8 +- .../infinispan/CacheConfiguration.java | 85 ++++++++++++++ .../infinispan/listener/CacheListener.java | 55 +++++++++ .../repository/HelloWorldRepository.java | 15 +++ .../infinispan/service/HelloWorldService.java | 90 +++++++++++++++ .../service/TransactionalService.java | 57 ++++++++++ .../infinispan/ConfigurationTest.java | 56 ++++++++++ .../service/HelloWorldServiceUnitTest.java | 105 ++++++++++++++++++ .../service/TransactionalServiceUnitTest.java | 24 ++++ 9 files changed, 494 insertions(+), 1 deletion(-) create mode 100644 libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java create mode 100644 libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java create mode 100644 libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java create mode 100644 libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java create mode 100644 libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java create mode 100644 libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java create mode 100644 libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java create mode 100644 libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index fa1839010c..64cc9c6a84 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -649,6 +649,11 @@ google-http-client-gson ${googleclient.version} + + org.infinispan + infinispan-core + ${infinispan.version} + @@ -811,5 +816,6 @@ 3.0.14 8.5.24 2.2.0 + 9.1.5.Final - \ No newline at end of file + diff --git a/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java new file mode 100644 index 0000000000..bf214458f3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java @@ -0,0 +1,85 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import org.infinispan.Cache; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.eviction.EvictionType; +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.transaction.LockingMode; +import org.infinispan.transaction.TransactionMode; + +import java.util.concurrent.TimeUnit; + +public class CacheConfiguration { + + public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache"; + public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache"; + public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache"; + public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache"; + public static final String TRANSACTIONAL_CACHE = "transactional-cache"; + + public DefaultCacheManager cacheManager() { + DefaultCacheManager cacheManager = new DefaultCacheManager(); + return cacheManager; + } + + public Cache transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration()); + } + + public Cache simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build()); + } + + public Cache expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration()); + } + + public Cache evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration()); + } + + public Cache passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration()); + } + + private Cache buildCache(String cacheName, DefaultCacheManager cacheManager, + CacheListener listener, Configuration configuration) { + + cacheManager.defineConfiguration(cacheName, configuration); + Cache cache = cacheManager.getCache(cacheName); + cache.addListener(listener); + return cache; + } + + private Configuration expiringConfiguration() { + return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS) + .build(); + } + + private Configuration evictingConfiguration() { + return new ConfigurationBuilder() + .memory().evictionType(EvictionType.COUNT).size(1) + .build(); + } + + private Configuration passivatingConfiguration() { + return new ConfigurationBuilder() + .memory().evictionType(EvictionType.COUNT).size(1) + .persistence() + .passivation(true) + .addSingleFileStore() + .purgeOnStartup(true) + .location(System.getProperty("java.io.tmpdir")) + .build(); + } + + private Configuration transactionalConfiguration() { + return new ConfigurationBuilder() + .transaction().transactionMode(TransactionMode.TRANSACTIONAL) + .lockingMode(LockingMode.PESSIMISTIC) + .build(); + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java new file mode 100644 index 0000000000..2f6536ad87 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java @@ -0,0 +1,55 @@ +package com.baeldung.infinispan.listener; + +import org.infinispan.notifications.Listener; +import org.infinispan.notifications.cachelistener.annotation.*; +import org.infinispan.notifications.cachelistener.event.*; + +@Listener +public class CacheListener { + + @CacheEntryCreated + public void entryCreated(CacheEntryCreatedEvent event) { + this.printLog("Adding key '" + event.getKey() + "' to cache", event); + } + + @CacheEntryExpired + public void entryExpired(CacheEntryExpiredEvent event) { + this.printLog("Expiring key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryVisited + public void entryVisited(CacheEntryVisitedEvent event) { + this.printLog("Key '" + event.getKey() + "' was visited", event); + } + + @CacheEntryActivated + public void entryActivated(CacheEntryActivatedEvent event) { + this.printLog("Activating key '" + event.getKey() + "' on cache", event); + } + + @CacheEntryPassivated + public void entryPassivated(CacheEntryPassivatedEvent event) { + this.printLog("Passivating key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryLoaded + public void entryLoaded(CacheEntryLoadedEvent event) { + this.printLog("Loading key '" + event.getKey() + "' to cache", event); + } + + @CacheEntriesEvicted + public void entriesEvicted(CacheEntriesEvictedEvent event) { + final StringBuilder builder = new StringBuilder(); + event.getEntries().entrySet().forEach((e) -> + builder.append(e.getKey() + ", ") + ); + System.out.println("Evicting following entries from cache: " + builder.toString()); + } + + private void printLog(String log, CacheEntryEvent event) { + if (!event.isPre()) { + System.out.println(log); + } + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java new file mode 100644 index 0000000000..85c0d539a3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.infinispan.repository; + +public class HelloWorldRepository { + + public String getHelloWorld() { + try { + System.out.println("Executing some heavy query"); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "Hello World!"; + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java new file mode 100644 index 0000000000..0d1ffb4168 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java @@ -0,0 +1,90 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import org.infinispan.Cache; + +import java.util.concurrent.TimeUnit; + +public class HelloWorldService { + + private final HelloWorldRepository repository; + + private final Cache simpleHelloWorldCache; + private final Cache expiringHelloWorldCache; + private final Cache evictingHelloWorldCache; + private final Cache passivatingHelloWorldCache; + + public HelloWorldService(HelloWorldRepository repository, CacheListener listener, + Cache simpleHelloWorldCache, + Cache expiringHelloWorldCache, + Cache evictingHelloWorldCache, + Cache passivatingHelloWorldCache) { + + this.repository = repository; + + this.simpleHelloWorldCache = simpleHelloWorldCache; + this.expiringHelloWorldCache = expiringHelloWorldCache; + this.evictingHelloWorldCache = evictingHelloWorldCache; + this.passivatingHelloWorldCache = passivatingHelloWorldCache; + } + + public String findSimpleHelloWorld() { + String cacheKey = "simple-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findExpiringHelloWorld() { + String cacheKey = "expiring-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findIdleHelloWorld() { + String cacheKey = "idle-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findSimpleHelloWorldInExpiringCache() { + String cacheKey = "simple-hello"; + String helloWorld = expiringHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + expiringHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findEvictingHelloWorld(String key) { + String value = evictingHelloWorldCache.get(key); + if(value == null) { + value = repository.getHelloWorld(); + evictingHelloWorldCache.put(key, value); + } + return value; + } + + public String findPassivatingHelloWorld(String key) { + String value = passivatingHelloWorldCache.get(key); + if(value == null) { + value = repository.getHelloWorld(); + passivatingHelloWorldCache.put(key, value); + } + return value; + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java new file mode 100644 index 0000000000..b0dbf5475f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java @@ -0,0 +1,57 @@ +package com.baeldung.infinispan.service; + +import org.infinispan.Cache; +import org.springframework.util.StopWatch; + +import javax.transaction.TransactionManager; + +public class TransactionalService { + + private final Cache transactionalCache; + + private static final String KEY = "key"; + + public TransactionalService(Cache transactionalCache) { + this.transactionalCache = transactionalCache; + + transactionalCache.put(KEY, 0); + } + + public Integer getQuickHowManyVisits() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + Integer howManyVisits = transactionalCache.get(KEY); + howManyVisits++; + System.out.println("Ill try to set HowManyVisits to " + howManyVisits); + StopWatch watch = new StopWatch(); + watch.start(); + transactionalCache.put(KEY, howManyVisits); + watch.stop(); + System.out.println("I was able to set HowManyVisits to " + howManyVisits + + " after waiting " + watch.getTotalTimeSeconds() + " seconds"); + + tm.commit(); + return howManyVisits; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + public void startBackgroundBatch() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + transactionalCache.put(KEY, 1000); + System.out.println("HowManyVisits should now be 1000, " + + "but we are holding the transaction"); + Thread.sleep(1000L); + tm.rollback(); + System.out.println("The slow batch suffered a rollback"); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java new file mode 100644 index 0000000000..b05314491b --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import com.baeldung.infinispan.service.HelloWorldService; +import com.baeldung.infinispan.service.TransactionalService; +import org.infinispan.Cache; +import org.infinispan.manager.DefaultCacheManager; +import org.junit.After; +import org.junit.Before; + +public class ConfigurationTest { + + private DefaultCacheManager cacheManager; + + private HelloWorldRepository repository = new HelloWorldRepository(); + + protected HelloWorldService helloWorldService; + protected TransactionalService transactionalService; + + @Before + public void setup() { + CacheConfiguration configuration = new CacheConfiguration(); + CacheListener listener = new CacheListener(); + + cacheManager = configuration.cacheManager(); + + Cache transactionalCache = + configuration.transactionalCache(cacheManager, listener); + + Cache simpleHelloWorldCache = + configuration.simpleHelloWorldCache(cacheManager, listener); + + Cache expiringHelloWorldCache = + configuration.expiringHelloWorldCache(cacheManager, listener); + + Cache evictingHelloWorldCache = + configuration.evictingHelloWorldCache(cacheManager, listener); + + Cache passivatingHelloWorldCache = + configuration.passivatingHelloWorldCache(cacheManager, listener); + + this.helloWorldService = new HelloWorldService(repository, + listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache, + passivatingHelloWorldCache); + + this.transactionalService = new TransactionalService(transactionalCache); + + } + + @After + public void tearDown() { + cacheManager.stop(); + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java new file mode 100644 index 0000000000..bda2e6886b --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java @@ -0,0 +1,105 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class HelloWorldServiceUnitTest extends ConfigurationTest { + + @Test + public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { + long milis = System.currentTimeMillis(); + helloWorldService.findSimpleHelloWorld(); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findSimpleHelloWorld(); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { + long milis = System.currentTimeMillis(); + helloWorldService.findExpiringHelloWorld(); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findExpiringHelloWorld(); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() + throws InterruptedException { + + long milis = System.currentTimeMillis(); + helloWorldService.findSimpleHelloWorldInExpiringCache(); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + Thread.sleep(1100); + + milis = System.currentTimeMillis(); + helloWorldService.findSimpleHelloWorldInExpiringCache(); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() + throws InterruptedException { + + long milis = System.currentTimeMillis(); + helloWorldService.findEvictingHelloWorld("key 1"); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findEvictingHelloWorld("key 2"); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findEvictingHelloWorld("key 1"); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() + throws InterruptedException { + + long milis = System.currentTimeMillis(); + helloWorldService.findPassivatingHelloWorld("key 1"); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findPassivatingHelloWorld("key 2"); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThanOrEqualTo(1000); + + milis = System.currentTimeMillis(); + helloWorldService.findPassivatingHelloWorld("key 1"); + executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isLessThan(100); + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java new file mode 100644 index 0000000000..f529222079 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class TransactionalServiceUnitTest extends ConfigurationTest { + + @Test + public void whenLockingAnEntry_thenItShouldBeInaccessible() throws InterruptedException { + Runnable backGroundJob = () -> transactionalService.startBackgroundBatch(); + Thread backgroundThread = new Thread(backGroundJob); + transactionalService.getQuickHowManyVisits(); + backgroundThread.start(); + Thread.sleep(100); //lets wait our thread warm up + long milis = System.currentTimeMillis(); + transactionalService.getQuickHowManyVisits(); + long executionTime = System.currentTimeMillis() - milis; + + assertThat(executionTime).isGreaterThan(500).isLessThan(1000); + } + +} From 5a3e15a6e9599d955b8cb12afd2c9e15c5b12c43 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 11 Feb 2018 17:10:07 +0200 Subject: [PATCH 076/102] update http client incubator package --- .../java9/httpclient/SimpleHttpRequestsUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java index 4a704139a1..aea9945e2b 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java @@ -8,10 +8,10 @@ import java.net.CookieManager; import java.net.CookiePolicy; import java.net.URI; import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpHeaders; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpHeaders; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Map; From 0b5a301c7c356a89fe36bd4cdd5c821f22029e51 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 11 Feb 2018 18:29:27 +0200 Subject: [PATCH 077/102] remove old http client api --- .../SimpleHttpRequestsUnitTest.java | 126 ------------------ 1 file changed, 126 deletions(-) delete mode 100644 core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java deleted file mode 100644 index aea9945e2b..0000000000 --- a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.baeldung.java9.httpclient; - -import static java.net.HttpURLConnection.HTTP_OK; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.CookieManager; -import java.net.CookiePolicy; -import java.net.URI; -import java.net.URISyntaxException; -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpHeaders; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import java.security.NoSuchAlgorithmException; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLParameters; - -import org.junit.Before; -import org.junit.Test; - -public class SimpleHttpRequestsUnitTest { - - private URI httpURI; - - @Before - public void init() throws URISyntaxException { - httpURI = new URI("http://www.baeldung.com/"); - } - - @Test - public void quickGet() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - HttpResponse response = request.response(); - int responseStatusCode = response.statusCode(); - String responseBody = response.body(HttpResponse.asString()); - assertTrue("Get response status code is bigger then 400", responseStatusCode < 400); - } - - @Test - public void asynchronousGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - long before = System.currentTimeMillis(); - CompletableFuture futureResponse = request.responseAsync(); - futureResponse.thenAccept(response -> { - String responseBody = response.body(HttpResponse.asString()); - }); - HttpResponse resp = futureResponse.get(); - HttpHeaders hs = resp.headers(); - assertTrue("There should be more then 1 header.", hs.map().size() > 1); - } - - @Test - public void postMehtod() throws URISyntaxException, IOException, InterruptedException { - HttpRequest.Builder requestBuilder = HttpRequest.create(httpURI); - requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE); - HttpRequest request = requestBuilder.POST(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - @Test - public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException { - CookieManager cManager = new CookieManager(); - cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); - - SSLParameters sslParam = new SSLParameters(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" }); - - HttpClient.Builder hcBuilder = HttpClient.create(); - hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam); - HttpClient httpClient = hcBuilder.build(); - HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://www.facebook.com")); - - HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException { - SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters(); - String[] proto = sslP1.getApplicationProtocols(); - String[] cifers = sslP1.getCipherSuites(); - System.out.println(printStringArr(proto)); - System.out.println(printStringArr(cifers)); - return sslP1; - } - - String printStringArr(String... args) { - if (args == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - for (String s : args) { - sb.append(s); - sb.append("\n"); - } - return sb.toString(); - } - - String printHeaders(HttpHeaders h) { - if (h == null) { - return null; - } - - StringBuilder sb = new StringBuilder(); - Map> hMap = h.map(); - for (String k : hMap.keySet()) { - sb.append(k).append(":"); - List l = hMap.get(k); - if (l != null) { - l.forEach(s -> { - sb.append(s).append(","); - }); - } - sb.append("\n"); - } - return sb.toString(); - } -} From 9cc582e66a26df4359f0d527d740c9392f54a29b Mon Sep 17 00:00:00 2001 From: Orry Date: Sun, 11 Feb 2018 18:40:56 +0200 Subject: [PATCH 078/102] BAEL-1517: Editor Review Changes (#3635) * Add XML, JavaConfig and Autowired examples. * BAEL-1517: Added Java7 style assertions * BAEL-1517: Upgrade AssertJ to 3.9.0; add Java 8 style assertion tests * Revert "Add XML, JavaConfig and Autowired examples." This reverts commit 8f4df6b903866dac1725832d06ee7382fc89d0ce. * BAEL-1517: Editor Review changes * BAEL-1517: Formatting... --- .../exceptions/Java8StyleAssertions.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java index 53f192bb2f..973b921654 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java @@ -5,19 +5,43 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.catchThrowable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import org.junit.Test; public class Java8StyleAssertions { @Test - public void whenDividingByZero_thenArithmeticException() { + public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() { assertThatThrownBy(() -> { - int numerator = 10; - int denominator = 0; - int quotient = numerator / denominator; - }).isInstanceOf(ArithmeticException.class) - .hasMessageContaining("/ by zero"); + ArrayList myStringList = new ArrayList(Arrays.asList("Strine one", "String two")); + myStringList.get(2); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageStartingWith("Index: 2") + .hasMessageContaining("2") + .hasMessageEndingWith("Size: 2") + .hasMessageContaining("Index: 2, Size: 2") + .hasMessage("Index: %s, Size: %s", 2, 2) + .hasMessageMatching("Index: \\d+, Size: \\d+") + .hasNoCause(); + } + @Test + public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() { + assertThatThrownBy(() -> { + try { + throw new IOException(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).isInstanceOf(RuntimeException.class) + .hasCauseInstanceOf(IOException.class) + .hasStackTraceContaining("IOException"); + } + + @Test + public void whenDividingByZero_thenArithmeticException() { assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> { int numerator = 10; int denominator = 0; @@ -25,7 +49,7 @@ public class Java8StyleAssertions { }) .withMessageContaining("/ by zero"); - // BDD style: + // Alternatively: // when Throwable thrown = catchThrowable(() -> { From 4d64a122710d4c6de532c7afc4d4f3e849862684 Mon Sep 17 00:00:00 2001 From: Shouvik Bhattacharya <33756821+shouvikbhattacharya@users.noreply.github.com> Date: Mon, 12 Feb 2018 10:28:07 +0530 Subject: [PATCH 079/102] BAEL-1525: Review comments incorporated. (#3639) --- .../java/com/baeldung/string/Palindrome.java | 46 ++++++++------- .../com/baeldung/string/PalindromeTest.java | 59 ++++++++----------- 2 files changed, 49 insertions(+), 56 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index 0759787249..97d4d36d07 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -5,58 +5,62 @@ import java.util.stream.IntStream; public class Palindrome { public boolean isPalindrome(String text) { - text = text.replaceAll("\\s+", "") - .toLowerCase(); - int length = text.length(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + int length = clean.length(); int forward = 0; int backward = length - 1; - boolean palindrome = true; while (backward > forward) { - char forwardChar = text.charAt(forward++); - char backwardChar = text.charAt(backward--); + char forwardChar = clean.charAt(forward++); + char backwardChar = clean.charAt(backward--); if (forwardChar != backwardChar) return false; } - return palindrome; + return true; } public boolean isPalindromeReverseTheString(String text) { - String reverse = ""; - text = text.replaceAll("\\s+", "").toLowerCase(); - char[] plain = text.toCharArray(); + StringBuilder reverse = new StringBuilder(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + char[] plain = clean.toCharArray(); for (int i = plain.length - 1; i >= 0; i--) - reverse += plain[i]; - return reverse.equals(text); + reverse.append(plain[i]); + return (reverse.toString()).equals(clean); } public boolean isPalindromeUsingStringBuilder(String text) { - StringBuilder plain = new StringBuilder(text); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuilder plain = new StringBuilder(clean); StringBuilder reverse = plain.reverse(); - return reverse.equals(plain); + return (reverse.toString()).equals(clean); } public boolean isPalindromeUsingStringBuffer(String text) { - StringBuffer plain = new StringBuffer(text); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuffer plain = new StringBuffer(clean); StringBuffer reverse = plain.reverse(); - return reverse.equals(plain); + return (reverse.toString()).equals(clean); } - public boolean isPalindromeRecursive(String text, int forward, int backward) { + public boolean isPalindromeRecursive(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + return recursivePalindrome(clean, 0, clean.length() - 1); + } + + private boolean recursivePalindrome(String text, int forward, int backward) { if (forward == backward) return true; if ((text.charAt(forward)) != (text.charAt(backward))) return false; if (forward < backward + 1) { - return isPalindromeRecursive(text, forward + 1, backward - 1); + return recursivePalindrome(text, forward + 1, backward - 1); } return true; } - + public boolean isPalindromeUsingIntStream(String text) { - String temp = text.replaceAll("\\s+", "").toLowerCase(); + String temp = text.replaceAll("\\s+", "").toLowerCase(); return IntStream.range(0, temp.length() / 2) .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); } - } diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java index d1a05b6617..bc6fee2cd9 100644 --- a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -25,84 +25,73 @@ public class PalindromeTest { @Test public void whenWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindrome(word)); } @Test public void whenSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindrome(sentence)); } - + @Test public void whenReverseWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeReverseTheString(word)); } - + @Test public void whenReverseSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindromeReverseTheString(sentence)); } - + @Test public void whenStringBuilderWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); } - + @Test public void whenStringBuilderSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); } - + @Test public void whenStringBufferWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); } - + @Test public void whenStringBufferSentence_shouldBePalindrome() { - for(String sentence:sentences) - + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); } - + @Test public void whenPalindromeRecursive_wordShouldBePalindrome() { - for(String word:words) { - word = word.replaceAll("\\s+", "").toLowerCase(); - int backward = word.length()-1; - - assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); - } + for (String word : words) + assertTrue(palindrome.isPalindromeRecursive(word)); } - + @Test public void whenPalindromeRecursive_sentenceShouldBePalindrome() { - for(String sentence:sentences) { - sentence = sentence.replaceAll("\\s+", "").toLowerCase(); - int backward = sentence.length()-1; - - assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); - } + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeRecursive(sentence)); } - + @Test public void whenPalindromeStreams_wordShouldBePalindrome() { - for(String word:words) { + for (String word : words) assertTrue(palindrome.isPalindromeUsingIntStream(word)); - } } - + @Test public void whenPalindromeStreams_sentenceShouldBePalindrome() { - for(String sentence:sentences) { + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); - } } } From 97f56f33f69c95d25d1d57d64087e62a7a6afe71 Mon Sep 17 00:00:00 2001 From: Rakesh Kumar Date: Mon, 12 Feb 2018 11:20:01 -0500 Subject: [PATCH 080/102] BAEL-1149 - Spring ResponseStatusException (#3648) --- .../baeldung/execption/ActorController.java | 41 +++++++++++++++++++ .../execption/ActorNotFoundException.java | 13 ++++++ .../com/baeldung/execption/ActorService.java | 35 ++++++++++++++++ .../execption/SpringExceptionApplication.java | 14 +++++++ 4 files changed, 103 insertions(+) create mode 100644 spring-5/src/main/java/com/baeldung/execption/ActorController.java create mode 100644 spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java create mode 100644 spring-5/src/main/java/com/baeldung/execption/ActorService.java create mode 100644 spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorController.java b/spring-5/src/main/java/com/baeldung/execption/ActorController.java new file mode 100644 index 0000000000..6c9c46253a --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorController.java @@ -0,0 +1,41 @@ +package com.baeldung.execption; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +@RestController +public class ActorController { + + @Autowired + ActorService actorService; + + @GetMapping("/actor/{id}") + public String getActorName(@PathVariable("id") int id) { + try { + return actorService.getActor(id); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex); + } + } + + @DeleteMapping("/actor/{id}") + public String getActor(@PathVariable("id") int id) { + return actorService.removeActor(id); + } + + @PutMapping("/actor/{id}/{name}") + public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) { + try { + return actorService.updateActor(id, name); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex); + } + } + +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java new file mode 100644 index 0000000000..642c075b5d --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java @@ -0,0 +1,13 @@ +package com.baeldung.execption; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found") +public class ActorNotFoundException extends Exception { + private static final long serialVersionUID = 1L; + + public ActorNotFoundException(String errorMessage) { + super(errorMessage); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorService.java b/spring-5/src/main/java/com/baeldung/execption/ActorService.java new file mode 100644 index 0000000000..956fa92015 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorService.java @@ -0,0 +1,35 @@ +package com.baeldung.execption; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +@Service +public class ActorService { + List actors = Arrays.asList("Jack Nicholson", "Marlon Brando", "Robert De Niro", "Al Pacino", "Tom Hanks"); + + public String getActor(int index) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + return actors.get(index); + } + + public String updateActor(int index, String actorName) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + actors.set(index, actorName); + return actorName; + } + + public String removeActor(int index) { + if (index >= actors.size()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Actor Not Found in Repsoitory"); + } + return actors.remove(index); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java new file mode 100644 index 0000000000..287356256c --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.execption; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@ComponentScan(basePackages = { "com.baeldung.execption" }) +public class SpringExceptionApplication { + public static void main(String[] args) { + SpringApplication.run(SpringExceptionApplication.class, args); + } +} \ No newline at end of file From a18ce8a3803dd4d5a384a5018f67c8e909acf59f Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Mon, 12 Feb 2018 23:09:31 +0530 Subject: [PATCH 081/102] Bael 1427 Life cycle of thread (#3637) * Interrupted thread before logging * abbrevated word thread and interrupted main thread --- .../threadlifecycle/BlockedState.java | 31 ++++++++++++++++ .../concurrent/threadlifecycle/NewState.java | 14 ++++++++ .../threadlifecycle/RunnableState.java | 15 ++++++++ .../threadlifecycle/TerminatedState.java | 15 ++++++++ .../threadlifecycle/TimedWaitingState.java | 25 +++++++++++++ .../threadlifecycle/WaitingState.java | 35 +++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java new file mode 100644 index 0000000000..19c6d08c2d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java @@ -0,0 +1,31 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class BlockedState { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new DemoThreadB()); + Thread t2 = new Thread(new DemoThreadB()); + + t1.start(); + t2.start(); + + Thread.sleep(1000); + + System.out.println(t2.getState()); + System.exit(0); + } +} + +class DemoThreadB implements Runnable { + @Override + public void run() { + commonResource(); + } + + public static synchronized void commonResource() { + while(true) { + // Infinite loop to mimic heavy processing + // Thread 't1' won't leave this method + // when Thread 't2' enters this + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java new file mode 100644 index 0000000000..b524cc5ec7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java @@ -0,0 +1,14 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class NewState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java new file mode 100644 index 0000000000..4aa8b36a76 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class RunnableState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + t.start(); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java new file mode 100644 index 0000000000..7c8884dc22 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TerminatedState implements Runnable { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new TerminatedState()); + t1.start(); + Thread.sleep(1000); + System.out.println(t1.getState()); + } + + @Override + public void run() { + // No processing in this block + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java new file mode 100644 index 0000000000..8d005352eb --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TimedWaitingState { + public static void main(String[] args) throws InterruptedException { + DemoThread obj1 = new DemoThread(); + Thread t1 = new Thread(obj1); + t1.start(); + // The following sleep will give enough time for ThreadScheduler + // to start processing of thread t1 + Thread.sleep(1000); + System.out.println(t1.getState()); + } +} + +class DemoThread implements Runnable { + @Override + public void run() { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java new file mode 100644 index 0000000000..98a6844309 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java @@ -0,0 +1,35 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class WaitingState implements Runnable { + public static Thread t1; + + public static void main(String[] args) { + t1 = new Thread(new WaitingState()); + t1.start(); + } + + public void run() { + Thread t2 = new Thread(new DemoThreadWS()); + t2.start(); + + try { + t2.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} + +class DemoThreadWS implements Runnable { + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + + System.out.println(WaitingState.t1.getState()); + } +} \ No newline at end of file From 3d4a179ca3483ac0deb889fc6e3efe10f99d2b08 Mon Sep 17 00:00:00 2001 From: christopherfranklin Date: Mon, 12 Feb 2018 17:36:16 -0500 Subject: [PATCH 082/102] [BAEL-1542] - Simple Tagging with JPA (#3626) * Code samples for Simple Tagging with JPA * Fix pom relative path for spring-jpa --- persistence-modules/spring-jpa/pom.xml | 2 +- .../persistence/dao/StudentRepository.java | 11 +++++ .../inmemory/persistence/model/Student.java | 13 ++++++ .../repository/InMemoryDBIntegrationTest.java | 45 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/persistence-modules/spring-jpa/pom.xml b/persistence-modules/spring-jpa/pom.xml index 04c64fafc3..bc0b2381f3 100644 --- a/persistence-modules/spring-jpa/pom.xml +++ b/persistence-modules/spring-jpa/pom.xml @@ -13,7 +13,7 @@ com.baeldung parent-modules 1.0.0-SNAPSHOT - ../ + ../../ diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index bfcf6f5cdc..f856b78c52 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -2,6 +2,17 @@ package org.baeldung.inmemory.persistence.dao; import org.baeldung.inmemory.persistence.model.Student; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface StudentRepository extends JpaRepository { + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)") + List retrieveByTag(@Param("tag") String tag); + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)") + List retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag); + } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index b50fe9122e..2e4e3ea2cb 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -1,7 +1,10 @@ package org.baeldung.inmemory.persistence.model; +import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; +import java.util.ArrayList; +import java.util.List; @Entity public class Student { @@ -10,6 +13,9 @@ public class Student { private long id; private String name; + @ElementCollection + private List tags = new ArrayList<>(); + public Student() { } @@ -35,4 +41,11 @@ public class Student { this.name = name; } + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags.addAll(tags); + } } diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 8380ab5434..28d7e3772c 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -12,6 +12,9 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; + import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @@ -34,4 +37,46 @@ public class InMemoryDBIntegrationTest { assertEquals("name incorrect", NAME, student2.getName()); } + @Test + public void givenStudentWithTags_whenSave_thenGetByTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByTag("full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + + @Test + public void givenMultipleStudentsWithTags_whenSave_thenGetByTagReturnsCorrectCount(){ + Student student = new Student(0, "Larry"); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = new Student(1, "Curly"); + student2.setTags(Arrays.asList("part time", "rocket science")); + studentRepository.save(student2); + + Student student3 = new Student(2, "Moe"); + student3.setTags(Arrays.asList("full time", "philosophy")); + studentRepository.save(student3); + + Student student4 = new Student(3, "Shemp"); + student4.setTags(Arrays.asList("part time", "mathematics")); + studentRepository.save(student4); + + List students = studentRepository.retrieveByTag("full time"); + assertEquals("size incorrect", 2, students.size()); + } + + @Test + public void givenStudentWithTags_whenSave_thenGetByNameAndTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByNameFilterByTag("John", "full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + } From bcc3b6ed95d0d3d5e330299c51efe63190a9c632 Mon Sep 17 00:00:00 2001 From: haseebahmad11 <35659302+haseebahmad11@users.noreply.github.com> Date: Wed, 14 Feb 2018 05:49:46 +0500 Subject: [PATCH 083/102] BAEL-1343 MVC Architecture with Servlets and JSP code (#3624) * BAEL-1343 MVC Architecture with Servlets and JSP code * BAEL-1343 code refactoring * BAEL-1343 code refactoring * BAEL-1343 updated code formatting * BAEL-1343 code refactoring --- .../baeldung/controller/StudentServlet.java | 43 +++++++++++++ .../main/java/com/baeldung/model/Student.java | 62 +++++++++++++++++++ .../com/baeldung/service/StudentService.java | 34 ++++++++++ .../com/baeldung/servlets/FormServlet.java | 2 +- .../main/webapp/WEB-INF/jsp}/index.jsp | 0 .../webapp/WEB-INF/jsp/student-record.jsp | 36 +++++++++++ .../{web => src/main/webapp}/WEB-INF/web.xml | 0 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java create mode 100644 javax-servlets/src/main/java/com/baeldung/model/Student.java create mode 100644 javax-servlets/src/main/java/com/baeldung/service/StudentService.java rename javax-servlets/{web => src/main/webapp/WEB-INF/jsp}/index.jsp (100%) create mode 100644 javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp rename javax-servlets/{web => src/main/webapp}/WEB-INF/web.xml (100%) diff --git a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java new file mode 100644 index 0000000000..3c893eab1a --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java @@ -0,0 +1,43 @@ +package com.baeldung.controller; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.baeldung.service.StudentService; + +/** + * + * @author haseeb + * + */ +@WebServlet(name = "StudentServlet", urlPatterns = "/student-record") +public class StudentServlet extends HttpServlet { + + protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + StudentService studentService = new StudentService(); + String studentID = request.getParameter("id"); + if (studentID != null) { + int id = Integer.parseInt(studentID); + request.setAttribute("studentRecord", studentService.getStudent(id)); + } + + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); + dispatcher.forward(request, response); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/model/Student.java b/javax-servlets/src/main/java/com/baeldung/model/Student.java new file mode 100644 index 0000000000..55afe6bc2a --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/model/Student.java @@ -0,0 +1,62 @@ +package com.baeldung.model; + +/** + * + * @author haseeb + * + */ +public class Student { + + private int id; + private String firstName; + private String lastName; + + public Student(int id, String firstName, String lastName) { + super(); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the firstName + */ + public String getFirstName() { + return firstName; + } + + /** + * @param firstName the firstName to set + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * @return the lastName + */ + public String getLastName() { + return lastName; + } + + /** + * @param lastName the lastName to set + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java new file mode 100644 index 0000000000..1666850d50 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java @@ -0,0 +1,34 @@ +package com.baeldung.service; + +import com.baeldung.model.Student; + +/** + * + * @author haseeb + * + */ +public class StudentService { + + /** + * + * @param id + * @return + */ + public Student getStudent(int id) { + + Student student = null; + + switch (id) { + case 1: + student = new Student(1, "John", "Doe"); + break; + case 2: + student = new Student(2, "Jane", "Goodall"); + break; + case 3: + student = new Student(3, "Max", "Born"); + break; + } + return student; + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index fcd9143dff..04c5fec42d 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -25,7 +25,7 @@ public class FormServlet extends HttpServlet { response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { diff --git a/javax-servlets/web/index.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp similarity index 100% rename from javax-servlets/web/index.jsp rename to javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp diff --git a/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp new file mode 100644 index 0000000000..4b9d9b378f --- /dev/null +++ b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + +<%@ page import="com.baeldung.model.Student"%> + + + + +Student Record + + + <% + if (request.getAttribute("studentRecord") != null) { + Student student = (Student) request.getAttribute("studentRecord"); + %> + +

Student Record

+
+ ID: + <%=student.getId()%>
+
+ First Name: + <%=student.getFirstName()%>
+
+ Last Name: + <%=student.getLastName()%>
+ + <% + } else { + %> +

No student record found.

+ <% + } + %> + + \ No newline at end of file diff --git a/javax-servlets/web/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from javax-servlets/web/WEB-INF/web.xml rename to javax-servlets/src/main/webapp/WEB-INF/web.xml From 83bc46ee228db8c44a5b36e37c676a064fc472a5 Mon Sep 17 00:00:00 2001 From: Allan Vital Date: Tue, 13 Feb 2018 23:13:37 -0200 Subject: [PATCH 084/102] BAEL-1368 Infinispan Guide (#3651) * BAEL-1368: infinispan article * BAEL-1368: new method timing method --- .../infinispan/ConfigurationTest.java | 15 ++- .../service/HelloWorldServiceUnitTest.java | 94 ++++++------------- .../service/TransactionalServiceUnitTest.java | 6 +- 3 files changed, 46 insertions(+), 69 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java index b05314491b..906a887e1a 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -9,13 +9,15 @@ import org.infinispan.manager.DefaultCacheManager; import org.junit.After; import org.junit.Before; +import java.util.concurrent.Callable; + public class ConfigurationTest { private DefaultCacheManager cacheManager; private HelloWorldRepository repository = new HelloWorldRepository(); - protected HelloWorldService helloWorldService; + protected HelloWorldService helloWorldService; protected TransactionalService transactionalService; @Before @@ -53,4 +55,15 @@ public class ConfigurationTest { cacheManager.stop(); } + protected long timeThis(Callable callable) { + try { + long milis = System.currentTimeMillis(); + callable.call(); + return System.currentTimeMillis() - milis; + } catch (Exception e) { + e.printStackTrace(); + } + return 0l; + } + } diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java index bda2e6886b..c9ecd57995 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java @@ -3,103 +3,69 @@ package com.baeldung.infinispan.service; import com.baeldung.infinispan.ConfigurationTest; import org.junit.Test; +import java.util.concurrent.Callable; +import java.util.function.Consumer; + import static org.assertj.core.api.Java6Assertions.assertThat; public class HelloWorldServiceUnitTest extends ConfigurationTest { @Test public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { - long milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorld(); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorld(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isLessThan(100); } @Test public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { - long milis = System.currentTimeMillis(); - helloWorldService.findExpiringHelloWorld(); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findExpiringHelloWorld(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isLessThan(100); } @Test public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() - throws InterruptedException { + throws InterruptedException { - long milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorldInExpiringCache(); - long executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); Thread.sleep(1100); - milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorldInExpiringCache(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); } @Test - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() - throws InterruptedException { + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { - long milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 1"); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); - milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 2"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 1"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); } @Test - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() - throws InterruptedException { + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { - long milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 1"); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); - milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 2"); - executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isLessThan(100); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 1"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); } } diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java index f529222079..99efacd18a 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java @@ -14,11 +14,9 @@ public class TransactionalServiceUnitTest extends ConfigurationTest { transactionalService.getQuickHowManyVisits(); backgroundThread.start(); Thread.sleep(100); //lets wait our thread warm up - long milis = System.currentTimeMillis(); - transactionalService.getQuickHowManyVisits(); - long executionTime = System.currentTimeMillis() - milis; - assertThat(executionTime).isGreaterThan(500).isLessThan(1000); + assertThat(timeThis(() -> transactionalService.getQuickHowManyVisits())) + .isGreaterThan(500).isLessThan(1000); } } From 4f6988b134ac47087cbcb9fc9d152d76bcfb01e7 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Wed, 14 Feb 2018 00:08:31 -0300 Subject: [PATCH 085/102] Method Overloading and Overriding in Java - BAEL-1507 (#3621) * Initial Commit * Update Multiplier.java * Refactored MethodOverridingUnitTest class * Update Car.java * Update Vehicle.java * Update MethodOverridingUnitTest.java * Update MethodOverridingUnitTest.java * Fix package names --- .../application/Application.java | 26 +++++++ .../model/Car.java | 9 +++ .../model/Vehicle.java | 16 +++++ .../util/Multiplier.java | 16 +++++ .../test/MethodOverloadingUnitTest.java | 41 +++++++++++ .../test/MethodOverridingUnitTest.java | 68 +++++++++++++++++++ 6 files changed, 176 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java create mode 100644 core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java create mode 100644 core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java create mode 100644 core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java create mode 100644 core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java new file mode 100644 index 0000000000..6e023e9fa8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java @@ -0,0 +1,26 @@ +package com.baeldung.methodoverloadingoverriding.application; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import com.baeldung.methodoverloadingoverriding.util.Multiplier; + +public class Application { + + public static void main(String[] args) { + Multiplier multiplier = new Multiplier(); + System.out.println(multiplier.multiply(10, 10)); + System.out.println(multiplier.multiply(10, 10, 10)); + System.out.println(multiplier.multiply(10, 10.5)); + System.out.println(multiplier.multiply(10.5, 10.5)); + + Vehicle vehicle = new Vehicle(); + System.out.println(vehicle.accelerate(100)); + System.out.println(vehicle.run()); + System.out.println(vehicle.stop()); + + Vehicle car = new Car(); + System.out.println(car.accelerate(80)); + System.out.println(car.run()); + System.out.println(car.stop()); + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java new file mode 100644 index 0000000000..aa5bdcaec9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java @@ -0,0 +1,9 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Car extends Vehicle { + + @Override + public String accelerate(long mph) { + return "The car accelerates at : " + mph + " MPH."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java new file mode 100644 index 0000000000..eb59e8ca23 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Vehicle { + + public String accelerate(long mph) { + return "The vehicle accelerates at : " + mph + " MPH."; + } + + public String stop() { + return "The vehicle has stopped."; + } + + public String run() { + return "The vehicle is running."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java new file mode 100644 index 0000000000..c43e61c78f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.util; + +public class Multiplier { + + public int multiply(int a, int b) { + return a * b; + } + + public int multiply(int a, int b, int c) { + return a * b * c; + } + + public double multiply(double a, double b) { + return a * b; + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java new file mode 100644 index 0000000000..081a30c34a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.util.Multiplier; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverloadingUnitTest { + + private static Multiplier multiplier; + + @BeforeClass + public static void setUpMultiplierInstance() { + multiplier = new Multiplier(); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java new file mode 100644 index 0000000000..554ac121bc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverridingUnitTest { + + private static Vehicle vehicle; + private static Car car; + + @BeforeClass + public static void setUpVehicleInstance() { + vehicle = new Vehicle(); + } + + @BeforeClass + public static void setUpCarInstance() { + car = new Car(); + } + + @Test + public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH."); + } + + @Test + public void givenVehicleInstance_whenCalledRun_thenOneAssertion() { + assertThat(vehicle.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenVehicleInstance_whenCalledStop_thenOneAssertion() { + assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH."); + } + + @Test + public void givenCarInstance_whenCalledRun_thenOneAssertion() { + assertThat(car.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenCarInstance_whenCalledStop_thenOneAssertion() { + assertThat(car.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() { + assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100)); + } + + @Test + public void givenVehicleCarInstances_whenCalledRun_thenEqual() { + assertThat(vehicle.run()).isEqualTo(car.run()); + } + + @Test + public void givenVehicleCarInstances_whenCalledStop_thenEqual() { + assertThat(vehicle.stop()).isEqualTo(car.stop()); + } +} From b8f433859ef3f4f73637de5ff575a35ca331bb5e Mon Sep 17 00:00:00 2001 From: Diaz Novandi Date: Mon, 12 Feb 2018 22:56:48 +0100 Subject: [PATCH 086/102] BAEL-681 Configuring DynamoDB locally for integration tests --- .../spring-data-dynamodb/pom.xml | 79 +++++++++++++++++++ .../ProductInfoRepositoryIntegrationTest.java | 10 ++- .../rule/LocalDynamoDBCreationRule.java | 36 +++++++++ 3 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index bf90779c29..0b78aac10e 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -23,6 +23,10 @@ 4.4.1 1.11.64 3.3.7-1 + 1.0.392 + 1.11.106 + 1.11.86 + https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release @@ -103,6 +107,57 @@ httpclient ${httpclient.version} + + + + + com.amazonaws + DynamoDBLocal + ${dynamodblocal.version} + test + + + com.almworks.sqlite4java + sqlite4java + ${sqlite4java.version} + test + + + com.almworks.sqlite4java + sqlite4java-win32-x86 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + sqlite4java-win32-x64 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + libsqlite4java-osx + ${sqlite4java.version} + dylib + test + + + com.almworks.sqlite4java + libsqlite4java-linux-i386 + ${sqlite4java.version} + so + test + + + com.almworks.sqlite4java + libsqlite4java-linux-amd64 + ${sqlite4java.version} + so + test + +
@@ -120,6 +175,25 @@ org.apache.maven.plugins maven-war-plugin + + org.apache.maven.plugins + maven-dependency-plugin + 2.10 + + + copy-dependencies + test-compile + + copy-dependencies + + + test + so,dll,dylib + ${project.basedir}/native-libs + + + + @@ -142,6 +216,11 @@ false + + dynamodb-local + DynamoDB Local Release Repository + ${dynamodblocal.repository.url} + diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 2ff418b4ec..05b21fd2af 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -8,8 +8,9 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; import com.baeldung.Application; import com.baeldung.spring.data.dynamodb.model.ProductInfo; import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository; +import com.baeldung.spring.data.dynamodb.repository.rule.LocalDynamoDBCreationRule; import org.junit.Before; -import org.junit.Ignore; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +31,9 @@ import static org.junit.Assert.assertTrue; @TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" }) public class ProductInfoRepositoryIntegrationTest { + @ClassRule + public static LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule(); + private DynamoDBMapper dynamoDBMapper; @Autowired @@ -42,7 +46,6 @@ public class ProductInfoRepositoryIntegrationTest { private static final String EXPECTED_PRICE = "50"; @Before - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void setup() throws Exception { try { @@ -57,12 +60,11 @@ public class ProductInfoRepositoryIntegrationTest { // Do nothing, table already created } - // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table + // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table dynamoDBMapper.batchDelete((List) repository.findAll()); } @Test - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() { ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java new file mode 100644 index 0000000000..5df377c508 --- /dev/null +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java @@ -0,0 +1,36 @@ +package com.baeldung.spring.data.dynamodb.repository.rule; + +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; +import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; +import org.junit.rules.ExternalResource; +import org.springframework.beans.factory.annotation.Autowired; + +public class LocalDynamoDBCreationRule extends ExternalResource { + + protected DynamoDBProxyServer server; + protected AmazonDynamoDB amazonDynamoDB; + + @Override + protected void before() throws Exception { + System.setProperty("sqlite4java.library.path", "native-libs"); + String port = "8000"; + this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); + server.start(); + } + + @Override + protected void after() { + try { + server.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Autowired + public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) { + this.amazonDynamoDB = amazonDynamoDB; + } + +} From 9376e8ce692612b0269b446bf48f19d15c233520 Mon Sep 17 00:00:00 2001 From: Diaz Novandi Date: Tue, 13 Feb 2018 13:34:03 +0100 Subject: [PATCH 087/102] Refine test using Hamcrest and refactor JUnit rule --- .../ProductInfoRepositoryIntegrationTest.java | 9 +++++--- .../rule/LocalDynamoDBCreationRule.java | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 05b21fd2af..8052aba3df 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -22,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @@ -71,7 +74,7 @@ public class ProductInfoRepositoryIntegrationTest { repository.save(productInfo); List result = (List) repository.findAll(); - assertTrue("Not empty", result.size() > 0); - assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); + assertThat(result.size(), is(greaterThan(0))); + assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST))); } } diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java index 5df377c508..62334b6d00 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java @@ -1,19 +1,21 @@ package com.baeldung.spring.data.dynamodb.repository.rule; -import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; import org.junit.rules.ExternalResource; -import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Optional; public class LocalDynamoDBCreationRule extends ExternalResource { protected DynamoDBProxyServer server; - protected AmazonDynamoDB amazonDynamoDB; + + public LocalDynamoDBCreationRule() { + System.setProperty("sqlite4java.library.path", "native-libs"); + } @Override protected void before() throws Exception { - System.setProperty("sqlite4java.library.path", "native-libs"); String port = "8000"; this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); server.start(); @@ -21,16 +23,15 @@ public class LocalDynamoDBCreationRule extends ExternalResource { @Override protected void after() { + Optional.ofNullable(server).ifPresent(this::stopUnchecked); + } + + protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) { try { - server.stop(); + dynamoDbServer.stop(); } catch (Exception e) { throw new RuntimeException(e); } } - @Autowired - public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) { - this.amazonDynamoDB = amazonDynamoDB; - } - } From 93213af112820b61e0a816ec9d9df5a79eec2bde Mon Sep 17 00:00:00 2001 From: linhvovn Date: Fri, 16 Feb 2018 00:22:23 +0800 Subject: [PATCH 088/102] [BAEL 1382] tlinh2110 - Remove unnecessary logs (#3667) * [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger * [tlinh2110-BAEL-1382] Update Unit Test * [tlinh2110-BAEL1382] Remove unnecessary logs --- spring-integration/pom.xml | 6 ------ spring-integration/src/test/resources/logback-test.xml | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 spring-integration/src/test/resources/logback-test.xml diff --git a/spring-integration/pom.xml b/spring-integration/pom.xml index 27cc7381e3..43e45dfd17 100644 --- a/spring-integration/pom.xml +++ b/spring-integration/pom.xml @@ -128,12 +128,6 @@ spring-security-test ${spring.version} test - - - org.springframework - spring-test - ${spring.version} - test junit diff --git a/spring-integration/src/test/resources/logback-test.xml b/spring-integration/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..a807be0ca2 --- /dev/null +++ b/spring-integration/src/test/resources/logback-test.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From 443e990fd128dbb52d98e6916465bb41ee923eb7 Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Thu, 15 Feb 2018 12:57:27 -0300 Subject: [PATCH 089/102] BAEL-1543: Spring Batch: Tasklets vs Chunks --- spring-batch/pom.xml | 15 ++- .../chunks/LineProcessor.java | 36 +++++++ .../taskletsvschunks/chunks/LineReader.java | 36 +++++++ .../taskletsvschunks/chunks/LinesWriter.java | 39 ++++++++ .../baeldung/taskletsvschunks/model/Line.java | 55 +++++++++++ .../tasklets/LinesProcessor.java | 49 ++++++++++ .../tasklets/LinesReader.java | 53 +++++++++++ .../tasklets/LinesWriter.java | 50 ++++++++++ .../taskletsvschunks/utils/FileUtils.java | 95 +++++++++++++++++++ spring-batch/src/main/resources/logback.xml | 33 +++++++ .../resources/taskletsvschunks/chunks.xml | 39 ++++++++ .../input/tasklets-vs-chunks.csv | 6 ++ .../resources/taskletsvschunks/tasklets.xml | 47 +++++++++ .../taskletsvschunks/chunks/ChunksTest.java | 24 +++++ .../tasklets/TaskletsTest.java | 24 +++++ 15 files changed, 599 insertions(+), 2 deletions(-) create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java create mode 100644 spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java create mode 100644 spring-batch/src/main/resources/logback.xml create mode 100644 spring-batch/src/main/resources/taskletsvschunks/chunks.xml create mode 100644 spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv create mode 100644 spring-batch/src/main/resources/taskletsvschunks/tasklets.xml create mode 100644 spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java create mode 100644 spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java diff --git a/spring-batch/pom.xml b/spring-batch/pom.xml index f372ebd724..f72024d32b 100644 --- a/spring-batch/pom.xml +++ b/spring-batch/pom.xml @@ -18,9 +18,10 @@ UTF-8 - 4.3.4.RELEASE - 3.0.7.RELEASE + 5.0.3.RELEASE + 4.0.0.RELEASE 3.15.1 + 4.1 @@ -51,6 +52,16 @@ spring-batch-core ${spring.batch.version} + + org.springframework.batch + spring-batch-test + ${spring.batch.version} + + + com.opencsv + opencsv + ${opencsv.version} + diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java new file mode 100644 index 0000000000..5b6cd9add3 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemProcessor; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +public class LineProcessor implements ItemProcessor, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineProcessor.class); + + @Override + public void beforeStep(StepExecution stepExecution) { + logger.debug("Line Processor initialized."); + } + + @Override + public Line process(Line line) throws Exception { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Line Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java new file mode 100644 index 0000000000..5f6b6de218 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemReader; + +public class LineReader implements ItemReader, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineReader.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Line Reader initialized."); + } + + @Override + public Line read() throws Exception { + Line line = fu.readLine(); + if (line != null) logger.debug("Read line: " + line.toString()); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + logger.debug("Line Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java new file mode 100644 index 0000000000..66f9103a56 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java @@ -0,0 +1,39 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemWriter; + +import java.util.List; + +public class LinesWriter implements ItemWriter, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("output.csv"); + logger.debug("Line Writer initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Line Writer ended."); + return ExitStatus.COMPLETED; + } + + @Override + public void write(List lines) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java new file mode 100644 index 0000000000..fee6fd31a6 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java @@ -0,0 +1,55 @@ +package org.baeldung.taskletsvschunks.model; + +import java.io.Serializable; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Line implements Serializable { + + private String name; + private LocalDate dob; + private Long age; + + public Line(String name, LocalDate dob) { + this.name = name; + this.dob = dob; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getDob() { + return dob; + } + + public void setDob(LocalDate dob) { + this.dob = dob; + } + + public Long getAge() { + return age; + } + + public void setAge(Long age) { + this.age = age; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(this.name); + sb.append(","); + sb.append(this.dob.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + if (this.age != null) { + sb.append(","); + sb.append(this.age); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java new file mode 100644 index 0000000000..ba7a3088d5 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java @@ -0,0 +1,49 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.List; + +public class LinesProcessor implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesProcessor.class); + + private List lines; + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + } + return RepeatStatus.FINISHED; + } + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + logger.debug("Lines Processor initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Lines Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java new file mode 100644 index 0000000000..9905ee8f8a --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java @@ -0,0 +1,53 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.ArrayList; +import java.util.List; + +public class LinesReader implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesReader.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + lines = new ArrayList(); + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Lines Reader initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + Line line = fu.readLine(); + while (line != null) { + lines.add(line); + logger.debug("Read line: " + line.toString()); + line = fu.readLine(); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + stepExecution + .getJobExecution() + .getExecutionContext() + .put("lines", this.lines); + logger.debug("Lines Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java new file mode 100644 index 0000000000..937288a890 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java @@ -0,0 +1,50 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.List; + +public class LinesWriter implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + fu = new FileUtils("output.csv"); + logger.debug("Lines Writer initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Lines Writer ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java new file mode 100644 index 0000000000..df36d5c756 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java @@ -0,0 +1,95 @@ +package org.baeldung.taskletsvschunks.utils; + +import com.opencsv.CSVReader; +import com.opencsv.CSVWriter; +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class FileUtils { + + private final Logger logger = LoggerFactory.getLogger(FileUtils.class); + + private String fileName; + private CSVReader CSVReader; + private CSVWriter CSVWriter; + private FileReader fileReader; + private FileWriter fileWriter; + private File file; + + public FileUtils(String fileName) { + this.fileName = fileName; + } + + public Line readLine() { + try { + if (CSVReader == null) initReader(); + String[] line = CSVReader.readNext(); + if (line == null) return null; + return new Line(line[0], LocalDate.parse(line[1], DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + } catch (Exception e) { + logger.error("Error while reading line in file: " + this.fileName); + return null; + } + } + + public void writeLine(Line line) { + try { + if (CSVWriter == null) initWriter(); + String[] lineStr = new String[2]; + lineStr[0] = line.getName(); + lineStr[1] = line + .getAge() + .toString(); + CSVWriter.writeNext(lineStr); + } catch (Exception e) { + logger.error("Error while writing line in file: " + this.fileName); + } + } + + private void initReader() throws Exception { + ClassLoader classLoader = this + .getClass() + .getClassLoader(); + if (file == null) file = new File(classLoader + .getResource(fileName) + .getFile()); + if (fileReader == null) fileReader = new FileReader(file); + if (CSVReader == null) CSVReader = new CSVReader(fileReader); + } + + private void initWriter() throws Exception { + if (file == null) { + file = new File(fileName); + file.createNewFile(); + } + if (fileWriter == null) fileWriter = new FileWriter(file, true); + if (CSVWriter == null) CSVWriter = new CSVWriter(fileWriter); + } + + public void closeWriter() { + try { + CSVWriter.close(); + fileWriter.close(); + } catch (IOException e) { + logger.error("Error while closing writer."); + } + } + + public void closeReader() { + try { + CSVReader.close(); + fileReader.close(); + } catch (IOException e) { + logger.error("Error while closing reader."); + } + } + +} diff --git a/spring-batch/src/main/resources/logback.xml b/spring-batch/src/main/resources/logback.xml new file mode 100644 index 0000000000..dc9218caf3 --- /dev/null +++ b/spring-batch/src/main/resources/logback.xml @@ -0,0 +1,33 @@ + + + + C:\Users\admin\Desktop\Baeldung\repo\magkrause\tutorials\spring-batch\src\main\resources\taskletsvschunks\logs.log + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n + + + + + + ${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log + + + 10MB + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/chunks.xml b/spring-batch/src/main/resources/taskletsvschunks/chunks.xml new file mode 100644 index 0000000000..f4b77ac10c --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/chunks.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv new file mode 100644 index 0000000000..214bd3cb70 --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv @@ -0,0 +1,6 @@ +Mae Hodges,10/22/1972 +Gary Potter,02/22/1953 +Betty Wise,02/17/1968 +Wayne Rose,04/06/1977 +Adam Caldwell,09/27/1995 +Lucille Phillips,05/14/1992 \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml b/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml new file mode 100644 index 0000000000..34ce2944bc --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java new file mode 100644 index 0000000000..d32edaf2ce --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java @@ -0,0 +1,24 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:/taskletsvschunks/chunks.xml") +public class ChunksTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void test() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java new file mode 100644 index 0000000000..4702b67a5c --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java @@ -0,0 +1,24 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:/taskletsvschunks/tasklets.xml") +public class TaskletsTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void test() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file From d09a828a58ea258dc30bda6deded16829553d5d7 Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Thu, 15 Feb 2018 13:28:56 -0300 Subject: [PATCH 090/102] BAEL-1543: Changing tests names. --- .../java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java | 2 +- .../org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java index d32edaf2ce..34b6315dc4 100644 --- a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java @@ -17,7 +17,7 @@ public class ChunksTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test - public void test() throws Exception { + public void givenChunksJob_WhenJobEnds_ThenStatusCompleted() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java index 4702b67a5c..53731feed4 100644 --- a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java @@ -17,7 +17,7 @@ public class TaskletsTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test - public void test() throws Exception { + public void givenTaskletsJob_WhenJobEnds_ThenStatusCompleted() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } From 7d0efdb67d0b32f3da5890ed21b5329ec62311e3 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Thu, 15 Feb 2018 17:53:22 -0600 Subject: [PATCH 091/102] BAEL-113 README (#3658) * BAEL-973: updated README * BAEL-1069: Updated README * BAEL-817: add README file * BAEL-1084: README update * BAEL-960: Update README * BAEL-1155: updated README * BAEL-1041: updated README * BAEL-973: Updated README * BAEL-1187: updated README * BAEL-1183: Update README * BAEL-1133: Updated README * BAEL-1098: README update * BAEL-719: add README.md * BAEL-1272: README update * BAEL-1272: README update * BAEL-1196: Update README * BAEL-1328: Updated README * BAEL-1371: Update README.md * BAEL-1371: Update README.md * BAEL-1278: Update README * BAEL-1326: Update README * BAEL-399: Update README * BAEL-1297: Update README * BAEL-1218: README * BAEL-1148 README update * BAEL-113 README --- spring-jinq/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 spring-jinq/README.md diff --git a/spring-jinq/README.md b/spring-jinq/README.md new file mode 100644 index 0000000000..10d7903f49 --- /dev/null +++ b/spring-jinq/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Introduction to Jinq with Spring](http://www.baeldung.com/spring-jinq) + From a27e73a57f909a3add358121f7fdb9a1da1de3d7 Mon Sep 17 00:00:00 2001 From: iaforek Date: Fri, 16 Feb 2018 06:13:26 +0000 Subject: [PATCH 092/102] BAEL-1298 - Variable renames (#3653) * Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention * BAEL-821 Added more test and updated round methods. * BAEL-837 - initial commit. A few examples of adding doubles. * BAEL-837 - Couple of smaller changes * BAEL-837 - Added jUnit test. * BAEL-579 Updated Spring Cloud Version I was getting error: java.lang.NoSuchMethodError: org.springframework.cloud.config.environment.Environment After version update, all is okay. * BAEL-579 Added actuator to Cloud Config Client. * BAEL-579 Enabled cloud bus and updated dependencies. * BAEL-579 Config Client using Spring Cloud Bus. * BAEL-579 Recreated Basic Config Server. * BAEL-579 Recreated Config Client. * BAEL-579 Removed test Git URL. * BAEL-579 Added Actuator to Config Client * BAEL-579 Added Spring Cloud Bus to Client. * BAEL-579 Server changes for Spring Cloud Bus Added dependencies and removed git.clone-on-start as this was causing server to throw errors after git properties change. * BAEL-579 Removed Git URL. * Revert "BAEL-579 Updated Spring Cloud Version" This reverts commit f775bf91e53a1ecfb9b70596688d7c8202bf495f. * Revert "BAEL-579 Config Client using Spring Cloud Bus." This reverts commit 1d96bc5761994a33af9a7a9aa5ab68604a5b44dc. * Revert "BAEL-579 Enabled cloud bus and updated dependencies." This reverts commit 7845da922d89d53506dd0fff387ea13694c50bc1. * Revert "BAEL-579 Added actuator to Cloud Config Client." This reverts commit 076657a26a57e0aa676989a4d97966a3b9d53e1c. * BAEL-579 Added missing dependency versions. * BAEL-579 Added missing dependency versions. * Updated gitignore * BAEL-1065 Simple performance check StringBuffer vs StringBuilder. * BAEL-1065 Added JMH benchmarks * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Sudoku - Backtracking Algorithm * BAEL-1298 Dancing Links Algorithm. Smaller changes to Backtracking * BAEL-1298 Resolve conflict - use most up-to-date POM * Updated code - mostly with CONSTANTS. Extracted methods. * Removed pointless Java8 code. Renamed constant * Extracted 'constraints' methods and renamed coverBoard variable * Extracted 'constraints' methods and renamed coverBoard variable * Renamed variables * More variable renames --- .../sudoku/BacktrackingAlgorithm.java | 48 +++++++++---------- .../sudoku/DancingLinksAlgorithm.java | 40 ++++++++-------- .../algorithms/sudoku/DancingNode.java | 26 +++++----- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java index ff426cbe68..4b37558aab 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -40,15 +40,15 @@ public class BacktrackingAlgorithm { } private boolean solve(int[][] board) { - for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) { - for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) { - if (board[r][c] == NO_VALUE) { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + if (board[row][column] == NO_VALUE) { for (int k = MIN_VALUE; k <= MAX_VALUE; k++) { - board[r][c] = k; - if (isValid(board, r, c) && solve(board)) { + board[row][column] = k; + if (isValid(board, row, column) && solve(board)) { return true; } - board[r][c] = NO_VALUE; + board[row][column] = NO_VALUE; } return false; } @@ -57,44 +57,44 @@ public class BacktrackingAlgorithm { return true; } - private boolean isValid(int[][] board, int r, int c) { - return rowConstraint(board, r) && - columnConstraint(board, c) && - subsectionConstraint(board, r, c); + private boolean isValid(int[][] board, int row, int column) { + return rowConstraint(board, row) && + columnConstraint(board, column) && + subsectionConstraint(board, row, column); } - private boolean subsectionConstraint(int[][] board, int r, int c) { + private boolean subsectionConstraint(int[][] board, int row, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; - int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE; - int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE; - for (int i = subsectionRowStart; i < subsectionRowEnd; i++) { - for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) { - if (!checkConstraint(board, i, constraint, j)) return false; + for (int r = subsectionRowStart; r < subsectionRowEnd; r++) { + for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) { + if (!checkConstraint(board, r, constraint, c)) return false; } } return true; } - private boolean columnConstraint(int[][] board, int c) { + private boolean columnConstraint(int[][] board, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, i, constraint, c)); + .allMatch(row -> checkConstraint(board, row, constraint, column)); } - private boolean rowConstraint(int[][] board, int r) { + private boolean rowConstraint(int[][] board, int row) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, r, constraint, i)); + .allMatch(column -> checkConstraint(board, row, constraint, column)); } - private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) { - if (board[r][c] != NO_VALUE) { - if (!constraint[board[r][c] - 1]) { - constraint[board[r][c] - 1] = true; + private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) { + if (board[row][column] != NO_VALUE) { + if (!constraint[board[row][column] - 1]) { + constraint[board[row][column] - 1] = true; } else { return false; } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java index 76b686afa6..df02ff3d11 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -34,8 +34,8 @@ public class DancingLinksAlgorithm { dlx.runSolver(); } - private int getIndex(int row, int col, int num) { - return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1); + private int getIndex(int row, int column, int num) { + return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1); } private boolean[][] createExactCoverBoard() { @@ -51,12 +51,12 @@ public class DancingLinksAlgorithm { } private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) { - for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { - for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) { - for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) { - int index = getIndex(br + rDelta, bc + cDelta, n); + for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) { + for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) { + int index = getIndex(row + rowDelta, column + columnDelta, n); coverBoard[index][hBase] = true; } } @@ -67,10 +67,10 @@ public class DancingLinksAlgorithm { } private int checkColumnConstraint(boolean[][] coverBoard, int hBase) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) { - int index = getIndex(r1, c, n); + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -79,10 +79,10 @@ public class DancingLinksAlgorithm { } private int checkRowConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) { - int index = getIndex(r, c1, n); + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -91,10 +91,10 @@ public class DancingLinksAlgorithm { } private int checkCellConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { - int index = getIndex(r, c, n); + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -104,13 +104,13 @@ public class DancingLinksAlgorithm { private boolean[][] initializeExactCoverBoard(int[][] board) { boolean[][] coverBoard = createExactCoverBoard(); - for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) { - for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) { - int n = board[i - 1][j - 1]; + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int n = board[row - 1][column - 1]; if (n != NO_VALUE) { for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { if (num != n) { - Arrays.fill(coverBoard[getIndex(i, j, num)], false); + Arrays.fill(coverBoard[getIndex(row, column, num)], false); } } } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java index b494eba9ef..2422ff0dff 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -4,21 +4,21 @@ class DancingNode { DancingNode L, R, U, D; ColumnNode C; - DancingNode hookDown(DancingNode n1) { - assert (this.C == n1.C); - n1.D = this.D; - n1.D.U = n1; - n1.U = this; - this.D = n1; - return n1; + DancingNode hookDown(DancingNode node) { + assert (this.C == node.C); + node.D = this.D; + node.D.U = node; + node.U = this; + this.D = node; + return node; } - DancingNode hookRight(DancingNode n1) { - n1.R = this.R; - n1.R.L = n1; - n1.L = this; - this.R = n1; - return n1; + DancingNode hookRight(DancingNode node) { + node.R = this.R; + node.R.L = node; + node.L = this; + this.R = node; + return node; } void unlinkLR() { From 666c07c7bee789f5f4c07c56f3b3172645879644 Mon Sep 17 00:00:00 2001 From: Harshil Sharma Date: Fri, 16 Feb 2018 19:03:34 +0530 Subject: [PATCH 093/102] BAEL-1539 shuffling collections (#3567) * BAEL-1539 Added list, set and map shuffling code exaamples * #BAEL-1539 fixed a typo * #BAEL-1539 refactored sample code, added unit tests * #BAEL-1539 Added unit tests and example for shuffling with custom randomness * #BAEL-1539 removed source code and kept only tests as tests are sufficient code sample themselves * #BAEL-1539 updated map shuffling example to use lambdas * #BAEL-1539 lambda refactoring * Fixed an indentation --- .../ShufflingCollectionsUnitTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java diff --git a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java new file mode 100644 index 0000000000..4823c7178a --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.shufflingcollections; + +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ShufflingCollectionsUnitTest { + + @Test + public void whenShufflingList_thenListIsShuffled() { + List students = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + + System.out.println("List before shuffling:"); + System.out.println(students); + + Collections.shuffle(students); + System.out.println("List after shuffling:"); + System.out.println(students); + } + + @Test + public void whenShufflingMapKeys_thenValuesAreShuffled() { + Map studentsById = new HashMap<>(); + studentsById.put(1, "Foo"); + studentsById.put(2, "Bar"); + studentsById.put(3, "Baz"); + studentsById.put(4, "Qux"); + + System.out.println("Students before shuffling:"); + System.out.println(studentsById.values()); + + List shuffledStudentIds = new ArrayList<>(studentsById.keySet()); + Collections.shuffle(shuffledStudentIds); + + List shuffledStudents = shuffledStudentIds.stream() + .map(id -> studentsById.get(id)) + .collect(Collectors.toList()); + + System.out.println("Students after shuffling"); + System.out.println(shuffledStudents); + } + + @Test + public void whenShufflingSet_thenElementsAreShuffled() { + Set students = new HashSet<>(Arrays.asList("Foo", "Bar", "Baz", "Qux")); + + System.out.println("Set before shuffling:"); + System.out.println(students); + + List studentList = new ArrayList<>(students); + + Collections.shuffle(studentList); + System.out.println("Shuffled set elements:"); + System.out.println(studentList); + } + + @Test + public void whenShufflingWithSameRandomness_thenElementsAreShuffledDeterministically() { + List students_1 = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + List students_2 = Arrays.asList("Foo", "Bar", "Baz", "Qux"); + + Collections.shuffle(students_1, new Random(5)); + Collections.shuffle(students_2, new Random(5)); + + assertThat(students_1).isEqualTo(students_2); + } +} From 53529f0c50e1d56af366b972bc75cb65e4f903f6 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 16 Feb 2018 21:35:11 +0200 Subject: [PATCH 094/102] fix spring-cloud-gateway project setup, update boot version --- .../gateway-service/pom.xml | 79 ------------------- spring-cloud/spring-cloud-gateway/pom.xml | 61 ++++++++++++-- .../spring/cloud/GatewayApplication.java | 0 .../src/main/resources/application.yml | 0 4 files changed, 55 insertions(+), 85 deletions(-) delete mode 100644 spring-cloud/spring-cloud-gateway/gateway-service/pom.xml rename spring-cloud/spring-cloud-gateway/{gateway-service => }/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java (100%) rename spring-cloud/spring-cloud-gateway/{gateway-service => }/src/main/resources/application.yml (100%) diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml b/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml deleted file mode 100644 index 14cde4901a..0000000000 --- a/spring-cloud/spring-cloud-gateway/gateway-service/pom.xml +++ /dev/null @@ -1,79 +0,0 @@ - - 4.0.0 - - gateway-service - 1.0.0-SNAPSHOT - jar - - Spring Cloud Gateway Service - - - com.baeldung.spring.cloud - spring-cloud-gateway - 1.0.0-SNAPSHOT - .. - - - - 2.0.0.M2 - - - - - org.springframework.boot - spring-boot-actuator - ${version} - - - org.springframework.boot - spring-boot-starter-webflux - ${version} - - - org.springframework.cloud - spring-cloud-gateway-core - ${version} - - - org.springframework.cloud - spring-cloud-starter-eureka - ${version} - - - org.hibernate - hibernate-validator-cdi - 6.0.2.Final - - - javax.validation - validation-api - 2.0.0.Final - - - io.projectreactor.ipc - reactor-netty - 0.7.0.M1 - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/pom.xml b/spring-cloud/spring-cloud-gateway/pom.xml index 5142b25400..90737f369d 100644 --- a/spring-cloud/spring-cloud-gateway/pom.xml +++ b/spring-cloud/spring-cloud-gateway/pom.xml @@ -4,13 +4,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.spring.cloud spring-cloud-gateway - 1.0.0-SNAPSHOT - - gateway-service - - pom + jar Spring Cloud Gateway @@ -25,7 +20,61 @@ UTF-8 3.7.0 1.4.2.RELEASE + 2.0.0.M6 + + + + org.springframework.boot + spring-boot-actuator + ${version} + + + org.springframework.boot + spring-boot-starter-webflux + ${version} + + + org.springframework.cloud + spring-cloud-gateway-core + ${version} + + + + org.hibernate + hibernate-validator-cdi + 6.0.2.Final + + + javax.validation + validation-api + 2.0.0.Final + + + io.projectreactor.ipc + reactor-netty + 0.7.0.M1 + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java similarity index 100% rename from spring-cloud/spring-cloud-gateway/gateway-service/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java rename to spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/spring/cloud/GatewayApplication.java diff --git a/spring-cloud/spring-cloud-gateway/gateway-service/src/main/resources/application.yml b/spring-cloud/spring-cloud-gateway/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-gateway/gateway-service/src/main/resources/application.yml rename to spring-cloud/spring-cloud-gateway/src/main/resources/application.yml From 8af29e8df5729ae1d4b9c3eb3b3d306741427f2a Mon Sep 17 00:00:00 2001 From: Magdalena Krause Date: Fri, 16 Feb 2018 16:54:04 -0300 Subject: [PATCH 095/102] BAEL-1543: Logging to console instead of file. --- spring-batch/src/main/resources/logback.xml | 23 +++++---------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/spring-batch/src/main/resources/logback.xml b/spring-batch/src/main/resources/logback.xml index dc9218caf3..b110d1c226 100644 --- a/spring-batch/src/main/resources/logback.xml +++ b/spring-batch/src/main/resources/logback.xml @@ -1,33 +1,20 @@ - - C:\Users\admin\Desktop\Baeldung\repo\magkrause\tutorials\spring-batch\src\main\resources\taskletsvschunks\logs.log - + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n - - - - - ${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log - - - 10MB - - - + - + - + \ No newline at end of file From 1372a565e2fba6a8a03e5fb427007fdb45488183 Mon Sep 17 00:00:00 2001 From: Alex Vargas Date: Fri, 16 Feb 2018 12:01:12 -0800 Subject: [PATCH 096/102] Adding solidity example file --- libraries/src/main/resources/Greeting.sol | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 libraries/src/main/resources/Greeting.sol diff --git a/libraries/src/main/resources/Greeting.sol b/libraries/src/main/resources/Greeting.sol new file mode 100644 index 0000000000..dde7b36cc3 --- /dev/null +++ b/libraries/src/main/resources/Greeting.sol @@ -0,0 +1,19 @@ +pragma solidity ^0.4.0; + +contract Greeting { + address creator; + string message; + + function Greeting(string _message) { + message = _message; + creator = msg.sender; + } + + function greet() constant returns (string) { + return message; + } + + function setGreeting(string _message) { + message = _message; + } +} \ No newline at end of file From 5d4bfa2e73e13eb0f889dbff1f42d8977aa38439 Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Sat, 17 Feb 2018 01:02:20 +0200 Subject: [PATCH 097/102] renaming the DSL to configurer --- ...rorLoggingDsl.java => ClientErrorLoggingConfigurer.java} | 6 +++--- .../src/main/java/com/baeldung/dsl/SecurityConfig.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) rename spring-5-security/src/main/java/com/baeldung/dsl/{ClientErrorLoggingDsl.java => ClientErrorLoggingConfigurer.java} (76%) diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java similarity index 76% rename from spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java rename to spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java index 6c7c0d2717..5a9479b664 100644 --- a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingConfigurer.java @@ -7,15 +7,15 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; -public class ClientErrorLoggingDsl extends AbstractHttpConfigurer { +public class ClientErrorLoggingConfigurer extends AbstractHttpConfigurer { private List errorCodes; - public ClientErrorLoggingDsl(List errorCodes) { + public ClientErrorLoggingConfigurer(List errorCodes) { this.errorCodes = errorCodes; } - public ClientErrorLoggingDsl() { + public ClientErrorLoggingConfigurer() { } diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java index 4494aaa131..382e222f64 100644 --- a/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java +++ b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java @@ -25,8 +25,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { } @Bean - public ClientErrorLoggingDsl clientErrorLogging() { - return new ClientErrorLoggingDsl(); + public ClientErrorLoggingConfigurer clientErrorLogging() { + return new ClientErrorLoggingConfigurer(); } @Override From 414d805f6b854cc772a8232cba882eac4d3deae9 Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Sat, 17 Feb 2018 01:03:29 +0200 Subject: [PATCH 098/102] renaming the DSL to configurer --- ...omDslApplication.java => CustomConfigurerApplication.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename spring-5-security/src/main/java/com/baeldung/dsl/{CustomDslApplication.java => CustomConfigurerApplication.java} (66%) diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java b/spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java similarity index 66% rename from spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java rename to spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java index 3e58bccaf4..2cd3d7fc7f 100644 --- a/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java +++ b/spring-5-security/src/main/java/com/baeldung/dsl/CustomConfigurerApplication.java @@ -4,10 +4,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class CustomDslApplication { +public class CustomConfigurerApplication { public static void main(String[] args) { - SpringApplication.run(CustomDslApplication.class, args); + SpringApplication.run(CustomConfigurerApplication.class, args); } } From b219245e5b22110d555ec44b9db2da5c4d4ca12e Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Sat, 17 Feb 2018 07:39:35 +0530 Subject: [PATCH 099/102] Gurinder spring cloud contract (#3547) * adding producer side sample for spring-cloud-contract * adding consumer side sample for spring-cloud-contract * removing un neccessary code * adding latest version for spring-cloud-contract in both producer and consumer * adding producer dependency in consumer * refactoring after review-1 * refactoring after review-2 * refactoring after review-3 --- spring-cloud/pom.xml | 3 +- spring-cloud/spring-cloud-contract/pom.xml | 23 ++++++ .../spring-cloud-contract-consumer/pom.xml | 68 +++++++++++++++++ ...pringCloudContractConsumerApplication.java | 18 +++++ .../controller/BasicMathController.java | 31 ++++++++ .../src/main/resources/application.yml | 0 .../BasicMathControllerIntegrationTest.java | 44 +++++++++++ .../spring-cloud-contract-producer/pom.xml | 74 +++++++++++++++++++ ...pringCloudContractProducerApplication.java | 11 +++ .../controller/EvenOddController.java | 15 ++++ .../src/main/resources/application.properties | 0 .../BaseTestClass.java | 29 ++++++++ ...uldReturnEvenWhenRequestParamIsEven.groovy | 17 +++++ ...houldReturnOddWhenRequestParamIsOdd.groovy | 17 +++++ 14 files changed, 349 insertions(+), 1 deletion(-) create mode 100644 spring-cloud/spring-cloud-contract/pom.xml create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/resources/application.yml create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/resources/application.properties create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy create mode 100644 spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index d94b334bc8..c093b87be3 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -21,7 +21,8 @@ spring-cloud-aws spring-cloud-consul spring-cloud-zuul-eureka-integration - + spring-cloud-contract + pom spring-cloud diff --git a/spring-cloud/spring-cloud-contract/pom.xml b/spring-cloud/spring-cloud-contract/pom.xml new file mode 100644 index 0000000000..3981aae2ac --- /dev/null +++ b/spring-cloud/spring-cloud-contract/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + + com.baeldung.spring.cloud + spring-cloud + 1.0.0-SNAPSHOT + + + spring-cloud-contract-producer + spring-cloud-contract-consumer + + pom + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml new file mode 100644 index 0000000000..67fea178eb --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-contract-consumer + 1.0.0-SNAPSHOT + jar + + spring-cloud-contract-consumer + Spring Cloud Consumer Sample + + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.cloud + spring-cloud-contract-wiremock + 1.2.2.RELEASE + test + + + org.springframework.cloud + spring-cloud-contract-stub-runner + 1.2.2.RELEASE + test + + + org.springframework.boot + spring-boot-starter-web + 1.5.9.RELEASE + + + org.springframework.boot + spring-boot-starter-data-rest + 1.5.9.RELEASE + + + com.baeldung.spring.cloud + spring-cloud-contract-producer + 1.0.0-SNAPSHOT + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java new file mode 100644 index 0000000000..7383ae5afa --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/SpringCloudContractConsumerApplication.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +public class SpringCloudContractConsumerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringCloudContractConsumerApplication.class, args); + } + + @Bean + RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java new file mode 100644 index 0000000000..f164af89e6 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathController.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer.controller; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + + +@RestController +public class BasicMathController { + + @Autowired + private RestTemplate restTemplate; + + @GetMapping("/calculate") + public String checkOddAndEven(@RequestParam("number") String number) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Content-Type", "application/json"); + + ResponseEntity responseEntity = restTemplate.exchange( + "http://localhost:8090/validate/prime-number?number=" + number, + HttpMethod.GET, + new HttpEntity<>(httpHeaders), + String.class); + + return responseEntity.getBody(); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/resources/application.yml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/main/resources/application.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java new file mode 100644 index 0000000000..5cf5c6d3b8 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-consumer/src/test/java/com/baeldung/spring/cloud/springcloudcontractconsumer/controller/BasicMathControllerIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.cloud.springcloudcontractconsumer.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) +@AutoConfigureMockMvc +@AutoConfigureJsonTesters +@AutoConfigureStubRunner(workOffline = true, + ids = "com.baeldung.spring.cloud:spring-cloud-contract-producer:+:stubs:8090") +public class BasicMathControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void given_WhenPassEvenNumberInQueryParam_ThenReturnEven() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/calculate?number=2") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("Even")); + } + + @Test + public void given_WhenPassOddNumberInQueryParam_ThenReturnOdd() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/calculate?number=1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string("Odd")); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml new file mode 100644 index 0000000000..ac27dbb645 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-contract-producer + 1.0.0-SNAPSHOT + jar + + spring-cloud-contract-producer + Spring Cloud Producer Sample + + + com.baeldung.spring.cloud + spring-cloud-contract + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + UTF-8 + 1.8 + Edgware.SR1 + + + + + org.springframework.cloud + spring-cloud-starter-contract-verifier + test + + + org.springframework.boot + spring-boot-starter-web + 1.5.9.RELEASE + + + org.springframework.boot + spring-boot-starter-data-rest + 1.5.9.RELEASE + + + + + + org.springframework.cloud + spring-cloud-contract-maven-plugin + 1.2.2.RELEASE + true + + com.baeldung.spring.cloud.springcloudcontractproducer.BaseTestClass + + + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java new file mode 100644 index 0000000000..770c68c817 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/SpringCloudContractProducerApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringCloudContractProducerApplication { + public static void main(String[] args) { + SpringApplication.run(SpringCloudContractProducerApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java new file mode 100644 index 0000000000..e61cc1120c --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/java/com/baeldung/spring/cloud/springcloudcontractproducer/controller/EvenOddController.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer.controller; + + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class EvenOddController { + + @GetMapping("/validate/prime-number") + public String isNumberPrime(@RequestParam("number") String number) { + return Integer.parseInt(number) % 2 == 0 ? "Even" : "Odd"; + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/resources/application.properties b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java new file mode 100644 index 0000000000..253924b247 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/java/com/baeldung/spring/cloud/springcloudcontractproducer/BaseTestClass.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.cloud.springcloudcontractproducer; + +import com.baeldung.spring.cloud.springcloudcontractproducer.controller.EvenOddController; +import io.restassured.module.mockmvc.RestAssuredMockMvc; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureMessageVerifier; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) +@DirtiesContext +@AutoConfigureMessageVerifier +public class BaseTestClass { + + @Autowired + private EvenOddController evenOddController; + + @Before + public void setup() { + StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(evenOddController); + RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder); + } +} diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy new file mode 100644 index 0000000000..78c36d7334 --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnEvenWhenRequestParamIsEven.groovy @@ -0,0 +1,17 @@ +import org.springframework.cloud.contract.spec.Contract + +Contract.make { + description "should return even when number input is even" + request { + method GET() + url("/validate/prime-number") { + queryParameters { + parameter("number", "2") + } + } + } + response { + body("Even") + status 200 + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy new file mode 100644 index 0000000000..215f987bbf --- /dev/null +++ b/spring-cloud/spring-cloud-contract/spring-cloud-contract-producer/src/test/resources/contracts/shouldReturnOddWhenRequestParamIsOdd.groovy @@ -0,0 +1,17 @@ +import org.springframework.cloud.contract.spec.Contract + +Contract.make { + description "should return odd when number input is odd" + request { + method GET() + url("/validate/prime-number") { + queryParameters { + parameter("number", "1") + } + } + } + response { + body("Odd") + status 200 + } +} \ No newline at end of file From 2a127b3df3e9225b5e8eab5d1bf1b124a7326fff Mon Sep 17 00:00:00 2001 From: Andrea Ligios Date: Sat, 17 Feb 2018 21:40:01 +0100 Subject: [PATCH 100/102] . (#3687) --- .../DecimalFormatExamplesTest.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java diff --git a/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java new file mode 100644 index 0000000000..8acd4e023e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/decimalformat/DecimalFormatExamplesTest.java @@ -0,0 +1,116 @@ +package com.baeldung.decimalformat; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; + +import org.junit.Test; + +public class DecimalFormatExamplesTest { + + double d = 1234567.89; + + @Test + public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1234567.89"); + + assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("001234567.890"); + + } + + @Test + public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() { + + assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9"); + + assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568"); + + } + + @Test + public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() { + + assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.9"); + + assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,568"); + + } + + @Test + public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The 1234568 number"); + + assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("The # 1234568 number"); + + } + + @Test + public void givenLocales_WhenFormatting_ThenCorrectOutput() { + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1,234,567.89"); + + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d)) + .isEqualTo("1.234.567,89"); + + assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d)) + .isEqualTo("1.234.567,89"); + + } + + @Test + public void givenE_WhenFormatting_ThenScientificNotation() { + + assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("12.3456789E5"); + + assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("123.456789E4"); + + assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) + .isEqualTo("1.23456789E6"); + + } + + @Test + public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException { + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89")) + .isEqualTo(1234567.89); + + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89")) + .isEqualTo(1234567.89); + + } + + @Test + public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException { + + NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)); + ((DecimalFormat) nf).setParseBigDecimal(true); + assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89)); + } + +} From 1b0e859d3a85970462c7daefc646e37155b87c0b Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Sun, 18 Feb 2018 00:08:19 +0100 Subject: [PATCH 101/102] BAEL-1273: RSS improvements with a custom model (#3665) * BAEL-1216: improve tests * BAEL-1448: Update Spring 5 articles to use the release version * Setting up the Maven Wrapper on a maven project * Add Maven Wrapper on spring-boot module * simple add * BAEL-976: Update spring version * BAEL-1273: Display RSS feed with spring mvc (AbstractRssFeedView) * Move RSS feed with Spring MVC from spring-boot to spring-mvc-simple * BAEL-1285: Update Jackson articles * BAEL-1273: implement both MVC and Rest approach to serve RSS content * RSS(XML & Json) with a custom model * BAEL-1273: remove a resource --- spring-mvc-simple/pom.xml | 24 ++++++++ .../ApplicationConfiguration.java | 30 ++++++++-- .../spring/controller/rss/ArticleFeed.java | 27 +++++++++ .../spring/controller/rss/ArticleItem.java | 25 +++++++++ .../controller/rss/ArticleRssController.java | 30 ++++------ .../spring/controller/rss/RssData.java | 56 +++++++++++++++++++ 6 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java diff --git a/spring-mvc-simple/pom.xml b/spring-mvc-simple/pom.xml index 595e58f5f3..e722573ab1 100644 --- a/spring-mvc-simple/pom.xml +++ b/spring-mvc-simple/pom.xml @@ -31,9 +31,16 @@ 5.0.2 1.0.2 1.9.0 + 2.9.4 + 1.4.9 + + org.springframework + spring-oxm + 5.0.2.RELEASE + javax.servlet javax.servlet-api @@ -121,6 +128,23 @@ rome ${rome.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.thoughtworks.xstream + xstream + ${xstream.version} + diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index 69c45d90b3..7f1182bb50 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,26 +1,33 @@ package com.baeldung.spring.configuration; import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; +import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.accept.ContentNegotiationManager; -import java.util.List; import java.util.ArrayList; +import java.util.List; @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) -class ApplicationConfiguration implements WebMvcConfigurer { +class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { @@ -49,4 +56,19 @@ class ApplicationConfiguration implements WebMvcConfigurer { multipartResolver.setMaxUploadSize(5242880); return multipartResolver; } + + @Override + public void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml(); + builder.indentOutput(true); + + XmlMapper xmlMapper = builder.createXmlMapper(true).build(); + xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + + converters.add(new RssChannelHttpMessageConverter()); + converters.add(new MappingJackson2HttpMessageConverter()); + converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper)); + + super.configureMessageConverters(converters); + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java new file mode 100644 index 0000000000..514c9a2353 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.controller.rss; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@JacksonXmlRootElement(localName="articles") +public class ArticleFeed extends RssData implements Serializable { + + @JacksonXmlElementWrapper(localName = "items", useWrapping = true) + private List items = new ArrayList(); + + public void addItem(ArticleItem articleItem) { + this.items.add(articleItem); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java new file mode 100644 index 0000000000..01b2cde1ba --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.controller.rss; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.io.Serializable; + +@JacksonXmlRootElement(localName="article") +public class ArticleItem extends RssData implements Serializable { + private String author; + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return "ArticleItem{" + + "author='" + author + '\'' + + '}'; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java index 8f23076e8e..77b8aceb73 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -14,46 +14,38 @@ import java.util.List; @Controller public class ArticleRssController { - @GetMapping(value = "/rssMvc") + @GetMapping(value = "/rss1") public String articleMvcFeed() { return "articleFeedView"; } - @GetMapping(value = "/rssRest", produces = "application/rss+xml") + @GetMapping(value = "/rss2") @ResponseBody - public String articleRestFeed() throws FeedException { - SyndFeed feed = new SyndFeedImpl(); - feed.setFeedType("rss_2.0"); + public ArticleFeed articleRestFeed2() { + ArticleFeed feed = new ArticleFeed(); feed.setLink("http://localhost:8080/spring-mvc-simple/rss"); feed.setTitle("Article Feed"); feed.setDescription("Article Feed Description"); feed.setPublishedDate(new Date()); - List list = new ArrayList(); - - SyndEntry item1 = new SyndEntryImpl(); + ArticleItem item1 = new ArticleItem(); item1.setLink("http://www.baeldung.com/netty-exception-handling"); item1.setTitle("Exceptions in Netty"); - SyndContent description1 = new SyndContentImpl(); - description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); - item1.setDescription(description1); + item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty."); item1.setPublishedDate(new Date()); item1.setAuthor("Carlos"); - SyndEntry item2 = new SyndEntryImpl(); + ArticleItem item2 = new ArticleItem(); item2.setLink("http://www.baeldung.com/cockroachdb-java"); item2.setTitle("Guide to CockroachDB in Java"); - SyndContent description2 = new SyndContentImpl(); - description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); - item2.setDescription(description2); + item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java."); item2.setPublishedDate(new Date()); item2.setAuthor("Baeldung"); - list.add(item1); - list.add(item2); - feed.setEntries(list); + feed.addItem(item1); + feed.addItem(item2); - return new SyndFeedOutput().outputString(feed); + return feed; } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java new file mode 100644 index 0000000000..258712eb2d --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.controller.rss; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class RssData implements Serializable { + private String link; + private String title; + private String description; + private String publishedDate; + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getPublishedDate() { + return publishedDate; + } + + public void setPublishedDate(Date publishedDate) { + DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + this.publishedDate = df.format(publishedDate); + } + + @Override + public String toString() { + return "RssData{" + + "link='" + link + '\'' + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + ", publishedDate=" + publishedDate + + '}'; + } +} From 5aa81d9be38c6f9398ff54406e9b953cfd3b8b51 Mon Sep 17 00:00:00 2001 From: Harsh Jain Date: Sun, 18 Feb 2018 12:19:44 +0530 Subject: [PATCH 102/102] BAEL-1526 : added exmaple unit test cases for string comparison --- .../baeldung/string/StringComparisonTest.java | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/string/StringComparisonTest.java diff --git a/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java new file mode 100644 index 0000000000..17b7f1f981 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/StringComparisonTest.java @@ -0,0 +1,154 @@ +package com.baeldung.string; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringComparisonTest { + + @Test + public void whenUsingComparisonOperator_ThenComparingStrings(){ + + String string1 = "using comparison operator"; + String string2 = "using comparison operator"; + String string3 = new String("using comparison operator"); + + assertThat(string1 == string2).isTrue(); + assertThat(string1 == string3).isFalse(); + } + + @Test + public void whenUsingEqualsMethod_ThenComparingStrings(){ + + String string1 = "using equals method"; + String string2 = "using equals method"; + + String string3 = "using EQUALS method"; + String string4 = new String("using equals method"); + + assertThat(string1.equals(string2)).isTrue(); + assertThat(string1.equals(string4)).isTrue(); + + assertThat(string1.equals(null)).isFalse(); + assertThat(string1.equals(string3)).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCase_ThenComparingStrings(){ + + String string1 = "using equals ignore case"; + String string2 = "USING EQUALS IGNORE CASE"; + + assertThat(string1.equalsIgnoreCase(string2)).isTrue(); + } + + @Test + public void whenUsingCompareTo_ThenComparingStrings(){ + + String Author = "author"; + String Book = "book"; + String duplicateBook = "book"; + + assertThat(Author.compareTo(Book)).isEqualTo(-1); + assertThat(Book.compareTo(Author)).isEqualTo(1); + assertThat(duplicateBook.compareTo(Book)).isEqualTo(0); + } + + @Test + public void whenUsingCompareToIgnoreCase_ThenComparingStrings(){ + + String Author = "Author"; + String Book = "book"; + String duplicateBook = "BOOK"; + + assertThat(Author.compareToIgnoreCase(Book)).isEqualTo(-1); + assertThat(Book.compareToIgnoreCase(Author)).isEqualTo(1); + assertThat(duplicateBook.compareToIgnoreCase(Book)).isEqualTo(0); + } + + @Test + public void whenUsingObjectsEqualsMethod_ThenComparingStrings(){ + + String string1 = "using objects equals"; + String string2 = "using objects equals"; + String string3 = new String("using objects equals"); + + assertThat(Objects.equals(string1, string2)).isTrue(); + assertThat(Objects.equals(string1, string3)).isTrue(); + + assertThat(Objects.equals(null, null)).isTrue(); + assertThat(Objects.equals(null, string1)).isFalse(); + } + + @Test + public void whenUsingEqualsOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equals(null, null)).isTrue(); + assertThat(StringUtils.equals(null, "equals method")).isFalse(); + assertThat(StringUtils.equals("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equals("equals method", "EQUALS METHOD")).isFalse(); + } + + @Test + public void whenUsingEqualsIgnoreCaseOfApacheCommons_ThenComparingStrings(){ + + assertThat(StringUtils.equalsIgnoreCase(null, null)).isTrue(); + assertThat(StringUtils.equalsIgnoreCase(null, "equals method")).isFalse(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method")).isTrue(); + assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD")).isTrue(); + } + + @Test + public void whenUsingEqualsAnyOf_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAny(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAny("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAny("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAny(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY")).isFalse(); + } + + @Test + public void whenUsingEqualsAnyIgnoreCase_ThenComparingStrings(){ + + assertThat(StringUtils.equalsAnyIgnoreCase(null, null, null)).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", "equals any", "any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase("equals any", null, "equals any")).isTrue(); + assertThat(StringUtils.equalsAnyIgnoreCase(null, "equals", "any")).isFalse(); + assertThat(StringUtils.equalsAnyIgnoreCase( + "equals any ignore case", "EQUALS ANY IGNORE CASE", "any")).isTrue(); + } + + @Test + public void whenUsingCompare_thenComparingStringsWithNulls(){ + + assertThat(StringUtils.compare(null, null)).isEqualTo(0); + assertThat(StringUtils.compare(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compare("abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compare("bbc", "abc")).isEqualTo(1); + assertThat(StringUtils.compare("abc", "abc")).isEqualTo(0); + } + + @Test + public void whenUsingCompareIgnoreCase_ThenComparingStringsWithNulls(){ + + assertThat(StringUtils.compareIgnoreCase(null, null)).isEqualTo(0); + assertThat(StringUtils.compareIgnoreCase(null, "abc")).isEqualTo(-1); + + assertThat(StringUtils.compareIgnoreCase("Abc", "bbc")).isEqualTo(-1); + assertThat(StringUtils.compareIgnoreCase("bbc", "ABC")).isEqualTo(1); + assertThat(StringUtils.compareIgnoreCase("abc", "ABC")).isEqualTo(0); + } + + @Test + public void whenUsingCompareWithNullIsLessOption_ThenComparingStrings(){ + + assertThat(StringUtils.compare(null, "abc", true)).isEqualTo(-1); + assertThat(StringUtils.compare(null, "abc", false)).isEqualTo(1); + } + +}