[dna-commits] DNA SVN: r195 - in branches/federation/dna-spi/src: test/java/org/jboss/dna/spi/graph/impl and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Sat May 24 12:36:24 EDT 2008


Author: rhauch
Date: 2008-05-24 12:36:24 -0400 (Sat, 24 May 2008)
New Revision: 195

Added:
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/InMemoryBinaryTest.java
Modified:
   branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/InMemoryBinary.java
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BinaryContains.java
Log:
DNA-67: Create graph API for federation engine
http://jira.jboss.org/jira/browse/DNA-67

Added unit test for InMemoryBinary, added a factory method to the "BinaryContains" JUnit (Hamcrest) matcher, and added a no-arg constructor to the InMemoryBinary class so that it can be used as externalizable.

Modified: branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/InMemoryBinary.java
===================================================================
--- branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/InMemoryBinary.java	2008-05-24 15:44:34 UTC (rev 194)
+++ branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/InMemoryBinary.java	2008-05-24 16:36:24 UTC (rev 195)
@@ -38,8 +38,14 @@
 @Immutable
 public class InMemoryBinary implements Binary {
 
+    protected static final byte[] EMPTY_CONTENT = new byte[0];
+
     private byte[] bytes;
 
+    public InMemoryBinary() {
+        this.bytes = EMPTY_CONTENT;
+    }
+
     public InMemoryBinary( byte[] bytes ) {
         ArgCheck.isNotNull(bytes, "bytes");
         this.bytes = bytes;
@@ -114,7 +120,7 @@
      * {@inheritDoc}
      */
     public void writeExternal( ObjectOutput out ) throws IOException {
-        out.write(this.bytes.length);
+        out.writeInt(this.bytes.length);
         out.write(this.bytes);
     }
 

Modified: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BinaryContains.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BinaryContains.java	2008-05-24 15:44:34 UTC (rev 194)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BinaryContains.java	2008-05-24 16:36:24 UTC (rev 195)
@@ -93,4 +93,9 @@
         return new BinaryContains(expectedContent);
     }
 
+    @Factory
+    public static Matcher<Binary> hasNoContent() {
+        return new BinaryContains(new byte[0]);
+    }
+
 }

Added: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/InMemoryBinaryTest.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/InMemoryBinaryTest.java	                        (rev 0)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/InMemoryBinaryTest.java	2008-05-24 16:36:24 UTC (rev 195)
@@ -0,0 +1,134 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.spi.graph.impl;
+
+import static org.hamcrest.core.Is.is;
+import static org.jboss.dna.spi.graph.impl.BinaryContains.hasContent;
+import static org.jboss.dna.spi.graph.impl.BinaryContains.hasNoContent;
+import static org.junit.Assert.assertThat;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.jboss.dna.common.util.IoUtil;
+import org.jboss.dna.spi.graph.Binary;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class InMemoryBinaryTest {
+
+    private byte[] validByteArrayContent;
+    private String validStringContent;
+    private InMemoryBinary binary;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        validStringContent = "This is a valid string content";
+        validByteArrayContent = this.validStringContent.getBytes("UTF-8");
+        binary = new InMemoryBinary(validByteArrayContent);
+    }
+
+    @Test
+    public void shouldConstructFromByteArray() {
+        binary = new InMemoryBinary(validByteArrayContent);
+        assertThat(binary.getSize(), is((long)validByteArrayContent.length));
+        assertThat(binary, hasContent(validByteArrayContent));
+    }
+
+    @Test
+    public void shouldConstructFromEmptyByteArray() {
+        validByteArrayContent = new byte[0];
+        binary = new InMemoryBinary(validByteArrayContent);
+        assertThat(binary.getSize(), is(0l));
+        assertThat(binary, hasNoContent());
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void shouldNotConstructFromNullByteArray() {
+        new InMemoryBinary(null);
+    }
+
+    @Test
+    public void shouldHaveSizeThatMatchesContentLength() {
+        assertThat(binary.getSize(), is((long)validByteArrayContent.length));
+    }
+
+    @Test
+    public void shouldProvideInputStreamToContent() throws IOException {
+        InputStream stream = binary.getStream();
+        byte[] actual = IoUtil.readBytes(stream); // closes the stream
+        assertThat(actual.length, is(validByteArrayContent.length));
+        for (int i = 0, len = actual.length; i != len; ++i) {
+            assertThat(actual[i], is(validByteArrayContent[i]));
+        }
+    }
+
+    @Test
+    public void shouldConsiderEquivalentThoseInstancesWithSameContent() {
+        Binary another = new InMemoryBinary(validByteArrayContent);
+        assertThat(binary.equals(another), is(true));
+        assertThat(binary.compareTo(another), is(0));
+        assertThat(binary, is(another));
+        assertThat(binary, hasContent(validByteArrayContent));
+        assertThat(another, hasContent(validByteArrayContent));
+    }
+
+    @Test
+    public void shouldUseSizeWhenComparing() {
+        byte[] shorterContent = new byte[validByteArrayContent.length - 2];
+        for (int i = 0; i != shorterContent.length; ++i) {
+            shorterContent[i] = validByteArrayContent[i];
+        }
+        Binary another = new InMemoryBinary(shorterContent);
+        assertThat(binary.equals(another), is(false));
+        assertThat(binary.compareTo(another), is(1));
+        assertThat(another.compareTo(binary), is(-1));
+        assertThat(another, hasContent(shorterContent));
+    }
+
+    @Test
+    public void shouldBeRecoverableFromExternalizedForm() throws Exception {
+        ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
+        ObjectOutputStream output = new ObjectOutputStream(contentStream);
+        // Write the binary object ...
+        binary.writeExternal(output);
+        output.close();
+        // Now read the object back in ...
+        ByteArrayInputStream contentInputStream = new ByteArrayInputStream(contentStream.toByteArray());
+        ObjectInputStream input = new ObjectInputStream(contentInputStream);
+        InMemoryBinary recovered = new InMemoryBinary();
+        recovered.readExternal(input);
+        input.close();
+        // Compare ...
+        assertThat(recovered, is(binary));
+        assertThat(recovered, hasContent(binary));
+    }
+
+}


Property changes on: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/InMemoryBinaryTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the dna-commits mailing list