Merge branch 'master' into master
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import org.eclipse.collections.api.block.procedure.Procedure;
|
||||
import org.eclipse.collections.api.tuple.Pair;
|
||||
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
|
||||
import org.eclipse.collections.impl.tuple.Tuples;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ForEachPatternTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void whenInstantiateAndChangeValues_thenCorrect() {
|
||||
Pair<Integer, String> pair1 = Tuples.pair(1, "One");
|
||||
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
|
||||
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
|
||||
|
||||
UnifiedMap<Integer, String> map = UnifiedMap.newMapWith(pair1, pair2, pair3);
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
map.put(i + 1, "New Value");
|
||||
}
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
assertEquals("New Value", map.get(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.collections.impl.factory.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InjectIntoPatternTest {
|
||||
|
||||
@Test
|
||||
public void whenInjectInto_thenCorrect() {
|
||||
List<Integer> list = Lists.mutable.of(1, 2, 3, 4);
|
||||
int result = 5;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Integer v = list.get(i);
|
||||
result = result + v.intValue();
|
||||
}
|
||||
|
||||
assertEquals(15, result);
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,9 @@ public class HLLUnitTest {
|
||||
|
||||
//when
|
||||
LongStream.range(0, numberOfElements).forEach(element -> {
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
hll.addRaw(hashedValue);
|
||||
}
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
hll.addRaw(hashedValue);
|
||||
}
|
||||
);
|
||||
|
||||
//then
|
||||
@@ -44,15 +44,15 @@ public class HLLUnitTest {
|
||||
|
||||
//when
|
||||
LongStream.range(0, numberOfElements).forEach(element -> {
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
firstHll.addRaw(hashedValue);
|
||||
}
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
firstHll.addRaw(hashedValue);
|
||||
}
|
||||
);
|
||||
|
||||
LongStream.range(numberOfElements, numberOfElements * 2).forEach(element -> {
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
secondHLL.addRaw(hashedValue);
|
||||
}
|
||||
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
|
||||
secondHLL.addRaw(hashedValue);
|
||||
}
|
||||
);
|
||||
|
||||
//then
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
import io.specto.hoverfly.junit.core.SimulationSource;
|
||||
import io.specto.hoverfly.junit.rule.HoverflyRule;
|
||||
|
||||
public class HoverflyApiTest {
|
||||
public class HoverflyApiIntegrationTest {
|
||||
|
||||
private static final SimulationSource source = dsl(
|
||||
service("http://www.baeldung.com")
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.noexception;
|
||||
|
||||
import com.machinezoo.noexception.Exceptions;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NoExceptionUnitTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void whenStdExceptionHandling_thenCatchAndLog() {
|
||||
try {
|
||||
System.out.println("Result is " + Integer.parseInt("foobar"));
|
||||
} catch (Throwable exception) {
|
||||
logger.error("Caught exception:", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDefaultNoException_thenCatchAndLog() {
|
||||
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
|
||||
Exceptions.log(logger).run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithMessage() {
|
||||
Exceptions.log(logger, "Something went wrong:").run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
|
||||
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> Integer.parseInt("foobar")).orElse(-1));
|
||||
}
|
||||
|
||||
@Test(expected = Error.class)
|
||||
public void givenCustomHandler_whenError_thenRethrowError() {
|
||||
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
|
||||
customExceptionHandler.run(() -> throwError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomHandler_whenException_thenCatchAndLog() {
|
||||
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
|
||||
customExceptionHandler.run(() -> throwException());
|
||||
}
|
||||
|
||||
private static void throwError() {
|
||||
throw new Error("This is very bad.");
|
||||
}
|
||||
|
||||
private static void throwException() {
|
||||
String testString = "foo";
|
||||
testString.charAt(5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.baeldung.pcollections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.pcollections.*;
|
||||
import org.pcollections.HashPMap;
|
||||
import org.pcollections.HashTreePMap;
|
||||
import org.pcollections.HashTreePSet;
|
||||
import org.pcollections.MapPSet;
|
||||
import org.pcollections.PVector;
|
||||
import org.pcollections.TreePVector;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@@ -27,7 +32,7 @@ public class PCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenExistingHashMap_whenFrom_thenCreateHashPMap() {
|
||||
Map map = new HashMap();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("mkey1", "mval1");
|
||||
map.put("mkey2", "mval2");
|
||||
|
||||
@@ -41,7 +46,7 @@ public class PCollectionsUnitTest {
|
||||
HashPMap<String, String> pmap = HashTreePMap.empty();
|
||||
HashPMap<String, String> pmap0 = pmap.plus("key1", "value1");
|
||||
|
||||
Map map = new HashMap();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("key2", "val2");
|
||||
map.put("key3", "val3");
|
||||
|
||||
@@ -57,22 +62,24 @@ public class PCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenTreePVectorMethods_thenPerformOperations() {
|
||||
TreePVector pVector = TreePVector.empty();
|
||||
TreePVector<String> pVector = TreePVector.empty();
|
||||
|
||||
TreePVector<String> pV1 = pVector.plus("e1");
|
||||
TreePVector<String> pV2 = pV1.plusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
|
||||
TreePVector pV1 = pVector.plus("e1");
|
||||
TreePVector pV2 = pV1.plusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
assertEquals(1, pV1.size());
|
||||
assertEquals(4, pV2.size());
|
||||
|
||||
TreePVector pV3 = pV2.minus("e1");
|
||||
TreePVector pV4 = pV3.minusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
TreePVector<String> pV3 = pV2.minus("e1");
|
||||
TreePVector<String> pV4 = pV3.minusAll(Arrays.asList("e2", "e3", "e4"));
|
||||
|
||||
assertEquals(pV3.size(), 3);
|
||||
assertEquals(pV4.size(), 0);
|
||||
|
||||
TreePVector pSub = pV2.subList(0, 2);
|
||||
TreePVector<String> pSub = pV2.subList(0, 2);
|
||||
assertTrue(pSub.contains("e1") && pSub.contains("e2"));
|
||||
|
||||
TreePVector pVW = (TreePVector) pV2.with(0, "e10");
|
||||
PVector<String> pVW = pV2.with(0, "e10");
|
||||
assertEquals(pVW.get(0), "e10");
|
||||
}
|
||||
|
||||
@@ -80,7 +87,7 @@ public class PCollectionsUnitTest {
|
||||
public void whenMapPSetMethods_thenPerformOperations() {
|
||||
|
||||
MapPSet pSet = HashTreePSet.empty()
|
||||
.plusAll(Arrays.asList("e1","e2","e3","e4"));
|
||||
.plusAll(Arrays.asList("e1", "e2", "e3", "e4"));
|
||||
assertEquals(pSet.size(), 4);
|
||||
|
||||
MapPSet pSet1 = pSet.minus("e4");
|
||||
|
||||
Reference in New Issue
Block a user