Java tutorial
/* * 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.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.net.URI; import java.util.Arrays; import java.util.UUID; import java.util.concurrent.TimeUnit; import javax.ws.rs.core.MediaType; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.NullInputStream; import org.junit.Before; import org.junit.Test; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; public abstract class AbstractStoreTest { protected Store store; protected URI itemURI = URI.create("http://example.com/things/1"); protected byte[] data = "Sleeping on your belly, you break my arms, you spoon my eyes".getBytes(); protected abstract Store getStore() throws Exception; @Before public void setup() throws Exception { store = getStore(); } @Test public void storedItemHasModifiedDateSetOnWrite() throws Exception { long before = System.currentTimeMillis(); TimeUnit.MILLISECONDS.sleep(1); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(itemURI, submitted); assertTrue(stored.getLastModified().getTime() > before); TimeUnit.MILLISECONDS.sleep(1); assertTrue(stored.getLastModified().getTime() < System.currentTimeMillis()); } @Test public void storedItemHasEtagGeneratedOnWrite() throws Exception { SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(itemURI, submitted); assertNotNull(stored.getEtag()); } @Test public void storedItemHasExpectedMediaType() throws Exception { SubmittedItem submitted = new SubmittedItem(MediaType.APPLICATION_JSON_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(itemURI, submitted); assertEquals(MediaType.APPLICATION_JSON_TYPE, stored.getMediaType()); } @Test public void storedItemHasExpectedMetadata() throws Exception { Multimap<String, String> meta = ArrayListMultimap.create(); meta.put("key-0", "value-0"); meta.put("key-0", "value-1"); meta.put("key-1", "value-3"); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024), meta); StoredItem stored = store.write(itemURI, submitted); Multimap<String, String> storedMeta = stored.getMetadata(); assertItemMetadataEquals(meta, stored.getMetadata()); } @Test public void readReturnsCopyOfReferencedItem() throws Exception { URI knownURI = URI.create("http://example.com/things/known-object"); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(knownURI, submitted); StoredItem retrieved = store.read(knownURI); assertStoredItemsEqual(stored, retrieved); assertNotSame(stored, retrieved); } @Test(expected = ItemNotFoundException.class) public void readThrowsExeceptionWhenReferencedItemNotFound() throws Exception { URI randomURI = URI.create("http://example.com/things/" + UUID.randomUUID().toString()); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem retrieved = store.read(randomURI); } @Test public void deleteRemovesItem() throws Exception { URI randomURI = URI.create("http://example.com/things/" + UUID.randomUUID().toString()); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(randomURI, submitted); StoredItem retrieved = store.read(randomURI); store.delete(randomURI); try { store.read(randomURI); fail("Expected an exception here"); } catch (ItemNotFoundException e) { //pass } catch (Exception e) { fail("Wrong type of exception caught"); } } @Test public void deleteIsIdempotent() throws Exception { URI randomURI = URI.create("http://example.com/things/" + UUID.randomUUID().toString()); SubmittedItem submitted = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new ByteArrayInputStream(data)); StoredItem stored = store.write(randomURI, submitted); StoredItem retrieved = store.read(randomURI); store.delete(randomURI); store.delete(randomURI); } protected void assertStoredItemsEqual(StoredItem first, StoredItem second) { assertEquals(first.getLastModified(), second.getLastModified()); assertEquals(first.getEtag(), second.getEtag()); assertEquals(first.getMediaType(), second.getMediaType()); assertItemMetadataEquals(first.getMetadata(), second.getMetadata()); try { byte[] firstEntity = IOUtils.toByteArray(first.getEntity()); byte[] secondEntity = IOUtils.toByteArray(second.getEntity()); assertTrue(Arrays.equals(firstEntity, secondEntity)); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception when reading entity"); } } protected void assertItemMetadataEquals(Multimap<String, String> first, Multimap<String, String> second) { assertTrue(Iterables.elementsEqual(first.keySet(), second.keySet())); for (String key : first.keySet()) { assertTrue(Iterables.elementsEqual(first.get(key), second.get(key))); } } }