Merge branch 'master' into pr/1014-stephen
This commit is contained in:
4
core-java/.gitignore
vendored
4
core-java/.gitignore
vendored
@@ -16,3 +16,7 @@
|
||||
*.txt
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea
|
||||
*.iml
|
||||
@@ -46,3 +46,11 @@
|
||||
- [Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||
- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [Slope One Algorithm: Collaborative Filtering Recommendation Systems](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
|
||||
- [Pattern Search with Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||
- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding)
|
||||
- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size)
|
||||
- [The Basics of Java Generics](http://www.baeldung.com/java-generics)
|
||||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
|
||||
|
||||
@@ -63,6 +63,12 @@
|
||||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.lmax</groupId>
|
||||
<artifactId>disruptor</artifactId>
|
||||
<version>${disruptor.version}</version>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
|
||||
<!-- marshalling -->
|
||||
@@ -352,7 +358,7 @@
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
@@ -363,6 +369,7 @@
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.chainedexception;
|
||||
|
||||
import com.baeldung.chainedexception.exceptions.GirlFriendOfManagerUpsetException;
|
||||
import com.baeldung.chainedexception.exceptions.ManagerUpsetException;
|
||||
import com.baeldung.chainedexception.exceptions.NoLeaveGrantedException;
|
||||
import com.baeldung.chainedexception.exceptions.TeamLeadUpsetException;
|
||||
|
||||
public class LogWithChain {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
getLeave();
|
||||
}
|
||||
|
||||
private static void getLeave() throws NoLeaveGrantedException {
|
||||
try {
|
||||
howIsTeamLead();
|
||||
} catch (TeamLeadUpsetException e) {
|
||||
throw new NoLeaveGrantedException("Leave not sanctioned.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsTeamLead() throws TeamLeadUpsetException {
|
||||
try {
|
||||
howIsManager();
|
||||
} catch (ManagerUpsetException e) {
|
||||
throw new TeamLeadUpsetException(
|
||||
"Team lead is not in good mood", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsManager() throws ManagerUpsetException {
|
||||
try {
|
||||
howIsGirlFriendOfManager();
|
||||
} catch (GirlFriendOfManagerUpsetException e) {
|
||||
throw new ManagerUpsetException("Manager is in bad mood", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsGirlFriendOfManager()
|
||||
throws GirlFriendOfManagerUpsetException {
|
||||
throw new GirlFriendOfManagerUpsetException(
|
||||
"Girl friend of manager is in bad mood");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.chainedexception;
|
||||
|
||||
import com.baeldung.chainedexception.exceptions.GirlFriendOfManagerUpsetException;
|
||||
import com.baeldung.chainedexception.exceptions.ManagerUpsetException;
|
||||
import com.baeldung.chainedexception.exceptions.NoLeaveGrantedException;
|
||||
import com.baeldung.chainedexception.exceptions.TeamLeadUpsetException;
|
||||
|
||||
public class LogWithoutChain {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
getLeave();
|
||||
}
|
||||
|
||||
private static void getLeave() throws NoLeaveGrantedException {
|
||||
try {
|
||||
howIsTeamLead();
|
||||
} catch (TeamLeadUpsetException e) {
|
||||
e.printStackTrace();
|
||||
throw new NoLeaveGrantedException("Leave not sanctioned.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsTeamLead() throws TeamLeadUpsetException {
|
||||
try {
|
||||
howIsManager();
|
||||
} catch (ManagerUpsetException e) {
|
||||
e.printStackTrace();
|
||||
throw new TeamLeadUpsetException(
|
||||
"Team lead is not in good mood");
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsManager() throws ManagerUpsetException {
|
||||
try {
|
||||
howIsGirlFriendOfManager();
|
||||
} catch (GirlFriendOfManagerUpsetException e) {
|
||||
e.printStackTrace();
|
||||
throw new ManagerUpsetException("Manager is in bad mood");
|
||||
}
|
||||
}
|
||||
|
||||
private static void howIsGirlFriendOfManager()
|
||||
throws GirlFriendOfManagerUpsetException {
|
||||
throw new GirlFriendOfManagerUpsetException(
|
||||
"Girl friend of manager is in bad mood");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.chainedexception.exceptions;
|
||||
|
||||
public class GirlFriendOfManagerUpsetException extends Exception {
|
||||
|
||||
public GirlFriendOfManagerUpsetException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public GirlFriendOfManagerUpsetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.chainedexception.exceptions;
|
||||
|
||||
public class ManagerUpsetException extends Exception {
|
||||
|
||||
public ManagerUpsetException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ManagerUpsetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.chainedexception.exceptions;
|
||||
|
||||
public class NoLeaveGrantedException extends Exception {
|
||||
|
||||
public NoLeaveGrantedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public NoLeaveGrantedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.chainedexception.exceptions;
|
||||
|
||||
public class TeamLeadUpsetException extends Exception {
|
||||
|
||||
public TeamLeadUpsetException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public TeamLeadUpsetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
|
||||
public class DelayedMultiEventProducer implements EventProducer {
|
||||
|
||||
@Override
|
||||
public void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count) {
|
||||
final Runnable simpleProducer = () -> produce(ringBuffer, count, false);
|
||||
final Runnable delayedProducer = () -> produce(ringBuffer, count, true);
|
||||
new Thread(simpleProducer).start();
|
||||
new Thread(delayedProducer).start();
|
||||
}
|
||||
|
||||
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count, final boolean addDelay) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
final long seq = ringBuffer.next();
|
||||
final ValueEvent valueEvent = ringBuffer.get(seq);
|
||||
valueEvent.setValue(i);
|
||||
ringBuffer.publish(seq);
|
||||
if (addDelay) {
|
||||
addDelay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addDelay() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
// No-Op lets swallow it
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
/**
|
||||
* Consumer that consumes event from ring buffer.
|
||||
*/
|
||||
public interface EventConsumer {
|
||||
/**
|
||||
* One or more event handler to handle event from ring buffer.
|
||||
*/
|
||||
public EventHandler<ValueEvent>[] getEventHandler();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
|
||||
/**
|
||||
* Producer that produces event for ring buffer.
|
||||
*/
|
||||
public interface EventProducer {
|
||||
/**
|
||||
* Start the producer that would start producing the values.
|
||||
* @param ringBuffer
|
||||
* @param count
|
||||
*/
|
||||
public void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
public class MultiEventPrintConsumer implements EventConsumer {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
|
||||
final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
|
||||
return new EventHandler[] { eventHandler, otherEventHandler };
|
||||
}
|
||||
|
||||
private void print(final int id, final long sequenceId) {
|
||||
logger.info("Id is " + id + " sequence id that was used is " + sequenceId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
public class SingleEventPrintConsumer implements EventConsumer {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
|
||||
return new EventHandler[] { eventHandler };
|
||||
}
|
||||
|
||||
private void print(final int id, final long sequenceId) {
|
||||
logger.info("Id is " + id + " sequence id that was used is " + sequenceId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
|
||||
public class SingleEventProducer implements EventProducer {
|
||||
|
||||
@Override
|
||||
public void startProducing(RingBuffer<ValueEvent> ringBuffer, int count) {
|
||||
final Runnable producer = () -> produce(ringBuffer, count);
|
||||
new Thread(producer).start();
|
||||
}
|
||||
|
||||
private void produce(final RingBuffer<ValueEvent> ringBuffer, final int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
final long seq = ringBuffer.next();
|
||||
final ValueEvent valueEvent = ringBuffer.get(seq);
|
||||
valueEvent.setValue(i);
|
||||
ringBuffer.publish(seq);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
|
||||
import com.lmax.disruptor.EventFactory;
|
||||
|
||||
public final class ValueEvent {
|
||||
|
||||
private int value;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public final static EventFactory<ValueEvent> EVENT_FACTORY = () -> new ValueEvent();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.java8.lambda.exceptions;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class LambdaExceptionWrappers {
|
||||
|
||||
public static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) {
|
||||
return i -> {
|
||||
try {
|
||||
consumer.accept(i);
|
||||
} catch (ArithmeticException e) {
|
||||
System.err.println("Arithmetic Exception occured : " + e.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static <T, E extends Exception> Consumer<T> consumerWrapper(Consumer<T> consumer, Class<E> clazz) {
|
||||
return i -> {
|
||||
try {
|
||||
consumer.accept(i);
|
||||
} catch (Exception ex) {
|
||||
try {
|
||||
E exCast = clazz.cast(ex);
|
||||
System.err.println("Exception occured : " + exCast.getMessage());
|
||||
} catch (ClassCastException ccEx) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer) {
|
||||
return i -> {
|
||||
try {
|
||||
throwingConsumer.accept(i);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> handlingConsumerWrapper(ThrowingConsumer<T, E> throwingConsumer, Class<E> exceptionClass) {
|
||||
return i -> {
|
||||
try {
|
||||
throwingConsumer.accept(i);
|
||||
} catch (Exception ex) {
|
||||
try {
|
||||
E exCast = exceptionClass.cast(ex);
|
||||
System.err.println("Exception occured : " + exCast.getMessage());
|
||||
} catch (ClassCastException ccEx) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.java8.lambda.exceptions;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingConsumer<T, E extends Exception> {
|
||||
|
||||
void accept(T t) throws E;
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.scripting;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
public class Nashorn {
|
||||
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
Object result = engine.eval(
|
||||
"var greeting='hello world';" +
|
||||
"print(greeting);" +
|
||||
"greeting");
|
||||
|
||||
System.out.println(result);
|
||||
|
||||
Bindings bindings = engine.createBindings();
|
||||
bindings.put("count", 3);
|
||||
bindings.put("name", "baeldung");
|
||||
|
||||
String script = "var greeting='Hello ';" +
|
||||
"for(var i=count;i>0;i--) { " +
|
||||
"greeting+=name + ' '" +
|
||||
"}" +
|
||||
"greeting";
|
||||
|
||||
Object bindingsResult = engine.eval(script, bindings);
|
||||
System.out.println(bindingsResult);
|
||||
|
||||
engine.eval("function composeGreeting(name) {" +
|
||||
"return 'Hello ' + name" +
|
||||
"}");
|
||||
Invocable invocable = (Invocable) engine;
|
||||
|
||||
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
||||
System.out.println(funcResult);
|
||||
|
||||
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" +
|
||||
"var map = new HashMap();" +
|
||||
"map.put('hello', 'world');" +
|
||||
"map");
|
||||
|
||||
System.out.println(map);
|
||||
}
|
||||
}
|
||||
15
core-java/src/main/resources/js/bind.js
Normal file
15
core-java/src/main/resources/js/bind.js
Normal file
@@ -0,0 +1,15 @@
|
||||
var first = {
|
||||
name: "Whiskey",
|
||||
age: 5
|
||||
};
|
||||
|
||||
var second = {
|
||||
volume: 100
|
||||
};
|
||||
|
||||
Object.bindProperties(first, second);
|
||||
|
||||
print(first.volume);
|
||||
|
||||
second.volume = 1000;
|
||||
print(first.volume);
|
||||
1
core-java/src/main/resources/js/locations.js
Normal file
1
core-java/src/main/resources/js/locations.js
Normal file
@@ -0,0 +1 @@
|
||||
print(__FILE__, __LINE__, __DIR__);
|
||||
19
core-java/src/main/resources/js/math_module.js
Normal file
19
core-java/src/main/resources/js/math_module.js
Normal file
@@ -0,0 +1,19 @@
|
||||
var math = {
|
||||
increment: function (num) {
|
||||
return ++num;
|
||||
},
|
||||
|
||||
failFunc: function () {
|
||||
try {
|
||||
throw "BOOM";
|
||||
} catch (e if typeof e === 'string') {
|
||||
print("String thrown: " + e);
|
||||
}
|
||||
catch (e) {
|
||||
print("this shouldn't happen!");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
math;
|
||||
11
core-java/src/main/resources/js/no_such.js
Normal file
11
core-java/src/main/resources/js/no_such.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var demo = {
|
||||
__noSuchProperty__: function (propName) {
|
||||
print("Accessed non-existing property: " + propName);
|
||||
},
|
||||
|
||||
__noSuchMethod__: function (methodName) {
|
||||
print("Invoked non-existing method: " + methodName);
|
||||
}
|
||||
};
|
||||
|
||||
demo;
|
||||
1
core-java/src/main/resources/js/script.js
Normal file
1
core-java/src/main/resources/js/script.js
Normal file
@@ -0,0 +1 @@
|
||||
function increment(num) ++num;
|
||||
2
core-java/src/main/resources/js/trim.js
Normal file
2
core-java/src/main/resources/js/trim.js
Normal file
@@ -0,0 +1,2 @@
|
||||
print(" hello world".trimLeft());
|
||||
print("hello world ".trimRight());
|
||||
9
core-java/src/main/resources/js/typed_arrays.js
Normal file
9
core-java/src/main/resources/js/typed_arrays.js
Normal file
@@ -0,0 +1,9 @@
|
||||
function arrays(arr) {
|
||||
|
||||
var javaIntArray = Java.to(arr, "int[]");
|
||||
print(javaIntArray[0]);
|
||||
print(javaIntArray[1]);
|
||||
print(javaIntArray[2]);
|
||||
}
|
||||
|
||||
arrays([100, "1654", true]);
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import com.lmax.disruptor.BusySpinWaitStrategy;
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
import com.lmax.disruptor.WaitStrategy;
|
||||
import com.lmax.disruptor.dsl.Disruptor;
|
||||
import com.lmax.disruptor.dsl.ProducerType;
|
||||
import com.lmax.disruptor.util.DaemonThreadFactory;
|
||||
|
||||
public class DisruptorTest {
|
||||
private Disruptor<ValueEvent> disruptor;
|
||||
private WaitStrategy waitStrategy;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
waitStrategy = new BusySpinWaitStrategy();
|
||||
}
|
||||
|
||||
private void createDisruptor(final ProducerType producerType, final EventConsumer eventConsumer) {
|
||||
final ThreadFactory threadFactory = DaemonThreadFactory.INSTANCE;
|
||||
disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, 16, threadFactory, producerType, waitStrategy);
|
||||
disruptor.handleEventsWith(eventConsumer.getEventHandler());
|
||||
}
|
||||
|
||||
private void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count, final EventProducer eventProducer) {
|
||||
eventProducer.startProducing(ringBuffer, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleProducerSingleConsumer_thenOutputInFifoOrder() {
|
||||
final EventConsumer eventConsumer = new SingleEventPrintConsumer();
|
||||
final EventProducer eventProducer = new DelayedMultiEventProducer();
|
||||
createDisruptor(ProducerType.MULTI, eventConsumer);
|
||||
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
|
||||
|
||||
startProducing(ringBuffer, 32, eventProducer);
|
||||
|
||||
disruptor.halt();
|
||||
disruptor.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSingleProducerSingleConsumer_thenOutputInFifoOrder() {
|
||||
final EventConsumer eventConsumer = new SingleEventConsumer();
|
||||
final EventProducer eventProducer = new SingleEventProducer();
|
||||
createDisruptor(ProducerType.SINGLE, eventConsumer);
|
||||
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
|
||||
|
||||
startProducing(ringBuffer, 32, eventProducer);
|
||||
|
||||
disruptor.halt();
|
||||
disruptor.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSingleProducerMultipleConsumer_thenOutputInFifoOrder() {
|
||||
final EventConsumer eventConsumer = new MultiEventConsumer();
|
||||
final EventProducer eventProducer = new SingleEventProducer();
|
||||
createDisruptor(ProducerType.SINGLE, eventConsumer);
|
||||
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
|
||||
|
||||
startProducing(ringBuffer, 32, eventProducer);
|
||||
|
||||
disruptor.halt();
|
||||
disruptor.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleProducerMultipleConsumer_thenOutputInFifoOrder() {
|
||||
final EventConsumer eventConsumer = new MultiEventPrintConsumer();
|
||||
final EventProducer eventProducer = new DelayedMultiEventProducer();
|
||||
createDisruptor(ProducerType.MULTI, eventConsumer);
|
||||
final RingBuffer<ValueEvent> ringBuffer = disruptor.start();
|
||||
|
||||
startProducing(ringBuffer, 32, eventProducer);
|
||||
|
||||
disruptor.halt();
|
||||
disruptor.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
public class MultiEventConsumer implements EventConsumer {
|
||||
|
||||
private int expectedValue = -1;
|
||||
private int otherExpectedValue = -1;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> assertExpectedValue(event.getValue());
|
||||
final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> assertOtherExpectedValue(event.getValue());
|
||||
return new EventHandler[] { eventHandler, otherEventHandler };
|
||||
}
|
||||
|
||||
private void assertExpectedValue(final int id) {
|
||||
assertEquals(++expectedValue, id);
|
||||
}
|
||||
|
||||
private void assertOtherExpectedValue(final int id) {
|
||||
assertEquals(++otherExpectedValue, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.disruptor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
public class SingleEventConsumer implements EventConsumer {
|
||||
|
||||
private int expectedValue = -1;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> assertExpectedValue(event.getValue());
|
||||
return new EventHandler[] { eventHandler };
|
||||
}
|
||||
|
||||
private void assertExpectedValue(final int id) {
|
||||
assertEquals(++expectedValue, id);
|
||||
}
|
||||
}
|
||||
122
core-java/src/test/java/com/baeldung/guava/GuavaBiMapTest.java
Normal file
122
core-java/src/test/java/com/baeldung/guava/GuavaBiMapTest.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package com.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.EnumHashBiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
|
||||
public class GuavaBiMapTest {
|
||||
@Test
|
||||
public void whenQueryByValue_shouldReturnKey() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
|
||||
final String countryHeadName = capitalCountryBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("New Delhi", countryHeadName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateBiMapFromExistingMap_shouldReturnKey() {
|
||||
final Map<String, String> personCountryHeadMap = new HashMap<>();
|
||||
personCountryHeadMap.put("New Delhi", "India");
|
||||
personCountryHeadMap.put("Washingon, D.C.", "USA");
|
||||
personCountryHeadMap.put("Moscow", "Russia");
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create(personCountryHeadMap);
|
||||
|
||||
final String countryHeadName = capitalCountryBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("New Delhi", countryHeadName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQueryByKey_shouldReturnValue() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
|
||||
assertEquals("USA", capitalCountryBiMap.get("Washingon, D.C."));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenSameValueIsBeingPresent_shouldThrowException() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
capitalCountryBiMap.put("Trump", "USA");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameValueIsBeingPresent_whenForcePutIsUsed_shouldCompleteSuccessfully() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
capitalCountryBiMap.forcePut("Trump", "USA");
|
||||
|
||||
assertEquals("USA", capitalCountryBiMap.get("Trump"));
|
||||
assertEquals("Trump", capitalCountryBiMap.inverse().get("USA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSameKeyIsBeingPresent_shouldReplaceAlreadyPresent() {
|
||||
final BiMap<String, String> capitalCountryBiMap = HashBiMap.create();
|
||||
|
||||
capitalCountryBiMap.put("New Delhi", "India");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "USA");
|
||||
capitalCountryBiMap.put("Moscow", "Russia");
|
||||
capitalCountryBiMap.put("Washingon, D.C.", "HongKong");
|
||||
|
||||
assertEquals("HongKong", capitalCountryBiMap.get("Washingon, D.C."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingImmutableBiMap_shouldAllowPutSuccessfully() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
assertEquals("USA", capitalCountryBiMap.get("Washingon, D.C."));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowRemove() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
capitalCountryBiMap.remove("New Delhi");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowPut() {
|
||||
final BiMap<String, String> capitalCountryBiMap = new ImmutableBiMap.Builder<String, String>().put("New Delhi", "India").put("Washingon, D.C.", "USA").put("Moscow", "Russia").build();
|
||||
|
||||
capitalCountryBiMap.put("New York", "USA");
|
||||
}
|
||||
|
||||
private enum Operation {
|
||||
ADD, SUBTRACT, MULTIPLY, DIVIDE
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEnumAsKeyInMap_shouldReplaceAlreadyPresent() {
|
||||
final BiMap<Operation, String> operationStringBiMap = EnumHashBiMap.create(Operation.class);
|
||||
|
||||
operationStringBiMap.put(Operation.ADD, "Add");
|
||||
operationStringBiMap.put(Operation.SUBTRACT, "Subtract");
|
||||
operationStringBiMap.put(Operation.MULTIPLY, "Multiply");
|
||||
operationStringBiMap.put(Operation.DIVIDE, "Divide");
|
||||
|
||||
assertEquals("Divide", operationStringBiMap.get(Operation.DIVIDE));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.java.conversion;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
|
||||
public class IterableStreamConversionTest {
|
||||
|
||||
@Test
|
||||
public void givenIterable_whenConvertedToStream_thenNotNull() {
|
||||
Iterable<String> iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream");
|
||||
|
||||
Assert.assertNotNull(StreamSupport.stream(iterable.spliterator(), false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToList_thenCorrect() {
|
||||
Iterable<String> iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream");
|
||||
|
||||
List<String> result = StreamSupport.stream(iterable.spliterator(), false)
|
||||
.map(String::toUpperCase).collect(Collectors.toList());
|
||||
|
||||
assertThat(result, contains("TESTING", "ITERABLE", "CONVERSION", "TO", "STREAM"));
|
||||
}
|
||||
}
|
||||
2
core-java/src/test/java/com/baeldung/java/map/README.md
Normal file
2
core-java/src/test/java/com/baeldung/java/map/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap)
|
||||
@@ -1,3 +1,11 @@
|
||||
### Relevant Articles:
|
||||
- [Introduction to the Java NIO2 File API](http://www.baeldung.com/java-nio-2-file-api)
|
||||
- [Java NIO2 Path API](http://www.baeldung.com/java-nio-2-path)
|
||||
- [A Guide To NIO2 Asynchronous File Channel](http://www.baeldung.com/java-nio2-async-file-channel)
|
||||
- [Guide to Selenium with JUnit / TestNG](http://www.baeldung.com/java-selenium-with-junit-and-testng)
|
||||
- [A Guide to NIO2 Asynchronous Socket Channel](http://www.baeldung.com/java-nio2-async-socket-channel)
|
||||
- [A Guide To NIO2 FileVisitor](http://www.baeldung.com/java-nio2-file-visitor)
|
||||
- [A Guide To NIO2 File Attribute APIs](http://www.baeldung.com/java-nio2-file-attribute)
|
||||
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
|
||||
- [A Guide to WatchService in Java NIO2](http://www.baeldung.com/java-nio2-watchservice)
|
||||
- [Guide to Java NIO2 Asynchronous Channel APIs](http://www.baeldung.com/java-nio-2-async-channels)
|
||||
|
||||
@@ -7,8 +7,10 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class Java8FindAnyFindFirstTest {
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.java8.lambda.exceptions;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.baeldung.java8.lambda.exceptions.LambdaExceptionWrappers.*;
|
||||
|
||||
public class LambdaExceptionWrappersTest {
|
||||
|
||||
private List<Integer> integers;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
integers = Arrays.asList(3, 9, 7, 0, 10, 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromLambdaWrapper_thenSuccess() {
|
||||
integers.forEach(lambdaWrapper(i -> System.out.println(50 / i)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(consumerWrapper(i -> System.out.println(50 / i), ArithmeticException.class));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void whenExceptionFromThrowingConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(throwingConsumerWrapper(i -> writeToFile(i)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoExceptionFromHandlingConsumerWrapper_thenSuccess() {
|
||||
integers.forEach(handlingConsumerWrapper(i -> writeToFile(i), IOException.class));
|
||||
}
|
||||
|
||||
private void writeToFile(Integer i) throws IOException {
|
||||
if (i == 0) {
|
||||
throw new IOException(); // mock IOException
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Guide To Java 8 Optional](http://www.baeldung.com/java-optional)
|
||||
111
core-java/src/test/java/com/baeldung/scripting/NashornTest.java
Normal file
111
core-java/src/test/java/com/baeldung/scripting/NashornTest.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.scripting;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.*;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NashornTest {
|
||||
|
||||
private ScriptEngine engine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trim() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/trim.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void locations() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/locations.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindProperties() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/bind.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void magicMethods() throws ScriptException {
|
||||
engine.eval("var demo = load('classpath:js/no_such.js');" + "var tmp = demo.doesNotExist;" + "var none = demo.callNonExistingMethod()");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typedArrays() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/typed_arrays.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicUsage() throws ScriptException {
|
||||
Object result = engine.eval("var greeting='hello world';" + "print(greeting);" + "greeting");
|
||||
|
||||
Assert.assertEquals("hello world", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonObjectExample() throws ScriptException {
|
||||
Object obj = engine.eval("Java.asJSONCompatible({ number: 42, greet: 'hello', primes: [2,3,5,7,11,13] })");
|
||||
Map<String, Object> map = (Map<String, Object>) obj;
|
||||
|
||||
Assert.assertEquals("hello", map.get("greet"));
|
||||
Assert.assertTrue(List.class.isAssignableFrom(map
|
||||
.get("primes")
|
||||
.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tryCatchGuard() throws ScriptException {
|
||||
engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.failFunc();");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionsExamples() throws ScriptException {
|
||||
String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" + "for each (var i in list) {" + "result+=i+'-';" + "};" + "print(result);";
|
||||
engine.eval(script);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindingsExamples() throws ScriptException {
|
||||
Bindings bindings = engine.createBindings();
|
||||
bindings.put("count", 3);
|
||||
bindings.put("name", "baeldung");
|
||||
|
||||
String script = "var greeting='Hello ';" + "for(var i=count;i>0;i--) { " + "greeting+=name + ' '" + "}" + "greeting";
|
||||
|
||||
Object bindingsResult = engine.eval(script, bindings);
|
||||
Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException {
|
||||
engine.eval("function composeGreeting(name) {" + "return 'Hello ' + name" + "}");
|
||||
|
||||
Invocable invocable = (Invocable) engine;
|
||||
|
||||
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
||||
Assert.assertEquals("Hello baeldung", funcResult);
|
||||
|
||||
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" + "var map = new HashMap();" + "map.put('hello', 'world');" + "map");
|
||||
|
||||
Assert.assertTrue(Map.class.isAssignableFrom(map.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadExamples() throws ScriptException {
|
||||
Object loadResult = engine.eval("load('classpath:js/script.js');" + "increment(5)");
|
||||
|
||||
Assert.assertEquals(6.0, loadResult);
|
||||
|
||||
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.increment(5);");
|
||||
|
||||
Assert.assertEquals(6.0, math);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split)
|
||||
- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets)
|
||||
@@ -1,38 +1,26 @@
|
||||
package org.baeldung.java.io;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class JavaInputStreamToXUnitTest {
|
||||
@@ -163,7 +151,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
// tests - InputStream to File
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJava_whenConvertingAnFullInputStreamToAFile_thenCorrect() throws IOException {
|
||||
public final void whenConvertingToFile_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt"));
|
||||
final byte[] buffer = new byte[initialStream.available()];
|
||||
initialStream.read(buffer);
|
||||
@@ -177,7 +165,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJava_whenConvertingAnInProgressInputStreamToAFile_thenCorrect() throws IOException {
|
||||
public final void whenConvertingInProgressToFile_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt"));
|
||||
final File targetFile = new File("src/main/resources/targetFile.tmp");
|
||||
final OutputStream outStream = new FileOutputStream(targetFile);
|
||||
@@ -193,7 +181,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJava8_whenConvertingAnInProgressInputStreamToAFile_thenCorrect() throws IOException {
|
||||
public final void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException {
|
||||
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt"));
|
||||
final File targetFile = new File("src/main/resources/targetFile.tmp");
|
||||
|
||||
@@ -203,7 +191,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingAnInputStreamToAFile_thenCorrect() throws IOException {
|
||||
public final void whenConvertingInputStreamToFile_thenCorrect3() throws IOException {
|
||||
final InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt"));
|
||||
final byte[] buffer = new byte[initialStream.available()];
|
||||
initialStream.read(buffer);
|
||||
@@ -215,7 +203,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAFile_thenCorrect() throws IOException {
|
||||
public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException {
|
||||
final InputStream initialStream = FileUtils.openInputStream(new File("src/main/resources/sample.txt"));
|
||||
|
||||
final File targetFile = new File("src/main/resources/targetFile.tmp");
|
||||
|
||||
Reference in New Issue
Block a user