[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.IOException; |
---|
| 22 | import java.util.Random; |
---|
| 23 | |
---|
| 24 | import junit.framework.TestCase; |
---|
| 25 | |
---|
| 26 | import org.apache.commons.logging.Log; |
---|
| 27 | import org.apache.commons.logging.LogFactory; |
---|
| 28 | import org.apache.hadoop.conf.Configuration; |
---|
| 29 | |
---|
| 30 | public class TestDefaultStringifier extends TestCase { |
---|
| 31 | |
---|
| 32 | private static Configuration conf = new Configuration(); |
---|
| 33 | private static final Log LOG = LogFactory.getLog(TestDefaultStringifier.class); |
---|
| 34 | |
---|
| 35 | private char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); |
---|
| 36 | |
---|
| 37 | public void testWithWritable() throws Exception { |
---|
| 38 | |
---|
| 39 | conf.set("io.serializations", "org.apache.hadoop.io.serializer.WritableSerialization"); |
---|
| 40 | |
---|
| 41 | LOG.info("Testing DefaultStringifier with Text"); |
---|
| 42 | |
---|
| 43 | Random random = new Random(); |
---|
| 44 | |
---|
| 45 | //test with a Text |
---|
| 46 | for(int i=0;i<10;i++) { |
---|
| 47 | //generate a random string |
---|
| 48 | StringBuilder builder = new StringBuilder(); |
---|
| 49 | int strLen = random.nextInt(40); |
---|
| 50 | for(int j=0; j< strLen; j++) { |
---|
| 51 | builder.append(alphabet[random.nextInt(alphabet.length)]); |
---|
| 52 | } |
---|
| 53 | Text text = new Text(builder.toString()); |
---|
| 54 | DefaultStringifier<Text> stringifier = new DefaultStringifier<Text>(conf, Text.class); |
---|
| 55 | |
---|
| 56 | String str = stringifier.toString(text); |
---|
| 57 | Text claimedText = stringifier.fromString(str); |
---|
| 58 | LOG.info("Object: " + text); |
---|
| 59 | LOG.info("String representation of the object: " + str); |
---|
| 60 | assertEquals(text, claimedText); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public void testWithJavaSerialization() throws Exception { |
---|
| 65 | conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization"); |
---|
| 66 | |
---|
| 67 | LOG.info("Testing DefaultStringifier with Serializable Integer"); |
---|
| 68 | |
---|
| 69 | //Integer implements Serializable |
---|
| 70 | Integer testInt = Integer.valueOf(42); |
---|
| 71 | DefaultStringifier<Integer> stringifier = new DefaultStringifier<Integer>(conf, Integer.class); |
---|
| 72 | |
---|
| 73 | String str = stringifier.toString(testInt); |
---|
| 74 | Integer claimedInt = stringifier.fromString(str); |
---|
| 75 | LOG.info("String representation of the object: " + str); |
---|
| 76 | |
---|
| 77 | assertEquals(testInt, claimedInt); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public void testStoreLoad() throws IOException { |
---|
| 81 | |
---|
| 82 | LOG.info("Testing DefaultStringifier#store() and #load()"); |
---|
| 83 | conf.set("io.serializations", "org.apache.hadoop.io.serializer.WritableSerialization"); |
---|
| 84 | Text text = new Text("uninteresting test string"); |
---|
| 85 | String keyName = "test.defaultstringifier.key1"; |
---|
| 86 | |
---|
| 87 | DefaultStringifier.store(conf,text, keyName); |
---|
| 88 | |
---|
| 89 | Text claimedText = DefaultStringifier.load(conf, keyName, Text.class); |
---|
| 90 | assertEquals("DefaultStringifier#load() or #store() might be flawed" |
---|
| 91 | , text, claimedText); |
---|
| 92 | |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public void testStoreLoadArray() throws IOException { |
---|
| 96 | LOG.info("Testing DefaultStringifier#storeArray() and #loadArray()"); |
---|
| 97 | conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization"); |
---|
| 98 | |
---|
| 99 | String keyName = "test.defaultstringifier.key2"; |
---|
| 100 | |
---|
| 101 | Integer[] array = new Integer[] {1,2,3,4,5}; |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | DefaultStringifier.storeArray(conf, array, keyName); |
---|
| 105 | |
---|
| 106 | Integer[] claimedArray = DefaultStringifier.<Integer>loadArray(conf, keyName, Integer.class); |
---|
| 107 | for (int i = 0; i < array.length; i++) { |
---|
| 108 | assertEquals("two arrays are not equal", array[i], claimedArray[i]); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | } |
---|