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:
committed by
Ilkka Seppälä
parent
160b737dcc
commit
50467c9e76
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user