Skip to content

Commit a8e2c15

Browse files
authored
Add files via upload
Test reworked completely. AppTest seperated.
1 parent 080965f commit a8e2c15

File tree

3 files changed

+435
-0
lines changed

3 files changed

+435
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Thomas Bauer
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.tls;
25+
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.Calendar;
29+
import java.util.Date;
30+
import java.util.List;
31+
import java.util.concurrent.ExecutorService;
32+
import java.util.concurrent.Executors;
33+
import java.util.concurrent.Future;
34+
35+
import org.junit.BeforeClass;
36+
import org.junit.Test;
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.fail;
39+
40+
/**
41+
*
42+
* Test of the Callable
43+
*
44+
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
45+
* <p>
46+
* After a successful run 5 date values should be in the result object. All dates should have
47+
* the same value (15.11.2015). To avoid problems with time zone not the date instances themselves
48+
* are compared by the test. For the test the dates are converted into string format DD.MM.YYY
49+
* <p>
50+
* Additionally the number of list entries are tested for both the list with the date values
51+
* and the list with the exceptions
52+
*
53+
* @author Thomas Bauer, January 2017
54+
*
55+
*/
56+
public class DateFormatRunnableTest {
57+
58+
// Class variables used in setup() have to be static because setup() has to be static
59+
/**
60+
* Result object given back by DateFormatCallable
61+
* -- Array with converted date values
62+
* -- Array with thrown exceptions
63+
*/
64+
static Result result;
65+
66+
/**
67+
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup() method
68+
*/
69+
static List<String> createdDateValues = new ArrayList<String>();
70+
71+
/**
72+
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
73+
*/
74+
int expectedCounterDateValues = 5;
75+
76+
/**
77+
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
78+
*/
79+
int expectedCounterExceptions = 0;
80+
81+
/**
82+
* Expected content of the list containing the date values created by the run of DateFormatRunnalbe
83+
*/
84+
List<String> expectedDateValues = Arrays.asList("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
85+
86+
/**
87+
* Run Callable and prepare results for usage in the test methods
88+
*/
89+
@BeforeClass
90+
public static void setup() {
91+
// Create a callable
92+
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
93+
// start thread using the Callable instance
94+
ExecutorService executor = Executors.newCachedThreadPool();
95+
Future<Result> futureResult = executor.submit(callableDf);
96+
try {
97+
result = futureResult.get();
98+
createdDateValues = convertDatesToString(result);
99+
} catch (Exception e) {
100+
fail("Setup failed: " + e);
101+
}
102+
executor.shutdown();
103+
}
104+
105+
private static List<String> convertDatesToString(Result res) {
106+
// Format date value as DD.MM.YYYY
107+
if (res == null || res.getDateList() == null || res.getDateList().size() == 0) {
108+
return null;
109+
}
110+
List<String> returnList = new ArrayList<String>();
111+
112+
for (Date dt : res.getDateList()) {
113+
Calendar cal = Calendar.getInstance();
114+
cal.setTime(dt);
115+
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
116+
}
117+
return returnList;
118+
}
119+
120+
/**
121+
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times 15.12.2015
122+
*/
123+
@Test
124+
public void testDateValues() {
125+
assertEquals(expectedDateValues, createdDateValues);
126+
}
127+
128+
/**
129+
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver 5 date values
130+
*/
131+
@Test
132+
public void testCounterDateValues() {
133+
assertEquals(expectedCounterDateValues, result.getDateList().size());
134+
}
135+
136+
/**
137+
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should deliver
138+
* no exceptions
139+
*/
140+
@Test
141+
public void testCounterExceptions() {
142+
assertEquals(expectedCounterExceptions, result.getExceptionList().size());
143+
}
144+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Thomas Bauer
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.tls;
25+
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.concurrent.ExecutorService;
30+
import java.util.concurrent.Executors;
31+
import java.util.concurrent.Future;
32+
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.fail;
37+
38+
/**
39+
*
40+
* Test of the Callable
41+
*
42+
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
43+
* <p>
44+
* An incorrect formatted date is passed to the Callable
45+
* After a successful run 0 date values and 5 exceptions should be in the result object.
46+
*
47+
* @author Thomas Bauer, January 2017
48+
*
49+
*/
50+
public class DateFormatRunnableTestIncorrectDateFormat {
51+
52+
// Class variables used in setup() have to be static because setup() has to be static
53+
/**
54+
* Result object given back by DateFormatCallable
55+
* -- Array with converted date values
56+
* -- Array with thrown exceptions
57+
*/
58+
static Result result;
59+
60+
/**
61+
* The date values created by the run of DateFormatRunnalbe. List will be filled in the setup() method
62+
*/
63+
static List<String> createdExceptions = new ArrayList<String>();
64+
65+
/**
66+
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
67+
*/
68+
int expectedCounterDateValues = 0;
69+
70+
/**
71+
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
72+
*/
73+
int expectedCounterExceptions = 5;
74+
75+
/**
76+
* Expected content of the list containing the exceptions created by the run of DateFormatRunnalbe
77+
*/
78+
List<String> expectedExceptions = Arrays.asList("class java.text.ParseException: Unparseable date: \"15.12.2015\"",
79+
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
80+
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
81+
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
82+
"class java.text.ParseException: Unparseable date: \"15.12.2015\"");
83+
84+
/**
85+
* Run Callable and prepare results for usage in the test methods
86+
*/
87+
@BeforeClass
88+
public static void setup() {
89+
// Create a callable. Pass a string date value not matching the format string
90+
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15.12.2015");
91+
// start thread using the Callable instance
92+
ExecutorService executor = Executors.newCachedThreadPool();
93+
Future<Result> futureResult = executor.submit(callableDf);
94+
try {
95+
result = futureResult.get();
96+
} catch (Exception e) {
97+
fail("Setup failed: " + e);
98+
}
99+
executor.shutdown();
100+
}
101+
102+
/**
103+
* Test Exceptions after the run of DateFormatRunnalbe. A correct run should deliver 5 times the
104+
* same exception
105+
*/
106+
@Test
107+
public void testExecptions() {
108+
assertEquals(expectedExceptions, result.getExceptionList());
109+
}
110+
111+
/**
112+
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver no date values
113+
*/
114+
@Test
115+
public void testCounterDateValues() {
116+
assertEquals(expectedCounterDateValues, result.getDateList().size());
117+
}
118+
119+
/**
120+
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
121+
* deliver 5 exceptions
122+
*/
123+
@Test
124+
public void testCounterExceptions() {
125+
assertEquals(expectedCounterExceptions, result.getExceptionList().size());
126+
}
127+
}

0 commit comments

Comments
 (0)