diff --git a/algorithms/README.md b/algorithms/README.md index 14ec7294e7..47ddd7028a 100644 --- a/algorithms/README.md +++ b/algorithms/README.md @@ -23,3 +23,4 @@ - [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic) - [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity) - [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element) +- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation) diff --git a/apache-avro/README.md b/apache-avro/README.md new file mode 100644 index 0000000000..32d84ecc76 --- /dev/null +++ b/apache-avro/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Guide to Apache Avro](http://www.baeldung.com/java-apache-avro) diff --git a/apache-cxf/pom.xml b/apache-cxf/pom.xml index 53d9d4054c..8918fd4450 100644 --- a/apache-cxf/pom.xml +++ b/apache-cxf/pom.xml @@ -1,5 +1,5 @@ + 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 apache-cxf @@ -17,6 +17,7 @@ cxf-spring cxf-jaxrs-implementation cxf-aegis + sse-jaxrs diff --git a/apache-cxf/sse-jaxrs/pom.xml b/apache-cxf/sse-jaxrs/pom.xml new file mode 100644 index 0000000000..d4b6c19d03 --- /dev/null +++ b/apache-cxf/sse-jaxrs/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + sse-jaxrs + pom + + + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + + + + sse-jaxrs-server + sse-jaxrs-client + + + diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-client/pom.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-client/pom.xml new file mode 100644 index 0000000000..0f5406fbc7 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-client/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + + com.baeldung + sse-jaxrs + 0.0.1-SNAPSHOT + + + sse-jaxrs-client + + + 3.2.0 + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + singleEvent + + java + + + com.baeldung.sse.jaxrs.client.SseClientApp + + + + broadcast + + java + + + com.baeldung.sse.jaxrs.client.SseClientBroadcastApp + + + + + + + + + + org.apache.cxf + cxf-rt-rs-client + ${cxf-version} + + + org.apache.cxf + cxf-rt-rs-sse + ${cxf-version} + + + + diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientApp.java b/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientApp.java new file mode 100644 index 0000000000..5d42b3a243 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientApp.java @@ -0,0 +1,48 @@ +package com.baeldung.sse.jaxrs.client; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.sse.InboundSseEvent; +import javax.ws.rs.sse.SseEventSource; +import java.util.function.Consumer; + +public class SseClientApp { + + private static final String url = "http://127.0.0.1:9080/sse-jaxrs-server/sse/stock/prices"; + + public static void main(String... args) throws Exception { + + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(url); + try (SseEventSource eventSource = SseEventSource.target(target).build()) { + + eventSource.register(onEvent, onError, onComplete); + eventSource.open(); + + //Consuming events for one hour + Thread.sleep(60 * 60 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + client.close(); + System.out.println("End"); + } + + // A new event is received + private static Consumer onEvent = (inboundSseEvent) -> { + String data = inboundSseEvent.readData(); + System.out.println(data); + }; + + //Error + private static Consumer onError = (throwable) -> { + throwable.printStackTrace(); + }; + + //Connection close and there is nothing to receive + private static Runnable onComplete = () -> { + System.out.println("Done!"); + }; + +} diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientBroadcastApp.java b/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientBroadcastApp.java new file mode 100644 index 0000000000..9afc187a6d --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/java/com/baeldung/sse/jaxrs/client/SseClientBroadcastApp.java @@ -0,0 +1,52 @@ +package com.baeldung.sse.jaxrs.client; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.sse.InboundSseEvent; +import javax.ws.rs.sse.SseEventSource; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class SseClientBroadcastApp { + + private static final String subscribeUrl = "http://localhost:9080/sse-jaxrs-server/sse/stock/subscribe"; + + + public static void main(String... args) throws Exception { + + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(subscribeUrl); + try (final SseEventSource eventSource = SseEventSource.target(target) + .reconnectingEvery(5, TimeUnit.SECONDS) + .build()) { + eventSource.register(onEvent, onError, onComplete); + eventSource.open(); + System.out.println("Wainting for incoming event ..."); + + //Consuming events for one hour + Thread.sleep(60 * 60 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + client.close(); + System.out.println("End"); + } + + // A new event is received + private static Consumer onEvent = (inboundSseEvent) -> { + String data = inboundSseEvent.readData(); + System.out.println(data); + }; + + //Error + private static Consumer onError = (throwable) -> { + throwable.printStackTrace(); + }; + + //Connection close and there is nothing to receive + private static Runnable onComplete = () -> { + System.out.println("Done!"); + }; + +} diff --git a/testing-modules/junit-abstract/src/main/resources/logback.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/resources/logback.xml similarity index 100% rename from testing-modules/junit-abstract/src/main/resources/logback.xml rename to apache-cxf/sse-jaxrs/sse-jaxrs-client/src/main/resources/logback.xml diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml new file mode 100644 index 0000000000..46572a2b75 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + + com.baeldung + sse-jaxrs + 0.0.1-SNAPSHOT + + + sse-jaxrs-server + war + + + 2.4.2 + false + 18.0.0.2 + + + + ${artifactId} + + + net.wasdev.wlp.maven.plugins + liberty-maven-plugin + ${liberty-maven-plugin.version} + + + io.openliberty + openliberty-webProfile8 + ${openliberty-version} + zip + + project + true + src/main/liberty/config/server.xml + + + + install-server + prepare-package + + install-server + create-server + install-feature + + + + install-apps + package + + install-apps + + + + + + + + + + + javax.ws.rs + javax.ws.rs-api + 2.1 + provided + + + javax.enterprise + cdi-api + 2.0 + provided + + + javax.json.bind + javax.json.bind-api + 1.0 + provided + + + + + diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/AppConfig.java b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/AppConfig.java new file mode 100644 index 0000000000..058d19f045 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/AppConfig.java @@ -0,0 +1,8 @@ +package com.baeldung.sse.jaxrs; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("sse") +public class AppConfig extends Application { +} diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/SseResource.java b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/SseResource.java new file mode 100644 index 0000000000..1f60168a1b --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/SseResource.java @@ -0,0 +1,119 @@ +package com.baeldung.sse.jaxrs; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.sse.OutboundSseEvent; +import javax.ws.rs.sse.Sse; +import javax.ws.rs.sse.SseBroadcaster; +import javax.ws.rs.sse.SseEventSink; + +@ApplicationScoped +@Path("stock") +public class SseResource { + + @Inject + private StockService stockService; + + private Sse sse; + private SseBroadcaster sseBroadcaster; + private OutboundSseEvent.Builder eventBuilder; + + @Context + public void setSse(Sse sse) { + this.sse = sse; + this.eventBuilder = sse.newEventBuilder(); + this.sseBroadcaster = sse.newBroadcaster(); + } + + @GET + @Path("prices") + @Produces("text/event-stream") + public void getStockPrices(@Context SseEventSink sseEventSink, + @HeaderParam(HttpHeaders.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastReceivedId) { + + int lastEventId = 1; + if (lastReceivedId != -1) { + lastEventId = ++lastReceivedId; + } + boolean running = true; + while (running) { + Stock stock = stockService.getNextTransaction(lastEventId); + if (stock != null) { + OutboundSseEvent sseEvent = this.eventBuilder + .name("stock") + .id(String.valueOf(lastEventId)) + .mediaType(MediaType.APPLICATION_JSON_TYPE) + .data(Stock.class, stock) + .reconnectDelay(3000) + .comment("price change") + .build(); + sseEventSink.send(sseEvent); + lastEventId++; + } + //Simulate connection close + if (lastEventId % 5 == 0) { + sseEventSink.close(); + break; + } + + try { + //Wait 5 seconds + Thread.sleep(5 * 1000); + } catch (InterruptedException ex) { + // ... + } + //Simulatae a while boucle break + running = lastEventId <= 2000; + } + sseEventSink.close(); + } + + @GET + @Path("subscribe") + @Produces(MediaType.SERVER_SENT_EVENTS) + public void listen(@Context SseEventSink sseEventSink) { + sseEventSink.send(sse.newEvent("Welcome !")); + this.sseBroadcaster.register(sseEventSink); + sseEventSink.send(sse.newEvent("You are registred !")); + } + + @GET + @Path("publish") + public void broadcast() { + Runnable r = new Runnable() { + @Override + public void run() { + int lastEventId = 0; + boolean running = true; + while (running) { + lastEventId++; + Stock stock = stockService.getNextTransaction(lastEventId); + if (stock != null) { + OutboundSseEvent sseEvent = eventBuilder + .name("stock") + .id(String.valueOf(lastEventId)) + .mediaType(MediaType.APPLICATION_JSON_TYPE) + .data(Stock.class, stock) + .reconnectDelay(3000) + .comment("price change") + .build(); + sseBroadcaster.broadcast(sseEvent); + } + try { + //Wait 5 seconds + Thread.currentThread().sleep(5 * 1000); + } catch (InterruptedException ex) { + // ... + } + //Simulatae a while boucle break + running = lastEventId <= 2000; + } + } + }; + new Thread(r).start(); + } +} diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/Stock.java b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/Stock.java new file mode 100644 index 0000000000..a186b32771 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/Stock.java @@ -0,0 +1,50 @@ +package com.baeldung.sse.jaxrs; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +public class Stock { + private Integer id; + private String name; + private BigDecimal price; + LocalDateTime dateTime; + + public Stock(Integer id, String name, BigDecimal price, LocalDateTime dateTime) { + this.id = id; + this.name = name; + this.price = price; + this.dateTime = dateTime; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public LocalDateTime getDateTime() { + return dateTime; + } + + public void setDateTime(LocalDateTime dateTime) { + this.dateTime = dateTime; + } +} diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/StockService.java b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/StockService.java new file mode 100644 index 0000000000..15818ead5d --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/java/com/baeldung/sse/jaxrs/StockService.java @@ -0,0 +1,78 @@ +package com.baeldung.sse.jaxrs; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Initialized; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; + +@ApplicationScoped +@Named +public class StockService { + + private static final BigDecimal UP = BigDecimal.valueOf(1.05f); + private static final BigDecimal DOWN = BigDecimal.valueOf(0.95f); + + List stockNames = Arrays.asList("GOOG", "IBM", "MS", "GOOG", "YAHO"); + List stocksDB = new ArrayList<>(); + private AtomicInteger counter = new AtomicInteger(0); + + public void init(@Observes @Initialized(ApplicationScoped.class) Object init) { + //Open price + System.out.println("@Start Init ..."); + stockNames.forEach(stockName -> { + stocksDB.add(new Stock(counter.incrementAndGet(), stockName, generateOpenPrice(), LocalDateTime.now())); + }); + + Runnable runnable = new Runnable() { + @Override + public void run() { + //Simulate Change price and put every x seconds + while (true) { + int indx = new Random().nextInt(stockNames.size()); + String stockName = stockNames.get(indx); + BigDecimal price = getLastPrice(stockName); + BigDecimal newprice = changePrice(price); + Stock stock = new Stock(counter.incrementAndGet(), stockName, newprice, LocalDateTime.now()); + stocksDB.add(stock); + + int r = new Random().nextInt(30); + try { + Thread.currentThread().sleep(r*1000); + } catch (InterruptedException ex) { + // ... + } + } + } + }; + new Thread(runnable).start(); + System.out.println("@End Init ..."); + } + + public Stock getNextTransaction(Integer lastEventId) { + return stocksDB.stream().filter(s -> s.getId().equals(lastEventId)).findFirst().orElse(null); + } + + BigDecimal generateOpenPrice() { + float min = 70; + float max = 120; + return BigDecimal.valueOf(min + new Random().nextFloat() * (max - min)).setScale(4,RoundingMode.CEILING); + } + + BigDecimal changePrice(BigDecimal price) { + return Math.random() >= 0.5 ? price.multiply(UP).setScale(4,RoundingMode.CEILING) : price.multiply(DOWN).setScale(4,RoundingMode.CEILING); + } + + private BigDecimal getLastPrice(String stockName) { + return stocksDB.stream().filter(stock -> stock.getName().equals(stockName)).findFirst().get().getPrice(); + } +} diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/liberty/config/server.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/liberty/config/server.xml new file mode 100644 index 0000000000..9bf66d7795 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/liberty/config/server.xml @@ -0,0 +1,7 @@ + + + jaxrs-2.1 + cdi-2.0 + + + diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/META-INF/beans.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..4f0b3cdeeb --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/META-INF/beans.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/logback.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/WEB-INF/web.xml b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..b4b8121fdd --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,11 @@ + + + Hello Servlet + + + index.html + + + diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/index.html b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/index.html new file mode 100644 index 0000000000..9015a7a32c --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/index.html @@ -0,0 +1 @@ +index diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse-broadcast.html b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse-broadcast.html new file mode 100644 index 0000000000..5a46e2a5d3 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse-broadcast.html @@ -0,0 +1,38 @@ + + + + Server-Sent Event Broadcasting + + +

Stock prices :

+
+
    +
+
+ + + \ No newline at end of file diff --git a/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse.html b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse.html new file mode 100644 index 0000000000..8fddae4717 --- /dev/null +++ b/apache-cxf/sse-jaxrs/sse-jaxrs-server/src/main/webapp/sse.html @@ -0,0 +1,38 @@ + + + + Server-Sent Event + + +

Stock prices :

+
+
    +
+
+ + + \ No newline at end of file diff --git a/apache-shiro/pom.xml b/apache-shiro/pom.xml index 9e11f6948f..3a72804ab1 100644 --- a/apache-shiro/pom.xml +++ b/apache-shiro/pom.xml @@ -8,9 +8,10 @@ 1.0-SNAPSHOT - org.springframework.boot - spring-boot-starter-parent - 1.5.2.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/cas/cas-secured-app/pom.xml b/cas/cas-secured-app/pom.xml index 98213b2b78..22d1522fbe 100644 --- a/cas/cas-secured-app/pom.xml +++ b/cas/cas-secured-app/pom.xml @@ -10,10 +10,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.13.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 diff --git a/core-java-10/README.md b/core-java-10/README.md index 863448c194..84fa381a26 100644 --- a/core-java-10/README.md +++ b/core-java-10/README.md @@ -3,3 +3,4 @@ - [Java 10 LocalVariable Type-Inference](http://www.baeldung.com/java-10-local-variable-type-inference) - [Guide to Java 10](http://www.baeldung.com/java-10-overview) +- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) diff --git a/core-java-8/README.md b/core-java-8/README.md index e69641b25c..6f31a8b886 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -56,4 +56,7 @@ - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) - [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string) - [Calculate Age in Java](http://www.baeldung.com/java-get-age) +- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) +- [Increment Date in Java](http://www.baeldung.com/java-increment-date) - [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date) +- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) diff --git a/core-java-8/src/test/java/com/baeldung/datetime/UseTimeZoneUnitTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UseTimeZoneUnitTest.java new file mode 100644 index 0000000000..4423ac6ce0 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UseTimeZoneUnitTest.java @@ -0,0 +1,72 @@ +package com.baeldung.datetime; + +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.junit.Assert; +import org.junit.Test; + +public class UseTimeZoneUnitTest { + + /* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */ + + String timeZone = "Asia/Singapore"; + + private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a"; + + @Test + public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() { + Date nowUtc = new Date(); + TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone); + + Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore); + nowAsiaSingapore.setTime(nowUtc); + + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone)); + + System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone() + .getID(), simpleDateFormat.format(nowAsiaSingapore.getTime()))); + + Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond()); + Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone()); + + } + + @Test + public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() { + Instant nowUtc = Instant.now(); + ZoneId asiaSingapore = ZoneId.of(timeZone); + + ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore); + + System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(), + nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN)))); + + Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond()); + Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone()); + } + + @Test + public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() { + org.joda.time.Instant nowUtc = org.joda.time.Instant.now(); + DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone); + + DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore); + + System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(), + nowAsiaSingapore.toString(PATTERN))); + + Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis()); + Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone()); + } + +} diff --git a/core-java-collections/README.md b/core-java-collections/README.md index 7b3745b486..8f5dd137ed 100644 --- a/core-java-collections/README.md +++ b/core-java-collections/README.md @@ -31,3 +31,8 @@ - [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys) - [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size) - [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards) +- [How to Filter a Collection in Java](http://www.baeldung.com/java-collection-filtering) +- [Add Multiple Items to an Java ArrayList](http://www.baeldung.com/java-add-items-array-list) +- [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list) +- [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map) +- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset) diff --git a/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java new file mode 100644 index 0000000000..2405c26aac --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java @@ -0,0 +1,19 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Collection; +import java.util.stream.Stream; +import static org.apache.commons.collections4.CollectionUtils.emptyIfNull; + +public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull { + + /** + * This method shows how to make a null safe stream from a collection through the use of + * emptyIfNull() method from Apache Commons CollectionUtils library + * + * @param collection The collection that is to be converted into a stream + * @return The stream that has been created from the collection or an empty stream if the collection is null + */ + public Stream collectionAsStream(Collection collection) { + return emptyIfNull(collection).stream(); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java new file mode 100644 index 0000000000..da767d4563 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java @@ -0,0 +1,21 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Collection; +import java.util.Optional; +import java.util.stream.Stream; + +public class NullSafeCollectionStreamsUsingJava8OptionalContainer { + + /** + * This method shows how to make a null safe stream from a collection through the use of + * Java SE 8’s Optional Container + * + * @param collection The collection that is to be converted into a stream + * @return The stream that has been created from the collection or an empty stream if the collection is null + */ + public Stream collectionAsStream(Collection collection) { + return Optional.ofNullable(collection) + .map(Collection::stream) + .orElseGet(Stream::empty); + } +} \ No newline at end of file diff --git a/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java new file mode 100644 index 0000000000..0c10f1cebc --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java @@ -0,0 +1,19 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Collection; +import java.util.stream.Stream; + +public class NullSafeCollectionStreamsUsingNullDereferenceCheck { + + /** + * This method shows how to make a null safe stream from a collection through the use of a check + * to prevent null dereferences + * + * @param collection The collection that is to be converted into a stream + * @return The stream that has been created from the collection or an empty stream if the collection is null + */ + public Stream collectionAsStream(Collection collection) { + return collection == null ? Stream.empty() : collection.stream(); + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/collection/CollectionsEmpty.java b/core-java-collections/src/test/java/com/baeldung/collection/CollectionsEmpty.java new file mode 100644 index 0000000000..86a262107d --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/collection/CollectionsEmpty.java @@ -0,0 +1,26 @@ +package com.baeldung.collection; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import org.junit.Assert; +import org.junit.Test; + +public class CollectionsEmpty { + + @Test + public void givenArrayList_whenAddingElement_addsNewElement() { + ArrayList mutableList = new ArrayList<>(); + mutableList.add("test"); + + Assert.assertEquals(mutableList.size(), 1); + Assert.assertEquals(mutableList.get(0), "test"); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenCollectionsEmptyList_whenAddingElement_throwsUnsupportedOperationException() { + List immutableList = Collections.emptyList(); + immutableList.add("test"); + } + +} \ No newline at end of file diff --git a/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java new file mode 100644 index 0000000000..875045946d --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.stream.Stream; +import org.junit.Test; +import static org.junit.Assert.*; + +public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest { + + private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance = + new NullSafeCollectionStreamsUsingCommonsEmptyIfNull(); + @Test + public void whenCollectionIsNull_thenExpectAnEmptyStream() { + Collection collection = null; + Stream expResult = Stream.empty(); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + @Test + public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { + Collection collection = Arrays.asList("a", "b", "c"); + Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + private static void assertStreamEquals(Stream s1, Stream s2) { + Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); + while (iter1.hasNext() && iter2.hasNext()) + assertEquals(iter1.next(), iter2.next()); + assert !iter1.hasNext() && !iter2.hasNext(); + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java new file mode 100644 index 0000000000..402f1a6a19 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.stream.Stream; +import org.junit.Test; +import static org.junit.Assert.*; + +public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest { + + private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance = + new NullSafeCollectionStreamsUsingJava8OptionalContainer(); + @Test + public void whenCollectionIsNull_thenExpectAnEmptyStream() { + Collection collection = null; + Stream expResult = Stream.empty(); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + @Test + public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { + Collection collection = Arrays.asList("a", "b", "c"); + Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + private static void assertStreamEquals(Stream s1, Stream s2) { + Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); + while (iter1.hasNext() && iter2.hasNext()) + assertEquals(iter1.next(), iter2.next()); + assert !iter1.hasNext() && !iter2.hasNext(); + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java new file mode 100644 index 0000000000..bb6152371d --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.nullsafecollectionstreams; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.stream.Stream; +import org.junit.Test; +import static org.junit.Assert.*; + +public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest { + + private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance = + new NullSafeCollectionStreamsUsingNullDereferenceCheck(); + + @Test + public void whenCollectionIsNull_thenExpectAnEmptyStream() { + Collection collection = null; + Stream expResult = Stream.empty(); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + @Test + public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { + Collection collection = Arrays.asList("a", "b", "c"); + Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); + Stream result = instance.collectionAsStream(collection); + assertStreamEquals(expResult, result); + } + + private static void assertStreamEquals(Stream s1, Stream s2) { + Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); + while (iter1.hasNext() && iter2.hasNext()) + assertEquals(iter1.next(), iter2.next()); + assert !iter1.hasNext() && !iter2.hasNext(); + } + +} diff --git a/core-java-concurrency/README.md b/core-java-concurrency/README.md index 8192a8e301..a16d885b0c 100644 --- a/core-java-concurrency/README.md +++ b/core-java-concurrency/README.md @@ -38,3 +38,4 @@ - [Priority-based Job Scheduling in Java](http://www.baeldung.com/java-priority-job-schedule) - [A Custom Spring SecurityConfigurer](http://www.baeldung.com/spring-security-custom-configurer) - [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle) +- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable) diff --git a/core-java-sun/pom.xml b/core-java-sun/pom.xml index f489f3b030..7292335232 100644 --- a/core-java-sun/pom.xml +++ b/core-java-sun/pom.xml @@ -303,7 +303,7 @@ 1.7.0 - 2.19.1 + 2.21.0 1.8.0 3.0.2 diff --git a/core-java/README.md b/core-java/README.md index e22ee505ba..f05b2625e8 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -168,3 +168,6 @@ - [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream) - [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object) - [Exception Handling in Java](http://www.baeldung.com/java-exceptions) +- [Guide to Java Instrumentation](http://www.baeldung.com/java-instrumentation) +- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type) +- [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) diff --git a/core-java/pom.xml b/core-java/pom.xml index b83cb478d4..3f44851f97 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -530,7 +530,7 @@ 3.10.0 - 2.19.1 + 2.21.0 4.3.4.RELEASE 1.5.8.RELEASE diff --git a/core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java b/core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java new file mode 100644 index 0000000000..45ff486a79 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/BufferedReaderExample.java @@ -0,0 +1,24 @@ +package com.baeldung.fileparser; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class BufferedReaderExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + + while (br.ready()) { + result.add(br.readLine()); + } + return result; + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java b/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java new file mode 100644 index 0000000000..f9dd2a9ec5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/FileReaderExample.java @@ -0,0 +1,31 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class FileReaderExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (FileReader f = new FileReader(filename)) { + StringBuffer sb = new StringBuffer(); + while (f.ready()) { + char c = (char) f.read(); + if (c == '\n') { + result.add(sb.toString()); + sb = new StringBuffer(); + } else { + sb.append(c); + } + } + if (sb.length() > 0) { + result.add(sb.toString()); + } + } + + return result; + } +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java b/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java new file mode 100644 index 0000000000..8e74f7d386 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/FilesReadLinesExample.java @@ -0,0 +1,18 @@ +package com.baeldung.fileparser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class FilesReadLinesExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + List result = Files.readAllLines(Paths.get(filename)); + + return (ArrayList) result; + } + +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java b/core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java new file mode 100644 index 0000000000..25d17af4ea --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/ScannerIntExample.java @@ -0,0 +1,24 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class ScannerIntExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (Scanner s = new Scanner(new FileReader(filename))) { + + while (s.hasNext()) { + result.add(s.nextInt()); + } + return result; + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java b/core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java new file mode 100644 index 0000000000..ec213c9490 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/fileparser/ScannerStringExample.java @@ -0,0 +1,24 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class ScannerStringExample { + + protected static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (Scanner s = new Scanner(new FileReader(filename))) { + + while (s.hasNext()) { + result.add(s.nextLine()); + } + return result; + } + + } + +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java new file mode 100644 index 0000000000..78f900d796 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/BufferedReaderUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class BufferedReaderUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = BufferedReaderExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java new file mode 100644 index 0000000000..a38e58d348 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/FileReaderUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class FileReaderUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = FileReaderExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java new file mode 100644 index 0000000000..c0b742fd47 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/FilesReadAllLinesUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class FilesReadAllLinesUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = FilesReadLinesExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java new file mode 100644 index 0000000000..0a398ba7c6 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/ScannerIntUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class ScannerIntUnitTest { + + protected static final String NUMBER_FILENAME = "src/test/resources/sampleNumberFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetIntArrayList() throws IOException { + List numbers = ScannerIntExample.generateArrayListFromFile(NUMBER_FILENAME); + assertTrue("File does not has 2 lines", numbers.size() == 2); + } +} diff --git a/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java b/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java new file mode 100644 index 0000000000..8f9b0a56c0 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/fileparser/ScannerStringUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.fileparser; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; + +import org.junit.Test; + +public class ScannerStringUnitTest { + + protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt"; + + @Test + public void whenParsingExistingTextFile_thenGetArrayList() throws IOException { + List lines = ScannerStringExample.generateArrayListFromFile(TEXT_FILENAME); + assertTrue("File does not has 2 lines", lines.size() == 2); + } +} diff --git a/core-java/src/test/resources/sampleNumberFile.txt b/core-java/src/test/resources/sampleNumberFile.txt new file mode 100644 index 0000000000..7787faa3c1 --- /dev/null +++ b/core-java/src/test/resources/sampleNumberFile.txt @@ -0,0 +1,2 @@ +111 +222 \ No newline at end of file diff --git a/core-java/src/test/resources/sampleTextFile.txt b/core-java/src/test/resources/sampleTextFile.txt new file mode 100644 index 0000000000..75cb50aafa --- /dev/null +++ b/core-java/src/test/resources/sampleTextFile.txt @@ -0,0 +1,2 @@ +Hello +World \ No newline at end of file diff --git a/core-kotlin/README.md b/core-kotlin/README.md index 9bef17e0d4..734b2c08b3 100644 --- a/core-kotlin/README.md +++ b/core-kotlin/README.md @@ -37,3 +37,4 @@ - [Create a Java and Kotlin Project with Maven](http://www.baeldung.com/kotlin-maven-java-project) - [Reflection with Kotlin](http://www.baeldung.com/kotlin-reflection) - [Get a Random Number in Kotlin](http://www.baeldung.com/kotlin-random-number) +- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging) diff --git a/core-kotlin/src/main/java/com/baeldung/constructor/Car.kt b/core-kotlin/src/main/java/com/baeldung/constructor/Car.kt new file mode 100644 index 0000000000..72b8d330e8 --- /dev/null +++ b/core-kotlin/src/main/java/com/baeldung/constructor/Car.kt @@ -0,0 +1,17 @@ +package com.baeldung.constructor + +class Car { + val id: String + val type: String + + constructor(id: String, type: String) { + this.id = id + this.type = type + } + +} + +fun main(args: Array) { + val car = Car("1", "sport") + val s= Car("2", "suv") +} \ No newline at end of file diff --git a/core-kotlin/src/main/java/com/baeldung/constructor/Employee.kt b/core-kotlin/src/main/java/com/baeldung/constructor/Employee.kt new file mode 100644 index 0000000000..4483bfcf08 --- /dev/null +++ b/core-kotlin/src/main/java/com/baeldung/constructor/Employee.kt @@ -0,0 +1,3 @@ +package com.baeldung.constructor + +class Employee(name: String, val salary: Int): Person(name) \ No newline at end of file diff --git a/core-kotlin/src/main/java/com/baeldung/constructor/Person.java b/core-kotlin/src/main/java/com/baeldung/constructor/Person.java new file mode 100644 index 0000000000..57911b24ee --- /dev/null +++ b/core-kotlin/src/main/java/com/baeldung/constructor/Person.java @@ -0,0 +1,19 @@ +package com.baeldung.constructor; + +class PersonJava { + final String name; + final String surname; + final Integer age; + + public PersonJava(String name, String surname) { + this.name = name; + this.surname = surname; + this.age = null; + } + + public PersonJava(String name, String surname, Integer age) { + this.name = name; + this.surname = surname; + this.age = age; + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/constructor/Person.kt b/core-kotlin/src/main/kotlin/com/baeldung/constructor/Person.kt new file mode 100644 index 0000000000..3779d74541 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/constructor/Person.kt @@ -0,0 +1,26 @@ +package com.baeldung.constructor + +open class Person( + val name: String, + val age: Int? = null +) { + val upperCaseName: String = name.toUpperCase() + + init { + println("Hello, I'm $name") + + if (age != null && age < 0) { + throw IllegalArgumentException("Age cannot be less than zero!") + } + } + + init { + println("upperCaseName is $upperCaseName") + } + +} + +fun main(args: Array) { + val person = Person("John") + val personWithAge = Person("John", 22) +} \ No newline at end of file diff --git a/core-kotlin/src/test/resources/Kotlin.out b/core-kotlin/src/test/resources/Kotlin.out new file mode 100644 index 0000000000..63d15d2528 --- /dev/null +++ b/core-kotlin/src/test/resources/Kotlin.out @@ -0,0 +1,2 @@ +Kotlin +Concise, Safe, Interoperable, Tool-friendly \ No newline at end of file diff --git a/ejb/README.md b/ejb/README.md index 994781b064..f47277bf8f 100644 --- a/ejb/README.md +++ b/ejb/README.md @@ -3,3 +3,4 @@ - [Guide to EJB Set-up](http://www.baeldung.com/ejb-intro) - [Java EE Session Beans](http://www.baeldung.com/ejb-session-beans) - [Introduction to EJB JNDI Lookup on WildFly Application Server](http://www.baeldung.com/wildfly-ejb-jndi) +- [A Guide to Message Driven Beans in EJB](http://www.baeldung.com/ejb-message-driven-beans) diff --git a/google-web-toolkit/README.md b/google-web-toolkit/README.md index 3526fe9962..65264375bc 100644 --- a/google-web-toolkit/README.md +++ b/google-web-toolkit/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Introduction to GWT](http://www.baeldung.com/gwt) +- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter) diff --git a/hazelcast/pom.xml b/hazelcast/pom.xml index cc366cd0a6..705792ad05 100644 --- a/hazelcast/pom.xml +++ b/hazelcast/pom.xml @@ -13,16 +13,12 @@ - - com.hazelcast - hazelcast - ${hazelcast.version} - - - com.hazelcast - hazelcast-client - ${hazelcast.version} - + + + com.hazelcast.jet + hazelcast-jet + ${hazelcast.jet.version} + @@ -36,8 +32,8 @@ - - 3.8.4 + + 0.6
\ No newline at end of file diff --git a/hazelcast/src/main/java/com/baeldung/hazelcast/jet/WordCounter.java b/hazelcast/src/main/java/com/baeldung/hazelcast/jet/WordCounter.java new file mode 100644 index 0000000000..971986bcae --- /dev/null +++ b/hazelcast/src/main/java/com/baeldung/hazelcast/jet/WordCounter.java @@ -0,0 +1,51 @@ +package com.baeldung.hazelcast.jet; + +import java.util.List; +import java.util.Map; + +import static com.hazelcast.jet.Traversers.traverseArray; +import static com.hazelcast.jet.aggregate.AggregateOperations.counting; +import static com.hazelcast.jet.function.DistributedFunctions.wholeItem; + +import com.hazelcast.jet.Jet; +import com.hazelcast.jet.JetInstance; +import com.hazelcast.jet.pipeline.Pipeline; +import com.hazelcast.jet.pipeline.Sinks; +import com.hazelcast.jet.pipeline.Sources; + +public class WordCounter { + + private static final String LIST_NAME = "textList"; + + private static final String MAP_NAME = "countMap"; + + private Pipeline createPipeLine() { + Pipeline p = Pipeline.create(); + p.drawFrom(Sources. list(LIST_NAME)) + .flatMap(word -> traverseArray(word.toLowerCase() + .split("\\W+"))) + .filter(word -> !word.isEmpty()) + .groupingKey(wholeItem()) + .aggregate(counting()) + .drainTo(Sinks.map(MAP_NAME)); + return p; + } + + public Long countWord(List sentences, String word) { + long count = 0; + JetInstance jet = Jet.newJetInstance(); + try { + List textList = jet.getList(LIST_NAME); + textList.addAll(sentences); + Pipeline p = createPipeLine(); + jet.newJob(p) + .join(); + Map counts = jet.getMap(MAP_NAME); + count = counts.get(word); + } finally { + Jet.shutdownAll(); + } + return count; + } + +} diff --git a/hazelcast/src/test/java/com/baeldung/hazelcast/jet/WordCounterUnitTest.java b/hazelcast/src/test/java/com/baeldung/hazelcast/jet/WordCounterUnitTest.java new file mode 100644 index 0000000000..95596b3860 --- /dev/null +++ b/hazelcast/src/test/java/com/baeldung/hazelcast/jet/WordCounterUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.hazelcast.jet; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class WordCounterUnitTest { + + @Test + public void whenGivenSentencesAndWord_ThenReturnCountOfWord() { + List sentences = new ArrayList<>(); + sentences.add("The first second was alright, but the second second was tough."); + WordCounter wordCounter = new WordCounter(); + long countSecond = wordCounter.countWord(sentences, "second"); + assertTrue(countSecond == 3); + } + +} diff --git a/java-numbers/pom.xml b/java-numbers/pom.xml index a9fb556517..bf4d3e8792 100644 --- a/java-numbers/pom.xml +++ b/java-numbers/pom.xml @@ -156,7 +156,7 @@ 1.19 1.19 - 2.19.1 + 2.21.0 3.0.0-M1 3.0.2 diff --git a/jee-7/pom.xml b/jee-7/pom.xml index d0246f650a..fbf102185d 100644 --- a/jee-7/pom.xml +++ b/jee-7/pom.xml @@ -418,7 +418,7 @@ 1.0.0.Final 2.6 4.2.3.RELEASE - 2.17 + 2.21.0 1.1.2 2.4 2.2.14 diff --git a/jersey/pom.xml b/jersey/pom.xml index 78e6d621ad..e248f9cf90 100644 --- a/jersey/pom.xml +++ b/jersey/pom.xml @@ -29,6 +29,28 @@ jaxrs-ri ${jersey.version} + + org.glassfish.jersey.containers + jersey-container-grizzly2-servlet + ${jersey.version} + + + org.glassfish.jersey.ext + jersey-mvc-freemarker + ${jersey.version} + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + ${jersey.version} + test + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + ${jersey.version} + test + @@ -41,6 +63,13 @@ false + + org.codehaus.mojo + exec-maven-plugin + + com.baeldung.jersey.server.http.EmbeddedHttpServer + + diff --git a/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java b/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java new file mode 100644 index 0000000000..d4744066c4 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/config/ViewApplicationConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.jersey.server.config; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature; + +public class ViewApplicationConfig extends ResourceConfig { + + public ViewApplicationConfig() { + packages("com.baeldung.jersey.server"); + property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "templates/freemarker"); + register(FreemarkerMvcFeature.class);; + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/http/EmbeddedHttpServer.java b/jersey/src/main/java/com/baeldung/jersey/server/http/EmbeddedHttpServer.java new file mode 100644 index 0000000000..4afa086858 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/http/EmbeddedHttpServer.java @@ -0,0 +1,35 @@ +package com.baeldung.jersey.server.http; + +import java.io.IOException; +import java.net.URI; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; + +import com.baeldung.jersey.server.config.ViewApplicationConfig; + +public class EmbeddedHttpServer { + + private static final URI BASE_URI = URI.create("http://localhost:8082/"); + + public static void main(String[] args) { + try { + final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new ViewApplicationConfig(), false); + + Runtime.getRuntime() + .addShutdownHook(new Thread(() -> { + server.shutdownNow(); + })); + + server.start(); + + System.out.println(String.format("Application started.\nTry out %s\nStop the application using CTRL+C", BASE_URI + "fruit")); + } catch (IOException ex) { + Logger.getLogger(EmbeddedHttpServer.class.getName()) + .log(Level.SEVERE, null, ex); + } + + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java b/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java new file mode 100644 index 0000000000..30620e331d --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/model/Fruit.java @@ -0,0 +1,20 @@ +package com.baeldung.jersey.server.model; + +public class Fruit { + + private final String name; + private final String colour; + + public Fruit(String name, String colour) { + this.name = name; + this.colour = colour; + } + + public String getName() { + return name; + } + + public String getColour() { + return colour; + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java b/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java new file mode 100644 index 0000000000..4e1fa4aa11 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/server/rest/FruitResource.java @@ -0,0 +1,55 @@ +package com.baeldung.jersey.server.rest; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.server.mvc.ErrorTemplate; +import org.glassfish.jersey.server.mvc.Template; +import org.glassfish.jersey.server.mvc.Viewable; + +import com.baeldung.jersey.server.model.Fruit; + +@Path("/fruit") +public class FruitResource { + + @GET + public Viewable get() { + return new Viewable("/index.ftl", "Fruit Index Page"); + } + + @GET + @Template(name = "/all.ftl") + @Path("/all") + @Produces(MediaType.TEXT_HTML) + public Map getAllFruit() { + final List fruits = new ArrayList<>(); + fruits.add(new Fruit("banana", "yellow")); + fruits.add(new Fruit("apple", "red")); + fruits.add(new Fruit("kiwi", "green")); + + final Map model = new HashMap(); + model.put("items", fruits); + return model; + } + + @GET + @ErrorTemplate(name = "/error.ftl") + @Template(name = "/named.ftl") + @Path("{name}") + @Produces(MediaType.TEXT_HTML) + public String getFruitByName(@PathParam("name") String name) { + if (!"banana".equalsIgnoreCase(name)) { + throw new IllegalArgumentException("Fruit not found: " + name); + } + return name; + } + +} diff --git a/jersey/src/main/resources/templates/freemarker/all.ftl b/jersey/src/main/resources/templates/freemarker/all.ftl new file mode 100644 index 0000000000..59406a60ca --- /dev/null +++ b/jersey/src/main/resources/templates/freemarker/all.ftl @@ -0,0 +1,14 @@ + + + All fruit! + + +

All fruit!

+

Fruits:

+
    + <#list items as fruit> +
  • ${fruit.name}
  • + +
+ + \ No newline at end of file diff --git a/jersey/src/main/resources/templates/freemarker/error.ftl b/jersey/src/main/resources/templates/freemarker/error.ftl new file mode 100644 index 0000000000..8ea6828ba5 --- /dev/null +++ b/jersey/src/main/resources/templates/freemarker/error.ftl @@ -0,0 +1,8 @@ + + + Welcome! + + +

Error - ${model.message}!

+ + \ No newline at end of file diff --git a/jersey/src/main/resources/templates/freemarker/index.ftl b/jersey/src/main/resources/templates/freemarker/index.ftl new file mode 100644 index 0000000000..01447f24e8 --- /dev/null +++ b/jersey/src/main/resources/templates/freemarker/index.ftl @@ -0,0 +1,8 @@ + + + Welcome! + + +

Welcome ${model}!

+ + \ No newline at end of file diff --git a/jersey/src/main/resources/templates/freemarker/named.ftl b/jersey/src/main/resources/templates/freemarker/named.ftl new file mode 100644 index 0000000000..ccde3307e6 --- /dev/null +++ b/jersey/src/main/resources/templates/freemarker/named.ftl @@ -0,0 +1,8 @@ + + + Welcome! + + +

Found fruit - ${model}!

+ + \ No newline at end of file diff --git a/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java b/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java new file mode 100644 index 0000000000..a0b6daed51 --- /dev/null +++ b/jersey/src/test/java/com/baeldung/jersey/server/rest/FruitResourceIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.jersey.server.rest; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.containsString; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.jersey.server.config.ViewApplicationConfig; + +public class FruitResourceIntegrationTest extends JerseyTest { + + @Override + protected Application configure() { + return new ViewApplicationConfig(); + } + + @Test + public void testAllFruit() { + final String response = target("/fruit/all").request() + .get(String.class); + Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi"))); + } + + @Test + public void testIndex() { + final String response = target("/fruit").request() + .get(String.class); + Assert.assertThat(response, containsString("Welcome Fruit Index Page!")); + } + + @Test + public void testErrorTemplate() { + final String response = target("/fruit/orange").request() + .get(String.class); + Assert.assertThat(response, containsString("Error - Fruit not found: orange!")); + } + +} diff --git a/jnosql/README.md b/jnosql/README.md new file mode 100644 index 0000000000..d580cbc920 --- /dev/null +++ b/jnosql/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [A Guide to Eclipse JNoSQL](http://www.baeldung.com/eclipse-jnosql) diff --git a/libraries-data/README.md b/libraries-data/README.md index 718f38d703..fbe49e9f74 100644 --- a/libraries-data/README.md +++ b/libraries-data/README.md @@ -8,4 +8,5 @@ - [Introduction to HikariCP](http://www.baeldung.com/hikaricp) - [Introduction to JCache](http://www.baeldung.com/jcache) - [A Guide to Apache Ignite](http://www.baeldung.com/apache-ignite) -- [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data) \ No newline at end of file +- [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data) +- [Guide to JMapper](https://github.com/eugenp/tutorials/tree/master/libraries-data) diff --git a/libraries/README.md b/libraries/README.md index 3d06442bae..aed808420e 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -86,6 +86,8 @@ - [Convert String to Date in Java](http://www.baeldung.com/java-string-to-date) - [Histograms with Apache Commons Frequency](http://www.baeldung.com/apache-commons-frequency) - [Guide to Resilience4j](http://www.baeldung.com/resilience4j) +- [Parsing YAML with SnakeYAML](http://www.baeldung.com/java-snake-yaml) +- [Guide to JMapper](http://www.baeldung.com/jmapper) The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own. diff --git a/maven/README.md b/maven/README.md index 7c3779143a..c5b875ce02 100644 --- a/maven/README.md +++ b/maven/README.md @@ -7,3 +7,4 @@ - [The Maven Failsafe Plugin](http://www.baeldung.com/maven-failsafe-plugin) - [The Maven Verifier Plugin](http://www.baeldung.com/maven-verifier-plugin) - [The Maven Clean Plugin](http://www.baeldung.com/maven-clean-plugin) +- [Build a Jar with Maven and Ignore the Test Results](http://www.baeldung.com/maven-ignore-test-results) diff --git a/micronaut/README.md b/micronaut/README.md new file mode 100644 index 0000000000..10a68ff2f8 --- /dev/null +++ b/micronaut/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Introduction to Micronaut Framework](http://www.baeldung.com/micronaut) diff --git a/mustache/pom.xml b/mustache/pom.xml index 863027b845..d385246182 100644 --- a/mustache/pom.xml +++ b/mustache/pom.xml @@ -8,11 +8,10 @@ mustache - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE - - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 @@ -154,7 +153,7 @@ 3.7.0 - 2.19.1 + 2.21.0 0.8 3.3.7 1.8 diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index ab6162a5a5..2fc46e4c28 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -8,18 +8,27 @@ Parent for all Spring Boot 2 modules - org.springframework.boot - spring-boot-starter-parent - 2.0.1.RELEASE - + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-dependencies + 2.0.1.RELEASE + pom + import + + + io.rest-assured rest-assured - ${rest-assured.version} - + org.springframework.boot spring-boot-starter-test @@ -27,79 +36,16 @@ - - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - 3 - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - **/*LiveTest.java - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + 2.0.1.RELEASE + + + - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - **/*LiveTest.java - **/AutoconfigurationTest.java - **/*UnitTest.java - - - **/*IntegrationTest.java - **/*IntTest.java - */EthControllerTestOne.java - **/*EntryPointsTest.java - - - - - - - json - - - - - - thin-jar @@ -122,13 +68,8 @@ - UTF-8 - UTF-8 - 1.8 3.1.0 - 1.8 - 1.8 1.0.11.RELEASE diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java new file mode 100644 index 0000000000..9f5e4d8fc4 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -0,0 +1,25 @@ +package com.baeldung.state; + +public class DeliveredState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new ReceivedState()); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new OrderedState()); + } + + @Override + public void printStatus() { + System.out.println("Package delivered to post office, not received yet."); + } + + @Override + public String toString() { + return "DeliveredState{}"; + } + +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java new file mode 100644 index 0000000000..0642c4c73c --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -0,0 +1,24 @@ +package com.baeldung.state; + +public class OrderedState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void prev(Package pkg) { + System.out.println("The package is in it's root state."); + } + + @Override + public void printStatus() { + System.out.println("Package ordered, not delivered to the office yet."); + } + + @Override + public String toString() { + return "OrderedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java new file mode 100644 index 0000000000..f3dfbb3fa7 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -0,0 +1,26 @@ +package com.baeldung.state; + +public class Package { + + private PackageState state = new OrderedState(); + + public PackageState getState() { + return state; + } + + public void setState(PackageState state) { + this.state = state; + } + + public void previousState() { + state.prev(this); + } + + public void nextState() { + state.next(this); + } + + public void printStatus() { + state.printStatus(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java new file mode 100644 index 0000000000..d6656c78ac --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -0,0 +1,10 @@ +package com.baeldung.state; + +public interface PackageState { + + void next(Package pkg); + + void prev(Package pkg); + + void printStatus(); +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java new file mode 100644 index 0000000000..84136fa48e --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -0,0 +1,24 @@ +package com.baeldung.state; + +public class ReceivedState implements PackageState { + + @Override + public void next(Package pkg) { + System.out.println("This package is already received by a client."); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void printStatus() { + System.out.println("Package was received by client."); + } + + @Override + public String toString() { + return "ReceivedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java new file mode 100644 index 0000000000..1a63ea3ddf --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class StateDemo { + + public static void main(String[] args) { + + Package pkg = new Package(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + } +} diff --git a/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java new file mode 100644 index 0000000000..731974f92b --- /dev/null +++ b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.state; + +import com.baeldung.state.Package; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; + +import org.junit.Test; + +public class StatePatternUnitTest { + + @Test + public void givenNewPackage_whenPackageReceived_thenStateReceived() { + Package pkg = new Package(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(DeliveredState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(ReceivedState.class)); + } + + @Test + public void givenDeliveredPackage_whenPrevState_thenStateOrdered() { + Package pkg = new Package(); + pkg.setState(new DeliveredState()); + pkg.previousState(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + } +} diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index 9f2b63c17c..e5bd78d208 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -177,46 +177,12 @@ - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - dynamodb-local DynamoDB Local Release Repository ${dynamodblocal.repository.url} - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - diff --git a/pom.xml b/pom.xml index 34b55fd1f4..db3bef7fda 100644 --- a/pom.xml +++ b/pom.xml @@ -43,9 +43,15 @@ org.junit.jupiter junit-jupiter-engine - ${junit.jupiter.version} + ${junit-jupiter.version} test + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + org.hamcrest hamcrest-core @@ -70,6 +76,14 @@ ${mockito.version} test + + org.apache.maven.surefire + surefire-logger-api + ${maven-surefire-plugin.version} + + test + true + @@ -98,6 +112,23 @@ **/*LiveTest.java + + + org.junit.platform + junit-platform-surefire-provider + ${junit-platform.version} + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + + + org.junit.vintage + junit-vintage-engine + ${junit-jupiter.version} + + org.apache.maven.plugins @@ -187,6 +218,10 @@ + + maven-war-plugin + ${maven-war-plugin.version} + @@ -406,6 +441,7 @@ spring-4 spring-5 spring-5-reactive + spring-5-reactive-client spring-5-mvc spring-5-security spring-activiti @@ -551,8 +587,6 @@ apache-meecrowave spring-reactive-kotlin jnosql - testing-modules/junit-abstract - sse-jaxrs spring-boot-angular-ecommerce @@ -664,6 +698,7 @@ spring-4 spring-5 spring-5-reactive + spring-5-reactive-client spring-5-mvc spring-5-security spring-activiti @@ -673,7 +708,6 @@ spring-amqp-simple spring-apache-camel spring-batch - testing-modules/junit-abstract jmh @@ -905,6 +939,7 @@ json jsoup testing-modules/junit-5 + testing-modules/junit5-migration jws libraries-data linkrest @@ -943,6 +978,7 @@ spark-java spring-4 spring-5-reactive + spring-5-reactive-client spring-5-mvc spring-5-security spring-activiti @@ -1079,7 +1115,6 @@ antlr maven-archetype apache-meecrowave - testing-modules/junit-abstract spring-hibernate4 xml @@ -1222,7 +1257,7 @@ 2.19.1 2.5 1.4 - 2.6 + 3.0.0 3.1.0 1.2 2.3.1 @@ -1230,7 +1265,8 @@ 1.2 2.5.0 1.3 - 5.0.2 + 1.2.0 + 5.2.0 0.3.1 2.5.1 0.0.1 diff --git a/spring-4/pom.xml b/spring-4/pom.xml index c8f821f1e7..d2632b5f55 100644 --- a/spring-4/pom.xml +++ b/spring-4/pom.xml @@ -8,10 +8,10 @@ spring-4 - org.springframework.boot - spring-boot-starter-parent - 1.5.10.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/spring-5-mvc/pom.xml b/spring-5-mvc/pom.xml index 0408550c79..f5346a0fa0 100644 --- a/spring-5-mvc/pom.xml +++ b/spring-5-mvc/pom.xml @@ -82,11 +82,6 @@ spring-boot-starter-test test - - junit - junit - test - com.jayway.restassured rest-assured @@ -137,46 +132,11 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - - false - - **/*IntegrationTest.java - **/*IntTest.java - **/*LiveTest.java - - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - + 2.9.0 1.1.2 - diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index b907013b91..f2f7dd1729 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -11,10 +11,10 @@ spring 5 sample project about new features - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M7 - + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 @@ -43,20 +43,6 @@ javax.json.bind javax.json.bind-api - - - - - - - - - - - - - - org.apache.geronimo.specs geronimo-json_1.1_spec @@ -102,28 +88,6 @@ test - - org.junit.jupiter - junit-jupiter-api - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - org.projectlombok lombok @@ -145,54 +109,10 @@ JAR - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LiveTest.java - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - UTF-8 - UTF-8 - 1.8 - 1.0.0 - 5.0.0 - 2.20 - 5.0.2.RELEASE 1.0.1.RELEASE 1.1.3 1.0 diff --git a/spring-5-reactive/README.md b/spring-5-reactive/README.md index 8a67b4d86d..0a7fe7a47e 100644 --- a/spring-5-reactive/README.md +++ b/spring-5-reactive/README.md @@ -13,3 +13,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Reactive WebSockets with Spring 5](http://www.baeldung.com/spring-5-reactive-websockets) - [Spring Webflux Filters](http://www.baeldung.com/spring-webflux-filters) - [How to Set a Header on a Response with Spring 5](http://www.baeldung.com/spring-response-header) +- [Spring Webflux and CORS](http://www.baeldung.com/spring-webflux-cors) +- [Handling Errors in Spring WebFlux](http://www.baeldung.com/spring-webflux-errors) +- [Guide to Spring 5 WebFlux](http://www.baeldung.com/spring-webflux) diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index acc82be0d1..f89fd45581 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -43,20 +43,6 @@ org.springframework.boot spring-boot-starter-actuator - - - - - - - - - - - - - - org.projectlombok lombok @@ -100,28 +86,6 @@ ${commons-collections4.version} test - - - org.junit.jupiter - junit-jupiter-api - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - org.apache.commons commons-lang3 @@ -159,29 +123,10 @@ JAR - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LiveTest.java - - - - 1.0.0 - 5.0.2 - 2.20 1.0.1.RELEASE 2.1.12 1.1.3 diff --git a/spring-5-security/pom.xml b/spring-5-security/pom.xml index 7024e6f873..1435019c24 100644 --- a/spring-5-security/pom.xml +++ b/spring-5-security/pom.xml @@ -76,44 +76,7 @@ JAR - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LiveTest.java - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - \ No newline at end of file diff --git a/spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerTest.java b/spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerIntegrationTest.java similarity index 97% rename from spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerTest.java rename to spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerIntegrationTest.java index 7a8ea7b248..9d08cb7cfa 100644 --- a/spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerTest.java +++ b/spring-5-security/src/test/java/com/baeldung/inmemory/InMemoryAuthControllerIntegrationTest.java @@ -14,7 +14,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = InMemoryAuthApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) -public class InMemoryAuthControllerTest { +public class InMemoryAuthControllerIntegrationTest { @Autowired private TestRestTemplate template; diff --git a/spring-5/README.md b/spring-5/README.md index baf03fb3b3..47bae09862 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -12,3 +12,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs) - [Spring ResponseStatusException](http://www.baeldung.com/spring-response-status-exception) - [Spring Assert Statements](http://www.baeldung.com/spring-assert) +- [Spring Security 5 – OAuth2 Login](http://www.baeldung.com/spring-security-5-oauth2-login) diff --git a/spring-5/pom.xml b/spring-5/pom.xml index e37833ff94..9f60b8a364 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -41,18 +41,6 @@ javax.json.bind javax.json.bind-api - - - - - - - - - - - - org.apache.geronimo.specs geronimo-json_1.1_spec @@ -85,45 +73,21 @@ org.springframework spring-test - - org.springframework.boot - spring-boot-starter-test - test - org.springframework.security spring-security-test test - org.apache.commons commons-collections4 ${commons-collections4.version} test - org.junit.jupiter junit-jupiter-api - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - org.springframework.restdocs @@ -147,22 +111,6 @@ JAR - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LiveTest.java - - - org.asciidoctor asciidoctor-maven-plugin @@ -190,9 +138,6 @@ - 1.0.0 - 5.0.2.RELEASE - 1.0.1.RELEASE 1.0 1.5.6 4.1 diff --git a/spring-activiti/pom.xml b/spring-activiti/pom.xml index b15fc48d7c..1b9e831a29 100644 --- a/spring-activiti/pom.xml +++ b/spring-activiti/pom.xml @@ -10,10 +10,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/spring-all/README.md b/spring-all/README.md index 215f9a64d2..ded5e26285 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -29,3 +29,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Quick Guide to the Spring @Order Annotation](http://www.baeldung.com/spring-order) - [Spring Web Contexts](http://www.baeldung.com/spring-web-contexts) - [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator) +- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary) diff --git a/spring-batch/README.md b/spring-batch/README.md index 754fc5f980..737e7e13f5 100644 --- a/spring-batch/README.md +++ b/spring-batch/README.md @@ -7,3 +7,4 @@ - [Introduction to Spring Batch](http://www.baeldung.com/introduction-to-spring-batch) - [Spring Batch using Partitioner](http://www.baeldung.com/spring-batch-partitioner) - [Spring Batch Tasklets vs Chunks Approach](http://www.baeldung.com/spring-batch-tasklet-chunk) +- [How to Trigger and Stop a Scheduled Spring Batch Job](http://www.baeldung.com/spring-batch-start-stop-job) diff --git a/spring-boot-jasypt/pom.xml b/spring-boot-jasypt/pom.xml index 7767130a43..8b7a475824 100644 --- a/spring-boot-jasypt/pom.xml +++ b/spring-boot-jasypt/pom.xml @@ -12,10 +12,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 2.0.1.RELEASE - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 diff --git a/spring-boot-logging-log4j2/pom.xml b/spring-boot-logging-log4j2/pom.xml index 3417043e91..61f949b820 100644 --- a/spring-boot-logging-log4j2/pom.xml +++ b/spring-boot-logging-log4j2/pom.xml @@ -10,10 +10,10 @@ Demo project for Spring Boot Logging with Log4J2 - org.springframework.boot - spring-boot-starter-parent - 2.0.3.RELEASE - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index 453a05d8e7..d0fce26bb5 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -10,11 +10,11 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 2.0.4.RELEASE - - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + diff --git a/spring-boot-mvc/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java b/spring-boot-mvc/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java deleted file mode 100644 index 1add635ed8..0000000000 --- a/spring-boot-mvc/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.springbootmvc; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringBootMvcApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/spring-boot-vue/README.md b/spring-boot-vue/README.md new file mode 100644 index 0000000000..e68f961e51 --- /dev/null +++ b/spring-boot-vue/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Vue.js Frontend with a Spring Boot Backend](http://www.baeldung.com/spring-boot-vue-js) diff --git a/spring-boot-vue/pom.xml b/spring-boot-vue/pom.xml index 151fd293bb..d581b11d68 100644 --- a/spring-boot-vue/pom.xml +++ b/spring-boot-vue/pom.xml @@ -12,11 +12,11 @@ Demo project for Spring Boot Vue project - org.springframework.boot - spring-boot-starter-parent - 2.0.2.RELEASE - - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + UTF-8 diff --git a/spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java b/spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationUnitTest.java similarity index 96% rename from spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java rename to spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationUnitTest.java index 6364351eb3..567b239ed2 100644 --- a/spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationTests.java +++ b/spring-boot-vue/src/test/java/com/baeldung/springbootmvc/SpringBootMvcApplicationUnitTest.java @@ -16,7 +16,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc -public class SpringBootMvcApplicationTests { +public class SpringBootMvcApplicationUnitTest { @Autowired private MockMvc mockMvc; diff --git a/spring-boot/README.MD b/spring-boot/README.MD index a572a16b67..1532889a5c 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -35,3 +35,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Guide to the Favicon in Spring Boot](http://www.baeldung.com/spring-boot-favicon) - [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks) - [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb) +- [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index ef30600d45..3a43dbd828 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -149,6 +149,12 @@ rome ${rome.version} + + + de.codecentric + chaos-monkey-spring-boot + ${chaos.monkey.version} + @@ -219,9 +225,11 @@ 8.5.11 2.4.1.Final 1.9.0 + 2.0.0 3.6.0 3.2.0 18.0 + 1.2.0 \ No newline at end of file diff --git a/spring-boot/src/main/java/com/baeldung/chaosmonkey/SpringBootChaosMonkeyApp.java b/spring-boot/src/main/java/com/baeldung/chaosmonkey/SpringBootChaosMonkeyApp.java new file mode 100644 index 0000000000..16a0aea13b --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/chaosmonkey/SpringBootChaosMonkeyApp.java @@ -0,0 +1,15 @@ +package com.baeldung.chaosmonkey; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by adi on 8/2/18. + */ +@SpringBootApplication(scanBasePackages = { "com.baeldung.chaosmonkey" }) +public class SpringBootChaosMonkeyApp { + + public static void main(String[] args) { + SpringApplication.run(SpringBootChaosMonkeyApp.class, args); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/chaosmonkey/controller/PermissionsController.java b/spring-boot/src/main/java/com/baeldung/chaosmonkey/controller/PermissionsController.java new file mode 100644 index 0000000000..6ceb117f4e --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/chaosmonkey/controller/PermissionsController.java @@ -0,0 +1,25 @@ +package com.baeldung.chaosmonkey.controller; + +import com.baeldung.chaosmonkey.service.PermissionsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * Created by adi on 8/2/18. + */ +@RestController +@RequestMapping("/permissions") +public class PermissionsController { + + @Autowired private PermissionsService permissionsService; + + @GetMapping + public List getAllPermissions() { + return permissionsService.getAllPermissions(); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/chaosmonkey/service/PermissionsService.java b/spring-boot/src/main/java/com/baeldung/chaosmonkey/service/PermissionsService.java new file mode 100644 index 0000000000..435e262901 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/chaosmonkey/service/PermissionsService.java @@ -0,0 +1,17 @@ +package com.baeldung.chaosmonkey.service; + +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; + +/** + * Created by adi on 8/2/18. + */ +@Service +public class PermissionsService { + + public List getAllPermissions() { + return Arrays.asList("CREATE", "READ", "UPDATE", "DELETE"); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java b/spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java index 69abeb0bdd..9554facb12 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java +++ b/spring-boot/src/main/java/org/baeldung/boot/config/WebConfig.java @@ -1,6 +1,8 @@ package org.baeldung.boot.config; import org.baeldung.boot.converter.StringToEmployeeConverter; +import org.baeldung.boot.converter.StringToEnumConverterFactory; +import org.baeldung.boot.converter.GenericBigDecimalConverter; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -11,5 +13,8 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new StringToEmployeeConverter()); + registry.addConverterFactory(new StringToEnumConverterFactory()); + registry.addConverter(new GenericBigDecimalConverter()); } } + diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java b/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java new file mode 100644 index 0000000000..8add28fc2d --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/GenericBigDecimalConverter.java @@ -0,0 +1,31 @@ +package org.baeldung.boot.converter; + +import com.google.common.collect.ImmutableSet; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.GenericConverter; +import java.math.BigDecimal; +import java.util.Set; +public class GenericBigDecimalConverter implements GenericConverter { + @Override + public Set getConvertibleTypes () { + ConvertiblePair[] pairs = new ConvertiblePair[] { + new ConvertiblePair(Number.class, BigDecimal.class), + new ConvertiblePair(String.class, BigDecimal.class)}; + return ImmutableSet.copyOf(pairs); + } + @Override + public Object convert (Object source, TypeDescriptor sourceType, + TypeDescriptor targetType) { + if (sourceType.getType() == BigDecimal.class) { + return source; + } + if(sourceType.getType() == String.class) { + String number = (String) source; + return new BigDecimal(number); + } else { + Number number = (Number) source; + BigDecimal converted = new BigDecimal(number.doubleValue()); + return converted.setScale(2, BigDecimal.ROUND_HALF_EVEN); + } + } +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java index 6902cd84d1..1bf75b38f0 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEmployeeConverter.java @@ -9,8 +9,6 @@ public class StringToEmployeeConverter implements Converter { @Override public Employee convert(String from) { String[] data = from.split(","); - return new Employee( - Long.parseLong(data[0]), - Double.parseDouble(data[1])); + return new Employee(Long.parseLong(data[0]), Double.parseDouble(data[1])); } } diff --git a/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java new file mode 100644 index 0000000000..ddb2cd2b08 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/converter/StringToEnumConverterFactory.java @@ -0,0 +1,30 @@ +package org.baeldung.boot.converter; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.ConverterFactory; +import org.springframework.stereotype.Component; + +@Component +public class StringToEnumConverterFactory + implements ConverterFactory { + + private static class StringToEnumConverter + implements Converter { + + private Class enumType; + + public StringToEnumConverter(Class enumType) { + this.enumType = enumType; + } + + public T convert(String source) { + return (T) Enum.valueOf(this.enumType, source.trim()); + } + } + + @Override + public Converter getConverter( + Class targetType) { + return new StringToEnumConverter(targetType); + } +} \ No newline at end of file diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index 8c02e528ab..629e880940 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -43,3 +43,34 @@ servlet.mapping=/dispatcherExampleURL #spring.banner.image.invert= //TODO contactInfoType=email + +#chaos monkey for spring boot props +management.endpoint.chaosmonkey.enabled=true +management.endpoint.chaosmonkeyjmx.enabled=true + +spring.profiles.active=chaos-monkey +#Determine whether should execute or not +chaos.monkey.enabled=true +#How many requests are to be attacked. 1: attack each request; 5: each 5th request is attacked +chaos.monkey.assaults.level=1 +#Minimum latency in ms added to the request +chaos.monkey.assaults.latencyRangeStart=3000 +#Maximum latency in ms added to the request +chaos.monkey.assaults.latencyRangeEnd=15000 +#Latency assault active +chaos.monkey.assaults.latencyActive=true +#Exception assault active +chaos.monkey.assaults.exceptionsActive=false +#AppKiller assault active +chaos.monkey.assaults.killApplicationActive=false +#Controller watcher active +chaos.monkey.watcher.controller=false +#RestController watcher active +chaos.monkey.watcher.restController=false +#Service watcher active +chaos.monkey.watcher.service=true +#Repository watcher active +chaos.monkey.watcher.repository=false +#Component watcher active +chaos.monkey.watcher.component=false + diff --git a/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java index 1356de6d0e..bd1ae2c8fa 100644 --- a/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterIntegrationTest.java @@ -43,7 +43,7 @@ public class CustomConverterIntegrationTest { @Test public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() { - assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(0, BigDecimal.ROUND_HALF_EVEN)); + assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN)); assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23))); assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32)); } diff --git a/spring-cloud-bus/spring-cloud-config-client/pom.xml b/spring-cloud-bus/spring-cloud-config-client/pom.xml index 0c7d4648c4..bd9fb45587 100644 --- a/spring-cloud-bus/spring-cloud-config-client/pom.xml +++ b/spring-cloud-bus/spring-cloud-config-client/pom.xml @@ -12,9 +12,10 @@ Demo Spring Cloud Config Client - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 diff --git a/spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientApplicationTests.java b/spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientLiveTest.java similarity index 84% rename from spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientApplicationTests.java rename to spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientLiveTest.java index 3b361f385a..364368713a 100644 --- a/spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientApplicationTests.java +++ b/spring-cloud-bus/spring-cloud-config-client/src/test/java/com/baeldung/SpringCloudConfigClientLiveTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class SpringCloudConfigClientApplicationTests { +public class SpringCloudConfigClientLiveTest { @Test public void contextLoads() { diff --git a/spring-cloud-bus/spring-cloud-config-server/pom.xml b/spring-cloud-bus/spring-cloud-config-server/pom.xml index 122f7bbce3..194289ea1e 100644 --- a/spring-cloud-bus/spring-cloud-config-server/pom.xml +++ b/spring-cloud-bus/spring-cloud-config-server/pom.xml @@ -12,9 +12,10 @@ Demo Spring Cloud Config Server - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 diff --git a/spring-cloud-data-flow/data-flow-server/pom.xml b/spring-cloud-data-flow/data-flow-server/pom.xml index 3b04985618..0133d65978 100644 --- a/spring-cloud-data-flow/data-flow-server/pom.xml +++ b/spring-cloud-data-flow/data-flow-server/pom.xml @@ -11,10 +11,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.4.3.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-aws/pom.xml b/spring-cloud/spring-cloud-aws/pom.xml index c15387044a..773bf27fc2 100644 --- a/spring-cloud/spring-cloud-aws/pom.xml +++ b/spring-cloud/spring-cloud-aws/pom.xml @@ -10,10 +10,10 @@ Spring Cloud AWS Examples - org.springframework.boot - spring-boot-starter-parent - 1.5.8.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-connectors-heroku/pom.xml b/spring-cloud/spring-cloud-connectors-heroku/pom.xml index e782052b29..f25c190d56 100644 --- a/spring-cloud/spring-cloud-connectors-heroku/pom.xml +++ b/spring-cloud/spring-cloud-connectors-heroku/pom.xml @@ -7,10 +7,10 @@ 1.0.0-SNAPSHOT - spring-boot-starter-parent - org.springframework.boot - 1.4.4.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 @@ -79,7 +79,7 @@ Brixton.SR7 - 2.19.1 + 2.21.0 9.4-1201-jdbc4 diff --git a/spring-cloud/spring-cloud-gateway/pom.xml b/spring-cloud/spring-cloud-gateway/pom.xml index db57373b6f..8babbff274 100644 --- a/spring-cloud/spring-cloud-gateway/pom.xml +++ b/spring-cloud/spring-cloud-gateway/pom.xml @@ -28,7 +28,7 @@ org.springframework.cloud - spring-cloud-starter-gateway + spring-cloud-starter org.springframework.boot @@ -54,27 +54,8 @@ - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - 2.0.0.RC2 + 2.0.1.RELEASE 6.0.2.Final diff --git a/spring-cloud/spring-cloud-ribbon-client/pom.xml b/spring-cloud/spring-cloud-ribbon-client/pom.xml index eb8398848c..fd69dbe043 100644 --- a/spring-cloud/spring-cloud-ribbon-client/pom.xml +++ b/spring-cloud/spring-cloud-ribbon-client/pom.xml @@ -38,25 +38,6 @@ - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - Brixton.SR7 diff --git a/spring-cloud/spring-cloud-security/auth-client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml index d4cd4d1856..f2d6308374 100644 --- a/spring-cloud/spring-cloud-security/auth-client/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -10,10 +10,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.9.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java index 5fa51a61c3..ca89575ee0 100644 --- a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java +++ b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java @@ -5,8 +5,10 @@ import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import com.baeldung.CloudSite; + @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(classes = CloudSite.class) public class Springoath2ApplicationTests { @Test diff --git a/spring-cloud/spring-cloud-security/auth-resource/pom.xml b/spring-cloud/spring-cloud-security/auth-resource/pom.xml index e1ed33b46d..0115259108 100644 --- a/spring-cloud/spring-cloud-security/auth-resource/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -12,10 +12,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.9.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-security/auth-server/pom.xml b/spring-cloud/spring-cloud-security/auth-server/pom.xml index 4515ecd610..b4367935e3 100644 --- a/spring-cloud/spring-cloud-security/auth-server/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -7,9 +7,10 @@ 0.0.1-SNAPSHOT - org.springframework.boot - spring-boot-starter-parent - 1.5.9.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-stream-starters/boot/pom.xml b/spring-cloud/spring-cloud-stream-starters/boot/pom.xml index b5a465d846..bf725da5cf 100644 --- a/spring-cloud/spring-cloud-stream-starters/boot/pom.xml +++ b/spring-cloud/spring-cloud-stream-starters/boot/pom.xml @@ -9,9 +9,10 @@ twitterhdfs - org.springframework.boot - spring-boot-starter-parent - 1.5.8.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml index c86c092b2e..afe609288d 100644 --- a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -6,10 +6,10 @@ 0.0.1-SNAPSHOT - org.springframework.boot - spring-boot-starter-parent - 1.5.10.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml index 0acd197c50..fdcbabdd51 100644 --- a/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml +++ b/spring-cloud/spring-cloud-task/springcloudtasksink/pom.xml @@ -11,10 +11,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.10.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java b/spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java similarity index 100% rename from spring-cloud/spring-cloud-task/springcloudtasksink/src/main/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java rename to spring-cloud/spring-cloud-task/springcloudtasksink/src/test/java/com/baeldung/SpringCloudTaskFinal/TaskSinkConfiguration.java diff --git a/spring-data-5-reactive/pom.xml b/spring-data-5-reactive/pom.xml index 806eafa2d6..36ace53da2 100644 --- a/spring-data-5-reactive/pom.xml +++ b/spring-data-5-reactive/pom.xml @@ -8,10 +8,10 @@ jar - org.springframework.boot - spring-boot-starter-parent - 2.0.1.RELEASE - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 diff --git a/spring-data-keyvalue/pom.xml b/spring-data-keyvalue/pom.xml index e6be025cb4..6ab928303d 100644 --- a/spring-data-keyvalue/pom.xml +++ b/spring-data-keyvalue/pom.xml @@ -6,10 +6,10 @@ 1.0 - org.springframework.boot - spring-boot-starter-parent - 2.0.1.RELEASE - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 diff --git a/spring-data-rest-querydsl/pom.xml b/spring-data-rest-querydsl/pom.xml index 251eb2d9bb..55ff78f0cb 100644 --- a/spring-data-rest-querydsl/pom.xml +++ b/spring-data-rest-querydsl/pom.xml @@ -8,9 +8,10 @@ spring-data-rest-querydsl - org.springframework.boot - spring-boot-starter-parent - 1.5.9.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/spring-jooq/pom.xml b/spring-jooq/pom.xml index 713f7eb5ad..e1c13ef774 100644 --- a/spring-jooq/pom.xml +++ b/spring-jooq/pom.xml @@ -5,10 +5,10 @@ 0.0.1-SNAPSHOT - org.springframework.boot - spring-boot-starter-parent - 1.4.4.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 @@ -192,13 +192,13 @@ 3.8.6 1.4.193 - 4.3.4.RELEASE + 4.3.17.RELEASE 1.0.0 1.5 1.0.0 - 1.4.4.RELEASE - + 1.5.13.RELEASE + org.jooq.example.spring.Application \ No newline at end of file diff --git a/spring-mustache/pom.xml b/spring-mustache/pom.xml index 3a53c63f8c..4e7a1ba5a3 100644 --- a/spring-mustache/pom.xml +++ b/spring-mustache/pom.xml @@ -11,11 +11,10 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE - - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/spring-mvc-forms-thymeleaf/pom.xml b/spring-mvc-forms-thymeleaf/pom.xml index 429bf3a5d4..07c49766ee 100644 --- a/spring-mvc-forms-thymeleaf/pom.xml +++ b/spring-mvc-forms-thymeleaf/pom.xml @@ -11,10 +11,10 @@ spring forms examples using thymeleaf - org.springframework.boot - spring-boot-starter-parent - 2.0.0.RELEASE - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 6b05ef569c..c4a0e3579c 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -34,3 +34,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Core Annotations](http://www.baeldung.com/spring-core-annotations) - [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity) - [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status) +- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) diff --git a/spring-reactive-kotlin/README.md b/spring-reactive-kotlin/README.md new file mode 100644 index 0000000000..cf5debc617 --- /dev/null +++ b/spring-reactive-kotlin/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Spring Webflux with Kotlin](http://www.baeldung.com/spring-webflux-kotlin) diff --git a/spring-reactive-kotlin/pom.xml b/spring-reactive-kotlin/pom.xml index 1425adc191..f2f0dc58ec 100644 --- a/spring-reactive-kotlin/pom.xml +++ b/spring-reactive-kotlin/pom.xml @@ -12,16 +12,13 @@ Demo project for Spring Boot - org.springframework.boot - spring-boot-starter-parent - 2.0.3.RELEASE - - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + - UTF-8 - UTF-8 - 1.8 1.2.41 diff --git a/spring-rest-embedded-tomcat/pom.xml b/spring-rest-embedded-tomcat/pom.xml index edccbb17d5..1a1adce6db 100644 --- a/spring-rest-embedded-tomcat/pom.xml +++ b/spring-rest-embedded-tomcat/pom.xml @@ -85,7 +85,6 @@ - 2.19.1 2.9.2 1.8 1.8 diff --git a/spring-rest-hal-browser/README.md b/spring-rest-hal-browser/README.md new file mode 100644 index 0000000000..d9760c8295 --- /dev/null +++ b/spring-rest-hal-browser/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Spring REST and HAL Browser](http://www.baeldung.com/spring-rest-hal) diff --git a/spring-rest-shell/pom.xml b/spring-rest-shell/pom.xml index e591c16427..2f7d1c8933 100644 --- a/spring-rest-shell/pom.xml +++ b/spring-rest-shell/pom.xml @@ -10,10 +10,10 @@ A simple project to demonstrate Spring REST Shell features. - org.springframework.boot - spring-boot-starter-parent - 1.5.8.RELEASE - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/spring-rest-simple/README.md b/spring-rest-simple/README.md index 9345cb70cc..ae853ba787 100644 --- a/spring-rest-simple/README.md +++ b/spring-rest-simple/README.md @@ -7,4 +7,4 @@ - [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring) - [Spring and Apache FileUpload](http://www.baeldung.com/spring-apache-file-upload) - [Spring RestTemplate Error Handling](http://www.baeldung.com/spring-rest-template-error-handling) - +- [Test a REST API with curl](http://www.baeldung.com/curl-rest) diff --git a/spring-rest-simple/pom.xml b/spring-rest-simple/pom.xml index f7981ee05c..f5ce46ade4 100644 --- a/spring-rest-simple/pom.xml +++ b/spring-rest-simple/pom.xml @@ -8,9 +8,10 @@ war - org.springframework.boot - spring-boot-starter-parent - 1.4.3.RELEASE + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 @@ -154,7 +155,7 @@ com.jayway.restassured rest-assured - ${rest-assured.version} + ${jayway-rest-assured.version} com.google.protobuf @@ -333,7 +334,7 @@ 20.0 - 2.9.0 + 2.9.0 1.6.0 diff --git a/spring-rest-template/README.md b/spring-rest-template/README.md index bf35f0d32c..d69d5c01c7 100644 --- a/spring-rest-template/README.md +++ b/spring-rest-template/README.md @@ -2,3 +2,6 @@ ### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: +- [Uploading MultipartFile with Spring RestTemplate](http://www.baeldung.com/spring-rest-template-multipart-upload) diff --git a/spring-security-mvc-login/README.md b/spring-security-mvc-login/README.md index f7eb314869..c19cbc87a5 100644 --- a/spring-security-mvc-login/README.md +++ b/spring-security-mvc-login/README.md @@ -12,6 +12,7 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com - [Spring HTTP/HTTPS Channel Security](http://www.baeldung.com/spring-channel-security-https) - [Spring Security - Customize the 403 Forbidden/Access Denied Page](http://www.baeldung.com/spring-security-custom-access-denied-page) - [Spring Security – Redirect to the Previous URL After Login](http://www.baeldung.com/spring-security-redirect-login) +- [Spring Security Custom AuthenticationFailureHandler](http://www.baeldung.com/spring-security-custom-authentication-failure-handler) ### Build the Project ``` diff --git a/spring-security-openid/pom.xml b/spring-security-openid/pom.xml index 108636ca8e..a2c0b6b119 100644 --- a/spring-security-openid/pom.xml +++ b/spring-security-openid/pom.xml @@ -11,10 +11,10 @@ Spring OpenID sample project - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M7 - + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 @@ -51,27 +51,6 @@ - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - 2.2.1.RELEASE 1.0.9.RELEASE diff --git a/spring-session/README.md b/spring-session/README.md index 733c9edfb9..30f70996cf 100644 --- a/spring-session/README.md +++ b/spring-session/README.md @@ -4,4 +4,4 @@ ### Relevant Articles: - [Introduction to Spring Session](http://www.baeldung.com/spring-session) -- [Spring Session with JDBC](http://www.baeldung.com) +- [Spring Session with JDBC](http://www.baeldung.com/spring-session-jdbc) diff --git a/spring-session/spring-session-jdbc/README.MD b/spring-session/spring-session-jdbc/README.MD index e22b7317c9..9293dfc953 100644 --- a/spring-session/spring-session-jdbc/README.MD +++ b/spring-session/spring-session-jdbc/README.MD @@ -3,4 +3,4 @@ Jira BAEL-1911 ### Relevant Articles: -- [Spring Session with JDBC](http://www.baeldung.com) +- [Spring Session with JDBC](http://www.baeldung.com/spring-session-jdbc) diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index c023dcdb5f..0926728aba 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -1,5 +1,5 @@ + 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-thymeleaf @@ -8,8 +8,9 @@ com.baeldung - parent-modules - 1.0.0-SNAPSHOT + parent-spring-5 + 0.0.1-SNAPSHOT + ../parent-spring-5 @@ -17,7 +18,7 @@ org.springframework spring-context - ${org.springframework-version} + ${spring.version} @@ -29,19 +30,38 @@ org.springframework spring-webmvc - ${org.springframework-version} + ${spring.version} + + + org.springframework.data + spring-data-commons + ${spring-data.version} + + + + javax.validation + validation-api + ${javax.validation-version} + + + org.hibernate.validator + hibernate-validator + ${hibernate-validator.version} + + org.springframework.security spring-security-web - ${springframework-security.version} + ${spring-security.version} org.springframework.security spring-security-config - ${springframework-security.version} + ${spring-security.version} + org.thymeleaf @@ -50,10 +70,9 @@ org.thymeleaf - thymeleaf-spring4 + thymeleaf-spring5 ${org.thymeleaf-version} - nz.net.ultraq.thymeleaf thymeleaf-layout-dialect @@ -64,60 +83,29 @@ thymeleaf-extras-java8time ${org.thymeleaf.extras-version} + javax.servlet javax.servlet-api - ${javax.servlet-version} + ${javax.servlet-api.version} provided - - - javax.validation - validation-api - ${javax.validation-version} - - - org.hibernate - hibernate-validator - ${hibernate-validator.version} - - + org.springframework spring-test - ${org.springframework-version} + ${spring.version} test - org.springframework.security spring-security-test - ${springframework-security.version} + ${spring-security.version} test - - - - - - org.springframework.data - spring-data-commons - ${springFramework-data.version} - @@ -131,7 +119,7 @@ false - + org.codehaus.cargo cargo-maven2-plugin @@ -151,7 +139,7 @@ - + org.apache.tomcat.maven tomcat7-maven-plugin @@ -176,22 +164,14 @@ - - 4.3.4.RELEASE - 4.2.0.RELEASE - 2.0.7.RELEASE - 3.1.0 - + 2.0.9.RELEASE 3.0.9.RELEASE - 3.0.0.RELEASE - 2.1.2 - - 1.1.0.Final - 5.3.3.Final - 5.2.5.Final + 3.0.1.RELEASE + 2.3.0 + 2.0.1.Final + 6.0.11.Final - 2.6 1.6.1 2.2 diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java index 2e76877199..34a59ea391 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java @@ -2,6 +2,9 @@ package com.baeldung.thymeleaf.config; import java.util.Locale; +import nz.net.ultraq.thymeleaf.LayoutDialect; +import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; + import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; @@ -15,23 +18,20 @@ import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; -import org.thymeleaf.TemplateEngine; import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; -import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.spring5.ISpringTemplateEngine; +import org.thymeleaf.spring5.SpringTemplateEngine; +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.ITemplateResolver; import com.baeldung.thymeleaf.formatter.NameFormatter; import com.baeldung.thymeleaf.utils.ArrayUtil; -import nz.net.ultraq.thymeleaf.LayoutDialect; -import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; - @Configuration @EnableWebMvc @ComponentScan({ "com.baeldung.thymeleaf" }) @@ -39,10 +39,11 @@ import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; * Java configuration file that is used for Spring MVC and Thymeleaf * configurations */ -public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { +public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware { private ApplicationContext applicationContext; + @Override public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @@ -77,7 +78,7 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application return resolver; } - private TemplateEngine templateEngine(ITemplateResolver templateResolver) { + private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.addDialect(new LayoutDialect(new GroupingStrategy())); engine.addDialect(new Java8TimeDialect()); diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java index 46bff38a3f..ea51ca3cd9 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java @@ -27,7 +27,7 @@ public class WebMVCSecurity extends WebSecurityConfigurerAdapter { @Override protected void configure(final AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER"); + auth.inMemoryAuthentication().withUser("user1").password("{noop}user1Pass").authorities("ROLE_USER"); } @Override diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsIntegrationTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsIntegrationTest.java index 5bc45d0004..c6158b76b1 100644 --- a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsIntegrationTest.java +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FragmentsIntegrationTest.java @@ -1,10 +1,22 @@ package com.baeldung.thymeleaf.controller; +import static org.hamcrest.Matchers.containsString; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.servlet.Filter; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -15,21 +27,6 @@ import com.baeldung.thymeleaf.config.WebApp; import com.baeldung.thymeleaf.config.WebMVCConfig; import com.baeldung.thymeleaf.config.WebMVCSecurity; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; - -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import javax.servlet.Filter; - -import static org.hamcrest.Matchers.containsString; - @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) diff --git a/spring-vertx/pom.xml b/spring-vertx/pom.xml index 339c72bcdf..4ba90b841e 100644 --- a/spring-vertx/pom.xml +++ b/spring-vertx/pom.xml @@ -11,11 +11,10 @@ A demo project with vertx spring integration - org.springframework.boot - spring-boot-starter-parent - 1.5.3.RELEASE - - + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index 2ef290a7c1..93365264ac 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -33,6 +33,12 @@ ${junit.vintage.version} test + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit.vintage.version} + test + org.apache.logging.log4j log4j-core @@ -54,6 +60,24 @@ spring-context ${spring.version} + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + junit + junit + + + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + test + @@ -94,13 +118,14 @@ - 5.1.0 - 1.1.0 + 5.2.0 + 1.2.0 5.2.0 2.8.2 1.4.196 - 2.11.0 - 2.19.1 + 2.8.9 + 1.7.4 + 2.21.0 1.6.0 5.0.1.RELEASE diff --git a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCalling.java b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCalling.java similarity index 83% rename from testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCalling.java rename to testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCalling.java index b0709bf6bf..e55c6b98e2 100644 --- a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCalling.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCalling.java @@ -1,7 +1,7 @@ /** * */ -package org.baeldung.testing.abstractclass.abstractmethod; +package com.baeldung.abstractclass.abstractmethod; /** * When method calls abstract method. diff --git a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependent.java b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependent.java similarity index 76% rename from testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependent.java rename to testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependent.java index 7456a51c43..4dce2665d5 100644 --- a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependent.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependent.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.indepedentmethod; +package com.baeldung.abstractclass.indepedentmethod; /** * Test Independent Method diff --git a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/ConcreteImpl.java b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/ConcreteImpl.java similarity index 68% rename from testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/ConcreteImpl.java rename to testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/ConcreteImpl.java index f568ad4eec..6e71b88946 100644 --- a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/indepedentmethod/ConcreteImpl.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/indepedentmethod/ConcreteImpl.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.indepedentmethod; +package com.baeldung.abstractclass.indepedentmethod; public class ConcreteImpl extends AbstractIndependent { diff --git a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFields.java b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFields.java similarity index 87% rename from testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFields.java rename to testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFields.java index 3761eb8c3b..262f72e393 100644 --- a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFields.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFields.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.instancefields; +package com.baeldung.abstractclass.instancefields; /** * Test Independent Method diff --git a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethods.java b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethods.java similarity index 84% rename from testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethods.java rename to testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethods.java index 98a9bcaa07..fe7fb25931 100644 --- a/testing-modules/junit-abstract/src/main/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethods.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethods.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.privatemethod; +package com.baeldung.abstractclass.privatemethod; import java.time.LocalDateTime; diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/AssertionUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/AssertionUnitTest.java index 79ba882205..31b6e14d6c 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/AssertionUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/AssertionUnitTest.java @@ -11,14 +11,17 @@ import java.util.Optional; import java.util.function.BooleanSupplier; import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; /** * Unit test that demonstrate the different assertions available within JUnit 4 */ +@DisplayName("Test case for assertions") public class AssertionUnitTest { @Test + @DisplayName("Arrays should be equals") public void whenAssertingArraysEquality_thenEqual() { char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'}; char[] actual = "Jupiter".toCharArray(); @@ -27,6 +30,7 @@ public class AssertionUnitTest { } @Test + @DisplayName("The area of two polygons should be equal") public void whenAssertingEquality_thenEqual() { float square = 2 * 2; float rectangle = 2 * 2; diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/NestedUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/NestedUnitTest.java index 7586a2f51a..299cac2480 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/NestedUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/NestedUnitTest.java @@ -10,13 +10,13 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class NestedUnitTest { + Stack stack; - boolean isRun = false; @Test @DisplayName("is instantiated with new Stack()") void isInstantiatedWithNew() { - new Stack(); + new Stack<>(); } @Nested @@ -25,7 +25,7 @@ public class NestedUnitTest { @BeforeEach void init() { - stack = new Stack(); + stack = new Stack<>(); } @Test diff --git a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java similarity index 94% rename from testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java index e02915db51..7ebd866f3d 100644 --- a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/abstractmethod/AbstractMethodCallingUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.abstractmethod; +package com.baeldung.abstractclass.abstractmethod; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; diff --git a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java similarity index 91% rename from testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java index 80e42af756..db9811e6c1 100644 --- a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/indepedentmethod/AbstractIndependentUnitTest.java @@ -1,7 +1,7 @@ /** * */ -package org.baeldung.testing.abstractclass.indepedentmethod; +package com.baeldung.abstractclass.indepedentmethod; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java similarity index 95% rename from testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java index ce2577521b..5187ce00ab 100644 --- a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/instancefields/AbstractInstanceFieldsUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.testing.abstractclass.instancefields; +package com.baeldung.abstractclass.instancefields; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java similarity index 95% rename from testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java index 7220d9cc47..9ce7ff8706 100644 --- a/testing-modules/junit-abstract/src/test/java/org/baeldung/testing/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/abstractclass/privatemethod/AbstractPrivateMethodsUnitTest.java @@ -1,9 +1,7 @@ /** * */ -package org.baeldung.testing.abstractclass.privatemethod; - -import java.time.LocalDateTime; +package com.baeldung.abstractclass.privatemethod; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -12,6 +10,8 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.time.LocalDateTime; + /** * Providing custom values for private methods using powermock * diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/AssumptionUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/AssumptionUnitTest.java similarity index 69% rename from testing-modules/junit-5/src/test/java/com/baeldung/AssumptionUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/AssumptionUnitTest.java index 7d67d93486..80201a3f44 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/AssumptionUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/AssumptionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.migration.junit5; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -10,19 +10,19 @@ import org.junit.jupiter.api.Test; public class AssumptionUnitTest { @Test - void trueAssumption() { - assumeTrue(5 > 1); + public void trueAssumption() { + assumeTrue(5 > 1, () -> "5 is greater the 1"); assertEquals(5 + 2, 7); } @Test - void falseAssumption() { - assumeFalse(5 < 1); + public void falseAssumption() { + assumeFalse(5 < 1, () -> "5 is less then 1"); assertEquals(5 + 2, 7); } @Test - void assumptionThat() { + public void assumptionThat() { String someString = "Just a string"; assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4)); } diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java new file mode 100644 index 0000000000..835132240e --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.runfromjava; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class FirstUnitTest { + + @Test + void whenThis_thenThat() { + assertTrue(true); + } + + @Test + void whenSomething_thenSomething() { + assertTrue(true); + } + + @Test + void whenSomethingElse_thenSomethingElse() { + assertTrue(true); + } + +} diff --git a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit5/runfromjava/RunJUnit5Tests.java b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/RunJUnit5TestsFromJava.java similarity index 90% rename from testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit5/runfromjava/RunJUnit5Tests.java rename to testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/RunJUnit5TestsFromJava.java index 8a8f3aae17..309c1bc8c9 100644 --- a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit5/runfromjava/RunJUnit5Tests.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/RunJUnit5TestsFromJava.java @@ -1,4 +1,4 @@ -package com.baeldung.junit5.runfromjava; +package com.baeldung.runfromjava; import org.junit.platform.launcher.Launcher; import org.junit.platform.launcher.LauncherDiscoveryRequest; @@ -14,7 +14,7 @@ import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNa import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage; -public class RunJUnit5Tests { +public class RunJUnit5TestsFromJava { SummaryGeneratingListener listener = new SummaryGeneratingListener(); public void runOne() { @@ -32,7 +32,7 @@ public class RunJUnit5Tests { public void runAll() { LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder .request() - .selectors(selectPackage("com.baeldung.junit5.runfromjava")) + .selectors(selectPackage("com.baeldung.runfromjava")) .filters(includeClassNamePatterns(".*Test")) .build(); Launcher launcher = LauncherFactory.create(); @@ -45,7 +45,7 @@ public class RunJUnit5Tests { } public static void main(String[] args) { - RunJUnit5Tests runner = new RunJUnit5Tests(); + RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava(); runner.runAll(); TestExecutionSummary summary = runner.listener.getSummary(); diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java new file mode 100644 index 0000000000..b6a387d852 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.runfromjava; + +import org.junit.jupiter.api.RepeatedTest; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SecondUnitTest { + + @RepeatedTest(10) + void whenSomething_thenSomething() { + assertTrue(true); + } + + @RepeatedTest(5) + void whenSomethingElse_thenSomethingElse() { + assertTrue(true); + } +} diff --git a/testing-modules/junit-abstract/pom.xml b/testing-modules/junit-abstract/pom.xml deleted file mode 100644 index 106ed8e3a4..0000000000 --- a/testing-modules/junit-abstract/pom.xml +++ /dev/null @@ -1,62 +0,0 @@ - - 4.0.0 - - junit-abstract - 1.0-SNAPSHOT - jar - - abstractclasses - http://maven.apache.org - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - UTF-8 - 1.7.4 - 1.8 - 5.1.0 - 1.1.0 - 5.2.0 - - - - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - junit - junit - - - - - org.powermock - powermock-api-mockito2 - ${powermock.version} - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.0 - - - junit-abstract - - - - - diff --git a/testing-modules/junit5-migration/README.md b/testing-modules/junit5-migration/README.md new file mode 100644 index 0000000000..5fe7fc1f16 --- /dev/null +++ b/testing-modules/junit5-migration/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: +- [JUnit4 -> JUnit5 migration guide](http://www.baeldung.com/junit4-junit5-migration-guide) + diff --git a/testing-modules/junit5-migration/pom.xml b/testing-modules/junit5-migration/pom.xml new file mode 100644 index 0000000000..9d9d418774 --- /dev/null +++ b/testing-modules/junit5-migration/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + junit5-migration + 1.0-SNAPSHOT + junit5-migration + JUnit 4 -> JUnit 5 migration + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.platform + junit-platform-engine + ${junit.platform.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + org.junit.jupiter + junit-jupiter-migrationsupport + ${junit.vintage.version} + test + + + + + + + src/test/resources + true + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + + java + + + + + com.baeldung.TestLauncher + + + + + + + 5.2.0 + 1.2.0 + 5.2.0 + 2.21.0 + 1.6.0 + + + diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AnnotationTestExampleUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AnnotationTestExampleUnitTest.java new file mode 100644 index 0000000000..a5f5c19c65 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AnnotationTestExampleUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.junit4; + +import com.baeldung.junit4.categories.Annotations; +import com.baeldung.junit4.categories.JUnit4UnitTest; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(value = { Annotations.class, JUnit4UnitTest.class }) +public class AnnotationTestExampleUnitTest { + @Test(expected = Exception.class) + public void shouldRaiseAnException() throws Exception { + throw new Exception("This is my expected exception"); + } + + @Test(timeout = 1) + @Ignore + public void shouldFailBecauseTimeout() throws InterruptedException { + Thread.sleep(10); + } + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssertionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssertionUnitTest.java new file mode 100644 index 0000000000..8979c9ecbc --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssertionUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.junit4; + +import org.junit.Assert; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.hasItems; +import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; + +@DisplayName("Test case for assertions") +public class AssertionUnitTest { + + @Test + @DisplayName("Arrays should be equals") + public void whenAssertingArraysEquality_thenEqual() { + char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'}; + char[] actual = "Jupiter".toCharArray(); + + assertArrayEquals("Arrays should be equal", expected, actual); + } + + @Test + public void givenMultipleAssertion_whenAssertingAll_thenOK() { + assertEquals("4 is 2 times 2", 4, 2 * 2); + assertEquals("java", "JAVA".toLowerCase()); + assertEquals("null is equal to null", null, null); + } + + @Test + public void testAssertThatHasItems() { + assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin")); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssumeUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssumeUnitTest.java new file mode 100644 index 0000000000..f7730a3dd8 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/AssumeUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.junit4; + + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeTrue; + +public class AssumeUnitTest { + + @Test + public void trueAssumption() { + assumeTrue("5 is greater the 1", 5 > 1); + assertEquals(5 + 2, 7); + } + + @Test + public void falseAssumption() { + assumeFalse("5 is less then 1", 5 < 1); + assertEquals(5 + 2, 7); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/ExceptionAssertionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/ExceptionAssertionUnitTest.java new file mode 100644 index 0000000000..38e7f640a2 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/ExceptionAssertionUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.junit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ExceptionAssertionUnitTest { + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Test(expected = NullPointerException.class) + public void whenExceptionThrown_thenExpectationSatisfied() { + String test = null; + test.length(); + } + + @Test + public void whenExceptionThrown_thenRuleIsApplied() { + exceptionRule.expect(NumberFormatException.class); + exceptionRule.expectMessage("For input string"); + Integer.parseInt("1a"); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/RuleExampleUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/RuleExampleUnitTest.java new file mode 100644 index 0000000000..35f96b4f6a --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/RuleExampleUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.junit4; + +import org.junit.Rule; +import org.junit.Test; + +public class RuleExampleUnitTest { + + @Rule + public final TraceUnitTestRule traceRuleTests = new TraceUnitTestRule(); + + @Test + public void whenTracingTests() { + System.out.println("This is my test"); + /*...*/ + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TestAnnotationsUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TestAnnotationsUnitTest.java new file mode 100644 index 0000000000..89c33cafe6 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TestAnnotationsUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.junit4; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; + +import java.util.logging.Logger; + +public class TestAnnotationsUnitTest { + + private static final Logger log = Logger.getLogger(TestAnnotationsUnitTest.class.getName()); + + @BeforeClass + static void setup() { + log.info("@BeforeAll - executes once before all test methods in this class"); + } + + @Before + void init() { + log.info("@BeforeEach - executes before each test method in this class"); + } + + @After + void tearDown() { + log.info("@AfterEach - executed after each test method."); + } + + @AfterClass + static void done() { + log.info("@AfterAll - executed after all test methods."); + } + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TraceUnitTestRule.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TraceUnitTestRule.java new file mode 100644 index 0000000000..9f4811da3d --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/TraceUnitTestRule.java @@ -0,0 +1,35 @@ +package com.baeldung.junit4; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.MultipleFailureException; +import org.junit.runners.model.Statement; + +import java.util.ArrayList; +import java.util.List; + +public class TraceUnitTestRule implements TestRule { + + @Override + public Statement apply(Statement base, Description description) { + + return new Statement() { + @Override + public void evaluate() throws Throwable { + List errors = new ArrayList(); + + System.out.println("Starting test ... " + description.getMethodName()); + try { + base.evaluate(); + } catch (Throwable e) { + errors.add(e); + } finally { + System.out.println("... test finished. " + description.getMethodName()); + } + + MultipleFailureException.assertEmpty(errors); + } + }; + } + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/Annotations.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/Annotations.java new file mode 100644 index 0000000000..22214d95b6 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/Annotations.java @@ -0,0 +1,5 @@ +package com.baeldung.junit4.categories; + +public interface Annotations { + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/JUnit4UnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/JUnit4UnitTest.java new file mode 100644 index 0000000000..a7474d5f24 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit4/categories/JUnit4UnitTest.java @@ -0,0 +1,5 @@ +package com.baeldung.junit4.categories; + +public interface JUnit4UnitTest { + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AnnotationTestExampleUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AnnotationTestExampleUnitTest.java new file mode 100644 index 0000000000..16de0be9d8 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AnnotationTestExampleUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import java.time.Duration; + +@Tag("annotations") +@Tag("junit5") +@RunWith(JUnitPlatform.class) +public class AnnotationTestExampleUnitTest { + @Test + public void shouldRaiseAnException() throws Exception { + Assertions.assertThrows(Exception.class, () -> { + throw new Exception("This is my expected exception"); + }); + } + + @Test + @Disabled + public void shouldFailBecauseTimeout() throws InterruptedException { + Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10)); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java new file mode 100644 index 0000000000..d1d08c6e62 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssertionUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisplayName("Test case for assertions") +public class AssertionUnitTest { + + @Test + @DisplayName("Arrays should be equals") + public void whenAssertingArraysEquality_thenEqual() { + char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'}; + char[] actual = "Jupiter".toCharArray(); + + assertArrayEquals(expected, actual, "Arrays should be equal"); + } + + @Test + @DisplayName("The area of two polygons should be equal") + public void whenAssertingEquality_thenEqual() { + float square = 2 * 2; + float rectangle = 2 * 2; + + assertEquals(square, rectangle); + } + + @Test + public void givenMultipleAssertion_whenAssertingAll_thenOK() { + assertAll( + "heading", + () -> assertEquals(4, 2 * 2, "4 is 2 times 2"), + () -> assertEquals("java", "JAVA".toLowerCase()), + () -> assertEquals(null, null, "null is equal to null") + ); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssumptionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssumptionUnitTest.java new file mode 100644 index 0000000000..4f3d827403 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/AssumptionUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assumptions.*; + +public class AssumptionUnitTest { + + @Test + public void trueAssumption() { + assumeTrue(5 > 1, () -> "5 is greater the 1"); + assertEquals(5 + 2, 7); + } + + @Test + public void falseAssumption() { + assumeFalse(5 < 1, () -> "5 is less then 1"); + assertEquals(5 + 2, 7); + } + + @Test + public void assumptionThat() { + String someString = "Just a string"; + assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4)); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/ConditionalExecutionUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/ConditionalExecutionUnitTest.java new file mode 100644 index 0000000000..7f76e237f1 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/ConditionalExecutionUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.*; + +import static org.junit.jupiter.api.Assertions.*; + +public class ConditionalExecutionUnitTest { + + @Test + @EnabledOnOs({OS.MAC}) + void whenOperatingSystemIsMac_thenTestIsEnabled() { + assertEquals(5 + 2, 7); + } + + @Test + @DisabledOnOs({OS.WINDOWS}) + void whenOperatingSystemIsWindows_thenTestIsDisabled() { + assertEquals(5 + 2, 7); + } + + @Test + @EnabledOnJre({JRE.JAVA_8}) + void whenRunningTestsOnJRE8_thenTestIsEnabled() { + assertTrue(5 > 4, "5 is greater the 4"); + assertTrue(null == null, "null is equal to null"); + } + + @Test + @DisabledOnJre({JRE.JAVA_10}) + void whenRunningTestsOnJRE10_thenTestIsDisabled() { + assertTrue(5 > 4, "5 is greater the 4"); + assertTrue(null == null, "null is equal to null"); + } + + @Test + @EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*") + public void whenRunningTestsOn64BitArchitectures_thenTestIsDisabled() { + Integer value = 5; // result of an algorithm + + assertNotEquals(0, value, "The result cannot be 0"); + } + + @Test + @DisabledIfSystemProperty(named = "ci-server", matches = "true") + public void whenRunningTestsOnCIServer_thenTestIsDisabled() { + Integer value = 5; // result of an algorithm + + assertNotEquals(0, value, "The result cannot be 0"); + } + + @Test + @EnabledIfEnvironmentVariable(named = "ENV", matches = "staging-server") + public void whenRunningTestsStagingServer_thenTestIsEnabled() { + char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'}; + char[] actual = "Jupiter".toCharArray(); + + assertArrayEquals(expected, actual, "Arrays should be equal"); + } + + @Test + @DisabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*") + public void whenRunningTestsDevelopmentEnvironment_thenTestIsDisabled() { + char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'}; + char[] actual = "Jupiter".toCharArray(); + + assertArrayEquals(expected, actual, "Arrays should be equal"); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/NestedUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/NestedUnitTest.java new file mode 100644 index 0000000000..9eeff471ba --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/NestedUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.*; + +import java.util.EmptyStackException; +import java.util.Stack; + +public class NestedUnitTest { + + Stack stack; + + @Test + @DisplayName("is instantiated with new Stack()") + void isInstantiatedWithNew() { + new Stack<>(); + } + + @Nested + @DisplayName("when new") + class WhenNew { + + @BeforeEach + void init() { + stack = new Stack<>(); + } + + @Test + @DisplayName("is empty") + void isEmpty() { + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("throws EmptyStackException when popped") + void throwsExceptionWhenPopped() { + Assertions.assertThrows(EmptyStackException.class, () -> stack.pop()); + } + + @Test + @DisplayName("throws EmptyStackException when peeked") + void throwsExceptionWhenPeeked() { + Assertions.assertThrows(EmptyStackException.class, () -> stack.peek()); + } + + @Nested + @DisplayName("after pushing an element") + class AfterPushing { + + String anElement = "an element"; + + @BeforeEach + void init() { + stack.push(anElement); + } + + @Test + @DisplayName("it is no longer empty") + void isEmpty() { + Assertions.assertFalse(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when popped and is empty") + void returnElementWhenPopped() { + Assertions.assertEquals(anElement, stack.pop()); + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when peeked but remains not empty") + void returnElementWhenPeeked() { + Assertions.assertEquals(anElement, stack.peek()); + Assertions.assertFalse(stack.isEmpty()); + } + } + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleExampleUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleExampleUnitTest.java new file mode 100644 index 0000000000..297d0f1730 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +@RunWith(JUnitPlatform.class) +@ExtendWith(TraceUnitExtension.class) +public class RuleExampleUnitTest { + + @Test + public void whenTracingTests() { + System.out.println("This is my test"); + /*...*/ + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleMigrationSupportUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleMigrationSupportUnitTest.java new file mode 100644 index 0000000000..bbaa42ec7b --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/RuleMigrationSupportUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.junit5; + +import org.junit.Rule; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport; +import org.junit.rules.ExpectedException; + +@EnableRuleMigrationSupport +public class RuleMigrationSupportUnitTest { + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Test + public void whenExceptionThrown_thenExpectationSatisfied() { + exceptionRule.expect(NullPointerException.class); + String test = null; + test.length(); + } + + @Test + public void whenExceptionThrown_thenRuleIsApplied() { + exceptionRule.expect(NumberFormatException.class); + exceptionRule.expectMessage("For input string"); + Integer.parseInt("1a"); + } +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TestAnnotationsUnitTest.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TestAnnotationsUnitTest.java new file mode 100644 index 0000000000..8daf952747 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TestAnnotationsUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.*; + +import java.util.logging.Logger; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestAnnotationsUnitTest { + + private static final Logger log = Logger.getLogger(TestAnnotationsUnitTest.class.getName()); + + @BeforeAll + static void setup() { + log.info("@BeforeAll - executes once before all test methods in this class"); + } + + @BeforeEach + void init() { + log.info("@BeforeEach - executes before each test method in this class"); + } + + @Test + @Disabled + void disabledTest() { + assertTrue(false); + } + + @AfterEach + void tearDown() { + log.info("@AfterEach - executed after each test method."); + } + + @AfterAll + static void done() { + log.info("@AfterAll - executed after all test methods."); + } + +} diff --git a/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TraceUnitExtension.java b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TraceUnitExtension.java new file mode 100644 index 0000000000..bd5ae47d63 --- /dev/null +++ b/testing-modules/junit5-migration/src/test/java/com/baeldung/junit5/TraceUnitExtension.java @@ -0,0 +1,19 @@ +package com.baeldung.junit5; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +public class TraceUnitExtension implements AfterEachCallback, BeforeEachCallback { + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + System.out.println("Starting test ... " + context.getDisplayName()); + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + System.out.println("... test finished. " + context.getDisplayName()); + } + +} diff --git a/testing-modules/parallel-tests-junit/math-test-functions/pom.xml b/testing-modules/parallel-tests-junit/math-test-functions/pom.xml new file mode 100644 index 0000000000..18d2b562f3 --- /dev/null +++ b/testing-modules/parallel-tests-junit/math-test-functions/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + com.baeldung + parallel-tests-junit + 0.0.1-SNAPSHOT + + math-test-functions + math-test-functions + http://maven.apache.org + + UTF-8 + + + + junit + junit + 4.12 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + all + 10 + 2 + 2 + 6 + 3.5 + 5 + true + + FunctionTestSuite.class + + + + + + diff --git a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/MathFunctionTest.java b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ArithmeticFunctionTest.java similarity index 78% rename from testing-modules/parallel-tests-junit/src/test/java/com/baeldung/MathFunctionTest.java rename to testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ArithmeticFunctionTest.java index 9a609c3e93..df0aa695fc 100644 --- a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/MathFunctionTest.java +++ b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ArithmeticFunctionTest.java @@ -1,29 +1,28 @@ -package com.baeldung; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class MathFunctionTest { - - @Test - public void test_addingIntegers_returnsSum() throws InterruptedException { - assertEquals(22, Math.addExact(10, 12)); - - } - - @Test - public void test_multiplyingIntegers_returnsProduct() { - assertEquals(120, Math.multiplyExact(10, 12)); - } - - @Test - public void test_subtractingIntegers_returnsDifference() { - assertEquals(2, Math.subtractExact(12, 10)); - } - - @Test - public void test_minimumInteger() { - assertEquals(10, Math.min(10, 12)); - } -} +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArithmeticFunctionTest { + + @Test + public void test_addingIntegers_returnsSum() { + assertEquals(22, Math.addExact(10, 12)); + } + + @Test + public void test_multiplyingIntegers_returnsProduct() { + assertEquals(120, Math.multiplyExact(10, 12)); + } + + @Test + public void test_subtractingIntegers_returnsDifference() { + assertEquals(2, Math.subtractExact(12, 10)); + } + + @Test + public void test_minimumInteger() { + assertEquals(10, Math.min(10, 12)); + } +} diff --git a/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ComparisonFunctionTest.java b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ComparisonFunctionTest.java new file mode 100644 index 0000000000..4f72c87279 --- /dev/null +++ b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/ComparisonFunctionTest.java @@ -0,0 +1,19 @@ +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ComparisonFunctionTest { + + @Test + public void test_findMax() { + assertEquals(20, Math.max(10, 20)); + } + + @Test + public void test_findMin() { + assertEquals(10, Math.min(10, 20)); + } + +} diff --git a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/FunctionTestSuite.java b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/FunctionTestSuite.java similarity index 71% rename from testing-modules/parallel-tests-junit/src/test/java/com/baeldung/FunctionTestSuite.java rename to testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/FunctionTestSuite.java index c7f4050b18..4fe551b365 100644 --- a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/FunctionTestSuite.java +++ b/testing-modules/parallel-tests-junit/math-test-functions/src/test/java/com/baeldung/FunctionTestSuite.java @@ -1,11 +1,11 @@ -package com.baeldung; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({StringFunctionTest.class, MathFunctionTest.class}) -public class FunctionTestSuite { - -} +package com.baeldung; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ComparisonFunctionTest.class, ArithmeticFunctionTest.class }) +public class FunctionTestSuite { + +} diff --git a/testing-modules/parallel-tests-junit/pom.xml b/testing-modules/parallel-tests-junit/pom.xml index 1a42437b1b..3fd4e695e5 100644 --- a/testing-modules/parallel-tests-junit/pom.xml +++ b/testing-modules/parallel-tests-junit/pom.xml @@ -1,50 +1,12 @@ - - 4.0.0 - - com.baeldung - parallel-tests-junit - 0.0.1-SNAPSHOT - jar - - parallel-tests-junit - http://maven.apache.org - - - UTF-8 - - - - - junit - junit - 4.12 - test - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.0 - - all - 10 - 2 - 2 - 6 - 3.5 - 5 - true - - FunctionTestSuite.class - - - - - - - + + + 4.0.0 + com.baeldung + parallel-tests-junit + 0.0.1-SNAPSHOT + pom + + math-test-functions + string-test-functions + diff --git a/testing-modules/parallel-tests-junit/string-test-functions/pom.xml b/testing-modules/parallel-tests-junit/string-test-functions/pom.xml new file mode 100644 index 0000000000..af61cfce8e --- /dev/null +++ b/testing-modules/parallel-tests-junit/string-test-functions/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.baeldung + parallel-tests-junit + 0.0.1-SNAPSHOT + + string-test-functions + string-test-functions + http://maven.apache.org + + UTF-8 + + + + + junit + junit + 4.12 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + all + true + 2 + + + + + diff --git a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/StringFunctionTest.java b/testing-modules/parallel-tests-junit/string-test-functions/src/test/java/com/baeldung/StringFunctionTest.java similarity index 94% rename from testing-modules/parallel-tests-junit/src/test/java/com/baeldung/StringFunctionTest.java rename to testing-modules/parallel-tests-junit/string-test-functions/src/test/java/com/baeldung/StringFunctionTest.java index 9adfea8ff0..7f2bc5e5e7 100644 --- a/testing-modules/parallel-tests-junit/src/test/java/com/baeldung/StringFunctionTest.java +++ b/testing-modules/parallel-tests-junit/string-test-functions/src/test/java/com/baeldung/StringFunctionTest.java @@ -1,18 +1,18 @@ -package com.baeldung; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class StringFunctionTest { - - @Test - public void test_upperCase() { - assertEquals("TESTCASE", "testCase".toUpperCase()); - } - - @Test - public void test_indexOf() { - assertEquals(1, "testCase".indexOf("e")); - } -} +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class StringFunctionTest { + + @Test + public void test_upperCase() { + assertEquals("TESTCASE", "testCase".toUpperCase()); + } + + @Test + public void test_indexOf() { + assertEquals(1, "testCase".indexOf("e")); + } +} diff --git a/testing-modules/runjunitfromjava/pom.xml b/testing-modules/runjunitfromjava/pom.xml deleted file mode 100644 index 8ad3e7ed00..0000000000 --- a/testing-modules/runjunitfromjava/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - 4.0.0 - - com.baeldung.junit - applicationtesting - 0.0.1-SNAPSHOT - jar - - applicationtesting - http://maven.apache.org - - - UTF-8 - 1.8 - 5.2.0 - 1.2.0 - 4.12 - 3.7.0 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${java.version} - ${java.version} - - - - - - - - org.junit.jupiter - junit-jupiter-engine - ${junit-jupiter.version} - - - org.junit.platform - junit-platform-launcher - ${junit-launcher.version} - - - junit - junit - ${junit4.version} - - - - diff --git a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/ListNode.java b/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/ListNode.java deleted file mode 100644 index 31cbda15d3..0000000000 --- a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/ListNode.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.junit.runfromjava.listnode; - -public class ListNode { - private int value; - private ListNode next; - - public ListNode(int v) { - value = v; - } - - public ListNode(int v, ListNode next) { - value = v; - this.next = next; - } - - public int getValue() { - return value; - } - - public ListNode getNext() { - return next; - } - - public void setNext(ListNode next) { - this.next = next; - } - - public String toString() { - String result = ""; - ListNode tmp = this; - - while (tmp.next != null) { - result += tmp.value + "->"; - tmp = tmp.next; - } - - result += tmp.value; - - return result.toString(); - } -} diff --git a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/MergeLists.java b/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/MergeLists.java deleted file mode 100644 index f2a24487c8..0000000000 --- a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/MergeLists.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.junit.runfromjava.listnode; - -public class MergeLists { - - public ListNode merge(ListNode list1, ListNode list2) { - - if (list1 == null) { - return list2; - } - if (list2 == null) { - return list1; - } - - if (list1.getValue() <= list2.getValue()) { - list1.setNext(merge(list1.getNext(), list2)); - return list1; - } else { - list2.setNext(merge(list2.getNext(), list1)); - return list2; - } - } -} \ No newline at end of file diff --git a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RemovedNthElement.java b/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RemovedNthElement.java deleted file mode 100644 index 22357aaeee..0000000000 --- a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RemovedNthElement.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.junit.runfromjava.listnode; - -public class RemovedNthElement { - public ListNode removeNthFromEnd(ListNode head, int n) { - - ListNode start = new ListNode(0); - start.setNext(head); - - ListNode fast = start; - ListNode slow = start; - - for (int i = 0; i < n + 1 && fast != null; i++) { - fast = fast.getNext(); - } - - while (fast != null) { - fast = fast.getNext(); - slow = slow.getNext(); - } - - slow.setNext(slow.getNext() - .getNext()); - - return start.getNext(); - } -} diff --git a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RotateList.java b/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RotateList.java deleted file mode 100644 index 52167cbacc..0000000000 --- a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/RotateList.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.junit.runfromjava.listnode; - -public class RotateList { - public ListNode rotateRight(ListNode list, int n) { - - if (list == null || list.getNext() == null) { - return list; - } - - ListNode tmpList = new ListNode(0); - tmpList.setNext(list); - ListNode fast = tmpList; - ListNode slow = tmpList; - - int listLength; - for (listLength = 0; fast.getNext() != null; listLength++) { - fast = fast.getNext(); - } - - for (int j = listLength - n % listLength; j > 0; j--) { - slow = slow.getNext(); - } - - fast.setNext(tmpList.getNext()); - tmpList.setNext(slow.getNext()); - slow.setNext(null); - - return tmpList.getNext(); - } -} diff --git a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/SwapNodes.java b/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/SwapNodes.java deleted file mode 100644 index 076fed0d5e..0000000000 --- a/testing-modules/runjunitfromjava/src/main/java/com/baeldung/junit/runfromjava/listnode/SwapNodes.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.junit.runfromjava.listnode; - -public class SwapNodes { - public ListNode swapPairs(ListNode listHead) { - - ListNode result = new ListNode(0); - result.setNext(listHead); - - ListNode current = result; - - while (current.getNext() != null && current - .getNext() - .getNext() != null) { - - ListNode first = current.getNext(); - ListNode second = current - .getNext() - .getNext(); - - first.setNext(second.getNext()); - current.setNext(second); - current - .getNext() - .setNext(first); - - current = current - .getNext() - .getNext(); - } - - return result.getNext(); - } -} diff --git a/testing-modules/test-containers/pom.xml b/testing-modules/test-containers/pom.xml index 2a8f434040..0ace187555 100644 --- a/testing-modules/test-containers/pom.xml +++ b/testing-modules/test-containers/pom.xml @@ -109,7 +109,7 @@ 1.7.2 42.2.2 3.12.0 - 2.19.1 + 2.21.0 diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java new file mode 100644 index 0000000000..0e08c43021 --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.runfromjava; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class FirstUnitTest { + + @Test + public void whenThis_thenThat() { + assertTrue(true); + } + + @Test + public void whenSomething_thenSomething() { + assertTrue(true); + } + + @Test + public void whenSomethingElse_thenSomethingElse() { + assertTrue(true); + } + +} diff --git a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/MyTestSuite.java b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/MyTestSuite.java similarity index 81% rename from testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/MyTestSuite.java rename to testing-modules/testing/src/test/java/com/baeldung/runfromjava/MyTestSuite.java index 4739b47b88..2413fb1ddf 100644 --- a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/MyTestSuite.java +++ b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/MyTestSuite.java @@ -1,4 +1,4 @@ -package com.baeldung.junit4.runfromjava; +package com.baeldung.runfromjava; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/RunJUnit4Tests.java b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java similarity index 97% rename from testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/RunJUnit4Tests.java rename to testing-modules/testing/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java index 69b821032a..b44b66440a 100644 --- a/testing-modules/runjunitfromjava/src/test/java/com/baeldung/junit4/runfromjava/RunJUnit4Tests.java +++ b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java @@ -1,4 +1,4 @@ -package com.baeldung.junit4.runfromjava; +package com.baeldung.runfromjava; import junit.extensions.ActiveTestSuite; import junit.extensions.RepeatedTest; @@ -10,7 +10,7 @@ import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; -public class RunJUnit4Tests { +public class RunJUnit4TestsFromJava { public static void runOne() { JUnitCore junit = new JUnitCore(); diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java new file mode 100644 index 0000000000..aa5883562e --- /dev/null +++ b/testing-modules/testing/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.runfromjava; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class SecondUnitTest { + + @Test + public void whenSomething_thenSomething() { + assertTrue(true); + } + + @Test + public void whensomethingElse_thenSomethingElse() { + assertTrue(true); + } +} diff --git a/vaadin-spring/pom.xml b/vaadin-spring/pom.xml index 3f411cc7a1..09f79f2676 100644 --- a/vaadin-spring/pom.xml +++ b/vaadin-spring/pom.xml @@ -9,9 +9,10 @@ 0.1.0 - org.springframework.boot - spring-boot-starter-parent - 2.0.3.RELEASE + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2