cz.seznam.euphoria.hadoop.SerializableWritableTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.seznam.euphoria.hadoop.SerializableWritableTest.java

Source

/**
 * Copyright 2016 Seznam.cz, a.s.
 *
 * 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 cz.seznam.euphoria.hadoop;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.junit.Assert.*;

public class SerializableWritableTest {

    @SuppressWarnings("unchecked")
    @Test
    public void testSerialization() throws Exception {
        Configuration conf = new Configuration();
        conf.set("key", "value");

        SerializableWritable<Configuration> sw = new SerializableWritable<>(conf);

        // clone by serialization
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(sw);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);

        sw = (SerializableWritable<Configuration>) ois.readObject();
        Configuration newConf = sw.getWritable();

        assertEquals(conf.get("key"), newConf.get("key"));
    }
}