Merge branch 'master' of github.com:eugenp/tutorials into BAEL-3886
This commit is contained in:
@@ -5,7 +5,7 @@ spring.jpa.hibernate.ddl-auto=create
|
||||
logging.file=azure.log
|
||||
logging.level.root=info
|
||||
|
||||
spring.datasource.url=jdbc:h2:file:~/test
|
||||
spring.datasource.url=jdbc:h2:mem:azure-test-db
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
@@ -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>
|
||||
+24
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -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");
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
+14
@@ -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();
|
||||
}
|
||||
}
|
||||
+15
@@ -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;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import java.security.BasicPermission;
|
||||
|
||||
public class ResourcePermission extends BasicPermission {
|
||||
public ResourcePermission(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
+74
@@ -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>
|
||||
|
||||
+124
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+78
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,123 +1,153 @@
|
||||
package com.baeldung.stack;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Stack;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
public class StackUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStackIsCreated_thenItHasSize0() {
|
||||
Stack intStack = new Stack();
|
||||
public void whenStackIsCreated_thenItHasSizeZero() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
|
||||
assertEquals(0, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStack_whenElementIsPushed_thenStackSizeisIncreased() {
|
||||
Stack intStack = new Stack();
|
||||
public void whenElementIsPushed_thenStackSizeIsIncreased() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(1);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStack_whenMultipleElementsArePushed_thenStackSizeisIncreased() {
|
||||
Stack intStack = new Stack();
|
||||
public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
|
||||
boolean result = intStack.addAll(intList);
|
||||
|
||||
assertTrue(result);
|
||||
assertEquals(7, intList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.pop();
|
||||
|
||||
Integer element = intStack.pop();
|
||||
|
||||
assertEquals(Integer.valueOf(5), element);
|
||||
assertTrue(intStack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.peek();
|
||||
|
||||
Integer element = intStack.peek();
|
||||
|
||||
assertEquals(Integer.valueOf(5), element);
|
||||
assertEquals(1, intStack.search(5));
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
assertEquals(1, intStack.search(5));
|
||||
intStack.push(8);
|
||||
|
||||
assertEquals(2, intStack.search(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
|
||||
int indexOf = intStack.indexOf(5);
|
||||
|
||||
assertEquals(0, indexOf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
|
||||
int lastIndexOf = intStack.lastIndexOf(5);
|
||||
|
||||
assertEquals(2, lastIndexOf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementOnStack_whenRemoveElementIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
public void whenRemoveElementIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(5);
|
||||
|
||||
intStack.removeElement(5);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementOnStack_whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(7);
|
||||
|
||||
intStack.removeElementAt(1);
|
||||
|
||||
assertEquals(-1, intStack.search(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementsOnStack_whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
intStack.push(5);
|
||||
intStack.push(7);
|
||||
|
||||
intStack.removeAllElements();
|
||||
|
||||
assertTrue(intStack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
intStack.add(500);
|
||||
|
||||
intStack.removeAll(intList);
|
||||
|
||||
assertEquals(1, intStack.size());
|
||||
assertEquals(1, intStack.search(500));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElementsOnStack_whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack();
|
||||
public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
|
||||
intStack.removeIf(element -> element < 6);
|
||||
|
||||
assertEquals(2, intStack.size());
|
||||
}
|
||||
|
||||
@@ -126,12 +156,28 @@ public class StackUnitTest {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
intStack.addAll(intList);
|
||||
|
||||
ListIterator<Integer> it = intStack.listIterator();
|
||||
Stack<Integer> result = new Stack();
|
||||
|
||||
Stack<Integer> result = new Stack<>();
|
||||
while(it.hasNext()) {
|
||||
result.push(it.next());
|
||||
}
|
||||
|
||||
assertThat(result, equalTo(intStack));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
|
||||
Stack<Integer> intStack = new Stack<>();
|
||||
List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
|
||||
intStack.addAll(inputIntList);
|
||||
|
||||
List<Integer> filtered = intStack
|
||||
.stream()
|
||||
.filter(element -> element <= 3)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(3, filtered.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.gson.jsoncompare;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JsonCompareUnitTest {
|
||||
@Test
|
||||
public void givenJsonStrings_whenCompared_thenNotEqual() {
|
||||
String string1 = "{\"fullName\": \"Emily Jenkins\", \"age\": 27 }";
|
||||
String string2 = "{\"fullName\": \"Emily Jenkins\", \"age\": 27}";
|
||||
|
||||
assertNotEquals(string1, string2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalSimpleObjects_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27 }}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\", \"fullName\": \"Emily Jenkins\",\"age\": 27}}";
|
||||
|
||||
assertTrue(parser.parse(string1)
|
||||
.isJsonObject());
|
||||
assertEquals(parser.parse(string1), parser.parse(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameObjectsInDifferentOrder_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27 }}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"age\": 27, \"fullName\": \"Emily Jenkins\" }}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalArrays_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "[10, 20, 30]";
|
||||
String string2 = "[10, 20, 30]";
|
||||
|
||||
assertTrue(parser.parse(string1)
|
||||
.isJsonArray());
|
||||
assertEquals(parser.parse(string1), parser.parse(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysInDifferentOrder_whenCompared_thenNotEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "[20, 10, 30]";
|
||||
String string2 = "[10, 20, 30]";
|
||||
|
||||
assertNotEquals(parser.parse(string1), parser.parse(string2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalNestedObjects_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"fav_product\": \"Coke\", \"last_buy\": \"2012-04-23\"}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"fav_product\": \"Coke\"}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalNestedObjectsWithArray_whenCompared_thenEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
|
||||
assertEquals(json1, json2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNestedObjectsDifferentArrayOrder_whenCompared_thenNotEqual() {
|
||||
JsonParser parser = new JsonParser();
|
||||
String string1 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"banana\", \"eggs\"]}}}";
|
||||
String string2 = "{\"customer\": {\"id\": \"44521\",\"fullName\": \"Emily Jenkins\", \"age\": 27, \"consumption_info\" : {\"last_buy\": \"2012-04-23\", \"prouducts\": [\"eggs\", \"banana\"]}}}";
|
||||
|
||||
JsonElement json1 = parser.parse(string1);
|
||||
JsonElement json2 = parser.parse(string2);
|
||||
|
||||
assertNotEquals(json1, json2);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 648 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 217 KiB |
@@ -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.
|
||||
|
||||
@@ -14,6 +14,7 @@ node_modules/
|
||||
npm-debug.log.*
|
||||
/.awcache/*
|
||||
/.cache-loader/*
|
||||
package-lock.json
|
||||
|
||||
######################
|
||||
# SASS
|
||||
|
||||
-18244
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -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;
|
||||
+58
-1
@@ -435,7 +435,23 @@
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>${reflections.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.uber.nullaway</groupId>
|
||||
<artifactId>nullaway</artifactId>
|
||||
<version>0.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.plexus</groupId>
|
||||
<artifactId>plexus-compiler-javac-errorprone</artifactId>
|
||||
<version>2.8</version>
|
||||
</dependency>
|
||||
<!-- override plexus-compiler-javac-errorprone's dependency on
|
||||
Error Prone with the latest version -->
|
||||
<dependency>
|
||||
<groupId>com.google.errorprone</groupId>
|
||||
<artifactId>error_prone_core</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
@@ -552,6 +568,47 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5</version>
|
||||
<configuration>
|
||||
<compilerId>javac-with-errorprone</compilerId>
|
||||
<forceJavacCompilerUse>true</forceJavacCompilerUse>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<showWarnings>true</showWarnings>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>com.uber.nullaway</groupId>
|
||||
<artifactId>nullaway</artifactId>
|
||||
<version>0.3.0</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<compilerArgs>
|
||||
<!-- NullAway will warn by default, uncomment the next line to make the build fail -->
|
||||
<!-- <arg>-Xep:NullAway:ERROR</arg> -->
|
||||
<arg>-XepExcludedPaths:(.*)/test/.*|(.*)/streamex/.*</arg>
|
||||
<arg>-XepOpt:NullAway:AnnotatedPackages=com.baeldung.nullaway</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.plexus</groupId>
|
||||
<artifactId>plexus-compiler-javac-errorprone</artifactId>
|
||||
<version>2.8</version>
|
||||
</dependency>
|
||||
<!-- override plexus-compiler-javac-errorprone's dependency on
|
||||
Error Prone with the latest version -->
|
||||
<dependency>
|
||||
<groupId>com.google.errorprone</groupId>
|
||||
<artifactId>error_prone_core</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nullaway;
|
||||
|
||||
import com.baeldung.distinct.Person;
|
||||
|
||||
public class NullAwayExample {
|
||||
|
||||
/*
|
||||
* 1- NullAway will warn about yearsToRetirement method
|
||||
* 2- Uncomment @Nullable in printAge and NullAway will warn about this method
|
||||
* 3- Add a standard null check to be NullAway compliant
|
||||
* 4- Build will be SUCCESS
|
||||
*/
|
||||
|
||||
static Integer getAge(/*@Nullable*/ Person person) {
|
||||
return person.getAge();
|
||||
}
|
||||
|
||||
Integer yearsToRetirement() {
|
||||
Person p = null;
|
||||
// ... p never gets set correctly...
|
||||
return 65 - getAge(p);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user