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

Java tutorial

Introduction

Here is the source code for com.talis.storage.s3.S3ObjectFactoryTest.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 java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.UUID;

import javax.ws.rs.core.MediaType;

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

public class S3ObjectFactoryTest {

    private S3ObjectFactory factory;
    private String bucketname;
    private byte[] entity = new String(
            "Then I'm laying out my winter clothes and " + "wishing I was gone, Going home").getBytes();
    private ByteBuffer buffer;

    @Before
    public void setup() {
        bucketname = UUID.randomUUID().toString();
        factory = new S3ObjectFactory(bucketname);
        buffer = ByteBuffer.allocate(1024);
        buffer.put(entity);
    }

    @Test
    public void returnedObjectIsAnExternalizableS3Object() throws Exception {
        S3Object obj = factory.newObject("first", 8, MediaType.TEXT_PLAIN_TYPE, buffer);
        assertTrue(obj instanceof ExternalizableS3Object);
    }

    @Test
    public void createdObjectKeyDerivedFromKeyAndIndex() throws Exception {
        S3Object obj = factory.newObject("first", 8, MediaType.TEXT_PLAIN_TYPE, buffer);
        assertEquals("first/8", obj.getKey());
    }

    @Test
    public void createdObjectDataContainsBytesFromBuffer() throws Exception {
        S3Object obj = factory.newObject("second", 8, MediaType.TEXT_PLAIN_TYPE, buffer);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(obj.getDataInputStream(), out);
        assertTrue(Arrays.equals(entity, out.toByteArray()));
    }

    @Test
    public void createdObjectHasCorrectContentLengthSet() throws Exception {
        S3Object obj = factory.newObject("third", 8, MediaType.TEXT_PLAIN_TYPE, buffer);
        assertEquals(entity.length, obj.getContentLength());
    }

    @Test
    public void createdObjectHasChunkMediaType() throws Exception {
        S3Object obj = factory.newObject("third", 8, MediaType.TEXT_PLAIN_TYPE, buffer);
        assertEquals(S3Store.TMB_CHUNK_TYPE, MediaType.valueOf(obj.getContentType()));
    }

    @Test
    public void createObjectMetadataContainsOriginalMediaType() throws Exception {
        S3Object obj = factory.newObject("fourth", 8, MediaType.TEXT_PLAIN_TYPE, buffer);
        assertEquals(MediaType.TEXT_PLAIN, obj.getMetadata(S3Store.ACTUAL_CONTENT_TYPE_HEADER));

        obj = factory.newObject("fifth", 8, MediaType.APPLICATION_OCTET_STREAM_TYPE, buffer);
        assertEquals(MediaType.APPLICATION_OCTET_STREAM, obj.getMetadata(S3Store.ACTUAL_CONTENT_TYPE_HEADER));

    }
}