Skip to content

Commit

Permalink
fix incorrect parsing of List/Writable parsing
Browse files Browse the repository at this point in the history
add unit test to prevent such mistakes from occurring again
fixes #36
fixes #37
fixes #38
  • Loading branch information
costin committed Apr 19, 2013
1 parent a1a7952 commit 48358d6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Expand Up @@ -27,6 +27,7 @@
import org.apache.hadoop.io.AbstractMapWritable;
import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.ByteWritable;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
Expand Down Expand Up @@ -61,6 +62,9 @@ else if (object instanceof Long) {
else if (object instanceof Integer) {
return new VIntWritable((Integer) object);
}
else if (object instanceof Byte) {
return new ByteWritable((Byte) object);
}
else if (object instanceof Double) {
return new DoubleWritable((Double) object);
}
Expand Down Expand Up @@ -136,6 +140,9 @@ else if (writable instanceof VLongWritable) {
else if (writable instanceof VIntWritable) {
return ((VIntWritable) writable).get();
}
else if (writable instanceof ByteWritable) {
return ((ByteWritable) writable).get();
}
else if (writable instanceof DoubleWritable) {
return ((DoubleWritable) writable).get();
}
Expand All @@ -154,6 +161,7 @@ else if (writable instanceof ArrayWritable) {
for (Writable wrt : writables) {
list.add(fromWritable(wrt));
}
return list;
}
else if (writable instanceof AbstractMapWritable) {
Map<Writable, Writable> smap = (Map) writable;
Expand Down
@@ -0,0 +1,89 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.elasticsearch.hadoop.unit.util;

import java.util.Arrays;
import java.util.Collections;

import org.elasticsearch.hadoop.util.WritableUtils;
import org.junit.Test;

import static org.junit.Assert.*;

/**
*
*/
public class WritableUtilsTest {

@Test
public void testNull() {
testToWritableAndBack(null);
}

@Test
public void testString() {
testToWritableAndBack("some string");
}

@Test
public void testLong() {
testToWritableAndBack(Long.MAX_VALUE);
}

@Test
public void testInteger() {
testToWritableAndBack(Integer.MAX_VALUE);
}

@Test
public void testDouble() {
testToWritableAndBack(Double.MAX_VALUE);
}

@Test
public void testFloat() {
testToWritableAndBack(Float.MAX_VALUE);
}

@Test
public void testBoolean() {
testToWritableAndBack(Boolean.TRUE);
}

@Test
public void testByte() {
testToWritableAndBack(Byte.MAX_VALUE);
}

@Test
public void testByteArray() {
testToWritableAndBack("byte array".getBytes());
}

@Test
public void testList() {
testToWritableAndBack(Arrays.asList(new String[] { "one", "two" }));
}

@Test
public void testMap() {
testToWritableAndBack(Collections.singletonMap("key", "value"));
}

private void testToWritableAndBack(Object obj) {
assertEquals(obj, WritableUtils.fromWritable(WritableUtils.toWritable(obj)));
}
}

0 comments on commit 48358d6

Please sign in to comment.