Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -32,9 +32,27 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>${maven.resources.plugin.version}</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<nonFilteredFileExtensions>
|
||||
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
|
||||
</nonFilteredFileExtensions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<poi.version>5.2.0</poi.version>
|
||||
<jexcel.version>1.0.6</jexcel.version>
|
||||
<maven.resources.plugin.version>3.2.0</maven.resources.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Binary file not shown.
+1
-1
@@ -12,7 +12,7 @@ import java.nio.file.Paths;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CellBorderHandlerUnitTest {
|
||||
private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx";
|
||||
private static final String FILE_NAME = "cellstyle/CellStyleBorderHandlerTest.xlsx";
|
||||
private static final int SHEET_INDEX = 0;
|
||||
|
||||
private static CellBordersHandler cellBordersHandler;
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.game;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
class RockPaperScissorsGame {
|
||||
|
||||
private static Map<Integer, String> movesMap = new HashMap<Integer, String>() {{
|
||||
put(0, "rock");
|
||||
put(1, "paper");
|
||||
put(2, "scissors");
|
||||
}};
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int wins = 0;
|
||||
int losses = 0;
|
||||
|
||||
System.out.println("Welcome to Rock-Paper-Scissors! Please enter \"rock\", \"paper\", \"scissors\", or \"quit\" to exit.");
|
||||
|
||||
while (true) {
|
||||
System.out.println("-------------------------");
|
||||
System.out.print("Enter your move: ");
|
||||
String playerMove = scanner.nextLine();
|
||||
|
||||
if (playerMove.equals("quit")) {
|
||||
System.out.println("You won " + wins + " times and lost " + losses + " times.");
|
||||
System.out.println("Thanks for playing! See you again.");
|
||||
break;
|
||||
}
|
||||
|
||||
if (!movesMap.containsValue(playerMove)) {
|
||||
System.out.println("Your move isn't valid!");
|
||||
continue;
|
||||
}
|
||||
|
||||
String computerMove = getComputerMove();
|
||||
|
||||
if (playerMove.equals(computerMove)) {
|
||||
System.out.println("It's a tie!");
|
||||
} else if (isPlayerWin(playerMove, computerMove)) {
|
||||
System.out.println("You won!");
|
||||
wins++;
|
||||
} else {
|
||||
System.out.println("You lost!");
|
||||
losses++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPlayerWin(String playerMove, String computerMove) {
|
||||
return playerMove.equals("rock") && computerMove.equals("scissors")
|
||||
|| (playerMove.equals("scissors") && computerMove.equals("paper"))
|
||||
|| (playerMove.equals("paper") && computerMove.equals("rock"));
|
||||
}
|
||||
|
||||
private static String getComputerMove() {
|
||||
Random random = new Random();
|
||||
int randomNumber = random.nextInt(3);
|
||||
String computerMove = movesMap.get(randomNumber);
|
||||
System.out.println("Computer move: " + computerMove);
|
||||
return computerMove;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.uuid;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UuidHelper {
|
||||
|
||||
public static byte[] convertUUIDToBytes(UUID uuid) {
|
||||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
||||
bb.putLong(uuid.getMostSignificantBits());
|
||||
bb.putLong(uuid.getLeastSignificantBits());
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
public static UUID convertBytesToUUID(byte[] bytes) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||
long high = byteBuffer.getLong();
|
||||
long low = byteBuffer.getLong();
|
||||
return new UUID(high, low);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
System.out.println("Original UUID: " + uuid);
|
||||
|
||||
byte[] bytes = convertUUIDToBytes(uuid);
|
||||
System.out.println("Converted byte array: " + Arrays.toString(bytes));
|
||||
|
||||
UUID uuidNew = convertBytesToUUID(bytes);
|
||||
System.out.println("Converted UUID: " + uuidNew);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.maps.initialize;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.NavigableMap;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class EmptyMapInitializer {
|
||||
|
||||
public static Map<String, String> articleMap;
|
||||
static {
|
||||
articleMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public static Map<String, String> createEmptyMap() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public void createMapUsingConstructors() {
|
||||
Map hashMap = new HashMap();
|
||||
Map linkedHashMap = new LinkedHashMap();
|
||||
Map treeMap = new TreeMap();
|
||||
}
|
||||
|
||||
public Map<String, String> createEmptyMapUsingMapsObject() {
|
||||
Map<String, String> emptyMap = Maps.newHashMap();
|
||||
return emptyMap;
|
||||
}
|
||||
|
||||
public Map createGenericEmptyMapUsingMapsObject() {
|
||||
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
||||
return genericEmptyMap;
|
||||
}
|
||||
|
||||
public static Map<String, String> createMapUsingGuava() {
|
||||
Map<String, String> emptyMapUsingGuava =
|
||||
Maps.newHashMap(ImmutableMap.of());
|
||||
return emptyMapUsingGuava;
|
||||
}
|
||||
|
||||
public SortedMap<String, String> createEmptySortedMap() {
|
||||
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
||||
return sortedMap;
|
||||
}
|
||||
|
||||
public NavigableMap<String, String> createEmptyNavigableMap() {
|
||||
NavigableMap<String, String> navigableMap = Collections.emptyNavigableMap();
|
||||
return navigableMap;
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.maps.initialize;
|
||||
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EmptyMapInitializerUnitTest {
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void givenEmptyMap_whenAddingEntries_throwsException() {
|
||||
Map<String, String> map = EmptyMapInitializer.createEmptyMap();
|
||||
map.put("key", "value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMap_whenChecked_returnsTrue() {
|
||||
assertTrue(EmptyMapInitializer.articleMap.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() {
|
||||
Map<String, String> emptyMapUsingGuava =
|
||||
EmptyMapInitializer.createMapUsingGuava();
|
||||
assertTrue(emptyMapUsingGuava.isEmpty());
|
||||
emptyMapUsingGuava.put("key", "value");
|
||||
assertFalse(emptyMapUsingGuava.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.mapandhashmap;
|
||||
|
||||
import com.baeldung.mapandhashmap.printer.HashMapPrinter;
|
||||
import com.baeldung.mapandhashmap.printer.MapPrinter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
HashMap<String, String> hashMap = new HashMap<>();
|
||||
TreeMap<String, String> treeMap = new TreeMap<>();
|
||||
|
||||
HashMapPrinter hashMapPrinter = new HashMapPrinter();
|
||||
hashMapPrinter.printMap(hashMap);
|
||||
// hashMapPrinter.printMap(treeMap); Compile time error
|
||||
// hashMapPrinter.printMap(map); Compile time error
|
||||
|
||||
MapPrinter mapPrinter = new MapPrinter();
|
||||
mapPrinter.printMap(hashMap);
|
||||
mapPrinter.printMap(treeMap);
|
||||
mapPrinter.printMap(map);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class HashMapPrinter {
|
||||
|
||||
public void printMap(final HashMap<?, ?> map) {
|
||||
for (final Entry<?, ?> entry : map.entrySet()) {
|
||||
System.out.println(entry.getKey() + " " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class MapPrinter {
|
||||
|
||||
public void printMap(final Map<?, ?> map) {
|
||||
for (final Entry<?, ?> entry : map.entrySet()) {
|
||||
System.out.println(entry.getKey() + " " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class MapReporter {
|
||||
|
||||
private final Map<?, ?> map;
|
||||
|
||||
public MapReporter(final Map<?, ?> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public void printMap() {
|
||||
for (final Entry<?, ?> entry : this.map.entrySet()) {
|
||||
System.out.println(entry.getKey() + " " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HashMapPrinterUnitTest {
|
||||
|
||||
private final HashMapPrinter mapPrinter = new HashMapPrinter();
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test hash map printer with HashMap")
|
||||
void testPrintHashMap() {
|
||||
// given
|
||||
String key = "HashMap";
|
||||
String value = "Main default implementation for the Map interface";
|
||||
String expected = key + " " + value;
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
mapPrinter.printMap(map);
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test hash map printer with LinkedHash")
|
||||
void testPrintLinkedHashMap() {
|
||||
// given
|
||||
String key = "LinkedHashMap";
|
||||
String value = "Use this implementation if you need keep the order of elements";
|
||||
String expected = key + " " + value;
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
mapPrinter.printMap(map);
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.TreeMap;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MapPrinterUnitTest {
|
||||
|
||||
private final MapPrinter mapPrinter = new MapPrinter();
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test printer with TreeMap")
|
||||
void testPrintTreeMap() {
|
||||
// given
|
||||
String key = "TreeMap";
|
||||
String value = "Used when sorting is needed";
|
||||
String expected = key + " " + value;
|
||||
TreeMap<String, String> map = new TreeMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
mapPrinter.printMap(map);
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test printer with HashMap")
|
||||
void testPrintHashMap() {
|
||||
// given
|
||||
String key = "HashMap";
|
||||
String value = "Main default implementation for the Map interface";
|
||||
String expected = key + " " + value;
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
mapPrinter.printMap(map);
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test printer with LinkedHash")
|
||||
void testPrintLinkedHashMap() {
|
||||
// given
|
||||
String key = "LinkedHashMap";
|
||||
String value = "Use this implementation if you need keep the order of elements";
|
||||
String expected = key + " " + value;
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
mapPrinter.printMap(map);
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.mapandhashmap.printer;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.TreeMap;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MapReporterUnitTest {
|
||||
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test reporter with TreeMap")
|
||||
void testPrintTreeMap() {
|
||||
// given
|
||||
String key = "TreeMap";
|
||||
String value = "Used when sorting is needed";
|
||||
String expected = key + " " + value;
|
||||
TreeMap<String, String> map = new TreeMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
MapReporter mapReporter = new MapReporter(map);
|
||||
mapReporter.printMap();
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test reporter with HashMap")
|
||||
void testPrintHashMap() {
|
||||
// given
|
||||
String key = "HashMap";
|
||||
String value = "Main default implementation for the Map interface";
|
||||
String expected = key + " " + value;
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
MapReporter mapReporter = new MapReporter(map);
|
||||
mapReporter.printMap();
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test reporter with LinkedHash")
|
||||
void testPrintLinkedHashMap() {
|
||||
// given
|
||||
String key = "LinkedHashMap";
|
||||
String value = "Use this implementation if you need keep the order of elements";
|
||||
String expected = key + " " + value;
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
map.put(key, value);
|
||||
// when
|
||||
MapReporter mapReporter = new MapReporter(map);
|
||||
mapReporter.printMap();
|
||||
// then
|
||||
String actual = outputStreamCaptor.toString().trim();
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
|
||||
}
|
||||
+22
-10
@@ -1,25 +1,22 @@
|
||||
package com.baeldung.atomicstampedreference;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicStampedReference;
|
||||
|
||||
public class StampedAccount {
|
||||
|
||||
private AtomicInteger stamp = new AtomicInteger(0);
|
||||
private AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
|
||||
|
||||
public int getBalance() {
|
||||
return account.getReference();
|
||||
}
|
||||
|
||||
public int getStamp() {
|
||||
return account.getStamp();
|
||||
}
|
||||
private final AtomicInteger stamp = new AtomicInteger(0);
|
||||
private final AtomicStampedReference<Integer> account = new AtomicStampedReference<>(0, 0);
|
||||
|
||||
public boolean deposit(int funds) {
|
||||
int[] stamps = new int[1];
|
||||
int current = this.account.get(stamps);
|
||||
int newStamp = this.stamp.incrementAndGet();
|
||||
|
||||
// Thread is paused here to allow other threads to update the stamp and amount (for testing only)
|
||||
sleep();
|
||||
|
||||
return this.account.compareAndSet(current, current + funds, stamps[0], newStamp);
|
||||
}
|
||||
|
||||
@@ -29,4 +26,19 @@ public class StampedAccount {
|
||||
int newStamp = this.stamp.incrementAndGet();
|
||||
return this.account.compareAndSet(current, current - funds, stamps[0], newStamp);
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return account.getReference();
|
||||
}
|
||||
|
||||
public int getStamp() {
|
||||
return account.getStamp();
|
||||
}
|
||||
|
||||
private static void sleep() {
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+23
-6
@@ -1,21 +1,38 @@
|
||||
package com.baeldung.atomicstampedreference;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadStampedAccountUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMultiThread_whenStampedAccount_thenSetBalance() throws InterruptedException {
|
||||
StampedAccount account = new StampedAccount();
|
||||
|
||||
Thread t = new Thread(() -> {
|
||||
while (!account.withdrawal(100))
|
||||
while (!account.deposit(100)) {
|
||||
Thread.yield();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
Assert.assertTrue(account.deposit(100));
|
||||
t.join(1_000);
|
||||
Assert.assertFalse(t.isAlive());
|
||||
Assert.assertSame(0, account.getBalance());
|
||||
|
||||
Thread t2 = new Thread(() -> {
|
||||
while (!account.withdrawal(100)) {
|
||||
Thread.yield();
|
||||
}
|
||||
});
|
||||
t2.start();
|
||||
|
||||
t.join(10_000);
|
||||
t2.join(10_000);
|
||||
|
||||
assertFalse(t.isAlive());
|
||||
assertFalse(t2.isAlive());
|
||||
|
||||
assertEquals(0, account.getBalance());
|
||||
assertTrue(account.getStamp() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-2
@@ -16,12 +16,17 @@ class LongRunningAction implements Runnable {
|
||||
public void run() {
|
||||
System.out.println("This is phase " + ph.getPhase());
|
||||
System.out.println("Thread " + threadName + " before long running action");
|
||||
ph.arriveAndAwaitAdvance();
|
||||
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Thread " + threadName + " action completed and waiting for others");
|
||||
ph.arriveAndAwaitAdvance();
|
||||
System.out.println("Thread " + threadName + " proceeding in phase " + ph.getPhase());
|
||||
|
||||
ph.arriveAndDeregister();
|
||||
}
|
||||
}
|
||||
+7
@@ -26,13 +26,20 @@ public class PhaserUnitTest {
|
||||
executorService.submit(new LongRunningAction("thread-3", ph));
|
||||
|
||||
//then
|
||||
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
|
||||
ph.arriveAndAwaitAdvance();
|
||||
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
|
||||
|
||||
assertEquals(1, ph.getPhase());
|
||||
|
||||
//and
|
||||
executorService.submit(new LongRunningAction("thread-4", ph));
|
||||
executorService.submit(new LongRunningAction("thread-5", ph));
|
||||
|
||||
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
|
||||
ph.arriveAndAwaitAdvance();
|
||||
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
|
||||
|
||||
assertEquals(2, ph.getPhase());
|
||||
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.exception.missingreturnstatement;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MissingReturnStatement {
|
||||
public static void main(String[] args) {
|
||||
int a = -12;
|
||||
int result = pow(a);
|
||||
System.out.println(result);
|
||||
Map<String, Integer> dictionary = createDictionary();
|
||||
dictionary.forEach((s, integer) -> System.out.println(s + " " + integer));
|
||||
}
|
||||
|
||||
public static int pow(int number) {
|
||||
int pow = number * number;
|
||||
return pow;
|
||||
}
|
||||
|
||||
public static String checkNumber(int number) {
|
||||
if (number == 0) {
|
||||
return "It's equals to zero";
|
||||
}
|
||||
|
||||
for (int i = 0; i < number; i++) {
|
||||
if (i > 100) {
|
||||
return "It's a big number";
|
||||
}
|
||||
}
|
||||
return "It's a negative number";
|
||||
}
|
||||
|
||||
public static Map<String, Integer> createDictionary() {
|
||||
List<String> words = Arrays.asList("Hello", "World");
|
||||
return words.stream()
|
||||
.collect(Collectors.toMap(s -> s, s -> 1));
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.exception.missingreturnstatement;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MissingReturnStatementUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenANumber_thenReturnItsPow() {
|
||||
int number = 10;
|
||||
int pow = MissingReturnStatement.pow(number);
|
||||
assertEquals(100, pow);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenABigNumber_thenReturnItsType() {
|
||||
int number = 200;
|
||||
String type = MissingReturnStatement.checkNumber(number);
|
||||
assertEquals("It's a big number", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANegativeNumber_thenReturnItsType() {
|
||||
int number = -10;
|
||||
String type = MissingReturnStatement.checkNumber(number);
|
||||
assertEquals("It's a negative number", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStringDictionary_thenPrintValues() {
|
||||
Map<String, Integer> dictionary = MissingReturnStatement.createDictionary();
|
||||
assertEquals(2, dictionary.size());
|
||||
dictionary.forEach((s, integer) -> System.out.println(s + " - " + integer));
|
||||
}
|
||||
}
|
||||
@@ -259,110 +259,7 @@
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<!-- java instrumentation profiles to build jars -->
|
||||
<profile>
|
||||
<id>buildAgentLoader</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agentLoader</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/AgentLoader.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildApplication</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>application</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildAgent</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agent</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -15,3 +15,13 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [What Causes java.lang.OutOfMemoryError: unable to create new native thread](https://www.baeldung.com/java-outofmemoryerror-unable-to-create-new-native-thread)
|
||||
- [View Bytecode of a Class File in Java](https://www.baeldung.com/java-class-view-bytecode)
|
||||
- More articles: [[next -->]](/core-java-modules/core-java-jvm-2)
|
||||
|
||||
|
||||
To run the code for the Instrumentation: https://www.baeldung.com/java-instrumentation article:
|
||||
1- build the module
|
||||
2- run the module 3 times to build the 3 jars:
|
||||
mvn install -PbuildAgentLoader
|
||||
mvn install -PbuildApplication
|
||||
mvn install -PbuildAgent
|
||||
3- update the commands in the article with the exact names of the jars generated in the target folder
|
||||
4- update the path in the AgentLoader class with the path of the agent on your system
|
||||
@@ -57,6 +57,11 @@
|
||||
<artifactId>jol-core</artifactId>
|
||||
<version>${jol-core.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
@@ -66,5 +71,114 @@
|
||||
<asm.version>8.0.1</asm.version>
|
||||
<bcel.version>6.5.0</bcel.version>
|
||||
</properties>
|
||||
<profiles>
|
||||
<!-- java instrumentation profiles to build jars -->
|
||||
<profile>
|
||||
<id>buildAgentLoader</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agentLoader</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.instrumentation.application.Launcher</mainClass>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/AgentLoader.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildApplication</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>application</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.instrumentation.application.Launcher</mainClass>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildAgent</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agent</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.instrumentation.application.Launcher</mainClass>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,5 @@
|
||||
Agent-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
|
||||
Can-Redefine-Classes: true
|
||||
Can-Retransform-Classes: true
|
||||
Premain-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
|
||||
Main-Class: com.baeldung.instrumentation.application.Launcher
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.truststore;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.PKIXParameters;
|
||||
|
||||
public class TrustStoreUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenOpeningTrustStore_thenExceptionIsThrown() throws Exception {
|
||||
KeyStore keyStore = getKeyStore();
|
||||
InvalidAlgorithmParameterException invalidAlgorithmParameterException =
|
||||
Assertions.assertThrows(InvalidAlgorithmParameterException.class, () -> new PKIXParameters(keyStore));
|
||||
Assertions.assertEquals("the trustAnchors parameter must be non-empty", invalidAlgorithmParameterException.getMessage());
|
||||
}
|
||||
|
||||
private KeyStore getKeyStore() throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException {
|
||||
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
ks.load(null, "changeIt".toCharArray());
|
||||
return ks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
FROM nginx:latest
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
||||
@@ -0,0 +1,8 @@
|
||||
events {}
|
||||
|
||||
http {
|
||||
server {
|
||||
listen 80;
|
||||
index index.html;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
FROM nginx:latest
|
||||
COPY sample-site/html/* /etc/nginx/html/
|
||||
COPY config/nginx.conf /etc/nginx/nginx.conf
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
FROM sample-site-base:latest
|
||||
COPY html/* /etc/nginx/html/
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
FROM nginx:latest
|
||||
COPY html/* /etc/nginx/html/
|
||||
COPY config/nginx.conf /etc/nginx/nginx.conf
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user