[120] | 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 | |
---|
| 19 | package org.apache.hadoop.io; |
---|
| 20 | |
---|
| 21 | import java.io.*; |
---|
| 22 | import java.util.Random; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.conf.Configurable; |
---|
| 25 | import org.apache.hadoop.conf.Configuration; |
---|
| 26 | import org.apache.hadoop.util.ReflectionUtils; |
---|
| 27 | |
---|
| 28 | import junit.framework.TestCase; |
---|
| 29 | |
---|
| 30 | /** Unit tests for WritableName. */ |
---|
| 31 | public class TestWritableName extends TestCase { |
---|
| 32 | public TestWritableName(String name) { |
---|
| 33 | super(name); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** Example class used in test cases below. */ |
---|
| 37 | public static class SimpleWritable implements Writable { |
---|
| 38 | private static final Random RANDOM = new Random(); |
---|
| 39 | |
---|
| 40 | int state = RANDOM.nextInt(); |
---|
| 41 | |
---|
| 42 | public void write(DataOutput out) throws IOException { |
---|
| 43 | out.writeInt(state); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public void readFields(DataInput in) throws IOException { |
---|
| 47 | this.state = in.readInt(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public static SimpleWritable read(DataInput in) throws IOException { |
---|
| 51 | SimpleWritable result = new SimpleWritable(); |
---|
| 52 | result.readFields(in); |
---|
| 53 | return result; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | /** Required by test code, below. */ |
---|
| 57 | public boolean equals(Object o) { |
---|
| 58 | if (!(o instanceof SimpleWritable)) |
---|
| 59 | return false; |
---|
| 60 | SimpleWritable other = (SimpleWritable)o; |
---|
| 61 | return this.state == other.state; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | private static final String testName = "mystring"; |
---|
| 66 | |
---|
| 67 | public void testGoodName() throws Exception { |
---|
| 68 | Configuration conf = new Configuration(); |
---|
| 69 | Class<?> test = WritableName.getClass("long",conf); |
---|
| 70 | assertTrue(test != null); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public void testSetName() throws Exception { |
---|
| 74 | Configuration conf = new Configuration(); |
---|
| 75 | WritableName.setName(SimpleWritable.class, testName); |
---|
| 76 | |
---|
| 77 | Class<?> test = WritableName.getClass(testName,conf); |
---|
| 78 | assertTrue(test.equals(SimpleWritable.class)); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | public void testAddName() throws Exception { |
---|
| 83 | Configuration conf = new Configuration(); |
---|
| 84 | String altName = testName + ".alt"; |
---|
| 85 | |
---|
| 86 | WritableName.addName(SimpleWritable.class, altName); |
---|
| 87 | |
---|
| 88 | Class<?> test = WritableName.getClass(altName, conf); |
---|
| 89 | assertTrue(test.equals(SimpleWritable.class)); |
---|
| 90 | |
---|
| 91 | // check original name still works |
---|
| 92 | test = WritableName.getClass(testName, conf); |
---|
| 93 | assertTrue(test.equals(SimpleWritable.class)); |
---|
| 94 | |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public void testBadName() throws Exception { |
---|
| 98 | Configuration conf = new Configuration(); |
---|
| 99 | try { |
---|
| 100 | Class<?> test = WritableName.getClass("unknown_junk",conf); |
---|
| 101 | assertTrue(false); |
---|
| 102 | } catch(IOException e) { |
---|
| 103 | assertTrue(e.getMessage().matches(".*unknown_junk.*")); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | } |
---|