| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.text.ParseException; |
| 36 | | | import java.util.logging.Level; |
| 37 | | | import java.util.logging.Logger; |
| 38 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 39 | | | import org.jcoderz.commons.TestCase; |
| 40 | | | import org.jcoderz.commons.types.Date; |
| 41 | | | import org.jcoderz.commons.types.Period; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | {@linkplain } |
| 49 | | | |
| 50 | | | |
| 51 | 100 | (1) | public class LogViewerTest |
| 52 | | | extends TestCase |
| 53 | | | { |
| 54 | | | |
| 55 | 75 | | private static final String CLASSNAME = LogViewerTest.class.getName(); |
| 56 | | | |
| 57 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | {@link } |
| 62 | | | |
| 63 | | | public void testGetDateFromOptValue () |
| 64 | | | { |
| 65 | 100 | | checkGetDateFromOptValueBad("wrong string"); |
| 66 | 100 | | checkGetDateFromOptValueBad("2005"); |
| 67 | 100 | | checkGetDateFromOptValueBad("2005-11"); |
| 68 | 100 | | checkGetDateFromOptValueBad("2005/11/01"); |
| 69 | 100 | | checkGetDateFromOptValueBad("2005-11-30T14:33:24.123"); |
| 70 | 100 | | checkGetDateFromOptValueBad("2005-11-30T14:33:24"); |
| 71 | | | |
| 72 | 100 | | final String expected = "2005-11-30T00:00:00.000Z"; |
| 73 | 100 | | checkGetDateFromOptValueGood("2005-11-30", expected); |
| 74 | 100 | | checkGetDateFromOptValueGood("2005-11-30Z", expected); |
| 75 | 100 | | String same = "2005-11-30T14:33:24Z"; |
| 76 | 100 | | checkGetDateFromOptValueGood(same, same); |
| 77 | 100 | | same = "2005-11-30T14:33:24.123Z"; |
| 78 | 100 | | checkGetDateFromOptValueGood(same, same); |
| 79 | 100 | | } |
| 80 | | | |
| 81 | | | |
| 82 | | | {@link } |
| 83 | | | |
| 84 | | | public void testGetDateFrom () |
| 85 | | | { |
| 86 | | | try |
| 87 | | | { |
| 88 | 100 | | assertDate(LogViewer.getDateFrom(null), Date.OLD_DATE); |
| 89 | 100 | | assertDate(LogViewer.getDateFrom(""), Date.OLD_DATE); |
| 90 | | | } |
| 91 | 0 | | catch (ParseException e) |
| 92 | | | { |
| 93 | 0 | | logErr("LogViewer.getDateFrom ", e); |
| 94 | 100 | | } |
| 95 | 100 | | } |
| 96 | | | |
| 97 | | | |
| 98 | | | {@link } |
| 99 | | | |
| 100 | | | public void testGetDateTo () |
| 101 | | | { |
| 102 | | | try |
| 103 | | | { |
| 104 | 100 | | assertDate(LogViewer.getDateTo(null), Date.FUTURE_DATE); |
| 105 | 100 | | assertDate(LogViewer.getDateTo(""), Date.FUTURE_DATE); |
| 106 | | | } |
| 107 | 0 | | catch (ParseException e) |
| 108 | | | { |
| 109 | 0 | | logErr("LogViewer.getDateTo ", e); |
| 110 | 100 | | } |
| 111 | 100 | | } |
| 112 | | | |
| 113 | | | |
| 114 | | | |
| 115 | | | {@linkplain } |
| 116 | | | |
| 117 | | | public void testGetPeriodsFromOptionsValues () |
| 118 | | | { |
| 119 | 100 | | final String start = "2005-11-30T00:00:00.000Z"; |
| 120 | 100 | | final String end = "2005-11-30T00:00:00.000Z"; |
| 121 | | | |
| 122 | 100 | | assertPeriod(start, end, start, end); |
| 123 | 100 | | assertPeriod(start, null, start, Date.FUTURE_DATE.toString()); |
| 124 | 100 | | assertPeriod(start, "", start, Date.FUTURE_DATE.toString()); |
| 125 | 100 | | assertPeriod(null, end, Date.OLD_DATE.toString(), end); |
| 126 | 100 | | assertPeriod("", end, Date.OLD_DATE.toString(), end); |
| 127 | 100 | | } |
| 128 | | | |
| 129 | | | private void assertPeriod (String start, String end, String expectedStart, |
| 130 | | | String expectedEndend) |
| 131 | | | { |
| 132 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 133 | 100 | | if (start != null) |
| 134 | | | { |
| 135 | 100 | | sb.append(start); |
| 136 | | | } |
| 137 | 100 | | sb.append(','); |
| 138 | 100 | | if (end != null) |
| 139 | | | { |
| 140 | 100 | | sb.append(end); |
| 141 | | | } |
| 142 | 100 | | final String [] values = {sb.toString()}; |
| 143 | | | |
| 144 | | | try |
| 145 | | | { |
| 146 | 100 | | final Period [] expectedPeriod = new Period [] { |
| 147 | | | Period.createPeriod(Date.fromString(expectedStart), |
| 148 | | | Date.fromString(expectedEndend))}; |
| 149 | 100 | | final Period [] p = LogViewer.getPeriodsFromOptionValues(values); |
| 150 | 100 | | assertNotNull("LogViewer.getPeriodsFromOptionValues returned null", p); |
| 151 | 100 | | assertEquals("Period array has an unexpected length", p.length, |
| 152 | | | expectedPeriod.length); |
| 153 | 100 | | assertEquals("Got unexcpected period", p[0], expectedPeriod[0]); |
| 154 | | | } |
| 155 | 0 | | catch (ArgumentMalformedException e) |
| 156 | | | { |
| 157 | 0 | | logErr(errMsgGetPeriodsFromOptionsValues(values[0]), e); |
| 158 | | | } |
| 159 | 0 | | catch (ParseException e) |
| 160 | | | { |
| 161 | 0 | | logErr(errMsgGetPeriodsFromOptionsValues(values[0]), e); |
| 162 | 50 | | } |
| 163 | 100 | | } |
| 164 | | | |
| 165 | | | private void assertDate (Date result, Date expected) |
| 166 | | | { |
| 167 | 100 | | assertNotNull("Returned value must not be null", result); |
| 168 | 100 | | assertEquals("Unexpected date", result, expected); |
| 169 | 100 | | } |
| 170 | | | |
| 171 | | | private void checkGetDateFromOptValueBad (String str) |
| 172 | | | { |
| 173 | | | try |
| 174 | | | { |
| 175 | 0 | | LogViewer.getDateFromOptValue(str, true); |
| 176 | 0 | | fail(errMsgGetDateFromOptValue(str) + "must throw the ParseException"); |
| 177 | | | } |
| 178 | 100 | | catch (ParseException e) |
| 179 | | | { |
| 180 | | | |
| 181 | 0 | | } |
| 182 | 100 | | } |
| 183 | | | |
| 184 | | | private void checkGetDateFromOptValueGood (String str, String expected) |
| 185 | | | { |
| 186 | 100 | | Date result = null; |
| 187 | 100 | | Date expDate = null; |
| 188 | | | try |
| 189 | | | { |
| 190 | 100 | | expDate = Date.fromString(expected); |
| 191 | 100 | | result = LogViewer.getDateFromOptValue(str, true); |
| 192 | | | } |
| 193 | 0 | | catch (Exception e) |
| 194 | | | { |
| 195 | 0 | | logErr(errMsgGetDateFromOptValue(str), e); |
| 196 | 100 | | } |
| 197 | 100 | | assertNotNull("Got a null date", result); |
| 198 | 100 | | assertEquals(errMsgGetDateFromOptValue(str) |
| 199 | | | + "returned unexcpected date.", result, expDate); |
| 200 | 100 | | } |
| 201 | | | |
| 202 | | | private String errMsgGetDateFromOptValue (String str) |
| 203 | | | { |
| 204 | 100 | | return "LogViewer.getDateFromOptValue(" + str + ", true) "; |
| 205 | | | } |
| 206 | | | |
| 207 | | | private String errMsgGetPeriodsFromOptionsValues (String str) |
| 208 | | | { |
| 209 | 0 | | return "LogViewer.getPeriodsFromOptionsValues(" + str + ") "; |
| 210 | | | } |
| 211 | | | |
| 212 | | | private void logErr (String str, Exception e) |
| 213 | | | { |
| 214 | 0 | | final String msg = str + " failed due to unexpected Exception " |
| 215 | | | + e.getMessage(); |
| 216 | 0 | | logger.log(Level.SEVERE, msg, e); |
| 217 | 0 | | fail(msg); |
| 218 | 0 | | } |
| 219 | | | } |