Java 11 migration: patterns (t-v) (#1085)
* Moves visitor pattern to java 11 * Moves value-object pattern to java 11 * Moves unit-of-work pattern to java 11 * Moves typeobjectpattern pattern to java 11 * Moves twin pattern to java 11 * Moves trampoline pattern to java 11 * Moves tolerant-reader pattern to java 11 * Moves tls pattern to java 11 * Moves throttling pattern to java 11 * Moves thread-pool pattern to java 11 * Moves template-method pattern to java 11
This commit is contained in:
parent
160b737dcc
commit
50467c9e76
@ -26,15 +26,12 @@ package com.iluwatar.templatemethod;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.templatemethod;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/29/15 - 18:15 PM
|
||||
*
|
||||
@ -41,8 +41,8 @@ public class HalflingThiefTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSteal() {
|
||||
final StealingMethod method = mock(StealingMethod.class);
|
||||
final HalflingThief thief = new HalflingThief(method);
|
||||
final var method = mock(StealingMethod.class);
|
||||
final var thief = new HalflingThief(method);
|
||||
|
||||
thief.steal();
|
||||
verify(method).steal();
|
||||
@ -55,13 +55,13 @@ public class HalflingThiefTest {
|
||||
*/
|
||||
@Test
|
||||
public void testChangeMethod() {
|
||||
final StealingMethod initialMethod = mock(StealingMethod.class);
|
||||
final HalflingThief thief = new HalflingThief(initialMethod);
|
||||
final var initialMethod = mock(StealingMethod.class);
|
||||
final var thief = new HalflingThief(initialMethod);
|
||||
|
||||
thief.steal();
|
||||
verify(initialMethod).steal();
|
||||
|
||||
final StealingMethod newMethod = mock(StealingMethod.class);
|
||||
final var newMethod = mock(StealingMethod.class);
|
||||
thief.changeMethod(newMethod);
|
||||
|
||||
thief.steal();
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
package com.iluwatar.templatemethod;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
@ -33,11 +36,9 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:12 PM
|
||||
*
|
||||
* @param <M> Type of StealingMethod
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
|
@ -56,7 +56,7 @@ public class App {
|
||||
LOGGER.info("Program started");
|
||||
|
||||
// Create a list of tasks to be executed
|
||||
List<Task> tasks = List.of(
|
||||
var tasks = List.of(
|
||||
new PotatoPeelingTask(3),
|
||||
new PotatoPeelingTask(6),
|
||||
new CoffeeMakingTask(2),
|
||||
@ -82,10 +82,7 @@ public class App {
|
||||
// Allocate new worker for each task
|
||||
// The worker is executed when a thread becomes
|
||||
// available in the thread pool
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
var worker = new Worker(tasks.get(i));
|
||||
executor.execute(worker);
|
||||
}
|
||||
tasks.stream().map(Worker::new).forEach(executor::execute);
|
||||
// All tasks were executed, now shutdown
|
||||
executor.shutdown();
|
||||
while (!executor.isTerminated()) {
|
||||
|
@ -27,15 +27,13 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*
|
||||
* @author ilkka
|
||||
*
|
||||
* @author ilkka
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,27 +23,25 @@
|
||||
|
||||
package com.iluwatar.threadpool;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:22 PM
|
||||
* Test for Tasks using a Thread Pool
|
||||
* Date: 12/30/15 - 18:22 PM Test for Tasks using a Thread Pool
|
||||
*
|
||||
* @param <T> Type of Task
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@ -87,14 +85,13 @@ public abstract class TaskTest<T extends Task> {
|
||||
@Test
|
||||
public void testIdGeneration() throws Exception {
|
||||
assertTimeout(ofMillis(10000), () -> {
|
||||
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
|
||||
final var service = Executors.newFixedThreadPool(THREAD_COUNT);
|
||||
|
||||
final List<Callable<Integer>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < TASK_COUNT; i++) {
|
||||
tasks.add(() -> factory.apply(1).getId());
|
||||
}
|
||||
final var tasks = IntStream.range(0, TASK_COUNT)
|
||||
.<Callable<Integer>>mapToObj(i -> () -> factory.apply(1).getId())
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
final List<Integer> ids = service.invokeAll(tasks)
|
||||
final var ids = service.invokeAll(tasks)
|
||||
.stream()
|
||||
.map(TaskTest::get)
|
||||
.filter(Objects::nonNull)
|
||||
@ -102,7 +99,7 @@ public abstract class TaskTest<T extends Task> {
|
||||
|
||||
service.shutdownNow();
|
||||
|
||||
final long uniqueIdCount = ids.stream()
|
||||
final var uniqueIdCount = ids.stream()
|
||||
.distinct()
|
||||
.count();
|
||||
|
||||
@ -117,7 +114,7 @@ public abstract class TaskTest<T extends Task> {
|
||||
*/
|
||||
@Test
|
||||
public void testTimeMs() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
assertEquals(this.expectedExecutionTime * i, this.factory.apply(i).getTimeMs());
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,13 @@
|
||||
|
||||
package com.iluwatar.threadpool;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:21 PM
|
||||
*
|
||||
@ -42,8 +42,8 @@ public class WorkerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRun() {
|
||||
final Task task = mock(Task.class);
|
||||
final Worker worker = new Worker(task);
|
||||
final var task = mock(Task.class);
|
||||
final var worker = new Worker(task);
|
||||
verifyZeroInteractions(task);
|
||||
|
||||
worker.run();
|
||||
|
@ -26,6 +26,7 @@ package com.iluwatar.throttling;
|
||||
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -73,14 +74,14 @@ public class App {
|
||||
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
|
||||
var timer = new ThrottleTimerImpl(10, callsCount);
|
||||
var service = new B2BService(timer, callsCount);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
|
||||
IntStream.range(0, 20).forEach(i -> {
|
||||
service.dummyCustomerApi(tenant);
|
||||
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread interrupted: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -73,8 +73,6 @@ public final class CallsCount {
|
||||
*/
|
||||
public void reset() {
|
||||
LOGGER.debug("Resetting the map.");
|
||||
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
|
||||
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
|
||||
}
|
||||
tenantCallsCount.replaceAll((k, v) -> new AtomicLong(0));
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
final String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.throttling;
|
||||
|
||||
import com.iluwatar.throttling.timer.Throttler;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.iluwatar.throttling.timer.Throttler;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* B2BServiceTest class to test the B2BService
|
||||
*/
|
||||
@ -38,15 +38,14 @@ public class B2BServiceTest {
|
||||
|
||||
@Test
|
||||
public void dummyCustomerApiTest() {
|
||||
Tenant tenant = new Tenant("testTenant", 2, callsCount);
|
||||
var tenant = new Tenant("testTenant", 2, callsCount);
|
||||
// In order to assure that throttling limits will not be reset, we use an empty throttling implementation
|
||||
Throttler timer = () -> { };
|
||||
B2BService service = new B2BService(timer, callsCount);
|
||||
var timer = (Throttler) () -> {
|
||||
};
|
||||
var service = new B2BService(timer, callsCount);
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
service.dummyCustomerApi(tenant);
|
||||
}
|
||||
long counter = callsCount.getCount(tenant.getName());
|
||||
IntStream.range(0, 5).mapToObj(i -> tenant).forEach(service::dummyCustomerApi);
|
||||
var counter = callsCount.getCount(tenant.getName());
|
||||
assertEquals(2, counter, "Counter limit must be reached");
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +23,10 @@
|
||||
|
||||
package com.iluwatar.throttling;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* TenantTest to test the creation of Tenant with valid parameters.
|
||||
@ -37,7 +36,7 @@ public class TenantTest {
|
||||
@Test
|
||||
public void constructorTest() {
|
||||
assertThrows(InvalidParameterException.class, () -> {
|
||||
Tenant tenant = new Tenant("FailTenant", -1, new CallsCount());
|
||||
new Tenant("FailTenant", -1, new CallsCount());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -24,10 +24,7 @@
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -71,20 +68,20 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int counterDateValues = 0;
|
||||
int counterExceptions = 0;
|
||||
var counterDateValues = 0;
|
||||
var counterExceptions = 0;
|
||||
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
var callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start 4 threads, each using the same Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
var executor = Executors.newCachedThreadPool();
|
||||
|
||||
Future<Result> futureResult1 = executor.submit(callableDf);
|
||||
Future<Result> futureResult2 = executor.submit(callableDf);
|
||||
Future<Result> futureResult3 = executor.submit(callableDf);
|
||||
Future<Result> futureResult4 = executor.submit(callableDf);
|
||||
var futureResult1 = executor.submit(callableDf);
|
||||
var futureResult2 = executor.submit(callableDf);
|
||||
var futureResult3 = executor.submit(callableDf);
|
||||
var futureResult4 = executor.submit(callableDf);
|
||||
try {
|
||||
Result[] result = new Result[4];
|
||||
var result = new Result[4];
|
||||
result[0] = futureResult1.get();
|
||||
result[1] = futureResult2.get();
|
||||
result[2] = futureResult3.get();
|
||||
@ -92,9 +89,9 @@ public class App {
|
||||
|
||||
// Print results of thread executions (converted dates and raised exceptions)
|
||||
// and count them
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
counterDateValues = counterDateValues + printAndCountDates(result[i]);
|
||||
counterExceptions = counterExceptions + printAndCountExceptions(result[i]);
|
||||
for (var value : result) {
|
||||
counterDateValues = counterDateValues + printAndCountDates(value);
|
||||
counterExceptions = counterExceptions + printAndCountExceptions(value);
|
||||
}
|
||||
|
||||
// a correct run should deliver 20 times 15.12.2015
|
||||
@ -115,15 +112,16 @@ public class App {
|
||||
*/
|
||||
private static int printAndCountDates(Result res) {
|
||||
// a correct run should deliver 5 times 15.12.2015 per each thread
|
||||
int counter = 0;
|
||||
for (Date dt : res.getDateList()) {
|
||||
var counter = 0;
|
||||
for (var dt : res.getDateList()) {
|
||||
counter++;
|
||||
Calendar cal = Calendar.getInstance();
|
||||
var cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
// Formatted output of the date value: DD.MM.YYYY
|
||||
LOGGER.info(
|
||||
cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal
|
||||
.get(Calendar.YEAR));
|
||||
LOGGER.info(cal.get(Calendar.DAY_OF_MONTH) + "."
|
||||
+ cal.get(Calendar.MONTH) + "."
|
||||
+ cal.get(Calendar.YEAR)
|
||||
);
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
@ -136,8 +134,8 @@ public class App {
|
||||
*/
|
||||
private static int printAndCountExceptions(Result res) {
|
||||
// a correct run shouldn't deliver any exception
|
||||
int counter = 0;
|
||||
for (String ex : res.getExceptionList()) {
|
||||
var counter = 0;
|
||||
for (var ex : res.getExceptionList()) {
|
||||
counter++;
|
||||
LOGGER.info(ex);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ package com.iluwatar.tls;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.IntStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -58,13 +59,10 @@ public class DateFormatCallable implements Callable<Result> {
|
||||
* @param inDateValue string date value, e.g. "21/06/2016"
|
||||
*/
|
||||
public DateFormatCallable(String inDateFormat, String inDateValue) {
|
||||
final String idf = inDateFormat; //TLTL
|
||||
this.df = new ThreadLocal<DateFormat>() { //TLTL
|
||||
@Override //TLTL
|
||||
protected DateFormat initialValue() { //TLTL
|
||||
return new SimpleDateFormat(idf); //TLTL
|
||||
} //TLTL
|
||||
}; //TLTL
|
||||
final var idf = inDateFormat; //TLTL
|
||||
this.df = ThreadLocal.withInitial(() -> { //TLTL
|
||||
return new SimpleDateFormat(idf); //TLTL
|
||||
}); //TLTL
|
||||
// this.df = new SimpleDateFormat(inDateFormat); //NTLNTL
|
||||
this.dateValue = inDateValue;
|
||||
}
|
||||
@ -72,10 +70,10 @@ public class DateFormatCallable implements Callable<Result> {
|
||||
@Override
|
||||
public Result call() {
|
||||
LOGGER.info(Thread.currentThread() + " started executing...");
|
||||
Result result = new Result();
|
||||
var result = new Result();
|
||||
|
||||
// Convert date value to date 5 times
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
IntStream.rangeClosed(1, 5).forEach(i -> {
|
||||
try {
|
||||
// this is the statement where it is important to have the
|
||||
// instance of SimpleDateFormat locally
|
||||
@ -86,8 +84,7 @@ public class DateFormatCallable implements Callable<Result> {
|
||||
// write the Exception to a list and continue work
|
||||
result.getExceptionList().add(e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
LOGGER.info(Thread.currentThread() + " finished processing part of the thread");
|
||||
|
||||
|
@ -39,11 +39,11 @@ import java.util.List;
|
||||
*/
|
||||
public class Result {
|
||||
// A list to collect the date values created in one thread
|
||||
private List<Date> dateList = new ArrayList<Date>();
|
||||
private List<Date> dateList = new ArrayList<>();
|
||||
|
||||
// A list to collect Exceptions thrown in one threads (should be none in
|
||||
// this example)
|
||||
private List<String> exceptionList = new ArrayList<String>();
|
||||
private List<String> exceptionList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Get list of date values collected within a thread execution.
|
||||
|
@ -27,14 +27,12 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests that thread local storage example runs without errors.
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,65 +23,62 @@
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
|
||||
* <p>
|
||||
* After a successful run 5 date values should be in the result object. All dates should have
|
||||
* the same value (15.11.2015). To avoid problems with time zone not the date instances themselves
|
||||
* are compared by the test. For the test the dates are converted into string format DD.MM.YYY
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency
|
||||
* situation)
|
||||
* <p>
|
||||
* Additionally the number of list entries are tested for both the list with the date values
|
||||
* and the list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
* After a successful run 5 date values should be in the result object. All dates should have the
|
||||
* same value (15.11.2015). To avoid problems with time zone not the date instances themselves are
|
||||
* compared by the test. For the test the dates are converted into string format DD.MM.YYY
|
||||
* <p>
|
||||
* Additionally the number of list entries are tested for both the list with the date values and the
|
||||
* list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*/
|
||||
public class DateFormatCallableTest {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
* Result object given back by DateFormatCallable -- Array with converted date values -- Array
|
||||
* with thrown exceptions
|
||||
*/
|
||||
static Result result;
|
||||
private static Result result;
|
||||
|
||||
/**
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup()
|
||||
* method
|
||||
*/
|
||||
static List<String> createdDateValues = new ArrayList<String>();
|
||||
private static List<String> createdDateValues = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
int expectedCounterDateValues = 5;
|
||||
private int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
int expectedCounterExceptions = 0;
|
||||
private int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by the run of DateFormatRunnalbe
|
||||
* Expected content of the list containing the date values created by the run of
|
||||
* DateFormatRunnalbe
|
||||
*/
|
||||
List<String> expectedDateValues = List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
private List<String> expectedDateValues =
|
||||
List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
@ -89,10 +86,10 @@ public class DateFormatCallableTest {
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
var callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult = executor.submit(callableDf);
|
||||
var executor = Executors.newCachedThreadPool();
|
||||
var futureResult = executor.submit(callableDf);
|
||||
try {
|
||||
result = futureResult.get();
|
||||
createdDateValues = convertDatesToString(result);
|
||||
@ -107,18 +104,22 @@ public class DateFormatCallableTest {
|
||||
if (res == null || res.getDateList() == null || res.getDateList().size() == 0) {
|
||||
return null;
|
||||
}
|
||||
List<String> returnList = new ArrayList<String>();
|
||||
var returnList = new ArrayList<String>();
|
||||
|
||||
for (Date dt : res.getDateList()) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
for (var dt : res.getDateList()) {
|
||||
var cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "."
|
||||
+ cal.get(Calendar.MONTH) + "."
|
||||
+ cal.get(Calendar.YEAR)
|
||||
);
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times 15.12.2015
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times
|
||||
* 15.12.2015
|
||||
*/
|
||||
@Test
|
||||
public void testDateValues() {
|
||||
@ -126,7 +127,8 @@ public class DateFormatCallableTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver 5 date values
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver 5 date values
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
@ -134,8 +136,8 @@ public class DateFormatCallableTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should deliver
|
||||
* no exceptions
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver no exceptions
|
||||
*/
|
||||
@Test
|
||||
public void testCounterExceptions() {
|
||||
|
@ -23,63 +23,55 @@
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
|
||||
* <p>
|
||||
* An incorrect formatted date is passed to the Callable
|
||||
* After a successful run 0 date values and 5 exceptions should be in the result object.
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency
|
||||
* situation)
|
||||
* <p>
|
||||
* An incorrect formatted date is passed to the Callable After a successful run 0 date values and 5
|
||||
* exceptions should be in the result object.
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*/
|
||||
public class DateFormatCallableTestIncorrectDateFormat {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
* Result object given back by DateFormatCallable -- Array with converted date values -- Array
|
||||
* with thrown exceptions
|
||||
*/
|
||||
static Result result;
|
||||
|
||||
/**
|
||||
* The date values created by the run of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
*/
|
||||
static List<String> createdExceptions = new ArrayList<String>();
|
||||
private static Result result;
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
int expectedCounterDateValues = 0;
|
||||
private int expectedCounterDateValues = 0;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
int expectedCounterExceptions = 5;
|
||||
private int expectedCounterExceptions = 5;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the exceptions created by the run of DateFormatRunnalbe
|
||||
* Expected content of the list containing the exceptions created by the run of
|
||||
* DateFormatRunnalbe
|
||||
*/
|
||||
List<String> expectedExceptions = List.of("class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
private List<String> expectedExceptions = List.of(
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"");
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\""
|
||||
);
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
@ -87,10 +79,10 @@ public class DateFormatCallableTestIncorrectDateFormat {
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// Create a callable. Pass a string date value not matching the format string
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15.12.2015");
|
||||
var callableDf = new DateFormatCallable("dd/MM/yyyy", "15.12.2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult = executor.submit(callableDf);
|
||||
var executor = Executors.newCachedThreadPool();
|
||||
var futureResult = executor.submit(callableDf);
|
||||
try {
|
||||
result = futureResult.get();
|
||||
} catch (Exception e) {
|
||||
@ -109,7 +101,8 @@ public class DateFormatCallableTestIncorrectDateFormat {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver no date values
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver no date values
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
@ -117,7 +110,7 @@ public class DateFormatCallableTestIncorrectDateFormat {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver 5 exceptions
|
||||
*/
|
||||
@Test
|
||||
|
@ -23,69 +23,66 @@
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* <p>
|
||||
* In this test {@link DateFormatCallable} is used by 4 threads in parallel
|
||||
* <p>
|
||||
* After a successful run 5 date values should be in the result object of each thread. All dates
|
||||
* After a successful run 5 date values should be in the result object of each thread. All dates
|
||||
* should have the same value (15.11.2015). To avoid problems with time zone not the date instances
|
||||
* themselves are compared by the test. For the test the dates are converted into string format DD.MM.YYY
|
||||
* themselves are compared by the test. For the test the dates are converted into string format
|
||||
* DD.MM.YYY
|
||||
* <p>
|
||||
* Additionally the number of list entries are tested for both the list with the date values
|
||||
* and the list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
* Additionally the number of list entries are tested for both the list with the date values and the
|
||||
* list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*/
|
||||
public class DateFormatCallableTestMultiThread {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable, one for each thread
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
* Result object given back by DateFormatCallable, one for each thread -- Array with converted
|
||||
* date values -- Array with thrown exceptions
|
||||
*/
|
||||
static Result[] result = new Result[4];
|
||||
private static Result[] result = new Result[4];
|
||||
|
||||
/**
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup()
|
||||
* method
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
static class StringArrayList extends ArrayList<String> {
|
||||
private static class StringArrayList extends ArrayList<String> {
|
||||
/* nothing needed here */
|
||||
}
|
||||
static List<String>[] createdDateValues = new StringArrayList[4];
|
||||
|
||||
private static List<String>[] createdDateValues = new StringArrayList[4];
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by each thread
|
||||
*/
|
||||
int expectedCounterDateValues = 5;
|
||||
private int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by each thread
|
||||
* Expected number of exceptions in the exception list created by each thread
|
||||
*/
|
||||
int expectedCounterExceptions = 0;
|
||||
private int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by each thread
|
||||
* Expected content of the list containing the date values created by each thread
|
||||
*/
|
||||
List<String> expectedDateValues = List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
private List<String> expectedDateValues =
|
||||
List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
@ -93,19 +90,19 @@ public class DateFormatCallableTestMultiThread {
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
var callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult1 = executor.submit(callableDf);
|
||||
Future<Result> futureResult2 = executor.submit(callableDf);
|
||||
Future<Result> futureResult3 = executor.submit(callableDf);
|
||||
Future<Result> futureResult4 = executor.submit(callableDf);
|
||||
var executor = Executors.newCachedThreadPool();
|
||||
var futureResult1 = executor.submit(callableDf);
|
||||
var futureResult2 = executor.submit(callableDf);
|
||||
var futureResult3 = executor.submit(callableDf);
|
||||
var futureResult4 = executor.submit(callableDf);
|
||||
try {
|
||||
result[0] = futureResult1.get();
|
||||
result[1] = futureResult2.get();
|
||||
result[2] = futureResult3.get();
|
||||
result[3] = futureResult4.get();
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
for (var i = 0; i < result.length; i++) {
|
||||
createdDateValues[i] = convertDatesToString(result[i]);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -119,46 +116,49 @@ public class DateFormatCallableTestMultiThread {
|
||||
if (res == null || res.getDateList() == null || res.getDateList().size() == 0) {
|
||||
return null;
|
||||
}
|
||||
List<String> returnList = new StringArrayList();
|
||||
var returnList = new StringArrayList();
|
||||
|
||||
for (Date dt : res.getDateList()) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
for (var dt : res.getDateList()) {
|
||||
var cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "."
|
||||
+ cal.get(Calendar.MONTH) + "."
|
||||
+ cal.get(Calendar.YEAR)
|
||||
);
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times 15.12.2015
|
||||
* by each thread
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times
|
||||
* 15.12.2015 by each thread
|
||||
*/
|
||||
@Test
|
||||
public void testDateValues() {
|
||||
for (int i = 0; i < createdDateValues.length; i++) {
|
||||
assertEquals(expectedDateValues, createdDateValues[i]);
|
||||
for (var createdDateValue : createdDateValues) {
|
||||
assertEquals(expectedDateValues, createdDateValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver 5 date values by each thread
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
assertEquals(expectedCounterDateValues, result[i].getDateList().size());
|
||||
for (var value : result) {
|
||||
assertEquals(expectedCounterDateValues, value.getDateList().size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver no exceptions
|
||||
*/
|
||||
@Test
|
||||
public void testCounterExceptions() {
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
assertEquals(expectedCounterExceptions, result[i].getExceptionList().size());
|
||||
for (var value : result) {
|
||||
assertEquals(expectedCounterExceptions, value.getExceptionList().size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,15 +82,18 @@ public final class RainbowFishSerializer {
|
||||
* Read V1 RainbowFish from file.
|
||||
*/
|
||||
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map = null;
|
||||
Map<String, String> map;
|
||||
|
||||
try (var fileIn = new FileInputStream(filename);
|
||||
var objIn = new ObjectInputStream(fileIn)) {
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
}
|
||||
|
||||
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer
|
||||
.parseInt(map.get("lengthMeters")),
|
||||
Integer.parseInt(map.get("weightTons")));
|
||||
return new RainbowFish(
|
||||
map.get("name"),
|
||||
Integer.parseInt(map.get("age")),
|
||||
Integer.parseInt(map.get("lengthMeters")),
|
||||
Integer.parseInt(map.get("weightTons"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -23,32 +23,28 @@
|
||||
|
||||
package com.iluwatar.tolerantreader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
File file1 = new File("fish1.out");
|
||||
var file1 = new File("fish1.out");
|
||||
file1.delete();
|
||||
File file2 = new File("fish2.out");
|
||||
var file2 = new File("fish2.out");
|
||||
file2.delete();
|
||||
}
|
||||
}
|
||||
|
@ -23,16 +23,15 @@
|
||||
|
||||
package com.iluwatar.tolerantreader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
import java.io.File;
|
||||
import org.junit.Rule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:39 PM
|
||||
*
|
||||
@ -62,10 +61,10 @@ public class RainbowFishSerializerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testWriteV1ReadV1() throws Exception {
|
||||
final File outputFile = this.testFolder.newFile();
|
||||
final var outputFile = this.testFolder.newFile();
|
||||
RainbowFishSerializer.writeV1(V1, outputFile.getPath());
|
||||
|
||||
final RainbowFish fish = RainbowFishSerializer.readV1(outputFile.getPath());
|
||||
final var fish = RainbowFishSerializer.readV1(outputFile.getPath());
|
||||
assertNotSame(V1, fish);
|
||||
assertEquals(V1.getName(), fish.getName());
|
||||
assertEquals(V1.getAge(), fish.getAge());
|
||||
@ -79,10 +78,10 @@ public class RainbowFishSerializerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testWriteV2ReadV1() throws Exception {
|
||||
final File outputFile = this.testFolder.newFile();
|
||||
final var outputFile = this.testFolder.newFile();
|
||||
RainbowFishSerializer.writeV2(V2, outputFile.getPath());
|
||||
|
||||
final RainbowFish fish = RainbowFishSerializer.readV1(outputFile.getPath());
|
||||
final var fish = RainbowFishSerializer.readV1(outputFile.getPath());
|
||||
assertNotSame(V2, fish);
|
||||
assertEquals(V2.getName(), fish.getName());
|
||||
assertEquals(V2.getAge(), fish.getAge());
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.tolerantreader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:34 PM
|
||||
*
|
||||
@ -39,7 +39,7 @@ public class RainbowFishTest {
|
||||
*/
|
||||
@Test
|
||||
public void testValues() {
|
||||
final RainbowFish fish = new RainbowFish("name", 1, 2, 3);
|
||||
final var fish = new RainbowFish("name", 1, 2, 3);
|
||||
assertEquals("name", fish.getName());
|
||||
assertEquals(1, fish.getAge());
|
||||
assertEquals(2, fish.getLengthMeters());
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
package com.iluwatar.tolerantreader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:35 PM
|
||||
*
|
||||
@ -41,7 +41,7 @@ public class RainbowFishV2Test {
|
||||
*/
|
||||
@Test
|
||||
public void testValues() {
|
||||
final RainbowFishV2 fish = new RainbowFishV2("name", 1, 2, 3, false, true, false);
|
||||
final var fish = new RainbowFishV2("name", 1, 2, 3, false, true, false);
|
||||
assertEquals("name", fish.getName());
|
||||
assertEquals(1, fish.getAge());
|
||||
assertEquals(2, fish.getLengthMeters());
|
||||
|
@ -99,13 +99,11 @@ public interface Trampoline<T> {
|
||||
}
|
||||
|
||||
T trampoline(final Trampoline<T> trampoline) {
|
||||
|
||||
return Stream.iterate(trampoline, Trampoline::jump)
|
||||
.filter(Trampoline::complete)
|
||||
.findFirst()
|
||||
.get()
|
||||
.result();
|
||||
|
||||
.map(Trampoline::result)
|
||||
.orElseThrow();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class TrampolineApp {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
log.info("start pattern");
|
||||
Integer result = loop(10, 1).result();
|
||||
var result = loop(10, 1).result();
|
||||
log.info("result {}", result);
|
||||
|
||||
}
|
||||
|
@ -23,22 +23,20 @@
|
||||
|
||||
package com.iluwatar.trampoline;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test for trampoline pattern.
|
||||
* */
|
||||
*/
|
||||
public class TrampolineAppTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testTrampolineWithFactorialFunction() {
|
||||
int result = TrampolineApp.loop(10, 1).result();
|
||||
long result = TrampolineApp.loop(10, 1).result();
|
||||
assertEquals("Be equal", 3628800, result);
|
||||
}
|
||||
|
||||
|
@ -26,15 +26,12 @@ package com.iluwatar.twin;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -34,11 +34,10 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
@ -62,27 +61,26 @@ public class BallItemTest {
|
||||
|
||||
@Test
|
||||
public void testClick() {
|
||||
final BallThread ballThread = mock(BallThread.class);
|
||||
final BallItem ballItem = new BallItem();
|
||||
final var ballThread = mock(BallThread.class);
|
||||
final var ballItem = new BallItem();
|
||||
ballItem.setTwin(ballThread);
|
||||
|
||||
final InOrder inOrder = inOrder(ballThread);
|
||||
final var inOrder = inOrder(ballThread);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
IntStream.range(0, 10).forEach(i -> {
|
||||
ballItem.click();
|
||||
inOrder.verify(ballThread).suspendMe();
|
||||
|
||||
ballItem.click();
|
||||
inOrder.verify(ballThread).resumeMe();
|
||||
}
|
||||
});
|
||||
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoDraw() {
|
||||
final BallItem ballItem = new BallItem();
|
||||
final BallThread ballThread = mock(BallThread.class);
|
||||
final var ballItem = new BallItem();
|
||||
final var ballThread = mock(BallThread.class);
|
||||
ballItem.setTwin(ballThread);
|
||||
|
||||
ballItem.draw();
|
||||
@ -95,8 +93,8 @@ public class BallItemTest {
|
||||
|
||||
@Test
|
||||
public void testMove() {
|
||||
final BallItem ballItem = new BallItem();
|
||||
final BallThread ballThread = mock(BallThread.class);
|
||||
final var ballItem = new BallItem();
|
||||
final var ballThread = mock(BallThread.class);
|
||||
ballItem.setTwin(ballThread);
|
||||
|
||||
ballItem.move();
|
||||
|
@ -23,15 +23,19 @@
|
||||
|
||||
package com.iluwatar.twin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.lang.Thread.UncaughtExceptionHandler;
|
||||
import static java.lang.Thread.sleep;
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:55 PM
|
||||
@ -44,11 +48,11 @@ public class BallThreadTest {
|
||||
* Verify if the {@link BallThread} can be resumed
|
||||
*/
|
||||
@Test
|
||||
public void testSuspend() throws Exception {
|
||||
public void testSuspend() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
final var ballThread = new BallThread();
|
||||
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
final var ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
||||
ballThread.start();
|
||||
@ -72,9 +76,9 @@ public class BallThreadTest {
|
||||
@Test
|
||||
public void testResume() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
final var ballThread = new BallThread();
|
||||
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
final var ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
||||
ballThread.suspendMe();
|
||||
@ -102,8 +106,8 @@ public class BallThreadTest {
|
||||
@Test
|
||||
public void testInterrupt() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
final UncaughtExceptionHandler exceptionHandler = mock(UncaughtExceptionHandler.class);
|
||||
final var ballThread = new BallThread();
|
||||
final var exceptionHandler = mock(UncaughtExceptionHandler.class);
|
||||
ballThread.setUncaughtExceptionHandler(exceptionHandler);
|
||||
ballThread.setTwin(mock(BallItem.class));
|
||||
ballThread.start();
|
||||
|
@ -25,6 +25,7 @@ package com.iluwatar.typeobject;
|
||||
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -55,21 +56,17 @@ public class CandyGame {
|
||||
}
|
||||
|
||||
static String numOfSpaces(int num) {
|
||||
String result = "";
|
||||
for (var i = 0; i < num; i++) {
|
||||
result += " ";
|
||||
}
|
||||
return result;
|
||||
return " ".repeat(Math.max(0, num));
|
||||
}
|
||||
|
||||
void printGameStatus() {
|
||||
LOGGER.info("");
|
||||
for (var i = 0; i < cells.length; i++) {
|
||||
for (Cell[] cell : cells) {
|
||||
for (var j = 0; j < cells.length; j++) {
|
||||
var candyName = cells[i][j].candy.name;
|
||||
var candyName = cell[j].candy.name;
|
||||
if (candyName.length() < 20) {
|
||||
var totalSpaces = 20 - candyName.length();
|
||||
LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name
|
||||
LOGGER.info(numOfSpaces(totalSpaces / 2) + cell[j].candy.name
|
||||
+ numOfSpaces(totalSpaces - totalSpaces / 2) + "|");
|
||||
} else {
|
||||
LOGGER.info(candyName + "|");
|
||||
@ -80,8 +77,8 @@ public class CandyGame {
|
||||
LOGGER.info("");
|
||||
}
|
||||
|
||||
ArrayList<Cell> adjacentCells(int y, int x) {
|
||||
ArrayList<Cell> adjacent = new ArrayList<Cell>();
|
||||
List<Cell> adjacentCells(int y, int x) {
|
||||
var adjacent = new ArrayList<Cell>();
|
||||
if (y == 0) {
|
||||
adjacent.add(this.cells[1][x]);
|
||||
}
|
||||
@ -115,8 +112,8 @@ public class CandyGame {
|
||||
for (var j = 0; j < this.cells.length; j++) {
|
||||
if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) {
|
||||
var adj = adjacentCells(i, j);
|
||||
for (var a = 0; a < adj.size(); a++) {
|
||||
if (this.cells[i][j].candy.name.equals(adj.get(a).candy.name)) {
|
||||
for (Cell cell : adj) {
|
||||
if (this.cells[i][j].candy.name.equals(cell.candy.name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -157,11 +154,11 @@ public class CandyGame {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < this.cells.length; i++) {
|
||||
for (Cell[] cell : this.cells) {
|
||||
var j = 0;
|
||||
var points = 0;
|
||||
while (j < cells.length - 1) {
|
||||
points = this.cells[i][j].interact(this.cells[i][j + 1], this.pool, this.cells);
|
||||
points = cell[j].interact(cell[j + 1], this.pool, this.cells);
|
||||
if (points != 0) {
|
||||
handleChange(points);
|
||||
} else {
|
||||
|
@ -27,6 +27,7 @@ import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
@ -38,12 +39,12 @@ import org.json.simple.parser.ParseException;
|
||||
|
||||
public class CellPool {
|
||||
private static final Random RANDOM = new Random();
|
||||
ArrayList<Cell> pool;
|
||||
List<Cell> pool;
|
||||
int pointer;
|
||||
Candy[] randomCode;
|
||||
|
||||
CellPool(int num) {
|
||||
this.pool = new ArrayList<Cell>(num);
|
||||
this.pool = new ArrayList<>(num);
|
||||
try {
|
||||
this.randomCode = assignRandomCandytypes();
|
||||
} catch (Exception e) {
|
||||
|
@ -25,11 +25,11 @@ package com.iluwatar.typeobject;
|
||||
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
@ -43,24 +43,24 @@ public class JsonParser {
|
||||
Hashtable<String, Candy> candies;
|
||||
|
||||
JsonParser() {
|
||||
this.candies = new Hashtable<String, Candy>();
|
||||
this.candies = new Hashtable<>();
|
||||
}
|
||||
|
||||
void parse() throws FileNotFoundException, IOException, ParseException {
|
||||
void parse() throws IOException, ParseException {
|
||||
var parser = new JSONParser();
|
||||
var jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath()
|
||||
+ "\\src\\main\\java\\com\\iluwatar\\typeobject\\candy.json"));
|
||||
var workingDirectory = new File("").getAbsolutePath();
|
||||
var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
|
||||
var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
|
||||
var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
|
||||
var a = (JSONArray) jo.get("candies");
|
||||
for (var o : a) {
|
||||
var candy = (JSONObject) o;
|
||||
var name = (String) candy.get("name");
|
||||
var parentName = (String) candy.get("parent");
|
||||
var t = (String) candy.get("type");
|
||||
Type type = null;
|
||||
var type = Type.crushableCandy;
|
||||
if (t.equals("rewardFruit")) {
|
||||
type = Type.rewardFruit;
|
||||
} else {
|
||||
type = Type.crushableCandy;
|
||||
}
|
||||
var points = Integer.parseInt((String) candy.get("points"));
|
||||
var c = new Candy(name, parentName, type, points);
|
||||
@ -70,7 +70,7 @@ public class JsonParser {
|
||||
}
|
||||
|
||||
void setParentAndPoints() {
|
||||
for (Enumeration<String> e = this.candies.keys(); e.hasMoreElements(); ) {
|
||||
for (var e = this.candies.keys(); e.hasMoreElements(); ) {
|
||||
var c = this.candies.get(e.nextElement());
|
||||
if (c.parentName == null) {
|
||||
c.parent = null;
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CandyGameTest class tests the methods in the {@link CandyGame} class.
|
||||
@ -36,7 +36,7 @@ class CandyGameTest {
|
||||
|
||||
@Test
|
||||
void adjacentCellsTest() {
|
||||
var cg = new CandyGame(3,new CellPool(9));
|
||||
var cg = new CandyGame(3, new CellPool(9));
|
||||
var arr1 = cg.adjacentCells(0, 0);
|
||||
var arr2 = cg.adjacentCells(1, 2);
|
||||
var arr3 = cg.adjacentCells(1, 1);
|
||||
@ -49,19 +49,19 @@ class CandyGameTest {
|
||||
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
|
||||
var c2 = new Candy("purple jelly", "jelly", Type.crushableCandy, 5);
|
||||
var c3 = new Candy("green apple", "apple", Type.rewardFruit, 10);
|
||||
matrix[0][0] = new Cell(c1,0,0);;
|
||||
matrix[0][1] = new Cell(c2,1,0);
|
||||
matrix[1][0] = new Cell(c3,0,1);
|
||||
matrix[1][1] = new Cell(c2,1,1);
|
||||
matrix[0][0] = new Cell(c1, 0, 0);
|
||||
matrix[0][1] = new Cell(c2, 1, 0);
|
||||
matrix[1][0] = new Cell(c3, 0, 1);
|
||||
matrix[1][1] = new Cell(c2, 1, 1);
|
||||
var p = new CellPool(4);
|
||||
var cg = new CandyGame(2,p);
|
||||
var cg = new CandyGame(2, p);
|
||||
cg.cells = matrix;
|
||||
var fruitInLastRow = cg.continueRound();
|
||||
matrix[1][0].crush(p, matrix);
|
||||
matrix[0][0] = new Cell(c3,0,0);
|
||||
var matchingCandy = cg.continueRound();
|
||||
matrix[0][1].crush(p,matrix);
|
||||
matrix[0][1] = new Cell(c3,1,0);
|
||||
matrix[0][0] = new Cell(c3, 0, 0);
|
||||
var matchingCandy = cg.continueRound();
|
||||
matrix[0][1].crush(p, matrix);
|
||||
matrix[0][1] = new Cell(c3, 1, 0);
|
||||
var noneLeft = cg.continueRound();
|
||||
assertTrue(fruitInLastRow && matchingCandy && !noneLeft);
|
||||
}
|
||||
|
@ -23,9 +23,10 @@
|
||||
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CellPoolTest class tests the methods in the {@link CellPool} class.
|
||||
@ -39,9 +40,7 @@ class CellPoolTest {
|
||||
var ht = new Hashtable<String, Boolean>();
|
||||
var parentTypes = 0;
|
||||
for (var i = 0; i < cp.randomCode.length; i++) {
|
||||
if (ht.get(cp.randomCode[i].name) == null) {
|
||||
ht.put(cp.randomCode[i].name, true);
|
||||
}
|
||||
ht.putIfAbsent(cp.randomCode[i].name, true);
|
||||
if (cp.randomCode[i].name.equals("fruit") || cp.randomCode[i].name.equals("candy")) {
|
||||
parentTypes++;
|
||||
}
|
||||
|
@ -23,14 +23,15 @@
|
||||
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CellTest class tests the methods in the {@link Cell} class.
|
||||
*/
|
||||
|
||||
class CellTest {
|
||||
|
||||
@Test
|
||||
@ -38,10 +39,10 @@ class CellTest {
|
||||
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
|
||||
var c2 = new Candy("green apple", "apple", Type.rewardFruit, 10);
|
||||
var matrix = new Cell[4][4];
|
||||
matrix[0][0] = new Cell(c1,0,0);
|
||||
matrix[0][1] = new Cell(c1,1,0);
|
||||
matrix[0][2] = new Cell(c2,2,0);
|
||||
matrix[0][3] = new Cell(c1,3,0);
|
||||
matrix[0][0] = new Cell(c1, 0, 0);
|
||||
matrix[0][1] = new Cell(c1, 1, 0);
|
||||
matrix[0][2] = new Cell(c2, 2, 0);
|
||||
matrix[0][3] = new Cell(c1, 3, 0);
|
||||
var cp = new CellPool(5);
|
||||
var points1 = matrix[0][0].interact(matrix[0][1], cp, matrix);
|
||||
var points2 = matrix[0][2].interact(matrix[0][3], cp, matrix);
|
||||
@ -53,9 +54,9 @@ class CellTest {
|
||||
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
|
||||
var c2 = new Candy("purple candy", "candy", Type.crushableCandy, 5);
|
||||
var matrix = new Cell[4][4];
|
||||
matrix[0][0] = new Cell(c1,0,0);
|
||||
matrix[1][0] = new Cell(c2,0,1);
|
||||
matrix[0][0] = new Cell(c1, 0, 0);
|
||||
matrix[1][0] = new Cell(c2, 0, 1);
|
||||
matrix[1][0].crush(new CellPool(5), matrix);
|
||||
assertTrue(matrix[1][0].candy.name.equals("green jelly"));
|
||||
assertEquals("green jelly", matrix[1][0].candy.name);
|
||||
}
|
||||
}
|
||||
|
@ -25,15 +25,12 @@ package com.iluwatar.unitofwork;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* AppTest
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,20 +23,22 @@
|
||||
|
||||
package com.iluwatar.unitofwork;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* tests {@link StudentRepository}
|
||||
*/
|
||||
@ -51,7 +53,7 @@ public class StudentRepositoryTest {
|
||||
private StudentRepository studentRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
context = new HashMap<>();
|
||||
studentRepository = new StudentRepository(context, studentDatabase);
|
||||
}
|
||||
@ -85,9 +87,9 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldSaveAllLocalChangesToDb() {
|
||||
context.put(IUnitOfWork.INSERT, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.MODIFY, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.DELETE, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@ -116,8 +118,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotInsertToDbIfNoRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.MODIFY, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.DELETE, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@ -126,8 +128,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotModifyToDbIfNotRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.INSERT, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.DELETE, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@ -136,8 +138,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotDeleteFromDbIfNotRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.INSERT, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.MODIFY, Collections.singletonList(student1));
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
|
@ -32,7 +32,6 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,13 @@
|
||||
|
||||
package com.iluwatar.value.object;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit test for HeroStat.
|
||||
*/
|
||||
@ -37,12 +37,10 @@ public class HeroStatTest {
|
||||
|
||||
/**
|
||||
* Tester for equals() and hashCode() methods of a class. Using guava's EqualsTester.
|
||||
*
|
||||
*
|
||||
* @see <a href="http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html">
|
||||
* http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html
|
||||
* </a>
|
||||
*
|
||||
*
|
||||
* http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html
|
||||
* </a>
|
||||
*/
|
||||
@Test
|
||||
public void testEquals() {
|
||||
|
@ -28,22 +28,22 @@ package com.iluwatar.visitor;
|
||||
* can be added without altering the node interface.</p>
|
||||
*
|
||||
* <p>In this example there is a unit hierarchy beginning from {@link Commander}. This hierarchy is
|
||||
* traversed by visitors. {@link SoldierVisitor} applies its operation on {@link Soldier}s,
|
||||
* {@link SergeantVisitor} on {@link Sergeant}s and so on.</p>
|
||||
*
|
||||
* traversed by visitors. {@link SoldierVisitor} applies its operation on {@link Soldier}s, {@link
|
||||
* SergeantVisitor} on {@link Sergeant}s and so on.</p>
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
var commander =
|
||||
new Commander(new Sergeant(new Soldier(), new Soldier(), new Soldier()), new Sergeant(
|
||||
new Soldier(), new Soldier(), new Soldier()));
|
||||
var commander = new Commander(
|
||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
||||
new Sergeant(new Soldier(), new Soldier(), new Soldier())
|
||||
);
|
||||
commander.accept(new SoldierVisitor());
|
||||
commander.accept(new SergeantVisitor());
|
||||
commander.accept(new CommanderVisitor());
|
||||
|
@ -23,6 +23,8 @@
|
||||
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Interface for the nodes in hierarchy.
|
||||
*/
|
||||
@ -38,8 +40,6 @@ public abstract class Unit {
|
||||
* Accept visitor.
|
||||
*/
|
||||
public void accept(UnitVisitor visitor) {
|
||||
for (var child : children) {
|
||||
child.accept(visitor);
|
||||
}
|
||||
Arrays.stream(children).forEach(child -> child.accept(visitor));
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -30,12 +30,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:59 PM.
|
||||
* Test related to Units
|
||||
* Date: 12/30/15 - 18:59 PM. Test related to Units
|
||||
*
|
||||
* @param <U> Type of Unit
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@ -65,9 +64,7 @@ public abstract class UnitTest<U extends Unit> {
|
||||
unit.accept(visitor);
|
||||
verifyVisit(unit, visitor);
|
||||
|
||||
for (final var child : children) {
|
||||
verify(child).accept(eq(visitor));
|
||||
}
|
||||
Arrays.stream(children).forEach(child -> verify(child).accept(eq(visitor)));
|
||||
|
||||
verifyNoMoreInteractions(children);
|
||||
verifyNoMoreInteractions(visitor);
|
||||
|
@ -31,15 +31,14 @@ import ch.qos.logback.core.AppenderBase;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:59 PM.
|
||||
* Test case for Visitor Pattern
|
||||
* Date: 12/30/15 - 18:59 PM. Test case for Visitor Pattern
|
||||
*
|
||||
* @param <V> Type of UnitVisitor
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@ -84,11 +83,12 @@ public abstract class VisitorTest<V extends UnitVisitor> {
|
||||
* @param sergeantResponse The optional expected response when being visited by a sergeant
|
||||
* @param soldierResponse The optional expected response when being visited by a soldier
|
||||
*/
|
||||
public VisitorTest(final V visitor,
|
||||
final Optional<String> commanderResponse,
|
||||
final Optional<String> sergeantResponse,
|
||||
final Optional<String> soldierResponse) {
|
||||
|
||||
public VisitorTest(
|
||||
final V visitor,
|
||||
final Optional<String> commanderResponse,
|
||||
final Optional<String> sergeantResponse,
|
||||
final Optional<String> soldierResponse
|
||||
) {
|
||||
this.visitor = visitor;
|
||||
this.commanderResponse = commanderResponse;
|
||||
this.sergeantResponse = sergeantResponse;
|
||||
|
Loading…
x
Reference in New Issue
Block a user