KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > ArchiveUtilsTest


1 /* ArchiveUtilsTest
2  *
3  * $Id: ArchiveUtilsTest.java,v 1.10.16.1 2007/01/13 01:31:39 stack-sf Exp $
4  *
5  * Created Tue Jan 20 14:17:59 PST 2004
6  *
7  * Copyright (C) 2004 Internet Archive.
8  *
9  * This file is part of the Heritrix web crawler (crawler.archive.org).
10  *
11  * Heritrix is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * any later version.
15  *
16  * Heritrix is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser Public License
22  * along with Heritrix; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */

25
26 package org.archive.util;
27
28 import java.util.Date JavaDoc;
29 import java.text.ParseException JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 /**
36  * JUnit test suite for ArchiveUtils
37  *
38  * @author <a HREF="mailto:me@jamesc.net">James Casey</a>
39  * @version $Id: ArchiveUtilsTest.java,v 1.10.16.1 2007/01/13 01:31:39 stack-sf Exp $
40  */

41 public class ArchiveUtilsTest extends TestCase {
42     
43     /**
44      * Create a new ArchiveUtilsTest object
45      *
46      * @param testName the name of the test
47      */

48     public ArchiveUtilsTest(final String JavaDoc testName) {
49         super(testName);
50     }
51
52     /**
53      * run all the tests for ArchiveUtilsTest
54      *
55      * @param argv the command line arguments
56      */

57     public static void main(String JavaDoc argv[]) {
58         junit.textui.TestRunner.run(suite());
59     }
60
61     /**
62      * return the suite of tests for ArchiveUtilsTest
63      *
64      * @return the suite of test
65      */

66     public static Test suite() {
67         return new TestSuite(ArchiveUtilsTest.class);
68     }
69
70     /** check the getXXDigitDate() methods produce valid dates*/
71     public void testGetXXDigitDate() {
72         // TODO - we only really test the date lengths here. How to test
73
// other stuff well ?
74
final String JavaDoc date12 = ArchiveUtils.get12DigitDate();
75         assertEquals("12 digits", 12, date12.length());
76
77         final String JavaDoc date14 = ArchiveUtils.get14DigitDate();
78         assertEquals("14 digits", 14, date14.length());
79
80         final String JavaDoc date17 = ArchiveUtils.get17DigitDate();
81         assertEquals("17 digits", 17, date17.length());
82
83         // now parse, and check they're all within 1 minute
84

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 JavaDoc e) {
94             fail("Could not parse a date : " + e.getMessage());
95         }
96     }
97
98     /** check that getXXDigitDate(long) does the right thing */
99     public void testGetXXDigitDateLong() {
100         final long now = System.currentTimeMillis();
101         final String JavaDoc date12 = ArchiveUtils.get12DigitDate(now);
102         assertEquals("12 digits", 12, date12.length());
103
104         final String JavaDoc 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 JavaDoc 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     /**
114      * Check that parseXXDigitDate() works
115      *
116      * @throws ParseException
117      */

118     public void testParseXXDigitDate() throws ParseException JavaDoc {
119         // given a date, check it get resolved properly
120
// It's 02 Jan 2004, 12:40:02.111
121
final String JavaDoc 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 JavaDoc e) {
131             fail("Could not parse a date : " + e.getMessage());
132         }
133     }
134     
135     public void testTooShortParseDigitDate() throws ParseException JavaDoc {
136         String JavaDoc d = "X";
137         boolean b = false;
138         try {
139             ArchiveUtils.getDate(d);
140         } catch (ParseException JavaDoc e) {
141             b = true;
142         }
143         assertTrue(b);
144         
145         Date JavaDoc date = ArchiveUtils.getDate("1999");
146         assertTrue(date.getTime() == 915148800000L);
147         
148         b = false;
149         try {
150             ArchiveUtils.getDate("19991");
151         } catch (ParseException JavaDoc 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     /** check that parse12DigitDate doesn't accept a bad date */
163     public void testBad12Date() {
164         // now try a badly formed dates
165
assertBad12DigitDate("a-stringy-digit-date");
166         assertBad12DigitDate("20031201"); // too short
167
}
168
169     /**
170      * check that parse14DigitDate doesn't accept a bad date
171      */

172     public void testBad14Date() {
173         // now try a badly formed dates
174
assertBad14DigitDate("a-stringy-digit-date");
175         assertBad14DigitDate("20031201"); // too short
176
assertBad14DigitDate("200401021240"); // 12 digit
177
}
178     /**
179      * check that parse12DigitDate doesn't accept a bad date
180      */

181     public void testBad17Date() {
182         // now try a badly formed dates
183
assertBad17DigitDate("a-stringy-digit-date");
184         assertBad17DigitDate("20031201"); // too short
185
assertBad17DigitDate("200401021240"); // 12 digit
186
assertBad17DigitDate("20040102124002"); // 14 digit
187
}
188
189     /** check that padTo(String) works */
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     /**
199      * check that padTo(int) works
200      */

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     /** check that byteArrayEquals() works */
211     public void testByteArrayEquals() {
212         // foo == foo2, foo != bar, foo != bar2
213
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         // now check with same length, with same (foo2) and different (bar2)
225
// contents
226
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     /** test doubleToString() */
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     /*
263      * helper methods
264      */

265
266     /** check that this is a bad date, and <code>fail()</code> if so.
267      *
268      * @param date the 12digit date to check
269      */

270     private void assertBad12DigitDate(final String JavaDoc date) {
271         try {
272             ArchiveUtils.parse12DigitDate(date);
273         } catch (ParseException JavaDoc e) {
274             return;
275         }
276         fail("Expected exception on parse of : " + date);
277
278     }
279     /**
280      * check that this is a bad date, and <code>fail()</code> if so.
281      *
282      * @param date the 14digit date to check
283      */

284     private void assertBad14DigitDate(final String JavaDoc date) {
285         try {
286             ArchiveUtils.parse14DigitDate(date);
287         } catch (ParseException JavaDoc e) {
288             return;
289         }
290         fail("Expected exception on parse of : " + date);
291
292     }
293
294     /**
295      * check that this is a bad date, and <code>fail()</code> if so.
296      *
297      * @param date the 17digit date to check
298      */

299     private void assertBad17DigitDate(final String JavaDoc date) {
300         try {
301             ArchiveUtils.parse17DigitDate(date);
302         } catch (ParseException JavaDoc e) {
303             return;
304         }
305         fail("Expected exception on parse of : " + date);
306
307     }
308
309     /** check that two longs are within a given <code>delta</code> */
310     private void assertClose(String JavaDoc 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 JavaDoc {
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 JavaDoc 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