Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
198281bca3
@ -15,7 +15,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class PrimeNumbersUnitTest {
|
||||
public class PrimeNumbersUnitManualTest {
|
||||
|
||||
private static Logger logger = Logger.getAnonymousLogger();
|
||||
|
17
core-java-modules/core-java-security-2/pom.xml
Normal file
17
core-java-modules/core-java-security-2/pom.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?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>
|
||||
<artifactId>core-java-security-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-security-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.callback.*;
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConsoleCallbackHandler implements CallbackHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
|
||||
Console console = System.console();
|
||||
for (Callback callback : callbacks) {
|
||||
if (callback instanceof NameCallback) {
|
||||
NameCallback nameCallback = (NameCallback) callback;
|
||||
nameCallback.setName(console.readLine(nameCallback.getPrompt()));
|
||||
} else if (callback instanceof PasswordCallback) {
|
||||
PasswordCallback passwordCallback = (PasswordCallback) callback;
|
||||
passwordCallback.setPassword(console.readPassword(passwordCallback.getPrompt()));
|
||||
} else {
|
||||
throw new UnsupportedCallbackException(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
public class JaasAuthentication {
|
||||
|
||||
public static void main(String[] args) throws LoginException {
|
||||
LoginService loginService = new LoginService();
|
||||
Subject subject = loginService.login();
|
||||
System.out.println(subject.getPrincipals().iterator().next() + " sucessfully logeed in");
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
public class JaasAuthorization {
|
||||
|
||||
public static void main(String[] args) throws LoginException {
|
||||
|
||||
LoginService loginService = new LoginService();
|
||||
Subject subject = loginService.login();
|
||||
|
||||
PrivilegedAction privilegedAction = new ResourceAction();
|
||||
Subject.doAsPrivileged(subject, privilegedAction, null);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginContext;
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
public class LoginService {
|
||||
|
||||
public Subject login() throws LoginException {
|
||||
LoginContext loginContext = new LoginContext("jaasApplication", new ConsoleCallbackHandler());
|
||||
loginContext.login();
|
||||
return loginContext.getSubject();
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
public class ResourceAction implements PrivilegedAction {
|
||||
@Override
|
||||
public Object run() {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null) {
|
||||
sm.checkPermission(new ResourcePermission("test_resource"));
|
||||
}
|
||||
System.out.println("I have access to test_resource !");
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import java.security.BasicPermission;
|
||||
|
||||
public class ResourcePermission extends BasicPermission {
|
||||
public ResourcePermission(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.baeldung.jaas.loginmodule;
|
||||
|
||||
import com.sun.security.auth.UserPrincipal;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.callback.*;
|
||||
import javax.security.auth.login.LoginException;
|
||||
import javax.security.auth.spi.LoginModule;
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
public class InMemoryLoginModule implements LoginModule {
|
||||
|
||||
private static final String USERNAME = "testuser";
|
||||
private static final String PASSWORD = "testpassword";
|
||||
|
||||
private Subject subject;
|
||||
private CallbackHandler callbackHandler;
|
||||
private Map<String, ?> sharedState;
|
||||
private Map<String, ?> options;
|
||||
|
||||
private String username;
|
||||
private boolean loginSucceeded = false;
|
||||
private Principal userPrincipal;
|
||||
|
||||
@Override
|
||||
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
|
||||
Map<String, ?> options) {
|
||||
this.subject = subject;
|
||||
this.callbackHandler = callbackHandler;
|
||||
this.sharedState = sharedState;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws LoginException {
|
||||
NameCallback nameCallback = new NameCallback("username: ");
|
||||
PasswordCallback passwordCallback = new PasswordCallback("password: ", false);
|
||||
try {
|
||||
callbackHandler.handle(new Callback[]{nameCallback, passwordCallback});
|
||||
username = nameCallback.getName();
|
||||
String password = new String(passwordCallback.getPassword());
|
||||
if (USERNAME.equals(username) && PASSWORD.equals(password)) {
|
||||
loginSucceeded = true;
|
||||
}
|
||||
} catch (IOException | UnsupportedCallbackException e) {
|
||||
throw new LoginException("Can't login");
|
||||
}
|
||||
return loginSucceeded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commit() throws LoginException {
|
||||
if (!loginSucceeded) {
|
||||
return false;
|
||||
}
|
||||
userPrincipal = new UserPrincipal(username);
|
||||
subject.getPrincipals().add(userPrincipal);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean abort() throws LoginException {
|
||||
logout();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean logout() throws LoginException {
|
||||
subject.getPrincipals().remove(userPrincipal);
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
jaasApplication {
|
||||
com.baeldung.jaas.loginmodule.InMemoryLoginModule required debug=true;
|
||||
};
|
@ -0,0 +1,14 @@
|
||||
grant codebase "file:./target/core-java-security-2-0.1.0-SNAPSHOT.jar" {
|
||||
permission javax.security.auth.AuthPermission "createLoginContext.jaasApplication";
|
||||
permission javax.security.auth.AuthPermission "doAsPrivileged";
|
||||
permission java.lang.RuntimePermission "readFileDescriptor";
|
||||
permission java.lang.RuntimePermission "writeFileDescriptor";
|
||||
};
|
||||
|
||||
grant codebase "file:./target/core-java-security-2-0.1.0-SNAPSHOT.jar" {
|
||||
permission javax.security.auth.AuthPermission "modifyPrincipals";
|
||||
};
|
||||
|
||||
grant principal com.sun.security.auth.UserPrincipal "testuser" {
|
||||
permission com.baeldung.jaas.ResourcePermission "test_resource";
|
||||
};
|
@ -8,5 +8,6 @@ This module contains articles about string operations.
|
||||
- [String Initialization in Java](https://www.baeldung.com/java-string-initialization)
|
||||
- [String toLowerCase and toUpperCase Methods in Java](https://www.baeldung.com/java-string-convert-case)
|
||||
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
|
||||
- [How to avoid String contains() Case Insensitive in Java](https://www.baeldung.com/how-to-avoid-string-contains-case-insensitive-in-java)
|
||||
- [Case-Insensitive String Matching in Java](https://www.baeldung.com/java-case-insensitive-string-matching)
|
||||
- [L-Trim and R-Trim in Java](https://www.baeldung.com/l-trim-and-r-trim-in-java)
|
||||
- More articles: [[<-- prev]](../core-java-string-operations)
|
||||
|
@ -109,7 +109,7 @@
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<validation-api.version>2.0.0.Final</validation-api.version>
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<guava.version>28.2-jre</guava.version>
|
||||
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||
<javax.el.version>2.2.6</javax.el.version>
|
||||
|
@ -0,0 +1,124 @@
|
||||
package com.baeldung.trim;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
|
||||
/**
|
||||
* Based on https://github.com/tedyoung/indexof-contains-benchmark
|
||||
*/
|
||||
@Fork(5)
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class LTrimRTrim {
|
||||
|
||||
private String src;
|
||||
private static String ltrimResult;
|
||||
private static String rtrimResult;
|
||||
private static Pattern LTRIM = Pattern.compile("^\\s+");
|
||||
private static Pattern RTRIM = Pattern.compile("\\s+$");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
src = " White spaces left and right ";
|
||||
ltrimResult = "White spaces left and right ";
|
||||
rtrimResult = " White spaces left and right";
|
||||
}
|
||||
|
||||
public static String whileLtrim(String s) {
|
||||
int i = 0;
|
||||
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
|
||||
i++;
|
||||
}
|
||||
return s.substring(i);
|
||||
}
|
||||
|
||||
public static String whileRtrim(String s) {
|
||||
int i = s.length() - 1;
|
||||
while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
|
||||
i--;
|
||||
}
|
||||
return s.substring(0, i + 1);
|
||||
}
|
||||
|
||||
private static boolean checkStrings(String ltrim, String rtrim) {
|
||||
boolean result = false;
|
||||
|
||||
if (ltrimResult.equalsIgnoreCase(ltrim) && rtrimResult.equalsIgnoreCase(rtrim))
|
||||
result = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Going through the String detecting Whitespaces
|
||||
@Benchmark
|
||||
public boolean whileCharacters() {
|
||||
String ltrim = whileLtrim(src);
|
||||
String rtrim = whileRtrim(src);
|
||||
|
||||
return checkStrings(ltrim, rtrim);
|
||||
}
|
||||
|
||||
// replaceAll() and Regular Expressions
|
||||
@Benchmark
|
||||
public boolean replaceAllRegularExpression() {
|
||||
String ltrim = src.replaceAll("^\\s+", "");
|
||||
String rtrim = src.replaceAll("\\s+$", "");
|
||||
|
||||
return checkStrings(ltrim, rtrim);
|
||||
}
|
||||
|
||||
public static String patternLtrim(String s) {
|
||||
return LTRIM.matcher(s)
|
||||
.replaceAll("");
|
||||
}
|
||||
|
||||
public static String patternRtrim(String s) {
|
||||
return RTRIM.matcher(s)
|
||||
.replaceAll("");
|
||||
}
|
||||
|
||||
// Pattern matches() with replaceAll
|
||||
@Benchmark
|
||||
public boolean patternMatchesLTtrimRTrim() {
|
||||
String ltrim = patternLtrim(src);
|
||||
String rtrim = patternRtrim(src);
|
||||
|
||||
return checkStrings(ltrim, rtrim);
|
||||
}
|
||||
|
||||
// Guava CharMatcher trimLeadingFrom / trimTrailingFrom
|
||||
@Benchmark
|
||||
public boolean guavaCharMatcher() {
|
||||
String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);
|
||||
String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
|
||||
|
||||
return checkStrings(ltrim, rtrim);
|
||||
}
|
||||
|
||||
// Apache Commons StringUtils containsIgnoreCase
|
||||
@Benchmark
|
||||
public boolean apacheCommonsStringUtils() {
|
||||
String ltrim = StringUtils.stripStart(src, null);
|
||||
String rtrim = StringUtils.stripEnd(src, null);
|
||||
|
||||
return checkStrings(ltrim, rtrim);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.baeldung.trim;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* BAEL-3755: LTrim and RTrim examples.
|
||||
*/
|
||||
public class LTrimRTrimUnitTest {
|
||||
|
||||
private String src = " White spaces left and right ";
|
||||
private final static String ltrimResult = "White spaces left and right ";
|
||||
private final static String rtrimResult = " White spaces left and right";
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingWhileCharacters_thenReturnsTrue() {
|
||||
String ltrim = LTrimRTrim.whileLtrim(src);
|
||||
String rtrim = LTrimRTrim.whileRtrim(src);
|
||||
|
||||
// Compare the Strings obtained and the expected
|
||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||
|
||||
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingContainsWithReplaceAll_shouldReturnTrue() {
|
||||
// Use replaceAll with Regular Expressions
|
||||
String ltrim = src.replaceAll("^\\s+", "");
|
||||
String rtrim = src.replaceAll("\\s+$", "");
|
||||
|
||||
// Compare the Strings obtained and the expected
|
||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||
|
||||
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingPaternCompileMatcherReplaceAll_thenReturnsTrue() {
|
||||
// Use Pattern Compile Matcher and Find to avoid case insensitive issues
|
||||
String ltrim = LTrimRTrim.patternLtrim(src);
|
||||
String rtrim = LTrimRTrim.patternRtrim(src);
|
||||
|
||||
// Compare the Strings obtained and the expected
|
||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||
|
||||
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingGuavaCharMatcher_thenReturnsTrue() {
|
||||
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
|
||||
String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);;
|
||||
String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
|
||||
|
||||
// Compare the Strings obtained and the expected
|
||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||
|
||||
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingStringUtilsStripStartEnd_thenReturnsTrue() {
|
||||
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
|
||||
String ltrim = StringUtils.stripStart(src, null);
|
||||
String rtrim = StringUtils.stripEnd(src, null);
|
||||
|
||||
// Compare the Strings obtained and the expected
|
||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
|
||||
}
|
||||
|
||||
}
|
@ -100,6 +100,7 @@
|
||||
<module>core-java-reflection</module>
|
||||
|
||||
<module>core-java-security</module>
|
||||
<module>core-java-security-2</module>
|
||||
<module>core-java-streams</module>
|
||||
<module>core-java-streams-2</module>
|
||||
<module>core-java-streams-3</module>
|
||||
|
@ -50,12 +50,24 @@
|
||||
<artifactId>imageio-bmp</artifactId>
|
||||
<version>${imageio.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.tess4j</groupId>
|
||||
<artifactId>tess4j</artifactId>
|
||||
<version>${tess4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>tesseract-platform</artifactId>
|
||||
<version>${tesseract-platform.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<core-image.version>1.3.5</core-image.version>
|
||||
<ij.version>1.51h</ij.version>
|
||||
<imageio.version>3.3.2</imageio.version>
|
||||
<tess4j.version>4.5.1</tess4j.version>
|
||||
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.tesseract;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
import java.io.File;
|
||||
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
|
||||
public class Tess4JExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String result = null;
|
||||
try {
|
||||
File image = new File("src/main/resources/images/baeldung.png");
|
||||
Tesseract tesseract = new Tesseract();
|
||||
tesseract.setLanguage("spa");
|
||||
tesseract.setPageSegMode(1);
|
||||
tesseract.setOcrEngineMode(1);
|
||||
tesseract.setHocr(true);
|
||||
tesseract.setDatapath("src/main/resources/tessdata");
|
||||
result = tesseract.doOCR(image, new Rectangle(1200, 200));
|
||||
} catch (TesseractException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.tesseract;
|
||||
|
||||
import org.bytedeco.javacpp.BytePointer;
|
||||
import org.bytedeco.leptonica.PIX;
|
||||
import org.bytedeco.tesseract.TessBaseAPI;
|
||||
|
||||
public class TesseractPlatformExample {
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
TessBaseAPI tessApi = new TessBaseAPI();
|
||||
tessApi.Init("src/main/resources/tessdata", "eng", 3);
|
||||
tessApi.SetPageSegMode(1);
|
||||
PIX image = org.bytedeco.leptonica.global.lept.pixRead("src/main/resources/images/baeldung.png");
|
||||
tessApi.SetImage(image);
|
||||
|
||||
BytePointer outText = tessApi.GetUTF8Text();
|
||||
System.out.println(outText.getString());
|
||||
tessApi.End();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
BIN
image-processing/src/main/resources/images/baeldung.png
Normal file
BIN
image-processing/src/main/resources/images/baeldung.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 648 KiB |
BIN
image-processing/src/main/resources/images/multiLanguageText.png
Normal file
BIN
image-processing/src/main/resources/images/multiLanguageText.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 217 KiB |
9
image-processing/src/main/resources/images/output.txt
Normal file
9
image-processing/src/main/resources/images/output.txt
Normal file
@ -0,0 +1,9 @@
|
||||
Der ,.schnelle” braune Fuchs springt
|
||||
iiber den faulen Hund. Le renard brun
|
||||
«rapide» saute par-dessus le chien
|
||||
paresseux. La volpe marrone rapida
|
||||
salta sopra il cane pigro. El zorro
|
||||
marron rapido salta sobre el perro
|
||||
perezoso. A raposa marrom rapida
|
||||
salta sobre 0 cao preguicoso.
|
||||
|
1
jhipster-5/bookstore-monolith/.gitignore
vendored
1
jhipster-5/bookstore-monolith/.gitignore
vendored
@ -14,6 +14,7 @@ node_modules/
|
||||
npm-debug.log.*
|
||||
/.awcache/*
|
||||
/.cache-loader/*
|
||||
package-lock.json
|
||||
|
||||
######################
|
||||
# SASS
|
||||
|
18244
jhipster-5/bookstore-monolith/package-lock.json
generated
18244
jhipster-5/bookstore-monolith/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -20,11 +20,11 @@ import static java.lang.String.format;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for the UTC Hibernate configuration.
|
||||
* Tests for the UTC Hibernate configuration.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class HibernateTimeZoneTest {
|
||||
public class HibernateTimeZoneIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DateTimeWrapperRepository dateTimeWrapperRepository;
|
@ -13,6 +13,12 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox-tools</artifactId>
|
||||
@ -80,6 +86,7 @@
|
||||
<poi-scratchpad.version>3.15</poi-scratchpad.version>
|
||||
<batik-transcoder.version>1.8</batik-transcoder.version>
|
||||
<poi-ooxml.version>3.15</poi-ooxml.version>
|
||||
<commons-codec.version>1.14</commons-codec.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.baeldung.pdf.base64;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EncodeDecodeUnitTest {
|
||||
|
||||
private static final String IN_FILE = "src/test/resources/input.pdf";
|
||||
private static final String OUT_FILE = "src/test/resources/output.pdf";
|
||||
private static byte[] inFileBytes;
|
||||
|
||||
@BeforeClass
|
||||
public static void fileToByteArray() throws IOException {
|
||||
inFileBytes = Files.readAllBytes(Paths.get(IN_FILE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {
|
||||
|
||||
byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes);
|
||||
|
||||
byte[] decoded = java.util.Base64.getDecoder().decode(encoded);
|
||||
|
||||
writeToFile(OUT_FILE, decoded);
|
||||
|
||||
assertNotEquals(encoded.length, decoded.length);
|
||||
assertEquals(inFileBytes.length, decoded.length);
|
||||
|
||||
assertArrayEquals(decoded, inFileBytes);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {
|
||||
|
||||
byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(inFileBytes);
|
||||
|
||||
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(encoded);
|
||||
|
||||
writeToFile(OUT_FILE, decoded);
|
||||
|
||||
assertNotEquals(encoded.length, decoded.length);
|
||||
assertEquals(inFileBytes.length, decoded.length);
|
||||
|
||||
assertArrayEquals(decoded, inFileBytes);
|
||||
}
|
||||
|
||||
private void writeToFile(String fileName, byte[] bytes) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(fileName);
|
||||
fos.write(bytes);
|
||||
fos.flush();
|
||||
fos.close();
|
||||
}
|
||||
}
|
BIN
pdf/src/test/resources/input.pdf
Normal file
BIN
pdf/src/test/resources/input.pdf
Normal file
Binary file not shown.
@ -3,7 +3,7 @@ server.port=8082
|
||||
|
||||
#spring boot mongodb
|
||||
spring.data.mongodb.host=localhost
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.port=0
|
||||
spring.data.mongodb.database=springboot-mongo
|
||||
|
||||
spring.thymeleaf.cache=false
|
||||
|
@ -1,5 +1,5 @@
|
||||
spring.jpa.show-sql=true
|
||||
#MySql
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
|
||||
spring.datasource.username=baeldung
|
||||
spring.datasource.password=baeldung
|
||||
#spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
|
||||
#spring.datasource.username=baeldung
|
||||
#spring.datasource.password=baeldung
|
6
pom.xml
6
pom.xml
@ -1339,7 +1339,7 @@
|
||||
<!-- plugins -->
|
||||
<!-- can't upgrade the plugin yet; as there is an issue with 2.22 no longer running all the tests-->
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
<java.version>1.8</java.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
@ -1367,9 +1367,9 @@
|
||||
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
|
||||
<custom-pmd.version>0.0.1</custom-pmd.version>
|
||||
<gitflow-incremental-builder.version>3.8</gitflow-incremental-builder.version>
|
||||
<maven-jxr-plugin.version>2.3</maven-jxr-plugin.version>
|
||||
<maven-jxr-plugin.version>3.0.0</maven-jxr-plugin.version>
|
||||
<!-- <maven-pmd-plugin.version>3.9.0</maven-pmd-plugin.version> -->
|
||||
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
||||
<maven-pmd-plugin.version>3.13.0</maven-pmd-plugin.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<h2.version>1.4.197</h2.version>
|
||||
</properties>
|
||||
|
@ -1,8 +1,10 @@
|
||||
package org.baeldung.batch.model;
|
||||
|
||||
import java.util.Date;
|
||||
import org.baeldung.batch.service.adapter.LocalDateTimeAdapter;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@SuppressWarnings("restriction")
|
||||
@XmlRootElement(name = "transactionRecord")
|
||||
@ -11,7 +13,7 @@ public class Transaction {
|
||||
private int userId;
|
||||
private int age;
|
||||
private String postCode;
|
||||
private Date transactionDate;
|
||||
private LocalDateTime transactionDate;
|
||||
private double amount;
|
||||
|
||||
/* getters and setters for the attributes */
|
||||
@ -32,11 +34,12 @@ public class Transaction {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Date getTransactionDate() {
|
||||
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
|
||||
public LocalDateTime getTransactionDate() {
|
||||
return transactionDate;
|
||||
}
|
||||
|
||||
public void setTransactionDate(Date transactionDate) {
|
||||
public void setTransactionDate(LocalDateTime transactionDate) {
|
||||
this.transactionDate = transactionDate;
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,19 @@
|
||||
package org.baeldung.batch.service;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.baeldung.batch.model.Transaction;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.file.transform.FieldSet;
|
||||
import org.springframework.validation.BindException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
|
||||
|
||||
public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyy");
|
||||
|
||||
Transaction transaction = new Transaction();
|
||||
// you can either use the indices or custom names
|
||||
// I personally prefer the custom names easy for debugging and
|
||||
@ -20,13 +21,10 @@ public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
|
||||
transaction.setUsername(fieldSet.readString("username"));
|
||||
transaction.setUserId(fieldSet.readInt("userid"));
|
||||
transaction.setAmount(fieldSet.readDouble(3));
|
||||
|
||||
// Converting the date
|
||||
String dateString = fieldSet.readString(2);
|
||||
try {
|
||||
transaction.setTransactionDate(dateFormat.parse(dateString));
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
transaction.setTransactionDate(LocalDate.parse(dateString, formatter).atStartOfDay());
|
||||
|
||||
return transaction;
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
package org.baeldung.batch.service.adapter;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
|
||||
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT);
|
||||
|
||||
public LocalDateTime unmarshal(String v) throws Exception {
|
||||
return LocalDateTime.parse(v, DATE_TIME_FORMATTER);
|
||||
}
|
||||
|
||||
public String marshal(LocalDateTime v) throws Exception {
|
||||
return DATE_TIME_FORMATTER.format(v);
|
||||
}
|
||||
}
|
@ -2,19 +2,19 @@
|
||||
<transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>10000.0</amount>
|
||||
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-10-31 00:00:00</transactionDate>
|
||||
<userId>1234</userId>
|
||||
<username>devendra</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>12321.0</amount>
|
||||
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-12-03 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>john</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>23411.0</amount>
|
||||
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-02-02 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>robin</username>
|
||||
</transactionRecord>
|
||||
|
@ -2,19 +2,19 @@
|
||||
<transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>10000.0</amount>
|
||||
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-10-31 00:00:00</transactionDate>
|
||||
<userId>1234</userId>
|
||||
<username>devendra</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>12321.0</amount>
|
||||
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-12-03 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>john</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>23411.0</amount>
|
||||
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-02-02 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>robin</username>
|
||||
</transactionRecord>
|
||||
|
@ -2,19 +2,19 @@
|
||||
<transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>10000.0</amount>
|
||||
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-10-31 00:00:00</transactionDate>
|
||||
<userId>1234</userId>
|
||||
<username>devendra</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>12321.0</amount>
|
||||
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-12-03 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>john</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>23411.0</amount>
|
||||
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-02-02 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>robin</username>
|
||||
</transactionRecord>
|
||||
|
@ -2,19 +2,19 @@
|
||||
<transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>10000.0</amount>
|
||||
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-10-31 00:00:00</transactionDate>
|
||||
<userId>1234</userId>
|
||||
<username>devendra</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>12321.0</amount>
|
||||
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-12-03 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>john</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>23411.0</amount>
|
||||
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-02-02 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>robin</username>
|
||||
</transactionRecord>
|
||||
|
@ -2,19 +2,19 @@
|
||||
<transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>10000.0</amount>
|
||||
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-10-31 00:00:00</transactionDate>
|
||||
<userId>1234</userId>
|
||||
<username>devendra</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>12321.0</amount>
|
||||
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-12-03 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>john</username>
|
||||
</transactionRecord>
|
||||
<transactionRecord>
|
||||
<amount>23411.0</amount>
|
||||
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
|
||||
<transactionDate>2015-02-02 00:00:00</transactionDate>
|
||||
<userId>2134</userId>
|
||||
<username>robin</username>
|
||||
</transactionRecord>
|
||||
|
@ -1 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31T00:00:00+05:30</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03T00:00:00+05:30</transactionDate><userId>9999</userId><username>john</username></transactionRecord></transactionRecord>
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31 00:00:00</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03 00:00:00</transactionDate><userId>9999</userId><username>john</username></transactionRecord></transactionRecord>
|
||||
|
@ -1 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><amount>10000.0</amount><transactionDate>2015-10-31T00:00:00+05:30</transactionDate><userId>1234</userId><username>devendra</username></transactionRecord><transactionRecord><amount>12321.0</amount><transactionDate>2015-12-03T00:00:00+05:30</transactionDate><userId>2134</userId><username>john</username></transactionRecord><transactionRecord><amount>23411.0</amount><transactionDate>2015-02-02T00:00:00+05:30</transactionDate><userId>2134</userId><username>robin</username></transactionRecord></transactionRecord>
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><amount>10000.0</amount><transactionDate>2015-10-31 00:00:00</transactionDate><userId>1234</userId><username>devendra</username></transactionRecord><transactionRecord><amount>12321.0</amount><transactionDate>2015-12-03 00:00:00</transactionDate><userId>2134</userId><username>john</username></transactionRecord><transactionRecord><amount>23411.0</amount><transactionDate>2015-02-02 00:00:00</transactionDate><userId>2134</userId><username>robin</username></transactionRecord></transactionRecord>
|
@ -1 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31T00:00:00+05:30</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03T00:00:00+05:30</transactionDate><userId>9999</userId><username>john</username></transactionRecord></transactionRecord>
|
||||
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31 00:00:00</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03 00:00:00</transactionDate><userId>9999</userId><username>john</username></transactionRecord></transactionRecord>
|
@ -32,6 +32,7 @@
|
||||
<module>spring-boot-crud</module>
|
||||
<module>spring-boot-data</module>
|
||||
<module>spring-boot-environment</module>
|
||||
<module>spring-boot-exceptions</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
|
||||
<module>spring-boot-jasypt</module>
|
||||
|
7
spring-boot-modules/spring-boot-exceptions/README.MD
Normal file
7
spring-boot-modules/spring-boot-exceptions/README.MD
Normal file
@ -0,0 +1,7 @@
|
||||
## Spring Boot
|
||||
|
||||
This module contains articles about Spring Boot Exceptions
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [The BeanDefinitionOverrideException in Spring Boot](https://www.baeldung.com/spring-boot-bean-definition-override-exception)
|
96
spring-boot-modules/spring-boot-exceptions/pom.xml
Normal file
96
spring-boot-modules/spring-boot-exceptions/pom.xml
Normal file
@ -0,0 +1,96 @@
|
||||
<?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>
|
||||
<artifactId>spring-boot-exceptions</artifactId>
|
||||
<name>spring-boot-exceptions</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Demo project for working with Spring Boot exceptions</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>2.2.3.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot-exceptions</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<delimiters>
|
||||
<delimiter>@</delimiter>
|
||||
</delimiters>
|
||||
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/AutoconfigurationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.intro.App</start-class>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
<configuration>
|
||||
<include resource="/org/springframework/boot/logging/logback/base.xml"/>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
<logger name="com.baeldung.testloglevel" level="debug"/>
|
||||
</configuration>
|
@ -31,4 +31,3 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [Spring Shutdown Callbacks](https://www.baeldung.com/spring-shutdown-callbacks)
|
||||
- [Container Configuration in Spring Boot 2](https://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot)
|
||||
- [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
|
||||
- [The BeanDefinitionOverrideException in Spring Boot](https://www.baeldung.com/spring-boot-bean-definition-override-exception)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
public interface Item {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
public class ItemImpl1 implements Item {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
<!-- Autowired injection -->
|
||||
|
||||
<bean id="item" class="org.baeldung.store.ItemImpl1" />
|
||||
<bean id="item" class="com.baeldung.store.ItemImpl1" />
|
||||
|
||||
<bean id="xml-store-by-autowire-type" class="org.baeldung.store.Store" autowire="byType">
|
||||
<bean id="xml-store-by-autowire-type" class="com.baeldung.store.Store" autowire="byType">
|
||||
</bean>
|
||||
|
||||
</beans>
|
@ -6,28 +6,28 @@
|
||||
|
||||
<!-- Constructor injection -->
|
||||
|
||||
<bean id="item1" class="org.baeldung.store.ItemImpl1" />
|
||||
<bean id="xml-store-by-constructor" class="org.baeldung.store.Store">
|
||||
<bean id="item1" class="com.baeldung.store.ItemImpl1" />
|
||||
<bean id="xml-store-by-constructor" class="com.baeldung.store.Store">
|
||||
<constructor-arg type="Item" index="0" name="item" ref="item1" />
|
||||
</bean>
|
||||
|
||||
<!-- Setter injection -->
|
||||
|
||||
<bean id="xml-store-by-setter" class="org.baeldung.store.Store">
|
||||
<bean id="xml-store-by-setter" class="com.baeldung.store.Store">
|
||||
<property name="item" ref="item1" />
|
||||
</bean>
|
||||
|
||||
<!-- Autowired injection -->
|
||||
|
||||
<bean id="item" class="org.baeldung.store.ItemImpl1" />
|
||||
<bean id="item" class="com.baeldung.store.ItemImpl1" />
|
||||
|
||||
<bean id="xml-store-by-autowire-name" class="org.baeldung.store.Store" autowire="byName">
|
||||
<bean id="xml-store-by-autowire-name" class="com.baeldung.store.Store" autowire="byName">
|
||||
</bean>
|
||||
|
||||
<!-- Lazy instantiation -->
|
||||
|
||||
<bean id="item1-lazy" class="org.baeldung.store.ItemImpl1" lazy-init="true" />
|
||||
<bean id="xml-store-by-setter-lazy" class="org.baeldung.store.Store">
|
||||
<bean id="item1-lazy" class="com.baeldung.store.ItemImpl1" lazy-init="true" />
|
||||
<bean id="xml-store-by-setter-lazy" class="com.baeldung.store.Store">
|
||||
<property name="item" ref="item1-lazy" />
|
||||
</bean>
|
||||
|
||||
|
@ -1,35 +1,35 @@
|
||||
package org.baeldung.store;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfig.class)
|
||||
public class AppConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("storeThroughConstructorInjection")
|
||||
private Store storeByConstructorInjection;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("storeThroughSetterInjection")
|
||||
private Store storeBySetterInjection;
|
||||
|
||||
@Test
|
||||
public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() {
|
||||
assertNotNull(storeByConstructorInjection);
|
||||
assertNotNull(storeByConstructorInjection.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() {
|
||||
assertNotNull(storeBySetterInjection);
|
||||
assertNotNull(storeByConstructorInjection.getItem());
|
||||
}
|
||||
}
|
||||
package com.baeldung.store;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfig.class)
|
||||
public class AppConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("storeThroughConstructorInjection")
|
||||
private Store storeByConstructorInjection;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("storeThroughSetterInjection")
|
||||
private Store storeBySetterInjection;
|
||||
|
||||
@Test
|
||||
public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() {
|
||||
assertNotNull(storeByConstructorInjection);
|
||||
assertNotNull(storeByConstructorInjection.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() {
|
||||
assertNotNull(storeBySetterInjection);
|
||||
assertNotNull(storeByConstructorInjection.getItem());
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.store;
|
||||
package com.baeldung.store;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import io.katharsis.spring.boot.v3.KatharsisConfigV3;
|
||||
|
@ -1,14 +1,14 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.baeldung.persistence.dao.RoleRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.model.Role;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import com.baeldung.persistence.dao.RoleRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.model.Role;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Role;
|
||||
import com.baeldung.persistence.model.Role;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface RoleRepository extends JpaRepository<Role, Long> {
|
@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
@ -1,12 +1,12 @@
|
||||
package org.baeldung.persistence.katharsis;
|
||||
package com.baeldung.persistence.katharsis;
|
||||
|
||||
|
||||
import com.baeldung.persistence.dao.RoleRepository;
|
||||
import io.katharsis.queryspec.QuerySpec;
|
||||
import io.katharsis.repository.ResourceRepositoryV2;
|
||||
import io.katharsis.resource.list.ResourceList;
|
||||
|
||||
import org.baeldung.persistence.dao.RoleRepository;
|
||||
import org.baeldung.persistence.model.Role;
|
||||
import com.baeldung.persistence.model.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -1,11 +1,11 @@
|
||||
package org.baeldung.persistence.katharsis;
|
||||
package com.baeldung.persistence.katharsis;
|
||||
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import io.katharsis.queryspec.QuerySpec;
|
||||
import io.katharsis.repository.ResourceRepositoryV2;
|
||||
import io.katharsis.resource.list.ResourceList;
|
||||
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -1,5 +1,8 @@
|
||||
package org.baeldung.persistence.katharsis;
|
||||
package com.baeldung.persistence.katharsis;
|
||||
|
||||
import com.baeldung.persistence.dao.RoleRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import io.katharsis.queryspec.QuerySpec;
|
||||
import io.katharsis.repository.RelationshipRepositoryV2;
|
||||
import io.katharsis.resource.list.ResourceList;
|
||||
@ -7,10 +10,7 @@ import io.katharsis.resource.list.ResourceList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.baeldung.persistence.dao.RoleRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.model.Role;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import com.baeldung.persistence.model.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import io.katharsis.resource.annotations.JsonApiId;
|
||||
import io.katharsis.resource.annotations.JsonApiRelation;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import io.katharsis.resource.annotations.JsonApiId;
|
||||
import io.katharsis.resource.annotations.JsonApiRelation;
|
@ -1,6 +1,5 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.Application;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.test;
|
||||
package com.baeldung.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -103,8 +105,8 @@ public class FooMappingExamplesController {
|
||||
// --- Ambiguous Mapping
|
||||
|
||||
@GetMapping(value = "foos/duplicate" )
|
||||
public String duplicate() {
|
||||
return "Duplicate";
|
||||
public ResponseEntity<String> duplicate() {
|
||||
return new ResponseEntity<>("Duplicate", HttpStatus.OK);
|
||||
}
|
||||
|
||||
// uncomment for exception of type java.lang.IllegalStateException: Ambiguous mapping
|
||||
@ -114,14 +116,14 @@ public class FooMappingExamplesController {
|
||||
// return "Duplicate";
|
||||
// }
|
||||
|
||||
@GetMapping(value = "foos/duplicate/xml", produces = MediaType.APPLICATION_XML_VALUE)
|
||||
public String duplicateXml() {
|
||||
return "Duplicate Xml";
|
||||
@GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_XML_VALUE)
|
||||
public ResponseEntity<String> duplicateXml() {
|
||||
return new ResponseEntity<>("<message>Duplicate</message>", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "foos/duplicate/json", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public String duplicateJson() {
|
||||
return "Duplicate Json";
|
||||
@GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<String> duplicateJson() {
|
||||
return new ResponseEntity<>("{\"message\":\"Duplicate\"}", HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.requestmapping;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(FooMappingExamplesController.class)
|
||||
public class FooMappingExamplesControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void givenAcceptsJson_whenGetDuplicate_thenJsonResponseReturned() throws Exception {
|
||||
mvc.perform(get("/ex/foos/duplicate")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("{\"message\":\"Duplicate\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAcceptsXml_whenGetDuplicate_thenXmlResponseReturned() throws Exception {
|
||||
mvc.perform(get("/ex/foos/duplicate")
|
||||
.accept(MediaType.APPLICATION_XML))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("<message>Duplicate</message>"));
|
||||
}
|
||||
}
|
@ -59,7 +59,7 @@ public class CustomUserDetailsServiceIntegrationTest {
|
||||
@WithAnonymousUser
|
||||
public void givenAnonymous_whenRequestFoo_thenRetrieveUnauthorized() throws Exception {
|
||||
this.mvc.perform(get("/foos/1").with(csrf()))
|
||||
.andExpect(status().isUnauthorized());
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -17,8 +17,7 @@ import static io.restassured.RestAssured.with;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
|
||||
public class RestAssured2IntegrationTest {
|
||||
private static final int PORT = 8084;
|
||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
||||
private static WireMockServer wireMockServer;
|
||||
|
||||
private static final String EVENTS_PATH = "/odds";
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
@ -27,9 +26,11 @@ public class RestAssured2IntegrationTest {
|
||||
@BeforeClass
|
||||
public static void before() throws Exception {
|
||||
System.out.println("Setting up!");
|
||||
final int port = Util.getAvailablePort();
|
||||
wireMockServer = new WireMockServer(port);
|
||||
wireMockServer.start();
|
||||
configureFor("localhost", PORT);
|
||||
RestAssured.port = PORT;
|
||||
configureFor("localhost", port);
|
||||
RestAssured.port = port;
|
||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||
aResponse().withStatus(200)
|
||||
.withHeader("Content-Type", APPLICATION_JSON)
|
||||
|
@ -21,8 +21,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
|
||||
public class RestAssuredIntegrationTest {
|
||||
private static final int PORT = 8083;
|
||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
||||
private static WireMockServer wireMockServer;
|
||||
|
||||
private static final String EVENTS_PATH = "/events?id=390";
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
@ -31,9 +30,11 @@ public class RestAssuredIntegrationTest {
|
||||
@BeforeClass
|
||||
public static void before() throws Exception {
|
||||
System.out.println("Setting up!");
|
||||
final int port = Util.getAvailablePort();
|
||||
wireMockServer = new WireMockServer(port);
|
||||
wireMockServer.start();
|
||||
RestAssured.port = PORT;
|
||||
configureFor("localhost", PORT);
|
||||
RestAssured.port = port;
|
||||
configureFor("localhost", port);
|
||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||
aResponse().withStatus(200)
|
||||
.withHeader("Content-Type", APPLICATION_JSON)
|
||||
|
@ -12,11 +12,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static io.restassured.RestAssured.get;
|
||||
import static io.restassured.RestAssured.port;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
|
||||
public class RestAssuredXML2IntegrationTest {
|
||||
private static final int PORT = 8082;
|
||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
||||
private static WireMockServer wireMockServer;
|
||||
|
||||
private static final String EVENTS_PATH = "/teachers";
|
||||
private static final String APPLICATION_XML = "application/xml";
|
||||
@ -25,9 +25,11 @@ public class RestAssuredXML2IntegrationTest {
|
||||
@BeforeClass
|
||||
public static void before() throws Exception {
|
||||
System.out.println("Setting up!");
|
||||
final int port = Util.getAvailablePort();
|
||||
wireMockServer = new WireMockServer(port);
|
||||
wireMockServer.start();
|
||||
RestAssured.port = PORT;
|
||||
configureFor("localhost", PORT);
|
||||
RestAssured.port = port;
|
||||
configureFor("localhost", port);
|
||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||
aResponse().withStatus(200)
|
||||
.withHeader("Content-Type", APPLICATION_XML)
|
||||
|
@ -17,8 +17,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.xml.HasXPath.hasXPath;
|
||||
|
||||
public class RestAssuredXMLIntegrationTest {
|
||||
private static final int PORT = 8081;
|
||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
||||
private static WireMockServer wireMockServer;
|
||||
|
||||
private static final String EVENTS_PATH = "/employees";
|
||||
private static final String APPLICATION_XML = "application/xml";
|
||||
@ -27,9 +26,11 @@ public class RestAssuredXMLIntegrationTest {
|
||||
@BeforeClass
|
||||
public static void before() throws Exception {
|
||||
System.out.println("Setting up!");
|
||||
final int port = Util.getAvailablePort();
|
||||
wireMockServer = new WireMockServer(port);
|
||||
wireMockServer.start();
|
||||
configureFor("localhost", PORT);
|
||||
RestAssured.port = PORT;
|
||||
configureFor("localhost", port);
|
||||
RestAssured.port = port;
|
||||
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||
aResponse().withStatus(200)
|
||||
.withHeader("Content-Type", APPLICATION_XML)
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.baeldung.restassured;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
final class Util {
|
||||
|
||||
private static final int DEFAULT_PORT = 8069;
|
||||
|
||||
private Util() {
|
||||
}
|
||||
|
||||
@ -12,4 +17,22 @@ final class Util {
|
||||
Scanner s = new Scanner(is).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
|
||||
static int getAvailablePort() {
|
||||
return new Random()
|
||||
.ints(6000, 9000)
|
||||
.filter(Util::isFree)
|
||||
.findFirst()
|
||||
.orElse(DEFAULT_PORT);
|
||||
}
|
||||
|
||||
|
||||
private static boolean isFree(int port) {
|
||||
try {
|
||||
new ServerSocket(port).close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user