1 25 26 package org.archive.util; 27 28 import java.util.Date ; 29 import java.text.ParseException ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 41 public class ArchiveUtilsTest extends TestCase { 42 43 48 public ArchiveUtilsTest(final String testName) { 49 super(testName); 50 } 51 52 57 public static void main(String argv[]) { 58 junit.textui.TestRunner.run(suite()); 59 } 60 61 66 public static Test suite() { 67 return new TestSuite(ArchiveUtilsTest.class); 68 } 69 70 71 public void testGetXXDigitDate() { 72 final String date12 = ArchiveUtils.get12DigitDate(); 75 assertEquals("12 digits", 12, date12.length()); 76 77 final String date14 = ArchiveUtils.get14DigitDate(); 78 assertEquals("14 digits", 14, date14.length()); 79 80 final String date17 = ArchiveUtils.get17DigitDate(); 81 assertEquals("17 digits", 17, date17.length()); 82 83 85 try { 86 final long long12 = ArchiveUtils.parse12DigitDate(date12).getTime(); 87 long long14 = ArchiveUtils.parse14DigitDate(date14).getTime(); 88 long long17 = ArchiveUtils.parse17DigitDate(date17).getTime(); 89 90 assertClose("12 and 14 close", long12, long14, 600000); 91 assertClose("12 and 17 close", long12, long17, 600000); 92 assertClose("14 and 17 close", long14, long17, 600000); 93 } catch (ParseException e) { 94 fail("Could not parse a date : " + e.getMessage()); 95 } 96 } 97 98 99 public void testGetXXDigitDateLong() { 100 final long now = System.currentTimeMillis(); 101 final String date12 = ArchiveUtils.get12DigitDate(now); 102 assertEquals("12 digits", 12, date12.length()); 103 104 final String date14 = ArchiveUtils.get14DigitDate(now); 105 assertEquals("14 digits", 14, date14.length()); 106 assertEquals("first twelve digits same as date12", date12, date14.substring(0, 12)); 107 final String date17 = ArchiveUtils.get17DigitDate(now); 108 assertEquals("17 digits", 17, date17.length()); 109 assertEquals("first twelve digits same as date12", date12, date17.substring(0, 12)); 110 assertEquals("first fourteen digits same as date14", date14, date17.substring(0, 14)); 111 } 112 113 118 public void testParseXXDigitDate() throws ParseException { 119 final String date = "20040102124002111"; 122 try { 123 final long long12 = ArchiveUtils.parse12DigitDate(date.substring(0, 12)).getTime(); 124 final long long14 = ArchiveUtils.parse14DigitDate(date.substring(0, 14)).getTime(); 125 final long long17 = ArchiveUtils.parse17DigitDate(date).getTime(); 126 127 assertClose("12 and 14 close", long12, long14, 600000); 128 assertClose("12 and 17 close", long12, long17, 600000); 129 assertClose("14 and 17 close", long14, long17, 600000); 130 } catch (ParseException e) { 131 fail("Could not parse a date : " + e.getMessage()); 132 } 133 } 134 135 public void testTooShortParseDigitDate() throws ParseException { 136 String d = "X"; 137 boolean b = false; 138 try { 139 ArchiveUtils.getDate(d); 140 } catch (ParseException e) { 141 b = true; 142 } 143 assertTrue(b); 144 145 Date date = ArchiveUtils.getDate("1999"); 146 assertTrue(date.getTime() == 915148800000L); 147 148 b = false; 149 try { 150 ArchiveUtils.getDate("19991"); 151 } catch (ParseException e) { 152 b = true; 153 } 154 assertTrue(b); 155 156 ArchiveUtils.getDate("19990101"); 157 ArchiveUtils.getDate("1999010101"); 158 ArchiveUtils.getDate("19990101010101"); 159 ArchiveUtils.getDate("1960"); 160 } 161 162 163 public void testBad12Date() { 164 assertBad12DigitDate("a-stringy-digit-date"); 166 assertBad12DigitDate("20031201"); } 168 169 172 public void testBad14Date() { 173 assertBad14DigitDate("a-stringy-digit-date"); 175 assertBad14DigitDate("20031201"); assertBad14DigitDate("200401021240"); } 178 181 public void testBad17Date() { 182 assertBad17DigitDate("a-stringy-digit-date"); 184 assertBad17DigitDate("20031201"); assertBad17DigitDate("200401021240"); assertBad17DigitDate("20040102124002"); } 188 189 190 public void testPadToString() { 191 assertEquals("pad to one (smaller)", "foo", ArchiveUtils.padTo("foo", 1)); 192 assertEquals("pad to 0 (no sense)", "foo", ArchiveUtils.padTo("foo", 0)); 193 assertEquals("pad to neg (nonsense)", "foo", ArchiveUtils.padTo("foo", 0)); 194 assertEquals("pad to 4", " foo", ArchiveUtils.padTo("foo", 4)); 195 assertEquals("pad to 10", " foo", ArchiveUtils.padTo("foo", 10)); 196 } 197 198 201 public void testPadToInt() { 202 assertEquals("pad to one (smaller)", "123", ArchiveUtils.padTo(123, 1)); 203 assertEquals("pad to 0 (no sense)", "123", ArchiveUtils.padTo(123, 0)); 204 assertEquals("pad to neg (nonsense)", "123", ArchiveUtils.padTo(123, 0)); 205 assertEquals("pad to 4", " 123", ArchiveUtils.padTo(123, 4)); 206 assertEquals("pad to 10", " 123", ArchiveUtils.padTo(123, 10)); 207 assertEquals("pad -123 to 10", " -123", ArchiveUtils.padTo(-123, 10)); 208 } 209 210 211 public void testByteArrayEquals() { 212 byte[] foo = new byte[10], bar = new byte[20]; 214 byte[] foo2 = new byte[10], bar2 = new byte[10]; 215 216 for (byte i = 0; i < 10 ; ++i) { 217 foo[i] = foo2[i] = bar[i] = i; 218 bar2[i] = (byte)(01 + i); 219 } 220 assertTrue("two nulls", ArchiveUtils.byteArrayEquals(null, null)); 221 assertFalse("lhs null", ArchiveUtils.byteArrayEquals(null, foo)); 222 assertFalse("rhs null", ArchiveUtils.byteArrayEquals(foo, null)); 223 224 assertFalse("different lengths", ArchiveUtils.byteArrayEquals(foo, bar)); 227 228 assertTrue("same to itself", ArchiveUtils.byteArrayEquals(foo, foo)); 229 assertTrue("same contents", ArchiveUtils.byteArrayEquals(foo, foo2)); 230 assertFalse("different contents", ArchiveUtils.byteArrayEquals(foo, bar2)); 231 } 232 233 234 public void testDoubleToString(){ 235 double test = 12.345; 236 assertTrue( 237 "cecking zero precision", 238 ArchiveUtils.doubleToString(test, 0).equals("12")); 239 assertTrue( 240 "cecking 2 character precision", 241 ArchiveUtils.doubleToString(test, 2).equals("12.34")); 242 assertTrue( 243 "cecking precision higher then the double has", 244 ArchiveUtils.doubleToString(test, 65).equals("12.345")); 245 } 246 247 public void testFormatBytesForDisplay(){ 248 long kb = 1024; 249 long mb = 1024*1024*2; 250 long gb = ((long)1024*1024)*1024*4; 251 252 assertEquals("formating negative number","0 B",ArchiveUtils.formatBytesForDisplay(-1)); 253 assertEquals("formating byte - lower bound","0 B",ArchiveUtils.formatBytesForDisplay(0)); 254 assertEquals("formating byte - upper bound","1023 B",ArchiveUtils.formatBytesForDisplay(kb-1)); 255 assertEquals("formating kilobyte - lower bound","1 KB",ArchiveUtils.formatBytesForDisplay(kb)); 256 assertEquals("formating kilobyte - upper bound","2047 KB",ArchiveUtils.formatBytesForDisplay(mb-1)); 257 assertEquals("formating megabyte - lower bound","2 MB",ArchiveUtils.formatBytesForDisplay(mb)); 258 assertEquals("formating megabyte - upper bound","4095 MB",ArchiveUtils.formatBytesForDisplay(gb-1)); 259 assertEquals("formating gigabyte - lower bound","4 GB",ArchiveUtils.formatBytesForDisplay(gb)); 260 } 261 262 265 266 270 private void assertBad12DigitDate(final String date) { 271 try { 272 ArchiveUtils.parse12DigitDate(date); 273 } catch (ParseException e) { 274 return; 275 } 276 fail("Expected exception on parse of : " + date); 277 278 } 279 284 private void assertBad14DigitDate(final String date) { 285 try { 286 ArchiveUtils.parse14DigitDate(date); 287 } catch (ParseException e) { 288 return; 289 } 290 fail("Expected exception on parse of : " + date); 291 292 } 293 294 299 private void assertBad17DigitDate(final String date) { 300 try { 301 ArchiveUtils.parse17DigitDate(date); 302 } catch (ParseException e) { 303 return; 304 } 305 fail("Expected exception on parse of : " + date); 306 307 } 308 309 310 private void assertClose(String desc, long date1, long date2, long delta) { 311 assertTrue(desc, date1 == date2 || 312 (date1 < date2 && date2 < (date1 + delta)) || 313 (date2 < date1 && date1 < (date2 + delta))); 314 } 315 316 public void testArrayToLong() { 317 testOneArrayToLong(-1); 318 testOneArrayToLong(1); 319 testOneArrayToLong(1000); 320 testOneArrayToLong(Integer.MAX_VALUE); 321 } 322 323 private void testOneArrayToLong(final long testValue) { 324 byte [] a = new byte[8]; 325 ArchiveUtils.longIntoByteArray(testValue, a, 0); 326 final long l = ArchiveUtils.byteArrayIntoLong(a, 0); 327 assertEquals(testValue, l); 328 } 329 330 public void testSecondsSinceEpochCalculation() throws ParseException { 331 assertEquals(ArchiveUtils.secondsSinceEpoch("20010909014640"), 332 "1000000000"); 333 assertEquals(ArchiveUtils.secondsSinceEpoch("20010909014639"), 334 "0999999999"); 335 assertEquals(ArchiveUtils.secondsSinceEpoch("19700101"), 336 "0000000000"); 337 assertEquals(ArchiveUtils.secondsSinceEpoch("2005"), "1104537600"); 338 assertEquals(ArchiveUtils.secondsSinceEpoch("200501"), "1104537600"); 339 assertEquals(ArchiveUtils.secondsSinceEpoch("20050101"), "1104537600"); 340 assertEquals(ArchiveUtils.secondsSinceEpoch("2005010100"), 341 "1104537600"); 342 boolean eThrown = false; 343 try { 344 ArchiveUtils.secondsSinceEpoch("20050"); 345 } catch (IllegalArgumentException e) { 346 eThrown = true; 347 } 348 assertTrue(eThrown); 349 } 350 351 public static void testZeroPadInteger() { 352 assertEquals(ArchiveUtils.zeroPadInteger(1), "0000000001"); 353 assertEquals(ArchiveUtils.zeroPadInteger(1000000000), "1000000000"); 354 } 355 } 356 357 | Popular Tags |