KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > io > TestUTF8


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.io;
5
6 import junit.framework.TestCase;
7 import java.util.Random JavaDoc;
8 import java.util.Arrays JavaDoc;
9
10 /** Unit tests for UTF8. */
11 public class TestUTF8 extends TestCase {
12   public TestUTF8(String JavaDoc name) { super(name); }
13
14   private static final Random JavaDoc RANDOM = new Random JavaDoc();
15
16   public static String JavaDoc getTestString() throws Exception JavaDoc {
17     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
18     int length = RANDOM.nextInt(100);
19     for (int i = 0; i < length; i++) {
20       buffer.append((char)(RANDOM.nextInt(Character.MAX_VALUE)));
21     }
22     return buffer.toString();
23   }
24
25   public void testWritable() throws Exception JavaDoc {
26     for (int i = 0; i < 10; i++) {
27       TestWritable.testWritable(new UTF8(getTestString()));
28     }
29   }
30
31   public void testGetBytes() throws Exception JavaDoc {
32     for (int i = 0; i < 10; i++) {
33
34       // generate a random string
35
String JavaDoc before = getTestString();
36
37       // check its utf8
38
assertEquals(before, new String JavaDoc(UTF8.getBytes(before), "UTF-8"));
39     }
40   }
41
42   public void testIO() throws Exception JavaDoc {
43     DataOutputBuffer out = new DataOutputBuffer();
44     DataInputBuffer in = new DataInputBuffer();
45
46     for (int i = 0; i < 10; i++) {
47       // generate a random string
48
String JavaDoc before = getTestString();
49
50       // write it
51
out.reset();
52       UTF8.writeString(out, before);
53
54       // test that it reads correctly
55
in.reset(out.getData(), out.getLength());
56       String JavaDoc after = UTF8.readString(in);
57       assertTrue(before.equals(after));
58
59       // test that it reads correctly with DataInput
60
in.reset(out.getData(), out.getLength());
61       String JavaDoc after2 = in.readUTF();
62       assertTrue(before.equals(after2));
63
64       // test that it is compatible with Java's other decoder
65
String JavaDoc after3 = new String JavaDoc(out.getData(), 2, out.getLength()-2, "UTF-8");
66       assertTrue(before.equals(after3));
67
68     }
69
70   }
71     
72 }
73
Popular Tags