Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
|
||||
The "REST with Spring" Classes
|
||||
The Courses
|
||||
==============================
|
||||
|
||||
Here's the Master Class of REST With Spring (along with the newly announced Boot 2 material): <br/>
|
||||
**[>> THE REST WITH SPRING - MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||
|
||||
And here's the Master Class of Learn Spring Security: <br/>
|
||||
**[>> LEARN SPRING SECURITY - MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||
Here's the new "Learn Spring" course: <br/>
|
||||
**[>> LEARN SPRING - THE MASTER CLASS](https://www.baeldung.com/learn-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=ls#master-class)**
|
||||
|
||||
Here's the Master Class of "REST With Spring" (along with the new announced Boot 2 material): <br/>
|
||||
**[>> THE REST WITH SPRING - MASTER CLASS](https://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||
|
||||
And here's the Master Class of "Learn Spring Security": <br/>
|
||||
**[>> LEARN SPRING SECURITY - MASTER CLASS](https://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +19,7 @@ Java and Spring Tutorials
|
||||
|
||||
This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
|
||||
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.
|
||||
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
||||
In additional to Spring, the modules here are covering a number of aspects in Java.
|
||||
|
||||
|
||||
Building the project
|
||||
@@ -32,8 +36,15 @@ Running a Spring Boot module
|
||||
====================
|
||||
To run a Spring Boot module run the command: `mvn spring-boot:run` in the module directory
|
||||
|
||||
###Running Tests
|
||||
|
||||
Working with the IDE
|
||||
====================
|
||||
This repo contains a large number of modules.
|
||||
When you're working with an individual module, there's no need to import all of them (or build all of them) - you can simply import that particular module in either Eclipse or IntelliJ.
|
||||
|
||||
|
||||
Running Tests
|
||||
=============
|
||||
The command `mvn clean install` will run the unit tests in a module.
|
||||
To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant articles:
|
||||
## Relevant Articles:
|
||||
|
||||
|
||||
- [String API Updates in Java 12](https://www.baeldung.com/java12-string-api)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
*.class
|
||||
|
||||
0.*
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
.resourceCache
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
||||
@@ -0,0 +1,50 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-arrays-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-arrays-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>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-arrays-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+1
-1
@@ -4,7 +4,7 @@ import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LoopDiagonallyTest {
|
||||
public class LoopDiagonallyUnitTest {
|
||||
|
||||
@Test
|
||||
public void twoArrayIsLoopedDiagonallyAsExpected() {
|
||||
@@ -1,3 +1,3 @@
|
||||
## Relevant articles:
|
||||
## Relevant Articles:
|
||||
|
||||
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.files;
|
||||
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingApacheCommonsIO;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingBufferedReader;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingGoogleGuava;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingLineNumberReader;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFileChannel;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFiles;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingScanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final String INPUT_FILE_NAME = "src/main/resources/input.txt";
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
System.out.printf("Total Number of Lines Using BufferedReader: %s%n", getTotalNumberOfLinesUsingBufferedReader(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using LineNumberReader: %s%n", getTotalNumberOfLinesUsingLineNumberReader(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using Scanner: %s%n", getTotalNumberOfLinesUsingScanner(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using NIO Files: %s%n", getTotalNumberOfLinesUsingNIOFiles(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using NIO FileChannel: %s%n", getTotalNumberOfLinesUsingNIOFileChannel(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using Apache Commons IO: %s%n", getTotalNumberOfLinesUsingApacheCommonsIO(INPUT_FILE_NAME));
|
||||
System.out.printf("Total Number of Lines Using NIO Google Guava: %s%n", getTotalNumberOfLinesUsingGoogleGuava(INPUT_FILE_NAME));
|
||||
}
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package com.baeldung.files;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.LineIterator;
|
||||
|
||||
public class NumberOfLineFinder {
|
||||
|
||||
public static int getTotalNumberOfLinesUsingBufferedReader(String fileName) {
|
||||
int lines = 0;
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
|
||||
while (reader.readLine() != null) {
|
||||
lines++;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingLineNumberReader(String fileName) {
|
||||
int lines = 0;
|
||||
try (LineNumberReader reader = new LineNumberReader(new FileReader(fileName))) {
|
||||
reader.skip(Integer.MAX_VALUE);
|
||||
lines = reader.getLineNumber() + 1;
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingScanner(String fileName) {
|
||||
int lines = 0;
|
||||
try (Scanner scanner = new Scanner(new FileReader(fileName))) {
|
||||
while (scanner.hasNextLine()) {
|
||||
scanner.nextLine();
|
||||
lines++;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingNIOFiles(String fileName) {
|
||||
int lines = 0;
|
||||
try (Stream<String> fileStream = Files.lines(Paths.get(fileName))) {
|
||||
lines = (int) fileStream.count();
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingNIOFileChannel(String fileName) {
|
||||
int lines = 1;
|
||||
try (FileChannel channel = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ)) {
|
||||
ByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
|
||||
while (byteBuffer.hasRemaining()) {
|
||||
byte currentChar = byteBuffer.get();
|
||||
if (currentChar == '\n') {
|
||||
lines++;
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingApacheCommonsIO(String fileName) {
|
||||
int lines = 0;
|
||||
try {
|
||||
LineIterator lineIterator = FileUtils.lineIterator(new File(fileName));
|
||||
while (lineIterator.hasNext()) {
|
||||
lineIterator.nextLine();
|
||||
lines++;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static int getTotalNumberOfLinesUsingGoogleGuava(String fileName) {
|
||||
int lines = 0;
|
||||
try {
|
||||
List<String> lineItems = com.google.common.io.Files.readLines(Paths.get(fileName)
|
||||
.toFile(), Charset.defaultCharset());
|
||||
lines = lineItems.size();
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingApacheCommonsIO;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingBufferedReader;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingGoogleGuava;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingLineNumberReader;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFileChannel;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFiles;
|
||||
import static com.baeldung.files.NumberOfLineFinder.getTotalNumberOfLinesUsingScanner;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NumberOfLineFinderUnitTest {
|
||||
private static final String INPUT_FILE_NAME = "src/main/resources/input.txt";
|
||||
private static final int ACTUAL_LINE_COUNT = 45;
|
||||
|
||||
@Test
|
||||
public void whenUsingBufferedReader_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingBufferedReader(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingLineNumberReader_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingLineNumberReader(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingScanner_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingScanner(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingNIOFiles_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingNIOFiles(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingNIOFileChannel_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingNIOFileChannel(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingApacheCommonsIO_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingApacheCommonsIO(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_thenReturnTotalNumberOfLines() {
|
||||
int lines = getTotalNumberOfLinesUsingGoogleGuava(INPUT_FILE_NAME);
|
||||
assertEquals(ACTUAL_LINE_COUNT, lines);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Car {
|
||||
|
||||
private List<Wheel> wheels;
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CarWithStaticInnerWheel {
|
||||
|
||||
private List<Wheel> wheels;
|
||||
|
||||
public static class Wheel {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
public class Wheel {
|
||||
|
||||
private Car car;
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.relationships.association;
|
||||
|
||||
public class Child {
|
||||
|
||||
private Mother mother;
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.relationships.association;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Mother {
|
||||
|
||||
private List<Child> children;
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.relationships.composition;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Building {
|
||||
|
||||
private String address;
|
||||
private List<Room> rooms;
|
||||
|
||||
public class Room {
|
||||
|
||||
public String getBuildingAddress() {
|
||||
return Building.this.address;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.relationships.composition;
|
||||
|
||||
public class BuildingWithDefinitionRoomInMethod {
|
||||
|
||||
public Room createAnonymousRoom() {
|
||||
return new Room() {
|
||||
@Override
|
||||
public void doInRoom() {}
|
||||
};
|
||||
}
|
||||
|
||||
public Room createInlineRoom() {
|
||||
class InlineRoom implements Room {
|
||||
@Override
|
||||
public void doInRoom() {}
|
||||
}
|
||||
return new InlineRoom();
|
||||
}
|
||||
|
||||
public Room createLambdaRoom() {
|
||||
return () -> {};
|
||||
}
|
||||
|
||||
public interface Room {
|
||||
void doInRoom();
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Department {
|
||||
|
||||
private List<Professor> professors;
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Professor {
|
||||
|
||||
private List<Department> department;
|
||||
private List<Professor> friends;
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class University {
|
||||
|
||||
private List<Department> department;
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.sasl;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.security.auth.callback.Callback;
|
||||
import javax.security.auth.callback.CallbackHandler;
|
||||
import javax.security.auth.callback.NameCallback;
|
||||
import javax.security.auth.callback.PasswordCallback;
|
||||
import javax.security.auth.callback.UnsupportedCallbackException;
|
||||
import javax.security.sasl.RealmCallback;
|
||||
|
||||
public class ClientCallbackHandler implements CallbackHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Callback[] cbs) throws IOException, UnsupportedCallbackException {
|
||||
for (Callback cb : cbs) {
|
||||
if (cb instanceof NameCallback) {
|
||||
NameCallback nc = (NameCallback) cb;
|
||||
nc.setName("username");
|
||||
} else if (cb instanceof PasswordCallback) {
|
||||
PasswordCallback pc = (PasswordCallback) cb;
|
||||
pc.setPassword("password".toCharArray());
|
||||
} else if (cb instanceof RealmCallback) {
|
||||
RealmCallback rc = (RealmCallback) cb;
|
||||
rc.setText("myServer");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.sasl;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.security.auth.callback.Callback;
|
||||
import javax.security.auth.callback.CallbackHandler;
|
||||
import javax.security.auth.callback.NameCallback;
|
||||
import javax.security.auth.callback.PasswordCallback;
|
||||
import javax.security.auth.callback.UnsupportedCallbackException;
|
||||
import javax.security.sasl.AuthorizeCallback;
|
||||
import javax.security.sasl.RealmCallback;
|
||||
|
||||
public class ServerCallbackHandler implements CallbackHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Callback[] cbs) throws IOException, UnsupportedCallbackException {
|
||||
for (Callback cb : cbs) {
|
||||
if (cb instanceof AuthorizeCallback) {
|
||||
AuthorizeCallback ac = (AuthorizeCallback) cb;
|
||||
ac.setAuthorized(true);
|
||||
} else if (cb instanceof NameCallback) {
|
||||
NameCallback nc = (NameCallback) cb;
|
||||
nc.setName("username");
|
||||
|
||||
} else if (cb instanceof PasswordCallback) {
|
||||
PasswordCallback pc = (PasswordCallback) cb;
|
||||
pc.setPassword("password".toCharArray());
|
||||
} else if (cb instanceof RealmCallback) {
|
||||
RealmCallback rc = (RealmCallback) cb;
|
||||
rc.setText("myServer");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.sasl;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.security.sasl.Sasl;
|
||||
import javax.security.sasl.SaslClient;
|
||||
import javax.security.sasl.SaslException;
|
||||
import javax.security.sasl.SaslServer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SaslUnitTest {
|
||||
|
||||
private static final String MECHANISM = "DIGEST-MD5";
|
||||
private static final String SERVER_NAME = "myServer";
|
||||
private static final String PROTOCOL = "myProtocol";
|
||||
private static final String AUTHORIZATION_ID = null;
|
||||
private static final String QOP_LEVEL = "auth-conf";
|
||||
|
||||
private SaslServer saslServer;
|
||||
private SaslClient saslClient;
|
||||
|
||||
@Before
|
||||
public void setUp() throws SaslException {
|
||||
|
||||
ServerCallbackHandler serverHandler = new ServerCallbackHandler();
|
||||
ClientCallbackHandler clientHandler = new ClientCallbackHandler();
|
||||
|
||||
Map<String, String> props = new HashMap<>();
|
||||
props.put(Sasl.QOP, QOP_LEVEL);
|
||||
|
||||
saslServer = Sasl.createSaslServer(MECHANISM, PROTOCOL, SERVER_NAME, props, serverHandler);
|
||||
saslClient = Sasl.createSaslClient(new String[] { MECHANISM }, AUTHORIZATION_ID, PROTOCOL, SERVER_NAME, props, clientHandler);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHandlers_whenStarted_thenAutenticationWorks() throws SaslException {
|
||||
|
||||
byte[] challenge;
|
||||
byte[] response;
|
||||
|
||||
challenge = saslServer.evaluateResponse(new byte[0]);
|
||||
response = saslClient.evaluateChallenge(challenge);
|
||||
|
||||
challenge = saslServer.evaluateResponse(response);
|
||||
response = saslClient.evaluateChallenge(challenge);
|
||||
|
||||
assertTrue(saslServer.isComplete());
|
||||
assertTrue(saslClient.isComplete());
|
||||
|
||||
String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP);
|
||||
assertEquals("auth-conf", qop);
|
||||
|
||||
byte[] outgoing = "Baeldung".getBytes();
|
||||
byte[] secureOutgoing = saslClient.wrap(outgoing, 0, outgoing.length);
|
||||
|
||||
byte[] secureIncoming = secureOutgoing;
|
||||
byte[] incoming = saslServer.unwrap(secureIncoming, 0, secureIncoming.length);
|
||||
assertEquals("Baeldung", new String(incoming, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws SaslException {
|
||||
saslClient.dispose();
|
||||
saslServer.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
## Relevant Articles
|
||||
|
||||
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Graph {
|
||||
|
||||
private Map<Integer, List<Integer>> adjVertices;
|
||||
|
||||
public Graph() {
|
||||
this.adjVertices = new HashMap<Integer, List<Integer>>();
|
||||
}
|
||||
|
||||
public void addVertex(int vertex) {
|
||||
adjVertices.putIfAbsent(vertex, new ArrayList<>());
|
||||
}
|
||||
|
||||
public void addEdge(int src, int dest) {
|
||||
adjVertices.get(src).add(dest);
|
||||
}
|
||||
|
||||
public void dfsWithoutRecursion(int start) {
|
||||
Stack<Integer> stack = new Stack<Integer>();
|
||||
boolean[] isVisited = new boolean[adjVertices.size()];
|
||||
stack.push(start);
|
||||
while (!stack.isEmpty()) {
|
||||
int current = stack.pop();
|
||||
isVisited[current] = true;
|
||||
System.out.print(" " + current);
|
||||
for (int dest : adjVertices.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
stack.push(dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dfs(int start) {
|
||||
boolean[] isVisited = new boolean[adjVertices.size()];
|
||||
dfsRecursive(start, isVisited);
|
||||
}
|
||||
|
||||
private void dfsRecursive(int current, boolean[] isVisited) {
|
||||
isVisited[current] = true;
|
||||
System.out.print(" " + current);
|
||||
for (int dest : adjVertices.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
dfsRecursive(dest, isVisited);
|
||||
}
|
||||
}
|
||||
|
||||
public void topologicalSort(int start) {
|
||||
Stack<Integer> result = new Stack<Integer>();
|
||||
boolean[] isVisited = new boolean[adjVertices.size()];
|
||||
topologicalSortRecursive(start, isVisited, result);
|
||||
while (!result.isEmpty()) {
|
||||
System.out.print(" " + result.pop());
|
||||
}
|
||||
}
|
||||
|
||||
private void topologicalSortRecursive(int current, boolean[] isVisited, Stack<Integer> result) {
|
||||
isVisited[current] = true;
|
||||
for (int dest : adjVertices.get(current)) {
|
||||
if (!isVisited[dest])
|
||||
topologicalSortRecursive(dest, isVisited, result);
|
||||
}
|
||||
result.push(current);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.baeldung.tree;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class BinaryTree {
|
||||
|
||||
@@ -147,6 +148,68 @@ public class BinaryTree {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void traverseInOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
while(current.left != null) {
|
||||
current = current.left;
|
||||
stack.push(current);
|
||||
}
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
if(current.right != null) {
|
||||
current = current.right;
|
||||
stack.push(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void traversePreOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
|
||||
if(current.right != null)
|
||||
stack.push(current.right);
|
||||
|
||||
if(current.left != null)
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
|
||||
public void traversePostOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node prev = root;
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
current = stack.peek();
|
||||
boolean hasChild = (current.left != null || current.right != null);
|
||||
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
|
||||
|
||||
if (!hasChild || isPrevLastChild) {
|
||||
current = stack.pop();
|
||||
System.out.print(" " + current.value);
|
||||
prev = current;
|
||||
} else {
|
||||
if (current.right != null) {
|
||||
stack.push(current.right);
|
||||
}
|
||||
if (current.left != null) {
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Node {
|
||||
int value;
|
||||
Node left;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.graph;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GraphUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenDFS_thenPrintAllValues() {
|
||||
Graph graph = createDirectedGraph();
|
||||
graph.dfs(0);
|
||||
System.out.println();
|
||||
graph.dfsWithoutRecursion(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
|
||||
Graph graph = createDirectedGraph();
|
||||
graph.topologicalSort(0);
|
||||
}
|
||||
|
||||
private Graph createDirectedGraph() {
|
||||
Graph graph = new Graph();
|
||||
graph.addVertex(0);
|
||||
graph.addVertex(1);
|
||||
graph.addVertex(2);
|
||||
graph.addVertex(3);
|
||||
graph.addVertex(4);
|
||||
graph.addVertex(5);
|
||||
graph.addEdge(0, 1);
|
||||
graph.addEdge(0, 2);
|
||||
graph.addEdge(1, 3);
|
||||
graph.addEdge(2, 3);
|
||||
graph.addEdge(3, 4);
|
||||
graph.addEdge(4, 5);
|
||||
return graph;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user