source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/TestUTF8.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.io;
20
21import junit.framework.TestCase;
22import java.util.Random;
23
24/** Unit tests for UTF8. */
25public class TestUTF8 extends TestCase {
26  public TestUTF8(String name) { super(name); }
27
28  private static final Random RANDOM = new Random();
29
30  public static String getTestString() throws Exception {
31    StringBuffer buffer = new StringBuffer();
32    int length = RANDOM.nextInt(100);
33    for (int i = 0; i < length; i++) {
34      buffer.append((char)(RANDOM.nextInt(Character.MAX_VALUE)));
35    }
36    return buffer.toString();
37  }
38
39  public void testWritable() throws Exception {
40    for (int i = 0; i < 10; i++) {
41      TestWritable.testWritable(new UTF8(getTestString()));
42    }
43  }
44
45  public void testGetBytes() throws Exception {
46    for (int i = 0; i < 10; i++) {
47
48      // generate a random string
49      String before = getTestString();
50
51      // check its utf8
52      assertEquals(before, new String(UTF8.getBytes(before), "UTF-8"));
53    }
54  }
55
56  public void testIO() throws Exception {
57    DataOutputBuffer out = new DataOutputBuffer();
58    DataInputBuffer in = new DataInputBuffer();
59
60    for (int i = 0; i < 10; i++) {
61      // generate a random string
62      String before = getTestString();
63
64      // write it
65      out.reset();
66      UTF8.writeString(out, before);
67
68      // test that it reads correctly
69      in.reset(out.getData(), out.getLength());
70      String after = UTF8.readString(in);
71      assertTrue(before.equals(after));
72
73      // test that it reads correctly with DataInput
74      in.reset(out.getData(), out.getLength());
75      String after2 = in.readUTF();
76      assertTrue(before.equals(after2));
77
78      // test that it is compatible with Java's other decoder
79      String after3 = new String(out.getData(), 2, out.getLength()-2, "UTF-8");
80      assertTrue(before.equals(after3));
81
82    }
83
84  }
85       
86}
Note: See TracBrowser for help on using the repository browser.