1 2 3 4 package net.nutch.io; 5 6 import junit.framework.TestCase; 7 import java.util.Random ; 8 import java.util.Arrays ; 9 10 11 public class TestUTF8 extends TestCase { 12 public TestUTF8(String name) { super(name); } 13 14 private static final Random RANDOM = new Random (); 15 16 public static String getTestString() throws Exception { 17 StringBuffer buffer = new StringBuffer (); 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 { 26 for (int i = 0; i < 10; i++) { 27 TestWritable.testWritable(new UTF8(getTestString())); 28 } 29 } 30 31 public void testGetBytes() throws Exception { 32 for (int i = 0; i < 10; i++) { 33 34 String before = getTestString(); 36 37 assertEquals(before, new String (UTF8.getBytes(before), "UTF-8")); 39 } 40 } 41 42 public void testIO() throws Exception { 43 DataOutputBuffer out = new DataOutputBuffer(); 44 DataInputBuffer in = new DataInputBuffer(); 45 46 for (int i = 0; i < 10; i++) { 47 String before = getTestString(); 49 50 out.reset(); 52 UTF8.writeString(out, before); 53 54 in.reset(out.getData(), out.getLength()); 56 String after = UTF8.readString(in); 57 assertTrue(before.equals(after)); 58 59 in.reset(out.getData(), out.getLength()); 61 String after2 = in.readUTF(); 62 assertTrue(before.equals(after2)); 63 64 String after3 = new String (out.getData(), 2, out.getLength()-2, "UTF-8"); 66 assertTrue(before.equals(after3)); 67 68 } 69 70 } 71 72 } 73 | Popular Tags |