Merge branch 'masterEugen'

This commit is contained in:
Marcos Lopez Gonzalez 2018-06-23 18:12:29 +02:00
commit ca5d22fc15
101 changed files with 2382 additions and 341 deletions

6
.gitignore vendored
View File

@ -42,3 +42,9 @@ spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
*.springBeans
20171220-JMeter.csv
.factorypath
dependency-reduced-pom.xml
*.so
*.dylib
*.dll

View File

@ -14,6 +14,24 @@
</parent>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
@ -42,4 +60,4 @@
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,18 @@
package com.baeldung.dependencyinjection.application;
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class FileApplication {
public static void main(String[] args) {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
ImageFileProcessor imageFileProcessor = container.select(ImageFileProcessor.class).get();
System.out.println(imageFileProcessor.openFile("file1.png"));
System.out.println(imageFileProcessor.writeFile("file1.png"));
System.out.println(imageFileProcessor.saveFile("file1.png"));
container.shutdown();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.dependencyinjection.factories;
import com.baeldung.dependencyinjection.loggers.TimeLogger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.enterprise.inject.Produces;
public class TimeLoggerFactory {
@Produces
public TimeLogger getTimeLogger() {
return new TimeLogger(new SimpleDateFormat("HH:mm"), Calendar.getInstance());
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.dependencyinjection.imagefileeditors;
import com.baeldung.dependencyinjection.qualifiers.GifFileEditorQualifier;
@GifFileEditorQualifier
public class GifFileEditor implements ImageFileEditor {
@Override
public String openFile(String fileName) {
return "Opening GIF file " + fileName;
}
@Override
public String editFile(String fileName) {
return "Editing GIF file " + fileName;
}
@Override
public String writeFile(String fileName) {
return "Writing GIF file " + fileName;
}
@Override
public String saveFile(String fileName) {
return "Saving GIF file " + fileName;
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.dependencyinjection.imagefileeditors;
public interface ImageFileEditor {
String openFile(String fileName);
String editFile(String fileName);
String writeFile(String fileName);
String saveFile(String fileName);
}

View File

@ -0,0 +1,27 @@
package com.baeldung.dependencyinjection.imagefileeditors;
import com.baeldung.dependencyinjection.qualifiers.JpgFileEditorQualifier;
@JpgFileEditorQualifier
public class JpgFileEditor implements ImageFileEditor {
@Override
public String openFile(String fileName) {
return "Opening JPG file " + fileName;
}
@Override
public String editFile(String fileName) {
return "Editing JPG file " + fileName;
}
@Override
public String writeFile(String fileName) {
return "Writing JPG file " + fileName;
}
@Override
public String saveFile(String fileName) {
return "Saving JPG file " + fileName;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.dependencyinjection.imagefileeditors;
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
@PngFileEditorQualifier
public class PngFileEditor implements ImageFileEditor {
@Override
public String openFile(String fileName) {
return "Opening PNG file " + fileName;
}
@Override
public String editFile(String fileName) {
return "Editing PNG file " + fileName;
}
@Override
public String writeFile(String fileName) {
return "Writing PNG file " + fileName;
}
@Override
public String saveFile(String fileName) {
return "Saving PNG file " + fileName;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.dependencyinjection.imageprocessors;
import com.baeldung.dependencyinjection.loggers.TimeLogger;
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
import javax.inject.Inject;
import com.baeldung.dependencyinjection.imagefileeditors.ImageFileEditor;
public class ImageFileProcessor {
private final ImageFileEditor imageFileEditor;
private final TimeLogger timeLogger;
@Inject
public ImageFileProcessor(@PngFileEditorQualifier ImageFileEditor imageFileEditor, TimeLogger timeLogger) {
this.imageFileEditor = imageFileEditor;
this.timeLogger = timeLogger;
}
public ImageFileEditor getImageFileditor() {
return imageFileEditor;
}
public TimeLogger getTimeLogger() {
return timeLogger;
}
public String openFile(String fileName) {
return imageFileEditor.openFile(fileName) + " at: " + timeLogger.getTime();
}
public String editFile(String fileName) {
return imageFileEditor.editFile(fileName) + " at: " + timeLogger.getTime();
}
public String writeFile(String fileName) {
return imageFileEditor.writeFile(fileName) + " at: " + timeLogger.getTime();
}
public String saveFile(String fileName) {
return imageFileEditor.saveFile(fileName)+ " at: " + timeLogger.getTime();
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.dependencyinjection.loggers;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TimeLogger {
private final SimpleDateFormat dateFormat;
private final Calendar calendar;
public TimeLogger(SimpleDateFormat dateFormat, Calendar calendar) {
this.dateFormat = dateFormat;
this.calendar = calendar;
}
public String getTime() {
return dateFormat.format(calendar.getTime());
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.dependencyinjection.qualifiers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
public @interface GifFileEditorQualifier {}

View File

@ -0,0 +1,12 @@
package com.baeldung.dependencyinjection.qualifiers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
public @interface JpgFileEditorQualifier {}

View File

@ -0,0 +1,12 @@
package com.baeldung.dependencyinjection.qualifiers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
public @interface PngFileEditorQualifier {}

View File

@ -0,0 +1,37 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class GifFileEditorUnitTest {
private static GifFileEditor gifFileEditor;
@BeforeClass
public static void setGifFileEditorInstance() {
gifFileEditor = new GifFileEditor();
}
@Test
public void givenGifFileEditorlInstance_whenCalledopenFile_thenOneAssertion() {
assertThat(gifFileEditor.openFile("file1.gif")).isEqualTo("Opening GIF file file1.gif");
}
@Test
public void givenGifFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
assertThat(gifFileEditor.editFile("file1.gif")).isEqualTo("Editing GIF file file1.gif");
}
@Test
public void givenGifFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
assertThat(gifFileEditor.writeFile("file1.gif")).isEqualTo("Writing GIF file file1.gif");
}
@Test
public void givenGifFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
assertThat(gifFileEditor.saveFile("file1.gif")).isEqualTo("Saving GIF file file1.gif");
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
import com.baeldung.dependencyinjection.loggers.TimeLogger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static org.assertj.core.api.Assertions.assertThat;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.BeforeClass;
import org.junit.Test;
public class ImageProcessorUnitTest {
private static ImageFileProcessor imageFileProcessor;
private static SimpleDateFormat dateFormat;
private static Calendar calendar;
@BeforeClass
public static void setImageProcessorInstance() {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
imageFileProcessor = container.select(ImageFileProcessor.class).get();
container.shutdown();
}
@BeforeClass
public static void setSimpleDateFormatInstance() {
dateFormat = new SimpleDateFormat("HH:mm");
}
@BeforeClass
public static void setCalendarInstance() {
calendar = Calendar.getInstance();
}
@Test
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
}
@Test
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() {
String currentTime = dateFormat.format(calendar.getTime());
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime);
}
@Test
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() {
String currentTime = dateFormat.format(calendar.getTime());
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime);
}
@Test
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() {
String currentTime = dateFormat.format(calendar.getTime());
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime);
}
@Test
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() {
String currentTime = dateFormat.format(calendar.getTime());
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class JpgFileEditorUnitTest {
private static JpgFileEditor jpgFileUtil;
@BeforeClass
public static void setJpgFileEditorInstance() {
jpgFileUtil = new JpgFileEditor();
}
@Test
public void givenJpgFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
assertThat(jpgFileUtil.openFile("file1.jpg")).isEqualTo("Opening JPG file file1.jpg");
}
@Test
public void givenJpgFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
assertThat(jpgFileUtil.editFile("file1.gif")).isEqualTo("Editing JPG file file1.gif");
}
@Test
public void givenJpgFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
assertThat(jpgFileUtil.writeFile("file1.jpg")).isEqualTo("Writing JPG file file1.jpg");
}
@Test
public void givenJpgFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
assertThat(jpgFileUtil.saveFile("file1.jpg")).isEqualTo("Saving JPG file file1.jpg");
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
@PngFileEditorQualifier
public class PngFileEditorUnitTest {
private static PngFileEditor pngFileEditor;
@BeforeClass
public static void setPngFileEditorInstance() {
pngFileEditor = new PngFileEditor();
}
@Test
public void givenPngFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
assertThat(pngFileEditor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png");
}
@Test
public void givenPngFileEditorInstance_whenCallededitFile_thenOneAssertion() {
assertThat(pngFileEditor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png");
}
@Test
public void givenPngFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
assertThat(pngFileEditor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png");
}
@Test
public void givenPngFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
assertThat(pngFileEditor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png");
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.factories.TimeLoggerFactory;
import com.baeldung.dependencyinjection.loggers.TimeLogger;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TimeLoggerFactoryUnitTest {
@Test
public void givenTimeLoggerFactory_whenCalledgetTimeLogger_thenOneAssertion() {
TimeLoggerFactory timeLoggerFactory = new TimeLoggerFactory();
assertThat(timeLoggerFactory.getTimeLogger()).isInstanceOf(TimeLogger.class);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.test.dependencyinjection;
import com.baeldung.dependencyinjection.loggers.TimeLogger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TimeLoggerUnitTest {
@Test
public void givenTimeLoggerInstance_whenCalledgetLogTime_thenOneAssertion() {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
Calendar calendar = Calendar.getInstance();
TimeLogger timeLogger = new TimeLogger(dateFormat, calendar);
String currentTime = dateFormat.format(calendar.getTime());
assertThat(timeLogger.getTime()).isEqualTo(currentTime);
}
}

View File

@ -220,6 +220,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
</excludes>
@ -289,6 +290,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -353,6 +353,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -222,6 +222,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
</excludes>
@ -387,6 +388,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -0,0 +1,18 @@
package com.baeldung.immutableobjects;
public final class Currency {
private final String value;
private Currency(String currencyValue) {
value = currencyValue;
}
public String getValue() {
return value;
}
public static Currency of(String value) {
return new Currency(value);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.immutableobjects;
// 4. Immutability in Java
public final class Money {
private final double amount;
private final Currency currency;
public Money(double amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}
public Currency getCurrency() {
return currency;
}
public double getAmount() {
return amount;
}
}

View File

@ -19,8 +19,7 @@ public class ExtensionUnitTest {
public void getExtension_whenStringHandle_thenExtensionIsTrue() {
String expectedExtension = "java";
Optional<String> actualExtension = extension.getExtensionByStringHandling("Demo.java");
Assert.assertTrue(actualExtension.isPresent());
actualExtension.ifPresent(ext -> Assert.assertEquals(expectedExtension,ext));
Assert.assertEquals(expectedExtension, actualExtension.get());
}
@Test

View File

@ -0,0 +1,36 @@
package com.baeldung.immutableobjects;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class ImmutableObjectsUnitTest {
@Test
public void whenCallingStringReplace_thenStringDoesNotMutate() {
// 2. What's an Immutable Object?
final String name = "baeldung";
final String newName = name.replace("dung", "----");
assertEquals("baeldung", name);
assertEquals("bael----", newName);
}
public void whenReassignFinalValue_thenCompilerError() {
// 3. The final Keyword in Java (1)
final String name = "baeldung";
// name = "bael...";
}
@Test
public void whenAddingElementToList_thenSizeChange() {
// 3. The final Keyword in Java (2)
final List<String> strings = new ArrayList<>();
assertEquals(0, strings.size());
strings.add("baeldung");
assertEquals(1, strings.size());
}
}

Binary file not shown.

View File

@ -11,6 +11,7 @@ public class UnitTestNamingConventionRule extends AbstractJavaRule {
private static List<String> allowedEndings = Arrays.asList(
"IntegrationTest",
"IntTest",
"ManualTest",
"JdbcTest",
"LiveTest",

View File

@ -0,0 +1,34 @@
package com.baeldung.servlets;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import static javax.servlet.RequestDispatcher.*;
@WebServlet(urlPatterns = "/errorHandler")
public class ErrorHandlerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html; charset=utf-8");
try (PrintWriter writer = resp.getWriter()) {
writer.write("<html><head><title>Error description</title></head><body>");
writer.write("<h2>Error description</h2>");
writer.write("<ul>");
Arrays.asList(ERROR_STATUS_CODE, ERROR_EXCEPTION_TYPE, ERROR_MESSAGE)
.forEach(e ->
writer.write("<li>" + e + ":" + req.getAttribute(e) + " </li>")
);
writer.write("</ul>");
writer.write("</html></body>");
}
Exception exception = (Exception) req.getAttribute(ERROR_EXCEPTION);
if (IllegalArgumentException.class.isInstance(exception)) {
getServletContext().log("Error on an application argument", exception);
}
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.servlets;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet(urlPatterns = "/randomError")
public class RandomErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, final HttpServletResponse resp) {
throw new IllegalStateException("Random error");
}
}

View File

@ -0,0 +1,16 @@
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<error-page>
<error-code>404</error-code>
<location>/error-404.html</location> <!-- /src/main/webapp/error-404.html-->
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorHandler</location>
</error-page>
</web-app>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error page</title>
</head>
<body>
<h2>Error: Page not found</h2>
<a href="./">Go back home</a>
</body>
</html>

View File

@ -1,48 +1,48 @@
package com.baeldung.jaxb.gen;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.baeldung.jaxb.gen package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.jaxb.gen
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link UserRequest }
*
*/
public UserRequest createUserRequest() {
return new UserRequest();
}
/**
* Create an instance of {@link UserResponse }
*
*/
public UserResponse createUserResponse() {
return new UserResponse();
}
}
package com.baeldung.jaxb.gen;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.baeldung.jaxb.gen package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.jaxb.gen
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link UserRequest }
*
*/
public UserRequest createUserRequest() {
return new UserRequest();
}
/**
* Create an instance of {@link UserResponse }
*
*/
public UserResponse createUserResponse() {
return new UserResponse();
}
}

View File

@ -1,87 +1,87 @@
package com.baeldung.jaxb.gen;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for UserRequest complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="UserRequest"&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserRequest", propOrder = {
"id",
"name"
})
@XmlRootElement(name = "userRequest")
public class UserRequest
implements Serializable
{
private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
/**
* Gets the value of the id property.
*
*/
public int getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(int value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
package com.baeldung.jaxb.gen;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for UserRequest complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="UserRequest"&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserRequest", propOrder = {
"id",
"name"
})
@XmlRootElement(name = "userRequest")
public class UserRequest
implements Serializable
{
private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
/**
* Gets the value of the id property.
*
*/
public int getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(int value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}

View File

@ -1,149 +1,149 @@
package com.baeldung.jaxb.gen;
import java.io.Serializable;
import java.util.Calendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.w3._2001.xmlschema.Adapter1;
/**
* <p>Java class for UserResponse complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="UserResponse"&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;element name="gender" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;element name="created" type="{http://www.w3.org/2001/XMLSchema}dateTime"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserResponse", propOrder = {
"id",
"name",
"gender",
"created"
})
@XmlRootElement(name = "userResponse")
public class UserResponse
implements Serializable
{
private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String gender;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected Calendar created;
/**
* Gets the value of the id property.
*
*/
public int getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(int value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the gender property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getGender() {
return gender;
}
/**
* Sets the value of the gender property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setGender(String value) {
this.gender = value;
}
/**
* Gets the value of the created property.
*
* @return
* possible object is
* {@link String }
*
*/
public Calendar getCreated() {
return created;
}
/**
* Sets the value of the created property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCreated(Calendar value) {
this.created = value;
}
}
package com.baeldung.jaxb.gen;
import java.io.Serializable;
import java.util.Calendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.w3._2001.xmlschema.Adapter1;
/**
* <p>Java class for UserResponse complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="UserResponse"&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;element name="gender" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;element name="created" type="{http://www.w3.org/2001/XMLSchema}dateTime"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserResponse", propOrder = {
"id",
"name",
"gender",
"created"
})
@XmlRootElement(name = "userResponse")
public class UserResponse
implements Serializable
{
private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String gender;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected Calendar created;
/**
* Gets the value of the id property.
*
*/
public int getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(int value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the gender property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getGender() {
return gender;
}
/**
* Sets the value of the gender property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setGender(String value) {
this.gender = value;
}
/**
* Gets the value of the created property.
*
* @return
* possible object is
* {@link String }
*
*/
public Calendar getCreated() {
return created;
}
/**
* Sets the value of the created property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCreated(Calendar value) {
this.created = value;
}
}

View File

@ -1,2 +1,2 @@
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.baeldung.com/jaxb/gen", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.baeldung.jaxb.gen;
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.baeldung.com/jaxb/gen", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.baeldung.jaxb.gen;

View File

@ -1,23 +1,23 @@
package org.w3._2001.xmlschema;
import java.util.Calendar;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
extends XmlAdapter<String, Calendar>
{
public Calendar unmarshal(String value) {
return (javax.xml.bind.DatatypeConverter.parseDateTime(value));
}
public String marshal(Calendar value) {
if (value == null) {
return null;
}
return (javax.xml.bind.DatatypeConverter.printDateTime(value));
}
}
package org.w3._2001.xmlschema;
import java.util.Calendar;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
extends XmlAdapter<String, Calendar>
{
public Calendar unmarshal(String value) {
return (javax.xml.bind.DatatypeConverter.parseDateTime(value));
}
public String marshal(Calendar value) {
if (value == null) {
return null;
}
return (javax.xml.bind.DatatypeConverter.printDateTime(value));
}
}

14
kotlin-ktor/.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
/bin/
#ignore gradle
.gradle/
#ignore build and generated files
build/
node/
out/
#ignore installed node modules and package lock file
node_modules/
package-lock.json

47
kotlin-ktor/build.gradle Executable file
View File

@ -0,0 +1,47 @@
group 'com.baeldung.ktor'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.40'
ext.ktor_version = '0.9.2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = 'APIServer.kt'
sourceCompatibility = 1.8
compileKotlin { kotlinOptions.jvmTarget = "1.8" }
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
kotlin { experimental { coroutines "enable" } }
repositories {
mavenCentral()
jcenter()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.1"
compile "io.ktor:ktor-gson:$ktor_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
task runServer(type: JavaExec) {
main = 'APIServer'
classpath = sourceSets.main.runtimeClasspath
}

BIN
kotlin-ktor/gradle/wrapper/gradle-wrapper.jar vendored Executable file

Binary file not shown.

View File

@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip

172
kotlin-ktor/gradlew vendored Executable file
View File

@ -0,0 +1,172 @@
#!/usr/bin/env sh
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"

84
kotlin-ktor/gradlew.bat vendored Executable file
View File

@ -0,0 +1,84 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -0,0 +1,11 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

2
kotlin-ktor/settings.gradle Executable file
View File

@ -0,0 +1,2 @@
rootProject.name = 'KtorWithKotlin'

View File

@ -0,0 +1,57 @@
@file:JvmName("APIServer")
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.CallLogging
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson
import io.ktor.http.ContentType
import io.ktor.request.path
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import org.slf4j.event.Level
data class Author(val name: String, val website: String)
fun main(args: Array<String>) {
val jsonResponse = """{
"id": 1,
"task": "Pay waterbill",
"description": "Pay water bill today",
}"""
embeddedServer(Netty, 8080) {
install(DefaultHeaders) {
header("X-Developer", "Baeldung")
}
install(CallLogging) {
level = Level.INFO
filter { call -> call.request.path().startsWith("/todo") }
filter { call -> call.request.path().startsWith("/author") }
}
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}
routing {
get("/todo") {
call.respondText(jsonResponse, ContentType.Application.Json)
}
get("/author") {
val author = Author("baeldung", "baeldung.com")
call.respond(author)
}
}
}.start(wait = true)
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- path to application.conf file, required -->
<!-- note that this file is always loaded as an absolute path from the classpath -->
<context-param>
<param-name>io.ktor.ktor.config</param-name>
<param-value>application.conf</param-value>
</context-param>
<servlet>
<display-name>KtorServlet</display-name>
<servlet-name>KtorServlet</servlet-name>
<servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
<!-- required! -->
<async-supported>true</async-supported>
<!-- 100mb max file upload, optional -->
<multipart-config>
<max-file-size>304857600</max-file-size>
<max-request-size>304857600</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>KtorServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

8
libraries/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
*.class
# Folders #
/gensrc
/target
# Packaged files #
*.jar

View File

@ -722,6 +722,17 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>${javapoet.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest-all.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -939,6 +950,8 @@
<xchart-version>3.5.2</xchart-version>
<commons-net.version>3.6</commons-net.version>
<mockftpserver.version>2.7.1</mockftpserver.version>
<javapoet.version>1.10.0</javapoet.version>
<hamcrest-all.version>1.3</hamcrest-all.version>
</properties>
</project>

View File

@ -0,0 +1,183 @@
package com.baeldung.javapoet;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import javax.lang.model.element.Modifier;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;
public class PersonGenerator {
private static final String FOUR_WHITESPACES = " ";
private static final String PERSON_PACKAGE_NAME = "com.baeldung.javapoet.test.person";
private File outputFile;
public PersonGenerator() {
outputFile = new File(getOutputPath().toUri());
}
public static String getPersonPackageName() {
return PERSON_PACKAGE_NAME;
}
public Path getOutputPath() {
return Paths.get(new File(".").getAbsolutePath() + "/gensrc");
}
public FieldSpec getDefaultNameField() {
return FieldSpec
.builder(String.class, "DEFAULT_NAME")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", "Alice")
.build();
}
public MethodSpec getSortByLengthMethod() {
return MethodSpec
.methodBuilder("sortByLength")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(ParameterSpec
.builder(ParameterizedTypeName.get(ClassName.get(List.class), TypeName.get(String.class)), "strings")
.build())
.addStatement("$T.sort($N, $L)", Collections.class, "strings", getComparatorAnonymousClass())
.build();
}
public MethodSpec getPrintNameMultipleTimesMethod() {
return MethodSpec
.methodBuilder("printNameMultipleTimes")
.addModifiers(Modifier.PUBLIC)
.addCode(getPrintNameMultipleTimesLambdaImpl())
.build();
}
public CodeBlock getPrintNameMultipleTimesImpl() {
return CodeBlock
.builder()
.beginControlFlow("for (int i = $L; i < $L; i++)")
.addStatement("System.out.println(name)")
.endControlFlow()
.build();
}
public CodeBlock getPrintNameMultipleTimesLambdaImpl() {
return CodeBlock
.builder()
.addStatement("$T<$T> names = new $T<>()", List.class, String.class, ArrayList.class)
.addStatement("$T.range($L, $L).forEach(i -> names.add(name))", IntStream.class, 0, 10)
.addStatement("names.forEach(System.out::println)")
.build();
}
public TypeSpec getGenderEnum() {
return TypeSpec
.enumBuilder("Gender")
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("MALE")
.addEnumConstant("FEMALE")
.addEnumConstant("UNSPECIFIED")
.build();
}
public TypeSpec getPersonInterface() {
return TypeSpec
.interfaceBuilder("Person")
.addModifiers(Modifier.PUBLIC)
.addField(getDefaultNameField())
.addMethod(MethodSpec
.methodBuilder("getName")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(String.class)
.build())
.addMethod(MethodSpec
.methodBuilder("getDefaultName")
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.returns(String.class)
.addCode(CodeBlock
.builder()
.addStatement("return DEFAULT_NAME")
.build())
.build())
.build();
}
public TypeSpec getStudentClass() {
return TypeSpec
.classBuilder("Student")
.addSuperinterface(ClassName.get(PERSON_PACKAGE_NAME, "Person"))
.addModifiers(Modifier.PUBLIC)
.addField(FieldSpec
.builder(String.class, "name")
.addModifiers(Modifier.PRIVATE)
.build())
.addMethod(MethodSpec
.methodBuilder("getName")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.addStatement("return this.name")
.build())
.addMethod(MethodSpec
.methodBuilder("setName")
.addParameter(String.class, "name")
.addModifiers(Modifier.PUBLIC)
.addStatement("this.name = name")
.build())
.addMethod(getPrintNameMultipleTimesMethod())
.addMethod(getSortByLengthMethod())
.build();
}
public TypeSpec getComparatorAnonymousClass() {
return TypeSpec
.anonymousClassBuilder("")
.addSuperinterface(ParameterizedTypeName.get(Comparator.class, String.class))
.addMethod(MethodSpec
.methodBuilder("compare")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(String.class, "a")
.addParameter(String.class, "b")
.returns(int.class)
.addStatement("return a.length() - b.length()")
.build())
.build();
}
public void generateGenderEnum() throws IOException {
writeToOutputFile(getPersonPackageName(), getGenderEnum());
}
public void generatePersonInterface() throws IOException {
writeToOutputFile(getPersonPackageName(), getPersonInterface());
}
public void generateStudentClass() throws IOException {
writeToOutputFile(getPersonPackageName(), getStudentClass());
}
private void writeToOutputFile(String packageName, TypeSpec typeSpec) throws IOException {
JavaFile javaFile = JavaFile
.builder(packageName, typeSpec)
.indent(FOUR_WHITESPACES)
.build();
javaFile.writeTo(outputFile);
}
}

View File

@ -0,0 +1,99 @@
package com.baeldung.javapoet.test;
import com.baeldung.javapoet.PersonGenerator;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class PersonGeneratorUnitTest {
private PersonGenerator generator;
private Path generatedFolderPath;
private Path expectedFolderPath;
@Before
public void setUp() {
String packagePath = this
.getClass()
.getPackage()
.getName()
.replace(".", "/") + "/person";
generator = new PersonGenerator();
generatedFolderPath = generator
.getOutputPath()
.resolve(packagePath);
expectedFolderPath = Paths.get(new File(".").getAbsolutePath() + "/src/test/java/" + packagePath);
}
@After
public void tearDown() throws Exception {
FileUtils.deleteDirectory(new File(generator
.getOutputPath()
.toUri()));
}
@Test
public void whenGenerateGenderEnum_thenGenerateGenderEnumAndWriteToFile() throws IOException {
generator.generateGenderEnum();
String fileName = "Gender.java";
assertThatFileIsGeneratedAsExpected(fileName);
deleteGeneratedFile(fileName);
}
@Test
public void whenGeneratePersonInterface_thenGeneratePersonInterfaceAndWriteToFile() throws IOException {
generator.generatePersonInterface();
String fileName = "Person.java";
assertThatFileIsGeneratedAsExpected(fileName);
deleteGeneratedFile(fileName);
}
@Test
public void whenGenerateStudentClass_thenGenerateStudentClassAndWriteToFile() throws IOException {
generator.generateStudentClass();
String fileName = "Student.java";
assertThatFileIsGeneratedAsExpected(fileName);
deleteGeneratedFile(fileName);
}
private void assertThatFileIsGeneratedAsExpected(String fileName) throws IOException {
String generatedFileContent = extractFileContent(generatedFolderPath.resolve(fileName));
String expectedFileContent = extractFileContent(expectedFolderPath.resolve(fileName));
assertThat("Generated file is identical to the file with the expected content", generatedFileContent, is(equalTo(expectedFileContent)));
}
private void deleteGeneratedFile(String fileName) throws IOException {
Path generatedFilePath = generatedFolderPath.resolve(fileName);
Files.delete(generatedFilePath);
}
private String extractFileContent(Path filePath) throws IOException {
byte[] fileContentAsBytes = Files.readAllBytes(filePath);
String fileContentAsString = new String(fileContentAsBytes, StandardCharsets.UTF_8);
if (!fileContentAsString.contains("\r\n")) {
// file is not in DOS format
// convert it first, so that the content comparison will be relevant
return fileContentAsString.replaceAll("\n", "\r\n");
}
return fileContentAsString;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.javapoet.test.person;
public enum Gender {
MALE,
FEMALE,
UNSPECIFIED
}

View File

@ -0,0 +1,13 @@
package com.baeldung.javapoet.test.person;
import java.lang.String;
public interface Person {
String DEFAULT_NAME = "Alice";
String getName();
default String getDefaultName() {
return DEFAULT_NAME;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.javapoet.test.person;
import java.lang.Override;
import java.lang.String;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;
public class Student implements Person {
private String name;
@Override
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public void printNameMultipleTimes() {
List<String> names = new ArrayList<>();
IntStream.range(0, 10).forEach(i -> names.add(name));
names.forEach(System.out::println);
}
public static void sortByLength(List<String> strings) {
Collections.sort(strings, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.length() - b.length();
}
});
}
}

View File

@ -0,0 +1,190 @@
package com.baeldung.jodatime;
import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.*;
public class JodaTimeUnitTest {
@Test
public void testDateTimeRepresentation() {
DateTimeZone.setDefault(DateTimeZone.forID("Europe/Bucharest"));
// representing current date and time
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
LocalDateTime currentLocalDateTime = LocalDateTime.now();
LocalDateTime currentDateTimeFromJavaDate = new LocalDateTime(new Date());
Date currentJavaDate = currentDateTimeFromJavaDate.toDate();
// representing custom date and time
Date oneMinuteAgoDate = new Date(System.currentTimeMillis() - (60 * 1000));
Instant oneMinutesAgoInstant = new Instant(oneMinuteAgoDate);
DateTime customDateTimeFromInstant = new DateTime(oneMinutesAgoInstant);
DateTime customDateTimeFromJavaDate = new DateTime(oneMinuteAgoDate);
DateTime customDateTimeFromString = new DateTime("2018-05-05T10:11:12.123");
DateTime customDateTimeFromParts = new DateTime(2018, 5, 5, 10, 11, 12, 123);
// parsing
DateTime parsedDateTime = DateTime.parse("2018-05-05T10:11:12.123");
assertEquals("2018-05-05T10:11:12.123+03:00", parsedDateTime.toString());
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
DateTime parsedDateTimeUsingFormatter = DateTime.parse("05/05/2018 10:11:12", dateTimeFormatter);
assertEquals("2018-05-05T10:11:12.000+03:00", parsedDateTimeUsingFormatter.toString());
// Instant
Instant instant = new Instant();
Instant.now();
Instant instantFromString = new Instant("2018-05-05T10:11:12");
Instant instantFromDate = new Instant(oneMinuteAgoDate);
Instant instantFromTimestamp = new Instant(System.currentTimeMillis() - (60 * 1000));
Instant parsedInstant = Instant.parse("05/05/2018 10:11:12", dateTimeFormatter);
Instant instantNow = Instant.now();
Instant oneMinuteAgoInstant = new Instant(oneMinuteAgoDate);
// epochMilli and epochSecond
long milliesFromEpochTime = System.currentTimeMillis();
long secondsFromEpochTime = milliesFromEpochTime / 1000;
Instant instantFromEpochMilli = Instant.ofEpochMilli(milliesFromEpochTime);
Instant instantFromEpocSeconds = Instant.ofEpochSecond(secondsFromEpochTime);
// convert Instants
DateTime dateTimeFromInstant = instant.toDateTime();
Date javaDateFromInstant = instant.toDate();
int year = instant.get(DateTimeFieldType.year());
int month = instant.get(DateTimeFieldType.monthOfYear());
int day = instant.get(DateTimeFieldType.dayOfMonth());
int hour = instant.get(DateTimeFieldType.hourOfDay());
// Duration, Period, Instant
long currentTimestamp = System.currentTimeMillis();
long oneHourAgo = currentTimestamp - 24*60*1000;
Duration duration = new Duration(oneHourAgo, currentTimestamp);
Instant.now().plus(duration);
long durationInDays = duration.getStandardDays();
long durationInHours = duration.getStandardHours();
long durationInMinutes = duration.getStandardMinutes();
long durationInSeconds = duration.getStandardSeconds();
long durationInMilli = duration.getMillis();
// converting between classes
DateTimeUtils.setCurrentMillisFixed(currentTimestamp);
LocalDateTime currentDateAndTime = LocalDateTime.now();
assertEquals(currentTimestamp, currentDateAndTime.toDate().getTime());
assertEquals(new DateTime(currentTimestamp), currentDateAndTime.toDateTime());
assertEquals(new LocalDate(currentTimestamp), currentDateAndTime.toLocalDate());
assertEquals(new LocalTime(currentTimestamp), currentDateAndTime.toLocalTime());
}
@Test
public void testJodaInstant() {
Date oneMinuteAgoDate = new Date(System.currentTimeMillis() - (60 * 1000));
Instant instantNow = Instant.now();
Instant oneMinuteAgoInstant = new Instant(oneMinuteAgoDate);
assertTrue(instantNow.compareTo(oneMinuteAgoInstant) > 0);
assertTrue(instantNow.isAfter(oneMinuteAgoInstant));
assertTrue(oneMinuteAgoInstant.isBefore(instantNow));
assertTrue(oneMinuteAgoInstant.isBeforeNow());
assertFalse(oneMinuteAgoInstant.isEqual(instantNow));
LocalDateTime localDateTime = new LocalDateTime("2018-02-01");
Period period = new Period().withMonths(1);
LocalDateTime datePlusPeriod = localDateTime.plus(period);
Instant startInterval1 = new Instant("2018-05-05T09:00:00.000");
Instant endInterval1 = new Instant("2018-05-05T11:00:00.000");
Interval interval1 = new Interval(startInterval1, endInterval1);
Instant startInterval2 = new Instant("2018-05-05T10:00:00.000");
Instant endInterval2 = new Instant("2018-05-05T11:00:00.000");
Interval interval2 = new Interval(startInterval2, endInterval2);
Instant startInterval3 = new Instant("2018-05-05T11:00:00.000");
Instant endInterval3 = new Instant("2018-05-05T13:00:00.000");
Interval interval3 = new Interval(startInterval3, endInterval3);
Interval overlappingInterval = interval1.overlap(interval2);
Interval notOverlappingInterval = interval1.overlap(interval3);
assertTrue(overlappingInterval.isEqual(new Interval(new Instant("2018-05-05T10:00:00.000"), new Instant("2018-05-05T11:00:00.000"))));
assertNotNull(overlappingInterval);
interval1.abuts(interval3);
assertTrue(interval1.abuts(new Interval(new Instant("2018-05-05T11:00:00.000"), new Instant("2018-05-05T13:00:00.000"))));
interval1.gap(interval2);
}
@Test
public void testDateTimeOperations() {
DateTimeUtils.setCurrentMillisFixed(1529612783288L);
DateTimeZone.setDefault(DateTimeZone.UTC);
LocalDateTime currentLocalDateTime = LocalDateTime.now();
assertEquals("2018-06-21T20:26:23.288", currentLocalDateTime.toString());
LocalDateTime nextDayDateTime = currentLocalDateTime.plusDays(1);
assertEquals("2018-06-22T20:26:23.288", nextDayDateTime.toString());
Period oneMonth = new Period().withMonths(1);
LocalDateTime nextMonthDateTime = currentLocalDateTime.plus(oneMonth);
assertEquals("2018-07-21T20:26:23.288", nextMonthDateTime.toString());
LocalDateTime previousDayLocalDateTime = currentLocalDateTime.minusDays(1);
assertEquals("2018-06-20T20:26:23.288", previousDayLocalDateTime.toString());
LocalDateTime currentDateAtHour10 = currentLocalDateTime
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0);
assertEquals("2018-06-21T00:00:00.000", currentDateAtHour10.toString());
}
@Test
public void testTimezones() {
System.getProperty("user.timezone");
DateTimeZone.getAvailableIDs();
// DateTimeZone.setDefault(DateTimeZone.forID("Europe/Bucharest"));
DateTimeUtils.setCurrentMillisFixed(1529612783288L);
DateTime dateTimeInChicago = new DateTime(DateTimeZone.forID("America/Chicago"));
assertEquals("2018-06-21T15:26:23.288-05:00", dateTimeInChicago.toString());
DateTime dateTimeInBucharest = new DateTime(DateTimeZone.forID("Europe/Bucharest"));
assertEquals("2018-06-21T23:26:23.288+03:00", dateTimeInBucharest.toString());
LocalDateTime localDateTimeInChicago = new LocalDateTime(DateTimeZone.forID("America/Chicago"));
assertEquals("2018-06-21T15:26:23.288", localDateTimeInChicago.toString());
DateTime convertedDateTime = localDateTimeInChicago.toDateTime(DateTimeZone.forID("Europe/Bucharest"));
assertEquals("2018-06-21T15:26:23.288+03:00", convertedDateTime.toString());
Date convertedDate = localDateTimeInChicago.toDate(TimeZone.getTimeZone("Europe/Bucharest"));
assertEquals("Thu Jun 21 15:26:23 EEST 2018", convertedDate.toString());
}
}

View File

@ -95,6 +95,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>
@ -125,6 +126,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>

View File

@ -47,6 +47,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
@ -88,8 +89,8 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>*/EthControllerTestOne.java</include>
<include>**/*IntTest.java</include>
<include>*/EthControllerTestOne.java</include>
<include>**/*EntryPointsTest.java</include>
</includes>
</configuration>

View File

@ -48,6 +48,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
@ -89,8 +90,8 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>*/EthControllerTestOne.java</include>
<include>**/*IntTest.java</include>
<include>*/EthControllerTestOne.java</include>
<include>**/*EntryPointsTest.java</include>
</includes>
</configuration>

View File

@ -248,6 +248,7 @@
<module>persistence-modules/liquibase</module>
<module>spring-boot-property-exp</module>
<module>testing-modules/mockserver</module>
<module>testing-modules/test-containers</module>
<module>undertow</module>
<module>vertx-and-rxjava</module>
<module>saas</module>
@ -347,6 +348,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
@ -472,6 +474,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -98,6 +98,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -145,6 +145,7 @@
<failIfNoTests>false</failIfNoTests>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -155,6 +155,7 @@
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -171,6 +171,7 @@
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -81,6 +81,7 @@
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -175,6 +175,7 @@
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -59,6 +59,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>

View File

@ -79,6 +79,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>

View File

@ -72,6 +72,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>

View File

@ -163,6 +163,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>

View File

@ -49,8 +49,6 @@
<factorypathentry kind="VARJAR" id="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/commons-fileupload/commons-fileupload/1.3.1/commons-fileupload-1.3.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/commons-io/commons-io/2.2/commons-io-2.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/graphql-java/graphiql-spring-boot-starter/3.6.0/graphiql-spring-boot-starter-3.6.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/graphql-java/graphiql-spring-boot-autoconfigure/3.6.0/graphiql-spring-boot-autoconfigure-3.6.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/graphql-java/graphql-java-tools/3.2.0/graphql-java-tools-3.2.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/jetbrains/kotlin/kotlin-stdlib/1.2.31/kotlin-stdlib-1.2.31.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/jetbrains/annotations/13.0/annotations-13.0.jar" enabled="true" runInBatchMode="false"/>
@ -70,22 +68,11 @@
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/tomcat/embed/tomcat-embed-core/8.5.29/tomcat-embed-core-8.5.29.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/tomcat/embed/tomcat-embed-el/8.5.29/tomcat-embed-el-8.5.29.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.29/tomcat-embed-websocket-8.5.29.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-starter-test/2.0.1.RELEASE/spring-boot-starter-test-2.0.1.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-test/2.0.1.RELEASE/spring-boot-test-2.0.1.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-test-autoconfigure/2.0.1.RELEASE/spring-boot-test-autoconfigure-2.0.1.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/assertj/assertj-core/3.9.1/assertj-core-3.9.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/mockito/mockito-core/2.15.0/mockito-core-2.15.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/bytebuddy/byte-buddy/1.7.11/byte-buddy-1.7.11.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/bytebuddy/byte-buddy-agent/1.7.11/byte-buddy-agent-1.7.11.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/objenesis/objenesis/2.6/objenesis-2.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/skyscreamer/jsonassert/1.5.0/jsonassert-1.5.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-jcl/5.0.5.RELEASE/spring-jcl-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-test/5.0.5.RELEASE/spring-test-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/xmlunit/xmlunit-core/2.5.1/xmlunit-core-2.5.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/io/dropwizard/metrics/metrics-core/3.2.6/metrics-core-3.2.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/h2database/h2/1.4.197/h2-1.4.197.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-starter/2.0.1.RELEASE/spring-boot-starter-2.0.1.RELEASE.jar" enabled="true" runInBatchMode="false"/>
@ -98,22 +85,10 @@
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/yaml/snakeyaml/1.19/snakeyaml-1.19.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/minidev/json-smart/2.3/json-smart-2.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/boot/spring-boot-starter-mail/2.0.1.RELEASE/spring-boot-starter-mail-2.0.1.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-context/5.0.5.RELEASE/spring-context-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-context-support/5.0.5.RELEASE/spring-context-support-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/sun/mail/javax.mail/1.6.1/javax.mail-1.6.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/subethamail/subethasmtp/3.1.7/subethasmtp-3.1.7.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/javax/mail/mail/1.4.4/mail-1.4.4.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/javax/activation/activation/1.1.1/activation-1.1.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/webjars/bootstrap/3.3.7-1/bootstrap-3.3.7-1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/webjars/jquery/3.1.1/jquery-3.1.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/guava/18.0/guava-18.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/tomcat/tomcat-servlet-api/8.5.11/tomcat-servlet-api-8.5.11.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-websocket/5.0.5.RELEASE/spring-websocket-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-context/5.0.5.RELEASE/spring-context-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-messaging/5.0.5.RELEASE/spring-messaging-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-beans/5.0.5.RELEASE/spring-beans-5.0.5.RELEASE.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/togglz/togglz-spring-boot-starter/2.4.1.Final/togglz-spring-boot-starter-2.4.1.Final.jar" enabled="true" runInBatchMode="false"/>
@ -149,7 +124,6 @@
<factorypathentry kind="VARJAR" id="M2_REPO/com/rometools/rome/1.9.0/rome-1.9.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/rometools/rome-utils/1.9.0/rome-utils-1.9.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/jdom/jdom2/2.0.6/jdom2-2.0.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/junit/junit/4.12/junit-4.12.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/io/rest-assured/rest-assured/3.1.0/rest-assured-3.1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/codehaus/groovy/groovy/2.4.15/groovy-2.4.15.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/codehaus/groovy/groovy-xml/2.4.15/groovy-xml-2.4.15.jar" enabled="true" runInBatchMode="false"/>

View File

@ -170,6 +170,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>

View File

@ -66,6 +66,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
</configuration>
</plugin>

View File

@ -65,6 +65,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>

View File

@ -55,6 +55,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<!-- Run only tests whose name end with "UnitTest" -->
<includes>

View File

@ -127,6 +127,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -127,6 +127,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -58,6 +58,7 @@
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -207,6 +207,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -112,6 +112,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<systemPropertyVariables>
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->

View File

@ -74,6 +74,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>

View File

@ -286,6 +286,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -305,6 +305,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*LiveTest.java</include>

View File

@ -227,6 +227,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
@ -282,6 +283,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -253,6 +253,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>

View File

@ -211,6 +211,7 @@
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/*EntryPointsTest.java</include>

View File

@ -30,6 +30,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>

View File

@ -30,6 +30,7 @@
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
@ -57,6 +58,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>

View File

@ -66,6 +66,7 @@
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>

View File

@ -0,0 +1,26 @@
package org.baeldung.mockito.service;
public class ActionHandler {
private Service service;
public ActionHandler(Service service) {
this.service = service;
}
public void doAction() {
service.doAction("our-request", new Callback<Response>() {
@Override
public void reply(Response response) {
handleResponse(response);
}
});
}
private void handleResponse(Response response) {
if (response.isValid()) {
response.setData(new Data("Successful data response"));
}
}
}

View File

@ -0,0 +1,6 @@
package org.baeldung.mockito.service;
public interface Callback<T> {
void reply(T response);
}

View File

@ -0,0 +1,15 @@
package org.baeldung.mockito.service;
public class Data {
private String message;
public Data(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.mockito.service;
public class Response {
private Data data;
private boolean isValid = true;
public boolean isValid() {
return isValid;
}
public void setIsValid(boolean isValid) {
this.isValid = isValid;
}
public void setData(Data data) {
this.data = data;
}
public Data getData() {
return data;
}
}

View File

@ -0,0 +1,7 @@
package org.baeldung.mockito.service;
public interface Service {
void doAction(String request, Callback<Response> callback);
}

View File

@ -0,0 +1,65 @@
package org.baeldung.mockito.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
public class ActionHandlerUnitTest {
@Mock
private Service service;
@Captor
private ArgumentCaptor<Callback<Response>> callbackCaptor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void givenServiceWithValidResponse_whenCallbackReceived_thenProcessed() {
ActionHandler handler = new ActionHandler(service);
handler.doAction();
verify(service).doAction(anyString(), callbackCaptor.capture());
Callback<Response> callback = callbackCaptor.getValue();
Response response = new Response();
callback.reply(response);
String expectedMessage = "Successful data response";
Data data = response.getData();
assertEquals("Should receive a successful message: ", expectedMessage, data.getMessage());
}
@Test
public void givenServiceWithInvalidResponse_whenCallbackReceived_thenNotProcessed() {
Response response = new Response();
response.setIsValid(false);
doAnswer((Answer<Void>) invocation -> {
Callback<Response> callback = invocation.getArgument(1);
callback.reply(response);
Data data = response.getData();
assertNull("No data in invalid response: ", data);
return null;
}).when(service)
.doAction(anyString(), any(Callback.class));
ActionHandler handler = new ActionHandler(service);
handler.doAction();
}
}

View File

@ -0,0 +1,2 @@
### Relevant Articles:
- [Docker Test Containers in Java Tests](TODO link to be added.)

View File

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId> -->
<artifactId>test-containers</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test-containers</name>
<description>Intro to Java Test Containers</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.baeldung.TestLauncher</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql </artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>selenium </artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.platform.version>1.0.1</junit.platform.version>
<junit.vintage.version>4.12.1</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version>
<h2.version>1.4.196</h2.version>
<mockito.version>2.11.0</mockito.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<spring.version>5.0.1.RELEASE</spring.version>
</properties>
</project>

View File

@ -0,0 +1,47 @@
package com.baeldung.testconainers;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.DockerComposeContainer;
public class DockerComposeContainerUnitTest {
@ClassRule
public static DockerComposeContainer compose =
new DockerComposeContainer(
new File("src/test/resources/test-compose.yml"))
.withExposedService("simpleWebServer_1", 80);
@Test
public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse()
throws Exception {
String address = "http://" + compose.getServiceHost("simpleWebServer_1", 80)
+ ":" + compose.getServicePort("simpleWebServer_1", 80);
String response = simpleGetRequest(address);
assertEquals(response, "Hello World!");
}
private String simpleGetRequest(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.testconainers;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.platform.commons.annotation.Testable;
import org.testcontainers.containers.GenericContainer;
@Testable
public class GenericContainerUnitTest {
@ClassRule
public static GenericContainer simpleWebServer =
new GenericContainer("alpine:3.2")
.withExposedPorts(80)
.withCommand("/bin/sh", "-c", "while true; do echo "
+ "\"HTTP/1.1 200 OK\n\nHello World!\" | nc -l -p 80; done");
@Test
public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse()
throws Exception {
String address = "http://"
+ simpleWebServer.getContainerIpAddress()
+ ":" + simpleWebServer.getMappedPort(80);
String response = simpleGetRequest(address);
assertEquals(response, "Hello World!");
}
private String simpleGetRequest(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.testconainers;
import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.platform.commons.annotation.Testable;
import org.testcontainers.containers.PostgreSQLContainer;
@Testable
public class PostgreSqlContainerUnitTest {
@Rule
public PostgreSQLContainer postgresContainer = new PostgreSQLContainer();
@Test
public void whenSelectQueryExecuted_thenResulstsReturned() throws Exception {
ResultSet resultSet = performQuery(postgresContainer, "SELECT 1");
resultSet.next();
int result = resultSet.getInt(1);
assertEquals(1, result);
}
private ResultSet performQuery(PostgreSQLContainer postgres, String query) throws SQLException {
String jdbcUrl = postgres.getJdbcUrl();
String username = postgres.getUsername();
String password = postgres.getPassword();
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
return conn.createStatement()
.executeQuery(query);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.testconainers;
import static org.junit.Assert.assertEquals;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.GenericContainer;
public class WebDriverContainerUnitTest {
@Rule
public BrowserWebDriverContainer chrome
= new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome());
@Test
public void whenNavigatedToPage_thenHeadingIsInThePage() {
RemoteWebDriver driver = chrome.getWebDriver();
driver.get("https://saucelabs.com/test/guinea-pig");
String heading = driver.findElement(By.xpath("/html/body/h1"))
.getText();
assertEquals("This page is a Selenium sandbox", heading);
}
}

Some files were not shown because too many files have changed in this diff Show More