|
| 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 | +} |
0 commit comments