Java tutorial
/** * Copyright 2014-2015 SHAF-WORK * * 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.shaf.core.util; import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import org.apache.commons.lang.SystemUtils; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.BooleanWritable; import org.apache.hadoop.io.ByteWritable; import org.apache.hadoop.io.DoubleWritable; import org.apache.hadoop.io.FloatWritable; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.ShortWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; import org.junit.Test; import org.shaf.core.event.ActionType; /** * The class {@code IOUtilsTest} contains tests for the class {@link IOUtils}. * * @author Mykola Galushka */ public class IOUtilsTest { /** * Test method for getting Hadoop home path. */ @Test public void testGetHadoopHomePath() { try { if (System.getenv().containsKey("HADOOP_HOME")) { assertEquals(System.getenv().get("HADOOP_HOME"), IOUtils.getHadoopHomePath().toString()); } else { assertNull(IOUtils.getHadoopHomePath()); } } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for getting Hadoop configuration path. */ @Test public void testGetHadoopConfigPath() { try { if (System.getenv().containsKey("HADOOP_CONF_DIR")) { assertEquals(System.getenv().get("HADOOP_CONF_DIR"), IOUtils.getHadoopConfigPath().toString()); } else if (System.getenv().containsKey("HADOOP_HOME")) { assertEquals(System.getenv().get("HADOOP_HOME") + "/etc/hadoop", IOUtils.getHadoopConfigPath().toString()); } else { assertNull(IOUtils.getHadoopConfigPath()); } } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for getting Hadoop configuration. */ @Test public void testGetHadoopConfig() { try { if (System.getenv().containsKey("HADOOP_HOME")) { assertNotNull(IOUtils.getHadoopConfig()); } else { assertNull(IOUtils.getHadoopConfig()); } } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for checking Hadoop availability. */ @Test public void testIsHadoopAvailable() { try { if (IOUtils.getHadoopConfig() == null) { assertFalse(IOUtils.isHadoopAvailable()); } else { assertTrue(IOUtils.isHadoopAvailable()); } } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for getting local file system. */ @Test public void testLocalFileSystem() { try { assertNotNull(IOUtils.getLocalFileSystem()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for getting default file system. */ @Test public void testFileSystem() { try { assertNotNull(IOUtils.getFileSystem()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for path normalization. */ @Test public void testNormalizePath() { try { if (SystemUtils.IS_OS_WINDOWS) { assertEquals(new Path("C:/user/some/dir"), IOUtils.normalizePath("file:///C:/user/some/dir")); assertEquals(new Path("C:/user/some/dir"), IOUtils.normalizePath("C:/user/some/dir")); } else { assertEquals(new Path("/home/user/some/dir"), IOUtils.normalizePath("file:/home/user/some/dir")); assertEquals(new Path("/home/user/some/dir"), IOUtils.normalizePath("/home/user/some/dir")); } assertEquals(new Path("/home/user/some/dir"), IOUtils.normalizePath("http://localhost/home/user/some/dir")); assertEquals(new Path("/home/user/some/dir"), IOUtils.normalizePath("http://localhost:9000/home/user/some/dir")); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test method for path merging. */ @Test public void testMergePath() { // The input defined as array of Strings if (SystemUtils.IS_OS_WINDOWS) { try { assertEquals(new Path("C:/user/some/dir"), IOUtils.mergePath("file:///C:/user", "some/dir")); assertEquals(new Path("C:/user/some/dir"), IOUtils.mergePath("file:///C:/user", "/some/dir")); } catch (Exception exc) { fail(exc.getMessage()); } } else { try { assertEquals(new Path("/home/user/some/dir"), IOUtils.mergePath("file:////home/user", "some/dir")); assertEquals(new Path("/home/user/some/dir"), IOUtils.mergePath("file:////home/user", "/some/dir")); } catch (Exception exc) { fail(exc.getMessage()); } } try { IOUtils.mergePath(new String[0]); } catch (Exception exc) { assertTrue(exc instanceof IOException); assertEquals("Invalid input.", exc.getMessage()); assertTrue(exc.getCause() instanceof IllegalArgumentException); assertEquals("paths.length=0", exc.getCause().getMessage()); } try { IOUtils.mergePath("file:/C:/user", null); } catch (Exception exc) { assertTrue(exc instanceof IOException); assertEquals("Invalid input.", exc.getMessage()); assertTrue(exc.getCause() instanceof NullPointerException); assertEquals("paths[1]", exc.getCause().getMessage()); } // The input defined as array of Paths try { assertEquals(new Path("/user/some/dir"), IOUtils.mergePath(new Path("/user"), new Path("some/dir"))); assertEquals(new Path("/user/some/dir"), IOUtils.mergePath(new Path("/user"), new Path("/some/dir"))); } catch (Exception exc) { fail(exc.getMessage()); } try { IOUtils.mergePath(new Path[0]); } catch (Exception exc) { assertTrue(exc instanceof IOException); assertEquals("Invalid input.", exc.getMessage()); assertTrue(exc.getCause() instanceof IllegalArgumentException); assertEquals("paths.length=0", exc.getCause().getMessage()); } try { IOUtils.mergePath(new Path("file:/C:/user"), null); } catch (Exception exc) { assertTrue(exc instanceof IOException); assertEquals("Invalid input.", exc.getMessage()); assertTrue(exc.getCause() instanceof NullPointerException); assertEquals("paths[1]", exc.getCause().getMessage()); } } /** * Test writing exception. */ @Test public void testWritingException() { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos);) { IOUtils.writeObject(null, out); } catch (IOException exc) { assertEquals("Writing object is not defined: null.", exc.getMessage()); } } /** * Test reading exception. */ @Test public void testReadingException() { try (ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); DataInputStream in = new DataInputStream(bais);) { IOUtils.readObject(null, in); } catch (IOException exc) { assertEquals("Reading class is not defined: null.", exc.getMessage()); } } /** * Test writing of {@code boolean} value. */ @Test public void testWriteBoolean() { byte[] buf = null; // Tests write for boolean "false". boolean value = false; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { BooleanWritable probe = new BooleanWritable(); probe.readFields(in); assertFalse(probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } // Tests write for boolean "true". value = true; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { BooleanWritable probe = new BooleanWritable(); probe.readFields(in); assertTrue(probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code boolean} value. */ @Test public void testReadBoolean() { byte[] buf = null; // Tests read for boolean "false". boolean value = false; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new BooleanWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertFalse((boolean) IOUtils.readObject(boolean.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } // Tests read for boolean "true". value = true; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new BooleanWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertTrue((boolean) IOUtils.readObject(boolean.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code byte} value. */ @Test public void testWriteByte() { byte[] buf = null; byte value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { ByteWritable probe = new ByteWritable(); probe.readFields(in); assertEquals(value, probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code byte} value. */ @Test public void testReadByte() { byte[] buf = null; byte value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new ByteWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (byte) IOUtils.readObject(byte.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code short} value. */ @Test public void testWriteShort() { byte[] buf = null; short value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { ShortWritable probe = new ShortWritable(); probe.readFields(in); assertEquals(value, probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code short} value. */ @Test public void testReadShort() { byte[] buf = null; short value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new ShortWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (short) IOUtils.readObject(short.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code integer} value. */ @Test public void testWriteInt() { byte[] buf = null; int value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { IntWritable probe = new IntWritable(); probe.readFields(in); assertEquals(value, probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code integer} value. */ @Test public void testReadInt() { byte[] buf = null; int value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new IntWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (int) IOUtils.readObject(int.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code long} value. */ @Test public void testWriteLong() { byte[] buf = null; long value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { LongWritable probe = new LongWritable(); probe.readFields(in); assertEquals(value, probe.get()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code long} value. */ @Test public void testReadLong() { byte[] buf = null; int value = 123; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new LongWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (long) IOUtils.readObject(long.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code float} value. */ @Test public void testWriteFloat() { byte[] buf = null; float value = 123.456f; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { FloatWritable probe = new FloatWritable(); probe.readFields(in); assertEquals(value, probe.get(), 0.0001); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code float} value. */ @Test public void testReadFloat() { byte[] buf = null; float value = 123.456f; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new FloatWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (float) IOUtils.readObject(float.class, in), 0.0001); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code double} value. */ @Test public void testWriteDouble() { byte[] buf = null; double value = 123.456; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { DoubleWritable probe = new DoubleWritable(); probe.readFields(in); assertEquals(value, probe.get(), 0.0001); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code double} value. */ @Test public void testReadDouble() { byte[] buf = null; double value = 123.456; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { new DoubleWritable(value).write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (double) IOUtils.readObject(double.class, in), 0.0001); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code String} value. */ @Test public void testWriteString() { byte[] buf = null; String value = "some text"; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(value, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, Text.readString(in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code String} value. */ @Test public void testReadString() { byte[] buf = null; String value = "some text"; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { Text.writeString(out, value); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(value, (String) IOUtils.readObject(String.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code Array} value. */ @Test public void testWriteArray() { byte[] buf = null; String[] array = { "h", "e", "l", "l", "o" }; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(array, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { IntWritable len = new IntWritable(); len.readFields(in); String[] probe = new String[len.get()]; for (int i = 0; i < probe.length; i++) { Text text = new Text(); text.readFields(in); probe[i] = text.toString(); } assertArrayEquals(array, probe); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code Array} value. */ @Test public void testReadArray() { byte[] buf = null; String[] array = { "h", "e", "l", "l", "o" }; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IntWritable len = new IntWritable(array.length); len.write(out); for (String value : array) { Text.writeString(out, value); } buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertArrayEquals(array, (String[]) IOUtils.readObject(String[].class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code Enum} value. */ @Test public void testWriteEnum() { byte[] buf = null; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(ActionType.MAP, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { IntWritable probe = new IntWritable(); probe.readFields(in); assertEquals(ActionType.MAP, ActionType.values()[probe.get()]); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code Enum} value. */ @Test public void testReadEnum() { byte[] buf = null; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IntWritable type = new IntWritable(ActionType.MAP.ordinal()); type.write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { assertEquals(ActionType.MAP, (ActionType) IOUtils.readObject(ActionType.class, in)); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test writing of {@code SomeWritable} value. */ @Test public void testWriteObject() { byte[] buf = null; SomeWritable object = new SomeWritable(123, "hello"); try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { IOUtils.writeObject(object, out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { SomeWritable probe = new SomeWritable(); probe.readFields(in); assertEquals(123, probe.getNumber()); assertEquals("hello", probe.getName()); } catch (IOException exc) { fail(exc.getMessage()); } } /** * Test reading of {@code SomeWritable} value. */ @Test public void testReadObject() { byte[] buf = null; SomeWritable object = new SomeWritable(123, "hello"); try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos);) { object.write(out); buf = baos.toByteArray(); } catch (IOException exc) { fail(exc.getMessage()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(bais);) { SomeWritable probe = (SomeWritable) IOUtils.readObject(SomeWritable.class, in); assertEquals(123, probe.getNumber()); assertEquals("hello", probe.getName()); } catch (IOException exc) { exc.printStackTrace(); fail(exc.getMessage()); } } /** * Some writable class. * * @author Mykola Galushka * */ public static class SomeWritable implements Writable { /** * A some number. */ private int number; /** * A some name. */ private String name; /** * Constructs a new some writable object. * * @param number * a some number. * @param name * a some name. */ public SomeWritable(int number, String name) { this.number = number; this.name = name; } /** * Constructs a new some writable object. */ public SomeWritable() { this(0, null); } @Override public void readFields(DataInput in) throws IOException { this.number = in.readInt(); this.name = in.readLine(); } @Override public void write(DataOutput out) throws IOException { out.writeInt(this.number); out.writeBytes(this.name); } /** * Returns a some number; * * @return a some number; */ public int getNumber() { return this.number; } /** * Returns a some name; * * @return a some name; */ public String getName() { return this.name; } } }