com.talis.storage.s3.ExternalizableS3ObjectTest.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.storage.s3.ExternalizableS3ObjectTest.java

Source

/*
 * Copyright 2010 Talis Information Ltd
 * 
 *    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 com.talis.storage.s3;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.jets3t.service.model.S3Object;
import org.junit.Test;

public class ExternalizableS3ObjectTest {

    @Test
    public void roundTripS3ObjectSerialization() throws Exception {
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(tmp);

        byte[] bytes = ("When replying to any emails you have prior to migration "
                + "(ie any in your inbox before you have been migrated) "
                + "you will need to change the way you reply to them").getBytes();

        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);

        S3ObjectFactory factory = new S3ObjectFactory("bucket");
        S3Object original = factory.newObject("foo", 0, MediaType.TEXT_PLAIN_TYPE, buffer);

        out.writeObject(original);
        out.flush();
        out.close();

        byte[] serialized = tmp.toByteArray();

        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serialized));
        S3Object clone = (S3Object) in.readObject();

        assertObjectsEqual(original, clone);
    }

    public static void assertObjectsEqual(S3Object first, S3Object second) {
        try {
            ByteArrayOutputStream b0 = new ByteArrayOutputStream();
            IOUtils.copy(first.getDataInputStream(), b0);
            ByteArrayOutputStream b1 = new ByteArrayOutputStream();
            IOUtils.copy(first.getDataInputStream(), b1);
            assertTrue(Arrays.equals(b0.toByteArray(), b1.toByteArray()));
        } catch (Exception e) {
            fail("Caught an Exception when reading entity bytes");
        }

        assertEquals(first.getContentType(), second.getContentType());
        assertEquals(first.getKey(), second.getKey());
        assertEquals(first.getBucketName(), second.getBucketName());
        assertEquals(first.getAcl(), second.getAcl());
        assertEquals(first.isMetadataComplete(), second.isMetadataComplete());
        assertEquals(first.getMetadataMap(), second.getMetadataMap());

    }

}