com.talis.storage.ItemFactoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.storage.ItemFactoryTest.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;

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

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

public class ItemFactoryTest {

    private byte[] entityBytes;
    private ItemFactory factory;

    @Before
    public void setup() {
        factory = new ItemFactory();
        entityBytes = "One baby to another says I'm lucky to have met you".getBytes();
    }

    @Test
    public void createItemWithCorrectMediaType() {
        SubmittedItem item = factory.newSubmittedItem(MediaType.APPLICATION_XML_TYPE,
                new ByteArrayInputStream(entityBytes));
        assertEquals(MediaType.APPLICATION_XML_TYPE, item.getMediaType());

        item = factory.newSubmittedItem(MediaType.APPLICATION_JSON_TYPE, new ByteArrayInputStream(entityBytes));
        assertEquals(MediaType.APPLICATION_JSON_TYPE, item.getMediaType());

        item = factory.newSubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(entityBytes));
        assertEquals(MediaType.TEXT_PLAIN_TYPE, item.getMediaType());
    }

    @Test
    public void ifMediaTypeIsNullSubstituteDefault() throws Exception {
        SubmittedItem item = factory.newSubmittedItem(null, new ByteArrayInputStream(entityBytes));
        assertEquals(ItemFactory.DEFAULT_MEDIA_TYPE, item.getMediaType());
    }

    @Test
    public void createItemWithCorrectEntity() throws Exception {
        SubmittedItem item = factory.newSubmittedItem(MediaType.APPLICATION_XML_TYPE,
                new ByteArrayInputStream(entityBytes));
        assertTrue(Arrays.equals(entityBytes, IOUtils.toByteArray(item.getEntity())));
    }
}