Merge branch 'master' into BAEL-1661_v2
This commit is contained in:
@@ -39,3 +39,4 @@ This tutorials project is being built **[>> HERE](https://rest-security.ci.cloud
|
||||
|
||||
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
|
||||
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
|
||||
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package com.baeldung.algorithms.analysis;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnalysisRunnerLiveTest {
|
||||
|
||||
int n = 10;
|
||||
int total = 0;
|
||||
|
||||
@Test
|
||||
public void whenConstantComplexity_thenConstantRuntime() {
|
||||
|
||||
System.out.println("**** n = " + n + " ****");
|
||||
System.out.println();
|
||||
|
||||
// Constant Time
|
||||
System.out.println("**** Constant time ****");
|
||||
|
||||
System.out.println("Hey - your input is: " + n);
|
||||
System.out.println("Running time not dependent on input size!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLogarithmicComplexity_thenLogarithmicRuntime() {
|
||||
// Logarithmic Time
|
||||
System.out.println("**** Logarithmic Time ****");
|
||||
for (int i = 1; i < n; i = i * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLinearComplexity_thenLinearRuntime() {
|
||||
// Linear Time
|
||||
System.out.println("**** Linear Time ****");
|
||||
for (int i = 0; i < n; i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNLogNComplexity_thenNLogNRuntime() {
|
||||
// N Log N Time
|
||||
System.out.println("**** nlogn Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j < n; j = j * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQuadraticComplexity_thenQuadraticRuntime() {
|
||||
// Quadratic Time
|
||||
System.out.println("**** Quadratic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCubicComplexity_thenCubicRuntime() {
|
||||
// Cubic Time
|
||||
System.out.println("**** Cubic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
for (int k = 1; k <= n; k++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j + " and " + k);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExponentialComplexity_thenExponentialRuntime() {
|
||||
// Exponential Time
|
||||
System.out.println("**** Exponential Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= Math.pow(2, n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorialComplexity_thenFactorialRuntime() {
|
||||
// Factorial Time
|
||||
System.out.println("**** Factorial Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <=
|
||||
|
||||
factorial(n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
}
|
||||
|
||||
static int factorial(int n) {
|
||||
if (n == 0 || n == 1)
|
||||
return 1;
|
||||
else
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,30 @@
|
||||
package com.baeldung.poi.powerpoint;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.xslf.usermodel.XMLSlideShow;
|
||||
import org.apache.poi.xslf.usermodel.XSLFShape;
|
||||
import org.apache.poi.xslf.usermodel.XSLFSlide;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
public class PowerPointIntegrationTest {
|
||||
|
||||
private PowerPointHelper pph;
|
||||
private String fileLocation;
|
||||
private static final String FILE_NAME = "presentation.pptx";
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
File currDir = new File(".");
|
||||
File currDir = tempFolder.newFolder();
|
||||
String path = currDir.getAbsolutePath();
|
||||
fileLocation = path.substring(0, path.length() - 1) + FILE_NAME;
|
||||
|
||||
|
||||
@@ -52,4 +52,4 @@
|
||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
|
||||
- [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table)
|
||||
|
||||
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
|
||||
|
||||
@@ -25,3 +25,4 @@
|
||||
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
|
||||
- [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity)
|
||||
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
|
||||
- [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api)
|
||||
|
||||
@@ -156,3 +156,7 @@
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [The Thread.join() Method in Java](http://www.baeldung.com/java-thread-join)
|
||||
- [Guide to the super Java Keyword](http://www.baeldung.com/java-super)
|
||||
- [Guide to the this Java Keyword](http://www.baeldung.com/java-this)
|
||||
- [Jagged Arrays In Java](http://www.baeldung.com/java-jagged-arrays)
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
/**
|
||||
* Implementation of a singly linked list.
|
||||
*/
|
||||
public class LinkedList {
|
||||
private Node head;
|
||||
private Node tail;
|
||||
|
||||
public Node head() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public void add(String data) {
|
||||
Node newNode = new Node(data);
|
||||
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Node {
|
||||
private Node next;
|
||||
private String data;
|
||||
|
||||
public Node(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public Node next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,23 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.linkedlist.Node;
|
||||
|
||||
public class MiddleElementLookup {
|
||||
|
||||
public static String findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
if (linkedList == null || linkedList.isEmpty()) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return linkedList.get((linkedList.size() - 1) / 2);
|
||||
return Optional.ofNullable(linkedList.get((linkedList.size() - 1) / 2));
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// calculate the size of the list
|
||||
@@ -33,17 +34,17 @@ public class MiddleElementLookup {
|
||||
current = current.next();
|
||||
}
|
||||
|
||||
return current.data();
|
||||
return Optional.ofNullable(current.data());
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
|
||||
findMiddleRecursively(head, middleAux);
|
||||
return middleAux.middle.data();
|
||||
return Optional.ofNullable(middleAux.middle.data());
|
||||
}
|
||||
|
||||
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
|
||||
@@ -63,9 +64,9 @@ public class MiddleElementLookup {
|
||||
middleAux.length--;
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Node slowPointer = head;
|
||||
@@ -78,7 +79,7 @@ public class MiddleElementLookup {
|
||||
slowPointer = slowPointer.next();
|
||||
}
|
||||
|
||||
return slowPointer.data();
|
||||
return Optional.ofNullable(slowPointer.data());
|
||||
}
|
||||
|
||||
private static class MiddleAuxRecursion {
|
||||
|
||||
@@ -46,7 +46,7 @@ public class JaggedArrayUnitTest {
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outContent));
|
||||
obj.printElements(jaggedArr);
|
||||
assertEquals("[1, 2]\n[3, 4, 5]\n[6, 7, 8, 9]\n", outContent.toString());
|
||||
assertEquals("[1, 2][3, 4, 5][6, 7, 8, 9]", outContent.toString().replace("\r", "").replace("\n", ""));
|
||||
System.setOut(System.out);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.baeldung.arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArraysUnitTest {
|
||||
private String[] intro;
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
intro = new String[] { "once", "upon", "a", "time" };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyOfRange_thenAbridgedArray() {
|
||||
String[] abridgement = Arrays.copyOfRange(intro, 0, 3);
|
||||
|
||||
assertArrayEquals(new String[] { "once", "upon", "a" }, abridgement);
|
||||
assertFalse(Arrays.equals(intro, abridgement));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyOf_thenNullElement() {
|
||||
String[] revised = Arrays.copyOf(intro, 3);
|
||||
String[] expanded = Arrays.copyOf(intro, 5);
|
||||
|
||||
assertArrayEquals(Arrays.copyOfRange(intro, 0, 3), revised);
|
||||
assertNull(expanded[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFill_thenAllMatch() {
|
||||
String[] stutter = new String[3];
|
||||
Arrays.fill(stutter, "once");
|
||||
|
||||
assertTrue(Stream.of(stutter).allMatch(el -> "once".equals(el)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEqualsContent_thenMatch() {
|
||||
assertTrue(Arrays.equals(new String[] { "once", "upon", "a", "time" }, intro));
|
||||
assertFalse(Arrays.equals(new String[] { "once", "upon", "a", null }, intro));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNestedArrays_thenDeepEqualsPass() {
|
||||
String[] end = { "the", "end" };
|
||||
Object[] story = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
|
||||
Object[] copy = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
|
||||
|
||||
assertTrue(Arrays.deepEquals(story, copy));
|
||||
assertFalse(Arrays.equals(story, copy));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSort_thenArraySorted() {
|
||||
String[] sorted = Arrays.copyOf(intro, 4);
|
||||
Arrays.sort(sorted);
|
||||
|
||||
assertArrayEquals(new String[] { "a", "once", "time", "upon" }, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBinarySearch_thenFindElements() {
|
||||
String[] sorted = Arrays.copyOf(intro, 4);
|
||||
Arrays.sort(sorted);
|
||||
int exact = Arrays.binarySearch(sorted, "time");
|
||||
int caseInsensitive = Arrays.binarySearch(sorted, "TiMe", String::compareToIgnoreCase);
|
||||
|
||||
assertEquals("time", sorted[exact]);
|
||||
assertEquals(2, exact);
|
||||
assertEquals(exact, caseInsensitive);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullElement_thenArraysHashCodeNotEqual() {
|
||||
int beforeChange = Arrays.hashCode(intro);
|
||||
int before = intro.hashCode();
|
||||
intro[3] = null;
|
||||
int after = intro.hashCode();
|
||||
int afterChange = Arrays.hashCode(intro);
|
||||
|
||||
assertNotEquals(beforeChange, afterChange);
|
||||
assertEquals(before, after);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNestedArrayNullElement_thenEqualsFailDeepHashPass() {
|
||||
Object[] looping = new Object[] { intro, intro };
|
||||
int deepHashBefore = Arrays.deepHashCode(looping);
|
||||
int hashBefore = Arrays.hashCode(looping);
|
||||
|
||||
intro[3] = null;
|
||||
|
||||
int hashAfter = Arrays.hashCode(looping);
|
||||
int deepHashAfter = Arrays.deepHashCode(looping);
|
||||
|
||||
assertEquals(hashAfter, hashBefore);
|
||||
assertNotEquals(deepHashAfter, deepHashBefore);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStreamBadIndex_thenException() {
|
||||
assertEquals(Arrays.stream(intro).count(), 4);
|
||||
|
||||
exception.expect(ArrayIndexOutOfBoundsException.class);
|
||||
Arrays.stream(intro, 2, 1).count();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSetAllToUpper_thenAppliedToAllElements() {
|
||||
String[] longAgo = new String[4];
|
||||
Arrays.setAll(longAgo, i -> intro[i].toUpperCase());
|
||||
|
||||
assertArrayEquals(longAgo, new String[] { "ONCE", "UPON", "A", "TIME" });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenFormattedArrayString() {
|
||||
assertEquals("[once, upon, a, time]", Arrays.toString(intro));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNestedArrayDeepString_thenFormattedArraysString() {
|
||||
String[] end = { "the", "end" };
|
||||
Object[] story = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
|
||||
|
||||
assertEquals("[[once, upon, a, time], [chapter one, chapter two], [the, end]]", Arrays.deepToString(story));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAsList_thenImmutableArray() {
|
||||
List<String> rets = Arrays.asList(intro);
|
||||
|
||||
assertTrue(rets.contains("upon"));
|
||||
assertTrue(rets.contains("time"));
|
||||
assertEquals(rets.size(), 4);
|
||||
|
||||
exception.expect(UnsupportedOperationException.class);
|
||||
rets.add("the");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
@@ -10,34 +11,83 @@ public class MiddleElementLookupUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleLinkedList_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementLinkedList(createLinkedList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementLinkedList(createLinkedList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassRecursively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead1PassRecursively(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead1PassRecursively(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassIteratively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead1PassIteratively(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead1PassIteratively(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListEmptyOrNull_thenMiddleNull() {
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementLinkedList(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead1PassIteratively(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead1PassRecursively(null));
|
||||
public void whenListEmptyOrNull_thenMiddleNotFound() {
|
||||
// null list
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(null)
|
||||
.isPresent());
|
||||
|
||||
// empty LinkedList
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(new LinkedList<>())
|
||||
.isPresent());
|
||||
|
||||
// LinkedList with nulls
|
||||
LinkedList<String> nullsList = new LinkedList<>();
|
||||
nullsList.add(null);
|
||||
nullsList.add(null);
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(nullsList)
|
||||
.isPresent());
|
||||
|
||||
// nodes with null values
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(new Node(null))
|
||||
.isPresent());
|
||||
}
|
||||
|
||||
private static LinkedList<String> createLinkedList(int n) {
|
||||
|
||||
@@ -179,7 +179,7 @@ public class JavaMoneyUnitManualTest {
|
||||
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
|
||||
.of(Locale.US)
|
||||
.set(CurrencyStyle.NAME)
|
||||
.set("pattern", "00000.00 �")
|
||||
.set("pattern", "00000.00 US Dollar")
|
||||
.build());
|
||||
String customFormatted = customFormat.format(oneDollar);
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ public class NashornUnitTest {
|
||||
public void loadExamples() throws ScriptException {
|
||||
Object loadResult = engine.eval("load('classpath:js/script.js');" + "increment(5)");
|
||||
|
||||
Assert.assertEquals(6.0, loadResult);
|
||||
Assert.assertEquals(6, ((Double) loadResult).intValue());
|
||||
|
||||
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.increment(5);");
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
@@ -18,6 +19,11 @@ public class ClusterServiceImpl implements ClusterService {
|
||||
|
||||
private Cluster cluster;
|
||||
private Map<String, Bucket> buckets = new ConcurrentHashMap<>();
|
||||
|
||||
@Autowired
|
||||
public ClusterServiceImpl(Cluster cluster) {
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
|
||||
+1
@@ -18,6 +18,7 @@ public class TutorialBucketService extends AbstractBucketService {
|
||||
@Autowired
|
||||
public TutorialBucketService(ClusterService clusterService) {
|
||||
super(clusterService);
|
||||
openBucket();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment;
|
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.couchbase.async.service", "com.baeldung.couchbase.n1ql"})
|
||||
public class PersonCrudServiceIntegrationTestConfig {
|
||||
|
||||
@Bean
|
||||
public Cluster cluster() {
|
||||
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
|
||||
.connectTimeout(60000)
|
||||
.build();
|
||||
return CouchbaseCluster.create(env, "127.0.0.1");
|
||||
}
|
||||
|
||||
}
|
||||
+13
-9
@@ -1,27 +1,31 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.couchbase.async.AsyncIntegrationTest;
|
||||
import com.baeldung.couchbase.async.person.Person;
|
||||
import com.baeldung.couchbase.async.person.PersonCrudService;
|
||||
import com.baeldung.couchbase.async.person.PersonDocumentConverter;
|
||||
import com.baeldung.couchbase.async.service.BucketService;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
|
||||
public class PersonCrudServiceIntegrationTest extends AsyncIntegrationTest {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {PersonCrudServiceIntegrationTestConfig.class})
|
||||
public class PersonCrudServiceLiveTest extends AsyncIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PersonCrudService personService;
|
||||
@@ -35,8 +39,8 @@ public class PersonCrudServiceIntegrationTest extends AsyncIntegrationTest {
|
||||
|
||||
private Bucket bucket;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
@Before
|
||||
public void init() {
|
||||
bucket = bucketService.getBucket();
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ import com.couchbase.client.java.Bucket;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { AsyncIntegrationTestConfig.class })
|
||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||
public class ClusterServiceIntegrationTest extends AsyncIntegrationTest {
|
||||
public class ClusterServiceLiveTest extends AsyncIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ClusterService couchbaseService;
|
||||
+2
-2
@@ -14,8 +14,8 @@ import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.view.ViewResult;
|
||||
import com.couchbase.client.java.view.ViewRow;
|
||||
|
||||
public class StudentGradeServiceIntegrationTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(StudentGradeServiceIntegrationTest.class);
|
||||
public class StudentGradeServiceLiveTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(StudentGradeServiceLiveTest.class);
|
||||
|
||||
static StudentGradeService studentGradeService;
|
||||
static Set<String> gradeIds = new HashSet<>();
|
||||
+20
-17
@@ -1,5 +1,23 @@
|
||||
package com.baeldung.couchbase.n1ql;
|
||||
|
||||
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
|
||||
import static com.couchbase.client.java.query.Select.select;
|
||||
import static com.couchbase.client.java.query.dsl.Expression.i;
|
||||
import static com.couchbase.client.java.query.dsl.Expression.s;
|
||||
import static com.couchbase.client.java.query.dsl.Expression.x;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
@@ -10,28 +28,13 @@ import com.couchbase.client.java.query.N1qlQueryResult;
|
||||
import com.couchbase.client.java.query.N1qlQueryRow;
|
||||
import com.couchbase.client.java.query.Statement;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
|
||||
import static com.couchbase.client.java.query.Select.select;
|
||||
import static com.couchbase.client.java.query.dsl.Expression.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { IntegrationTestConfig.class })
|
||||
public class N1QLIntegrationTest {
|
||||
public class N1QLLiveTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
+1
-1
@@ -10,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.baeldung.couchbase.spring.IntegrationTest;
|
||||
|
||||
public class PersonCrudServiceIntegrationTest extends IntegrationTest {
|
||||
public class PersonCrudServiceLiveTest extends IntegrationTest {
|
||||
|
||||
private static final String CLARK_KENT = "Clark Kent";
|
||||
private static final String SMALLVILLE = "Smallville";
|
||||
+1
-1
@@ -17,7 +17,7 @@ import com.couchbase.client.java.Bucket;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { IntegrationTestConfig.class })
|
||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||
public class ClusterServiceIntegrationTest extends IntegrationTest {
|
||||
public class ClusterServiceLiveTest extends IntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ClusterService couchbaseService;
|
||||
@@ -1,3 +1,2 @@
|
||||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Alpine Linux with OpenJDK JRE
|
||||
FROM openjdk:8-jre-alpine
|
||||
RUN apk add --no-cache bash
|
||||
|
||||
# copy fat WAR
|
||||
COPY spring-boot-app-0.0.1-SNAPSHOT.war /app.war
|
||||
|
||||
# copy fat WAR
|
||||
COPY logback.xml /logback.xml
|
||||
|
||||
COPY run.sh /run.sh
|
||||
|
||||
# runs application
|
||||
#CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=default", "-Dlogging.config=/logback.xml", "/app.war"]
|
||||
|
||||
ENTRYPOINT ["/run.sh"]
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
|
||||
<file>/var/log/Application/application.log</file>
|
||||
<append>true</append>
|
||||
<encoder>
|
||||
<pattern>%-7d{yyyy-MM-dd HH:mm:ss:SSS} %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
</configuration>
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
java -Dspring.profiles.active=$1 -Dlogging.config=/logback.xml -jar /app.war
|
||||
|
||||
@@ -51,6 +51,22 @@
|
||||
<warSourceDirectory>WebContent</warSourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.stackify.Application</mainClass>
|
||||
<outputDirectory>${project.basedir}/docker</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -58,4 +74,4 @@
|
||||
<tomcat.version>8.0.43</tomcat.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.hibernate;
|
||||
|
||||
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
|
||||
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
|
||||
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
||||
@@ -70,6 +72,8 @@ public class HibernateUtil {
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.hibernate.optimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class OptimisticLockingCourse {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinTable(name = "optimistic_student_course")
|
||||
private OptimisticLockingStudent student;
|
||||
|
||||
public OptimisticLockingCourse(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OptimisticLockingCourse() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public OptimisticLockingStudent getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(OptimisticLockingStudent student) {
|
||||
this.student = student;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.hibernate.optimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class OptimisticLockingStudent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String lastName;
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<OptimisticLockingCourse> courses;
|
||||
|
||||
public OptimisticLockingStudent(Long id, String name, String lastName, List<OptimisticLockingCourse> courses) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.courses = courses;
|
||||
}
|
||||
|
||||
public OptimisticLockingStudent() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<OptimisticLockingCourse> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<OptimisticLockingCourse> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user