[BAEL-16824] - Added modules alphabatically
This commit is contained in:
commit
c3fa0a1e92
|
@ -1,11 +1,12 @@
|
|||
package com.baeldung.concurrent.mutex;
|
||||
|
||||
public class SequenceGenerator {
|
||||
|
||||
private int currentValue = 0;
|
||||
|
||||
public int getNextSequence() throws InterruptedException {
|
||||
public int getNextSequence() {
|
||||
currentValue = currentValue + 1;
|
||||
Thread.sleep(500);
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,15 +4,16 @@ import com.google.common.util.concurrent.Monitor;
|
|||
|
||||
public class SequenceGeneratorUsingMonitor extends SequenceGenerator {
|
||||
|
||||
private Monitor monitor = new Monitor();
|
||||
private Monitor mutex = new Monitor();
|
||||
|
||||
@Override
|
||||
public int getNextSequence() throws InterruptedException {
|
||||
monitor.enter();
|
||||
public int getNextSequence() {
|
||||
mutex.enter();
|
||||
try {
|
||||
return super.getNextSequence();
|
||||
} finally {
|
||||
monitor.leave();
|
||||
mutex.leave();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ public class SequenceGeneratorUsingReentrantLock extends SequenceGenerator {
|
|||
private ReentrantLock mutex = new ReentrantLock();
|
||||
|
||||
@Override
|
||||
public int getNextSequence() throws InterruptedException {
|
||||
public int getNextSequence() {
|
||||
try {
|
||||
mutex.lock();
|
||||
return super.getNextSequence();
|
||||
|
@ -15,4 +15,5 @@ public class SequenceGeneratorUsingReentrantLock extends SequenceGenerator {
|
|||
mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,12 +7,15 @@ public class SequenceGeneratorUsingSemaphore extends SequenceGenerator {
|
|||
private Semaphore mutex = new Semaphore(1);
|
||||
|
||||
@Override
|
||||
public int getNextSequence() throws InterruptedException {
|
||||
public int getNextSequence() {
|
||||
try {
|
||||
mutex.acquire();
|
||||
return super.getNextSequence();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException("Exception in critical section.", e);
|
||||
} finally {
|
||||
mutex.release();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ package com.baeldung.concurrent.mutex;
|
|||
|
||||
public class SequenceGeneratorUsingSynchronizedBlock extends SequenceGenerator {
|
||||
|
||||
private Object mutex = new Object();
|
||||
|
||||
@Override
|
||||
public int getNextSequence() throws InterruptedException {
|
||||
synchronized (this) {
|
||||
public int getNextSequence() {
|
||||
synchronized (mutex) {
|
||||
return super.getNextSequence();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.baeldung.concurrent.mutex;
|
|||
public class SequenceGeneratorUsingSynchronizedMethod extends SequenceGenerator {
|
||||
|
||||
@Override
|
||||
public synchronized int getNextSequence() throws InterruptedException {
|
||||
public synchronized int getNextSequence() {
|
||||
return super.getNextSequence();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.concurrent.mutex;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
@ -12,59 +12,58 @@ import java.util.concurrent.TimeUnit;
|
|||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.concurrent.mutex.SequenceGenerator;
|
||||
import com.baeldung.concurrent.mutex.SequenceGeneratorUsingMonitor;
|
||||
import com.baeldung.concurrent.mutex.SequenceGeneratorUsingReentrantLock;
|
||||
import com.baeldung.concurrent.mutex.SequenceGeneratorUsingSemaphore;
|
||||
import com.baeldung.concurrent.mutex.SequenceGeneratorUsingSynchronizedBlock;
|
||||
import com.baeldung.concurrent.mutex.SequenceGeneratorUsingSynchronizedMethod;
|
||||
|
||||
public class MutexUnitTest {
|
||||
|
||||
private final int RANGE = 30;
|
||||
|
||||
@Test
|
||||
// @Test
|
||||
// This test verifies the race condition use case, it may pass or fail based on execution environment
|
||||
// Uncomment @Test to run it
|
||||
public void givenUnsafeSequenceGenerator_whenRaceCondition_thenUnexpectedBehavior() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGenerator());
|
||||
Assert.assertNotEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGenerator(), count);
|
||||
Assert.assertNotEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSequenceGeneratorUsingSynchronizedMethod_whenRaceCondition_thenSuccess() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGeneratorUsingSynchronizedMethod());
|
||||
Assert.assertEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSynchronizedMethod(), count);
|
||||
Assert.assertEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSequenceGeneratorUsingSynchronizedBlock_whenRaceCondition_thenSuccess() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGeneratorUsingSynchronizedBlock());
|
||||
Assert.assertEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSynchronizedBlock(), count);
|
||||
Assert.assertEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSequenceGeneratorUsingReentrantLock_whenRaceCondition_thenSuccess() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGeneratorUsingReentrantLock());
|
||||
Assert.assertEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingReentrantLock(), count);
|
||||
Assert.assertEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSequenceGeneratorUsingSemaphore_whenRaceCondition_thenSuccess() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGeneratorUsingSemaphore());
|
||||
Assert.assertEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingSemaphore(), count);
|
||||
Assert.assertEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSequenceGeneratorUsingMonitor_whenRaceCondition_thenSuccess() throws Exception {
|
||||
Set<Integer> uniqueSequences = getASetOFUniqueSequences(new SequenceGeneratorUsingMonitor());
|
||||
Assert.assertEquals(RANGE, uniqueSequences.size());
|
||||
int count = 1000;
|
||||
Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGeneratorUsingMonitor(), count);
|
||||
Assert.assertEquals(count, uniqueSequences.size());
|
||||
}
|
||||
|
||||
private Set<Integer> getASetOFUniqueSequences(SequenceGenerator generator) throws Exception {
|
||||
private Set<Integer> getUniqueSequences(SequenceGenerator generator, int count) throws Exception {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||
Set<Integer> uniqueSequences = new HashSet<>();
|
||||
Set<Integer> uniqueSequences = new LinkedHashSet<>();
|
||||
List<Future<Integer>> futures = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < RANGE; i++) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
futures.add(executor.submit(generator::getNextSequence));
|
||||
}
|
||||
|
||||
|
@ -72,7 +71,7 @@ public class MutexUnitTest {
|
|||
uniqueSequences.add(future.get());
|
||||
}
|
||||
|
||||
executor.awaitTermination(15, TimeUnit.SECONDS);
|
||||
executor.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executor.shutdown();
|
||||
|
||||
return uniqueSequences;
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
public class CheckedUncheckedExceptions {
|
||||
public static void checkedExceptionWithThrows() throws FileNotFoundException {
|
||||
File file = new File("not_existing_file.txt");
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
}
|
||||
|
||||
public static void checkedExceptionWithTryCatch() {
|
||||
File file = new File("not_existing_file.txt");
|
||||
try {
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static int divideByZero() {
|
||||
int numerator = 1;
|
||||
int denominator = 0;
|
||||
return numerator / denominator;
|
||||
}
|
||||
|
||||
public static void checkFile(String fileName) throws IncorrectFileNameException {
|
||||
if (fileName == null || fileName.isEmpty()) {
|
||||
throw new NullOrEmptyException("The filename is null.");
|
||||
}
|
||||
if (!isCorrectFileName(fileName)) {
|
||||
throw new IncorrectFileNameException("Incorrect filename : " + fileName);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isCorrectFileName(String fileName) {
|
||||
if (fileName.equals("wrongFileName.txt"))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
public class IncorrectFileNameException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public IncorrectFileNameException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
|
||||
public IncorrectFileNameException(String errorMessage, Throwable thr) {
|
||||
super(errorMessage, thr);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
public class NullOrEmptyException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public NullOrEmptyException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
|
||||
public NullOrEmptyException(String errorMessage, Throwable thr) {
|
||||
super(errorMessage, thr);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests the {@link CheckedUncheckedExceptions}.
|
||||
*/
|
||||
public class CheckedUncheckedExceptionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFileNotExist_thenThrowException() {
|
||||
assertThrows(FileNotFoundException.class, () -> {
|
||||
CheckedUncheckedExceptions.checkedExceptionWithThrows();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTryCatchExcetpion_thenSuccess() {
|
||||
try {
|
||||
CheckedUncheckedExceptions.checkedExceptionWithTryCatch();
|
||||
} catch (Exception e) {
|
||||
Assertions.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenThrowException() {
|
||||
assertThrows(ArithmeticException.class, () -> {
|
||||
CheckedUncheckedExceptions.divideByZero();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidFile_thenThrowException() {
|
||||
|
||||
assertThrows(IncorrectFileNameException.class, () -> {
|
||||
CheckedUncheckedExceptions.checkFile("wrongFileName.txt");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullOrEmptyFile_thenThrowException() {
|
||||
assertThrows(NullOrEmptyException.class, () -> {
|
||||
CheckedUncheckedExceptions.checkFile(null);
|
||||
});
|
||||
assertThrows(NullOrEmptyException.class, () -> {
|
||||
CheckedUncheckedExceptions.checkFile("");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String privateId;
|
||||
public String name;
|
||||
private boolean manager;
|
||||
|
||||
public Employee(String id, String name) {
|
||||
changeId(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private Employee(String id, String name, boolean managerAttribute) {
|
||||
this.privateId = id;
|
||||
this.name = name;
|
||||
this.privateId = id + "_ID-MANAGER";
|
||||
}
|
||||
|
||||
public void changeId(String customId) {
|
||||
if (customId.endsWith("_ID")) {
|
||||
this.privateId = customId;
|
||||
} else {
|
||||
this.privateId = customId + "_ID";
|
||||
}
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return privateId;
|
||||
}
|
||||
|
||||
public boolean isManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public void elevateToManager() {
|
||||
if ("Carl".equals(this.name)) {
|
||||
setManager(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setManager(boolean manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public static Employee buildManager(String id, String name) {
|
||||
return new Employee(id, name, true);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class ExampleClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Employee employee = new Employee("Bob","ABC123");
|
||||
employee.changeId("BCD234");
|
||||
System.out.println(employee.getId());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class PublicOuterClass {
|
||||
|
||||
public PrivateInnerClass getInnerClassInstance() {
|
||||
PrivateInnerClass myPrivateClassInstance = this.new PrivateInnerClass();
|
||||
myPrivateClassInstance.id = "ID1";
|
||||
myPrivateClassInstance.name = "Bob";
|
||||
return myPrivateClassInstance;
|
||||
}
|
||||
|
||||
private class PrivateInnerClass {
|
||||
public String name;
|
||||
public String id;
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
/build/
|
||||
/bin/
|
||||
.settings
|
||||
.gradle
|
||||
.project
|
||||
.classpath
|
|
@ -0,0 +1,26 @@
|
|||
*.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
|
||||
*.txt
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
|
@ -0,0 +1,6 @@
|
|||
=========
|
||||
|
||||
## Basic Implementation of Blockchian in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- []()
|
|
@ -0,0 +1,40 @@
|
|||
<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.blockchain</groupId>
|
||||
<artifactId>java-blockchain</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>java-blockchain</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
<build>
|
||||
<finalName>java-blockchain</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.blockchain;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Block {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Block.class.getName());
|
||||
|
||||
private String hash;
|
||||
private String previousHash;
|
||||
private String data;
|
||||
private long timeStamp;
|
||||
private int nonce;
|
||||
|
||||
public Block(String data, String previousHash) {
|
||||
this.data = data;
|
||||
this.previousHash = previousHash;
|
||||
this.timeStamp = new Date().getTime();
|
||||
this.hash = calculateBlockHash();
|
||||
}
|
||||
|
||||
public String mineBlock(int prefix) {
|
||||
String prefixString = new String(new char[prefix]).replace('\0', '0');
|
||||
while (!hash.substring(0, prefix)
|
||||
.equals(prefixString)) {
|
||||
nonce++;
|
||||
hash = calculateBlockHash();
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
public String calculateBlockHash() {
|
||||
String dataToHash = previousHash + Long.toString(timeStamp) + Integer.toString(nonce) + data;
|
||||
MessageDigest digest = null;
|
||||
byte[] bytes = null;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
bytes = digest.digest(dataToHash.getBytes("UTF-8"));
|
||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
|
||||
logger.log(Level.SEVERE, ex.getMessage());
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (byte b : bytes) {
|
||||
buffer.append(String.format("%02x", b));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String getHash() {
|
||||
return this.hash;
|
||||
}
|
||||
|
||||
public String getPreviousHash() {
|
||||
return this.previousHash;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<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="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.blockchain;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BlockchainUnitTest {
|
||||
|
||||
public static List<Block> blockchain = new ArrayList<Block>();
|
||||
public static int prefix = 4;
|
||||
public static String prefixString = new String(new char[prefix]).replace('\0', '0');
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
Block genesisBlock = new Block("The is the Genesis Block.", "0");
|
||||
genesisBlock.mineBlock(prefix);
|
||||
blockchain.add(genesisBlock);
|
||||
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
|
||||
firstBlock.mineBlock(prefix);
|
||||
blockchain.add(firstBlock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
|
||||
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
|
||||
.getHash());
|
||||
newBlock.mineBlock(prefix);
|
||||
assertTrue(newBlock.getHash()
|
||||
.substring(0, prefix)
|
||||
.equals(prefixString));
|
||||
blockchain.add(newBlock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBlockchain_whenValidated_thenSuccess() {
|
||||
boolean flag = true;
|
||||
for (int i = 0; i < blockchain.size(); i++) {
|
||||
String previousHash = i == 0 ? "0"
|
||||
: blockchain.get(i - 1)
|
||||
.getHash();
|
||||
flag = blockchain.get(i)
|
||||
.getHash()
|
||||
.equals(blockchain.get(i)
|
||||
.calculateBlockHash())
|
||||
&& previousHash.equals(blockchain.get(i)
|
||||
.getPreviousHash())
|
||||
&& blockchain.get(i)
|
||||
.getHash()
|
||||
.substring(0, prefix)
|
||||
.equals(prefixString);
|
||||
if (!flag)
|
||||
break;
|
||||
}
|
||||
assertTrue(flag);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
blockchain.clear();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.jdi;
|
||||
|
||||
public class JDIExample {
|
||||
public class JDIExampleDebuggee {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String jpda = "Java Platform Debugger Architecture";
|
|
@ -123,7 +123,7 @@ public class JDIExampleDebugger {
|
|||
public static void main(String[] args) throws Exception {
|
||||
|
||||
JDIExampleDebugger debuggerInstance = new JDIExampleDebugger();
|
||||
debuggerInstance.setDebugClass(JDIExample.class);
|
||||
debuggerInstance.setDebugClass(JDIExampleDebuggee.class);
|
||||
int[] breakPoints = {6, 9};
|
||||
debuggerInstance.setBreakPointLines(breakPoints);
|
||||
VirtualMachine vm = null;
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.string.streamtokenizer;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StreamTokenizerDemo {
|
||||
|
||||
private static final String INPUT_FILE = "/stream-tokenizer-example.txt";
|
||||
private static final int QUOTE_CHARACTER = '\'';
|
||||
private static final int DOUBLE_QUOTE_CHARACTER = '"';
|
||||
|
||||
public static List<Object> streamTokenizerWithDefaultConfiguration(Reader reader) throws IOException {
|
||||
StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
|
||||
List<Object> tokens = new ArrayList<>();
|
||||
|
||||
int currentToken = streamTokenizer.nextToken();
|
||||
while (currentToken != StreamTokenizer.TT_EOF) {
|
||||
|
||||
if (streamTokenizer.ttype == StreamTokenizer.TT_NUMBER) {
|
||||
tokens.add(streamTokenizer.nval);
|
||||
} else if (streamTokenizer.ttype == StreamTokenizer.TT_WORD
|
||||
|| streamTokenizer.ttype == QUOTE_CHARACTER
|
||||
|| streamTokenizer.ttype == DOUBLE_QUOTE_CHARACTER) {
|
||||
tokens.add(streamTokenizer.sval);
|
||||
} else {
|
||||
tokens.add((char) currentToken);
|
||||
}
|
||||
|
||||
currentToken = streamTokenizer.nextToken();
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public static List<Object> streamTokenizerWithCustomConfiguration(Reader reader) throws IOException {
|
||||
StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
|
||||
List<Object> tokens = new ArrayList<>();
|
||||
|
||||
streamTokenizer.wordChars('!', '-');
|
||||
streamTokenizer.ordinaryChar('/');
|
||||
streamTokenizer.commentChar('#');
|
||||
streamTokenizer.eolIsSignificant(true);
|
||||
|
||||
int currentToken = streamTokenizer.nextToken();
|
||||
while (currentToken != StreamTokenizer.TT_EOF) {
|
||||
|
||||
if (streamTokenizer.ttype == StreamTokenizer.TT_NUMBER) {
|
||||
tokens.add(streamTokenizer.nval);
|
||||
} else if (streamTokenizer.ttype == StreamTokenizer.TT_WORD
|
||||
|| streamTokenizer.ttype == QUOTE_CHARACTER
|
||||
|| streamTokenizer.ttype == DOUBLE_QUOTE_CHARACTER) {
|
||||
tokens.add(streamTokenizer.sval);
|
||||
} else {
|
||||
tokens.add((char) currentToken);
|
||||
}
|
||||
currentToken = streamTokenizer.nextToken();
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public static Reader createReaderFromFile() throws FileNotFoundException {
|
||||
String inputFile = StreamTokenizerDemo.class.getResource(INPUT_FILE).getFile();
|
||||
return new FileReader(inputFile);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
List<Object> tokens1 = streamTokenizerWithDefaultConfiguration(createReaderFromFile());
|
||||
List<Object> tokens2 = streamTokenizerWithCustomConfiguration(createReaderFromFile());
|
||||
|
||||
System.out.println(tokens1);
|
||||
System.out.println(tokens2);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
3 quick brown foxes jump over the "lazy" dog!
|
||||
#test1
|
||||
//test2
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.string.streamtokenizer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class StreamTokenizerDemoUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStreamTokenizerWithDefaultConfigurationIsCalled_ThenCorrectTokensAreReturned() throws IOException {
|
||||
Reader reader = StreamTokenizerDemo.createReaderFromFile();
|
||||
List<Object> expectedTokens = Arrays.asList(3.0, "quick", "brown", "foxes", "jump", "over", "the", "lazy", "dog", '!', '#', "test1");
|
||||
|
||||
List<Object> actualTokens = StreamTokenizerDemo.streamTokenizerWithDefaultConfiguration(reader);
|
||||
|
||||
assertArrayEquals(expectedTokens.toArray(), actualTokens.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStreamTokenizerWithCustomConfigurationIsCalled_ThenCorrectTokensAreReturned() throws IOException {
|
||||
Reader reader = StreamTokenizerDemo.createReaderFromFile();
|
||||
List<Object> expectedTokens = Arrays.asList(3.0, "quick", "brown", "foxes", "jump", "over", "the", "\"lazy\"", "dog!", '\n', '\n', '/', '/', "test2");
|
||||
|
||||
List<Object> actualTokens = StreamTokenizerDemo.streamTokenizerWithCustomConfiguration(reader);
|
||||
|
||||
assertArrayEquals(expectedTokens.toArray(), actualTokens.toArray());
|
||||
}
|
||||
|
||||
}
|
|
@ -32,7 +32,7 @@
|
|||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
<artifactId>jersey-container-grizzly2-servlet</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.ext</groupId>
|
||||
|
@ -44,6 +44,21 @@
|
|||
<artifactId>jersey-bean-validation</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.security</groupId>
|
||||
<artifactId>oauth1-client</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.security</groupId>
|
||||
<artifactId>oauth2-client</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-sse</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.test-framework</groupId>
|
||||
<artifactId>jersey-test-framework-core</artifactId>
|
||||
|
|
|
@ -0,0 +1,189 @@
|
|||
package com.baeldung.jersey.client;
|
||||
|
||||
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
|
||||
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
|
||||
import org.glassfish.jersey.client.oauth1.AccessToken;
|
||||
import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
|
||||
import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
|
||||
import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.Invocation;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.core.Feature;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.sse.InboundSseEvent;
|
||||
import javax.ws.rs.sse.SseEventSource;
|
||||
|
||||
import static org.glassfish.jersey.client.authentication.HttpAuthenticationFeature.*;
|
||||
|
||||
public class JerseyClientHeaders {
|
||||
|
||||
private static final String BEARER_CONSUMER_SECRET = "my-consumer-secret";
|
||||
private static final String BEARER_ACCESS_TOKEN_SECRET = "my-access-token-secret";
|
||||
private static final String TARGET = "http://localhost:9998/";
|
||||
private static final String MAIN_RESOURCE = "echo-headers";
|
||||
private static final String RESOURCE_AUTH_DIGEST = "digest";
|
||||
|
||||
private static String sseHeaderValue;
|
||||
|
||||
public static Response simpleHeader(String headerKey, String headerValue) {
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget webTarget = client.target(TARGET);
|
||||
WebTarget resourceWebTarget = webTarget.path(MAIN_RESOURCE);
|
||||
Invocation.Builder invocationBuilder = resourceWebTarget.request();
|
||||
invocationBuilder.header(headerKey, headerValue);
|
||||
return invocationBuilder.get();
|
||||
}
|
||||
|
||||
public static Response simpleHeaderFluently(String headerKey, String headerValue) {
|
||||
Client client = ClientBuilder.newClient();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.header(headerKey, headerValue)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response basicAuthenticationAtClientLevel(String username, String password) {
|
||||
//To simplify we removed de SSL/TLS protection, but it's required to have an encryption
|
||||
// when using basic authentication schema as it's send only on Base64 encoding
|
||||
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(username, password);
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response basicAuthenticationAtRequestLevel(String username, String password) {
|
||||
//To simplify we removed de SSL/TLS protection, but it's required to have an encryption
|
||||
// when using basic authentication schema as it's send only on Base64 encoding
|
||||
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build();
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.property(HTTP_AUTHENTICATION_BASIC_USERNAME, username)
|
||||
.property(HTTP_AUTHENTICATION_BASIC_PASSWORD, password)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response digestAuthenticationAtClientLevel(String username, String password) {
|
||||
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest(username, password);
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.path(RESOURCE_AUTH_DIGEST)
|
||||
.request()
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response digestAuthenticationAtRequestLevel(String username, String password) {
|
||||
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest();
|
||||
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.path(RESOURCE_AUTH_DIGEST)
|
||||
.request()
|
||||
.property(HTTP_AUTHENTICATION_DIGEST_USERNAME, username)
|
||||
.property(HTTP_AUTHENTICATION_DIGEST_PASSWORD, password)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response bearerAuthenticationWithOAuth1AtClientLevel(String token, String consumerKey) {
|
||||
ConsumerCredentials consumerCredential = new ConsumerCredentials(consumerKey, BEARER_CONSUMER_SECRET);
|
||||
AccessToken accessToken = new AccessToken(token, BEARER_ACCESS_TOKEN_SECRET);
|
||||
Feature feature = OAuth1ClientSupport
|
||||
.builder(consumerCredential)
|
||||
.feature()
|
||||
.accessToken(accessToken)
|
||||
.build();
|
||||
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response bearerAuthenticationWithOAuth1AtRequestLevel(String token, String consumerKey) {
|
||||
ConsumerCredentials consumerCredential = new ConsumerCredentials(consumerKey, BEARER_CONSUMER_SECRET);
|
||||
AccessToken accessToken = new AccessToken(token, BEARER_ACCESS_TOKEN_SECRET);
|
||||
Feature feature = OAuth1ClientSupport
|
||||
.builder(consumerCredential)
|
||||
.feature()
|
||||
.build();
|
||||
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.property(OAuth1ClientSupport.OAUTH_PROPERTY_ACCESS_TOKEN, accessToken)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response bearerAuthenticationWithOAuth2AtClientLevel(String token) {
|
||||
Feature feature = OAuth2ClientSupport.feature(token);
|
||||
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response bearerAuthenticationWithOAuth2AtRequestLevel(String token, String otherToken) {
|
||||
Feature feature = OAuth2ClientSupport.feature(token);
|
||||
|
||||
Client client = ClientBuilder.newBuilder().register(feature).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.property(OAuth2ClientSupport.OAUTH2_PROPERTY_ACCESS_TOKEN, otherToken)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response filter() {
|
||||
Client client = ClientBuilder.newBuilder().register(AddHeaderOnRequestFilter.class).build();
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.get();
|
||||
}
|
||||
|
||||
public static Response sendRestrictedHeaderThroughDefaultTransportConnector(String headerKey, String headerValue) {
|
||||
Client client = ClientBuilder.newClient();
|
||||
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||
|
||||
return client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.request()
|
||||
.header(headerKey, headerValue)
|
||||
.get();
|
||||
}
|
||||
|
||||
public static String simpleSSEHeader() throws InterruptedException {
|
||||
Client client = ClientBuilder.newBuilder()
|
||||
.register(AddHeaderOnRequestFilter.class)
|
||||
.build();
|
||||
|
||||
WebTarget webTarget = client.target(TARGET)
|
||||
.path(MAIN_RESOURCE)
|
||||
.path("events");
|
||||
|
||||
SseEventSource sseEventSource = SseEventSource.target(webTarget).build();
|
||||
sseEventSource.register(JerseyClientHeaders::receiveEvent);
|
||||
sseEventSource.open();
|
||||
Thread.sleep(3_000);
|
||||
sseEventSource.close();
|
||||
|
||||
return sseHeaderValue;
|
||||
}
|
||||
|
||||
private static void receiveEvent(InboundSseEvent event) {
|
||||
sseHeaderValue = event.readData();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.jersey.client.filter;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AddHeaderOnRequestFilter implements ClientRequestFilter {
|
||||
|
||||
public static final String FILTER_HEADER_VALUE = "filter-header-value";
|
||||
public static final String FILTER_HEADER_KEY = "x-filter-header";
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) throws IOException {
|
||||
requestContext.getHeaders().add(FILTER_HEADER_KEY, FILTER_HEADER_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.*;
|
||||
import javax.ws.rs.sse.OutboundSseEvent;
|
||||
import javax.ws.rs.sse.Sse;
|
||||
import javax.ws.rs.sse.SseEventSink;
|
||||
|
||||
@Path("/echo-headers")
|
||||
public class EchoHeaders {
|
||||
|
||||
static final String REALM_KEY = "realm";
|
||||
static final String REALM_VALUE = "Baeldung";
|
||||
static final String QOP_KEY = "qop";
|
||||
static final String QOP_VALUE = "auth";
|
||||
static final String NONCE_KEY = "nonce";
|
||||
static final String NONCE_VALUE = "dcd98b7102dd2f0e8b11d0f600bfb0c093";
|
||||
static final String OPAQUE_KEY = "opaque";
|
||||
static final String OPAQUE_VALUE = "5ccc069c403ebaf9f0171e9517f40e41";
|
||||
static final String SSE_HEADER_KEY = "x-sse-header-key";
|
||||
|
||||
@Context
|
||||
HttpHeaders headers;
|
||||
|
||||
@GET
|
||||
public Response getHeadersBack() {
|
||||
return echoHeaders();
|
||||
}
|
||||
|
||||
@RolesAllowed("ADMIN")
|
||||
@GET
|
||||
@Path("/digest")
|
||||
public Response getHeadersBackFromDigestAuthentication() {
|
||||
// As the Digest authentication require some complex steps to work we'll simulate the process
|
||||
// https://en.wikipedia.org/wiki/Digest_access_authentication#Example_with_explanation
|
||||
if (headers.getHeaderString("authorization") == null) {
|
||||
String authenticationRequired = "Digest " + REALM_KEY + "=\"" + REALM_VALUE + "\", " + QOP_KEY + "=\"" + QOP_VALUE + "\", " + NONCE_KEY + "=\"" + NONCE_VALUE + "\", " + OPAQUE_KEY + "=\"" + OPAQUE_VALUE + "\"";
|
||||
return Response.status(Response.Status.UNAUTHORIZED)
|
||||
.header("WWW-Authenticate", authenticationRequired)
|
||||
.build();
|
||||
} else {
|
||||
return echoHeaders();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/events")
|
||||
@Produces(MediaType.SERVER_SENT_EVENTS)
|
||||
public void getServerSentEvents(@Context SseEventSink eventSink, @Context Sse sse) {
|
||||
OutboundSseEvent event = sse.newEventBuilder()
|
||||
.name("echo-headers")
|
||||
.data(String.class, headers.getHeaderString(AddHeaderOnRequestFilter.FILTER_HEADER_KEY))
|
||||
.build();
|
||||
eventSink.send(event);
|
||||
}
|
||||
|
||||
private Response echoHeaders() {
|
||||
Response.ResponseBuilder responseBuilder = Response.noContent();
|
||||
|
||||
headers.getRequestHeaders()
|
||||
.forEach((k, v) -> {
|
||||
v.forEach(value -> responseBuilder.header(k, value));
|
||||
});
|
||||
|
||||
return responseBuilder.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import com.baeldung.jersey.client.JerseyClientHeaders;
|
||||
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
|
||||
import org.glassfish.jersey.media.sse.SseFeature;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.test.JerseyTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.ws.rs.core.Application;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class EchoHeadersUnitTest extends JerseyTest {
|
||||
|
||||
private static final String SIMPLE_HEADER_KEY = "my-header-key";
|
||||
private static final String SIMPLE_HEADER_VALUE = "my-header-value";
|
||||
private static final String USERNAME = "baeldung";
|
||||
private static final String PASSWORD = "super-secret";
|
||||
private static final String AUTHORIZATION_HEADER_KEY = "authorization";
|
||||
private static final String BEARER_TOKEN_VALUE = "my-token";
|
||||
private static final String BEARER_CONSUMER_KEY_VALUE = "my-consumer-key";
|
||||
private static final String BEARER_REQUEST_TOKEN_VALUE = "my-request-token";
|
||||
|
||||
@Test
|
||||
public void whenCallingSimpleHeader_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.simpleHeader(SIMPLE_HEADER_KEY, SIMPLE_HEADER_VALUE);
|
||||
|
||||
assertEquals(response.getHeaderString(SIMPLE_HEADER_KEY), SIMPLE_HEADER_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingSimpleHeaderFluently_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.simpleHeaderFluently(SIMPLE_HEADER_KEY, SIMPLE_HEADER_VALUE);
|
||||
|
||||
assertEquals(response.getHeaderString(SIMPLE_HEADER_KEY), SIMPLE_HEADER_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBasicAuthenticationAtClientLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.basicAuthenticationAtClientLevel(USERNAME, PASSWORD);
|
||||
|
||||
assertBasicAuthenticationHeaders(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBasicAuthenticationAtRequestLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.basicAuthenticationAtRequestLevel(USERNAME, PASSWORD);
|
||||
|
||||
assertBasicAuthenticationHeaders(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingDigestAuthenticationAtClientLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.digestAuthenticationAtClientLevel(USERNAME, PASSWORD);
|
||||
|
||||
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 7);
|
||||
|
||||
assertDigestAuthenticationHeaders(subHeadersMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingDigestAuthenticationAtRequestLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.digestAuthenticationAtRequestLevel(USERNAME, PASSWORD);
|
||||
|
||||
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 7);
|
||||
|
||||
assertDigestAuthenticationHeaders(subHeadersMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBearerAuthenticationWithOAuth1AtClientLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth1AtClientLevel(BEARER_TOKEN_VALUE, BEARER_CONSUMER_KEY_VALUE);
|
||||
|
||||
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 6);
|
||||
|
||||
assertBearerAuthenticationHeaders(subHeadersMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBearerAuthenticationWithOAuth1AtRequestLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth1AtRequestLevel(BEARER_TOKEN_VALUE, BEARER_CONSUMER_KEY_VALUE);
|
||||
|
||||
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 6);
|
||||
|
||||
assertBearerAuthenticationHeaders(subHeadersMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBearerAuthenticationWithOAuth2AtClientLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth2AtClientLevel(BEARER_TOKEN_VALUE);
|
||||
|
||||
assertEquals("Bearer " + BEARER_TOKEN_VALUE, response.getHeaderString(AUTHORIZATION_HEADER_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingBearerAuthenticationWithOAuth2AtRequestLevel_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth2AtRequestLevel(BEARER_TOKEN_VALUE, BEARER_REQUEST_TOKEN_VALUE);
|
||||
|
||||
assertEquals("Bearer " + BEARER_REQUEST_TOKEN_VALUE, response.getHeaderString(AUTHORIZATION_HEADER_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingFilter_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.filter();
|
||||
|
||||
assertEquals(AddHeaderOnRequestFilter.FILTER_HEADER_VALUE, response.getHeaderString(AddHeaderOnRequestFilter.FILTER_HEADER_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingSendRestrictedHeaderThroughDefaultTransportConnector_thenHeadersReturnedBack() {
|
||||
Response response = JerseyClientHeaders.sendRestrictedHeaderThroughDefaultTransportConnector("keep-alive", "keep-alive-value");
|
||||
|
||||
assertEquals("keep-alive-value", response.getHeaderString("keep-alive"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingSimpleSSEHeader_thenHeadersReturnedBack() throws InterruptedException {
|
||||
String sseHeaderBackValue = JerseyClientHeaders.simpleSSEHeader();
|
||||
|
||||
assertEquals(AddHeaderOnRequestFilter.FILTER_HEADER_VALUE, sseHeaderBackValue);
|
||||
}
|
||||
|
||||
private void assertBearerAuthenticationHeaders(Map<String, String> subHeadersMap) {
|
||||
|
||||
assertEquals(BEARER_TOKEN_VALUE, subHeadersMap.get("oauth_token"));
|
||||
assertEquals(BEARER_CONSUMER_KEY_VALUE, subHeadersMap.get("oauth_consumer_key"));
|
||||
assertNotNull(subHeadersMap.get("oauth_nonce"));
|
||||
assertNotNull(subHeadersMap.get("oauth_signature"));
|
||||
assertNotNull(subHeadersMap.get("oauth_callback"));
|
||||
assertNotNull(subHeadersMap.get("oauth_signature_method"));
|
||||
assertNotNull(subHeadersMap.get("oauth_version"));
|
||||
assertNotNull(subHeadersMap.get("oauth_timestamp"));
|
||||
}
|
||||
|
||||
private void assertDigestAuthenticationHeaders(Map<String, String> subHeadersMap) {
|
||||
assertEquals(EchoHeaders.NONCE_VALUE, subHeadersMap.get(EchoHeaders.NONCE_KEY));
|
||||
assertEquals(EchoHeaders.OPAQUE_VALUE, subHeadersMap.get(EchoHeaders.OPAQUE_KEY));
|
||||
assertEquals(EchoHeaders.QOP_VALUE, subHeadersMap.get(EchoHeaders.QOP_KEY));
|
||||
assertEquals(EchoHeaders.REALM_VALUE, subHeadersMap.get(EchoHeaders.REALM_KEY));
|
||||
|
||||
assertEquals(USERNAME, subHeadersMap.get("username"));
|
||||
assertEquals("/echo-headers/digest", subHeadersMap.get("uri"));
|
||||
assertNotNull(subHeadersMap.get("cnonce"));
|
||||
assertEquals("00000001", subHeadersMap.get("nc"));
|
||||
assertNotNull(subHeadersMap.get("response"));
|
||||
}
|
||||
|
||||
private Map<String, String> parseAuthenticationSubHeader(Response response, int startAt) {
|
||||
String authorizationHeader = response.getHeaderString(AUTHORIZATION_HEADER_KEY);
|
||||
// The substring(startAt) is used to cut off the authentication schema part from the value returned.
|
||||
String[] subHeadersKeyValue = authorizationHeader.substring(startAt).split(",");
|
||||
Map<String, String> subHeadersMap = new HashMap<>();
|
||||
|
||||
for (String subHeader : subHeadersKeyValue) {
|
||||
String[] keyValue = subHeader.split("=");
|
||||
|
||||
if (keyValue[1].startsWith("\"")) {
|
||||
keyValue[1] = keyValue[1].substring(1, keyValue[1].length() - 1);
|
||||
}
|
||||
|
||||
subHeadersMap.put(keyValue[0].trim(), keyValue[1].trim());
|
||||
}
|
||||
return subHeadersMap;
|
||||
}
|
||||
|
||||
private void assertBasicAuthenticationHeaders(Response response) {
|
||||
String base64Credentials = response.getHeaderString(AUTHORIZATION_HEADER_KEY);
|
||||
// The substring(6) is used to cut the "Basic " part of the value returned,
|
||||
// as it's used to indicates the authentication schema and does not belong to the credentials
|
||||
byte[] credentials = Base64.getDecoder().decode(base64Credentials.substring(6));
|
||||
String[] credentialsParsed = new String(credentials).split(":");
|
||||
|
||||
assertEquals(credentialsParsed[0], USERNAME);
|
||||
assertEquals(credentialsParsed[1], PASSWORD);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Application configure() {
|
||||
return new ResourceConfig()
|
||||
.register(EchoHeaders.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
// We need this definition here, because if you are running
|
||||
// the complete suit test the sendingRestrictedHeaderThroughDefaultTransportConnector_shouldReturnThanBack
|
||||
// will fail if only defined on the client method, since the JerseyTest is created once.
|
||||
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
|
||||
}
|
||||
}
|
|
@ -1,3 +1,10 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Performance of Java Mapping Frameworks](http://www.baeldung.com/java-performance-mapping-frameworks)
|
||||
|
||||
### Running
|
||||
|
||||
To run the performance benchmarks:
|
||||
|
||||
1: `mvn clean install`
|
||||
2: `java -jar target/benchmarks.jar`
|
|
@ -1,17 +1,20 @@
|
|||
<?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"
|
||||
<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>performance-tests</artifactId>
|
||||
<name>performance-tests</name>
|
||||
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>performance-tests</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ma.glasnost.orika</groupId>
|
||||
|
@ -32,7 +35,16 @@
|
|||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-jdk8</artifactId>
|
||||
<version>${mapstruct-jdk8.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>1.2.0.Final</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.modelmapper</groupId>
|
||||
<artifactId>modelmapper</artifactId>
|
||||
|
@ -46,46 +58,141 @@
|
|||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${mapstruct-processor.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<!--
|
||||
JMH version to use with this project.
|
||||
-->
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<orika.version>1.5.2</orika.version>
|
||||
<dozer.version>5.5.1</dozer.version>
|
||||
<dozer-jdk8-support.version>1.0.2</dozer-jdk8-support.version>
|
||||
<mapstruct-jdk8.version>1.2.0.Final</mapstruct-jdk8.version>
|
||||
<modelmapper.version>1.1.0</modelmapper.version>
|
||||
<jmapper.version>1.6.0.1</jmapper.version>
|
||||
<java.version>1.8</java.version>
|
||||
<mapstruct-processor.version>1.2.0.Final</mapstruct-processor.version>
|
||||
<jmh-core.version>1.21</jmh-core.version>
|
||||
<jmh-generator.version>1.21</jmh-generator.version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
|
||||
<!--
|
||||
Java source/target to use for compilation.
|
||||
-->
|
||||
<javac.target>1.8</javac.target>
|
||||
|
||||
<!--
|
||||
Name of the benchmark Uber-JAR to generate.
|
||||
-->
|
||||
<uberjar.name>benchmarks</uberjar.name>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<compilerVersion>${javac.target}</compilerVersion>
|
||||
<source>${javac.target}</source>
|
||||
<target>${javac.target}</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${mapstruct-processor.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>${uberjar.name}</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<!--
|
||||
Shading signed JARs will fail without this.
|
||||
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
|
||||
-->
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/*.SF</exclude>
|
||||
<exclude>META-INF/*.DSA</exclude>
|
||||
<exclude>META-INF/*.RSA</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.17</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
43
pom.xml
43
pom.xml
|
@ -350,6 +350,7 @@
|
|||
<module>apache-fop</module>
|
||||
<module>apache-geode</module>
|
||||
<module>apache-meecrowave</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-opennlp</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-pulsar</module>
|
||||
|
@ -369,13 +370,15 @@
|
|||
<module>axon</module>
|
||||
<module>azure</module>
|
||||
|
||||
<module>bazel</module>
|
||||
<module>blade</module>
|
||||
|
||||
<module>bootique</module>
|
||||
|
||||
<module>cas</module>
|
||||
<module>cdi</module>
|
||||
<module>checker-plugin</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-client</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-resource-server</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-groovy-2</module>
|
||||
<module>core-groovy-collections</module>
|
||||
|
@ -438,6 +441,7 @@
|
|||
<module>gson</module>
|
||||
<module>guava</module>
|
||||
<module>guava-collections</module>
|
||||
<module>guava-collections-set</module>
|
||||
<module>guava-modules</module>
|
||||
<!-- <module>guest</module> --> <!-- not to be built as its for guest articles -->
|
||||
<module>guice</module>
|
||||
|
@ -462,6 +466,7 @@
|
|||
<!-- <module>java-dates-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||
<!-- <module>java-ee-8-security-api</module> --> <!-- long running -->
|
||||
<module>java-lite</module>
|
||||
<module>java-math</module>
|
||||
<module>java-numbers</module>
|
||||
<module>java-numbers-2</module>
|
||||
<module>java-rmi</module>
|
||||
|
@ -481,6 +486,7 @@
|
|||
<!-- JIRA-10842
|
||||
<module>jee-7</module> -->
|
||||
<module>jee-7-security</module>
|
||||
<module>jee-kotlin</module>
|
||||
<module>jersey</module>
|
||||
<module>jgit</module>
|
||||
<module>jgroups</module>
|
||||
|
@ -561,23 +567,17 @@
|
|||
<module>software-security/sql-injection-samples</module>
|
||||
|
||||
<module>tensorflow-java</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>bazel</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-client</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-resource-server</module>
|
||||
<module>guava-collections-set</module>
|
||||
<module>java-math</module>
|
||||
<module>jee-kotlin</module>
|
||||
<module>spf4j</module>
|
||||
<module>spring-boot-flowable</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-performance</module>
|
||||
<module>spring-boot-properties</module>
|
||||
<!-- <module>spring-mvc-basics</module> --> <!-- Compilation failure -->
|
||||
<module>spring-security-kerberos</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>java-blockchain</module>
|
||||
<!-- <module>Twitter4J</module> --> <!-- Builds locally, but fails in Jenkins, Failed to parse POMs -->
|
||||
</modules>
|
||||
|
||||
|
@ -815,6 +815,7 @@
|
|||
<module>spring-security-kerberos</module>
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>java-blockchain</module>
|
||||
|
||||
</modules>
|
||||
|
||||
|
@ -1075,6 +1076,7 @@
|
|||
<module>apache-fop</module>
|
||||
<module>apache-geode</module>
|
||||
<module>apache-meecrowave</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-opennlp</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-pulsar</module>
|
||||
|
@ -1093,12 +1095,14 @@
|
|||
<module>aws-lambda</module>
|
||||
<module>axon</module>
|
||||
<module>azure</module>
|
||||
|
||||
<module>bazel</module>
|
||||
<module>bootique</module>
|
||||
|
||||
<module>cas</module>
|
||||
<module>cdi</module>
|
||||
<module>checker-plugin</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-client</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-resource-server</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-groovy-2</module>
|
||||
<module>core-groovy-collections</module>
|
||||
|
@ -1156,6 +1160,7 @@
|
|||
<module>gson</module>
|
||||
<module>guava</module>
|
||||
<module>guava-collections</module>
|
||||
<module>guava-collections-set</module>
|
||||
<module>guava-modules</module>
|
||||
<!-- <module>guest</module> --> <!-- not to be built as its for guest articles -->
|
||||
<module>guice</module>
|
||||
|
@ -1179,6 +1184,7 @@
|
|||
<!-- <module>java-dates</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
|
||||
<module>java-ee-8-security-api</module>
|
||||
<module>java-lite</module>
|
||||
<module>java-math</module>
|
||||
<module>java-numbers</module>
|
||||
<module>java-numbers-2</module>
|
||||
<module>java-rmi</module>
|
||||
|
@ -1198,6 +1204,7 @@
|
|||
<!-- JIRA-10842
|
||||
<module>jee-7</module> -->
|
||||
<module>jee-7-security</module>
|
||||
<module>jee-kotlin</module>
|
||||
<module>jersey</module>
|
||||
<module>jgit</module>
|
||||
<module>jgroups</module>
|
||||
|
@ -1272,15 +1279,7 @@
|
|||
<module>rxjava</module>
|
||||
<module>rxjava-2</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>bazel</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-client</module>
|
||||
<module>cloud-foundry-uaa/cf-uaa-oauth2-resource-server</module>
|
||||
<module>guava-collections-set</module>
|
||||
<module>java-math</module>
|
||||
<module>jee-kotlin</module>
|
||||
<module>spf4j</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-performance</module>
|
||||
<module>spring-boot-properties</module>
|
||||
<!-- <module>spring-mvc-basics</module> --> <!-- Compilation failure -->
|
||||
|
|
|
@ -86,6 +86,11 @@
|
|||
<artifactId>javafaker</artifactId>
|
||||
<version>0.18</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -167,5 +172,6 @@
|
|||
<jayway-rest-assured.version>2.9.0</jayway-rest-assured.version>
|
||||
<kotlin.version>1.1.2</kotlin.version>
|
||||
<start-class>com.baeldung.Spring5Application</start-class>
|
||||
<httpclient.version>4.5.8</httpclient.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.web.controller;
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.ResponseTransfer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -10,7 +10,6 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.transfer;
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
public class LoginForm {
|
||||
private String username;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.transfer;
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
public class ResponseTransfer {
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
package com.baeldung.services;
|
||||
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
|
||||
@Service
|
||||
public class ExampleService {
|
||||
|
||||
|
|
|
@ -17,9 +17,9 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.SpringBootRestApplication;
|
||||
import com.baeldung.requestresponsebody.ExamplePostController;
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import com.baeldung.web.controller.ExamplePostController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootRestApplication.class)
|
||||
|
|
|
@ -18,9 +18,9 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.SpringBootRestApplication;
|
||||
import com.baeldung.requestresponsebody.ExamplePostController;
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import com.baeldung.web.controller.ExamplePostController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootRestApplication.class)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.app;
|
||||
package com.baeldung.app;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
|
@ -12,9 +12,9 @@ import org.springframework.web.filter.DelegatingFilterProxy;
|
|||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories("org.baeldung.repository")
|
||||
@ComponentScan("org.baeldung")
|
||||
@EntityScan("org.baeldung.entity")
|
||||
@EnableJpaRepositories("com.baeldung.repository")
|
||||
@ComponentScan("com.baeldung")
|
||||
@EntityScan("com.baeldung.entity")
|
||||
public class App extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.auditing;
|
||||
package com.baeldung.auditing;
|
||||
|
||||
import org.springframework.boot.actuate.audit.AuditEvent;
|
||||
import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.auditing;
|
||||
package com.baeldung.auditing;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,11 +1,12 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.baeldung.entity.Task;
|
||||
import org.baeldung.repository.TaskRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.entity.Task;
|
||||
import com.baeldung.repository.TaskRepository;
|
||||
|
||||
@Component
|
||||
public class DatabaseLoader implements CommandLineRunner {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
|
@ -1,7 +1,5 @@
|
|||
package org.baeldung.controller;
|
||||
package com.baeldung.controller;
|
||||
|
||||
import org.baeldung.entity.Task;
|
||||
import org.baeldung.service.TaskService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
@ -9,6 +7,9 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.entity.Task;
|
||||
import com.baeldung.service.TaskService;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("api/tasks")
|
||||
public class TaskController {
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.entity;
|
||||
package com.baeldung.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.filter;
|
||||
package com.baeldung.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity.annotation;
|
||||
package com.baeldung.methodsecurity.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity.config;
|
||||
package com.baeldung.methodsecurity.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity.entity;
|
||||
package com.baeldung.methodsecurity.entity;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -1,16 +1,17 @@
|
|||
package org.baeldung.methodsecurity.repository;
|
||||
package com.baeldung.methodsecurity.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.baeldung.methodsecurity.entity.CustomUser;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.methodsecurity.entity.CustomUser;
|
||||
|
||||
@Service
|
||||
public class UserRoleRepository {
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
package org.baeldung.methodsecurity.service;
|
||||
package com.baeldung.methodsecurity.service;
|
||||
|
||||
import org.baeldung.methodsecurity.repository.UserRoleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.methodsecurity.repository.UserRoleRepository;
|
||||
|
||||
@Service("userDetailService")
|
||||
public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity.service;
|
||||
package com.baeldung.methodsecurity.service;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Service;
|
|
@ -1,13 +1,10 @@
|
|||
package org.baeldung.methodsecurity.service;
|
||||
package com.baeldung.methodsecurity.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
import org.baeldung.methodsecurity.annotation.IsViewer;
|
||||
import org.baeldung.methodsecurity.entity.CustomUser;
|
||||
import org.baeldung.methodsecurity.repository.UserRoleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.security.access.prepost.PostAuthorize;
|
||||
|
@ -18,6 +15,10 @@ import org.springframework.security.core.context.SecurityContext;
|
|||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.methodsecurity.annotation.IsViewer;
|
||||
import com.baeldung.methodsecurity.entity.CustomUser;
|
||||
import com.baeldung.methodsecurity.repository.UserRoleRepository;
|
||||
|
||||
@Service
|
||||
public class UserRoleService {
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
package org.baeldung.repository;
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.baeldung.entity.Task;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.baeldung.entity.Task;
|
||||
|
||||
public interface TaskRepository extends CrudRepository<Task, Long> {
|
||||
|
||||
}
|
|
@ -1,12 +1,13 @@
|
|||
package org.baeldung.service;
|
||||
package com.baeldung.service;
|
||||
|
||||
import org.baeldung.entity.Task;
|
||||
import org.baeldung.repository.TaskRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PostFilter;
|
||||
import org.springframework.security.access.prepost.PreFilter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.entity.Task;
|
||||
import com.baeldung.repository.TaskRepository;
|
||||
|
||||
@Service
|
||||
public class TaskService {
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.app.App;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.app.App;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = App.class)
|
||||
public class SpringContextIntegrationTest {
|
|
@ -1,11 +1,12 @@
|
|||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.app.App;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.app.App;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = App.class)
|
||||
public class SpringContextTest {
|
|
@ -1,8 +1,7 @@
|
|||
package org.baeldung.methodsecurity;
|
||||
package com.baeldung.methodsecurity;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.baeldung.methodsecurity.service.SystemService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -13,15 +12,17 @@ import org.springframework.security.test.context.support.WithMockUser;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.methodsecurity.service.SystemService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestClassLevelSecurity {
|
||||
public class ClassLevelSecurityIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
SystemService systemService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.methodsecurity.*")
|
||||
@ComponentScan("com.baeldung.methodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity;
|
||||
package com.baeldung.methodsecurity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.methodsecurity.service.UserRoleService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -19,15 +18,17 @@ import org.springframework.security.test.context.support.WithMockUser;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.methodsecurity.service.UserRoleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestMethodSecurity {
|
||||
public class MethodSecurityIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
UserRoleService userRoleService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.methodsecurity.*")
|
||||
@ComponentScan("com.baeldung.methodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
package org.baeldung.methodsecurity;
|
||||
package com.baeldung.methodsecurity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.baeldung.methodsecurity.service.UserRoleService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -12,10 +11,12 @@ import org.springframework.security.test.context.support.WithMockUser;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.methodsecurity.service.UserRoleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
@WithMockUser(username = "john", roles = { "VIEWER" })
|
||||
public class TestWithMockUserAtClassLevel {
|
||||
public class MockUserAtClassLevelIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() {
|
||||
|
@ -27,7 +28,7 @@ public class TestWithMockUserAtClassLevel {
|
|||
UserRoleService userService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.methodsecurity.*")
|
||||
@ComponentScan("com.baeldung.methodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
package org.baeldung.methodsecurity;
|
||||
package com.baeldung.methodsecurity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.baeldung.methodsecurity.entity.CustomUser;
|
||||
import org.baeldung.methodsecurity.service.UserRoleService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -14,15 +12,18 @@ import org.springframework.security.test.context.support.WithUserDetails;
|
|||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.methodsecurity.entity.CustomUser;
|
||||
import com.baeldung.methodsecurity.service.UserRoleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestWithUserDetails {
|
||||
public class UserDetailsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
UserRoleService userService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.methodsecurity.*")
|
||||
@ComponentScan("com.baeldung.methodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.methodsecurity;
|
||||
package com.baeldung.methodsecurity;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
|
@ -1,11 +1,10 @@
|
|||
package org.baeldung.test;
|
||||
package com.baeldung.test;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.baeldung.app.App;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
@ -20,6 +19,8 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.app.App;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
Loading…
Reference in New Issue