You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2011/06/28 19:53:39 UTC

svn commit: r1140753 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/cos/ main/java/org/apache/pdfbox/pdfwriter/ test/java/org/apache/pdfbox/ test/java/org/apache/pdfbox/cos/

Author: lehmi
Date: Tue Jun 28 17:53:39 2011
New Revision: 1140753

URL: http://svn.apache.org/viewvc?rev=1140753&view=rev
Log:
PDFBOX-1050: added new/improved test cases for simple cos objects as proposed by Mehdi Houshmand

Added:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSBase.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSNumber.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSStandardOutputStream.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSInteger.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSString.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java?rev=1140753&r1=1140752&r2=1140753&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java Tue Jun 28 17:53:39 2011
@@ -78,12 +78,8 @@ public class COSString extends COSBase
     private String str = null;
 
     /**
-     * Forces the string to be serialized in literal form but not hexa form.
-     */
-    private boolean forceLiteralForm = false;
-
-    /**
-     * Forces the string to be serialized in hex form but not literal form.
+     * Forces the string to be serialized in hex form but not literal form, the default is to stream
+     * in literal form.
      */
     private boolean forceHexForm = false;
 
@@ -165,7 +161,7 @@ public class COSString extends COSBase
 
     public void setForceLiteralForm(boolean v)
     {
-        forceLiteralForm = v;
+        forceHexForm = !v;
     }
 
     /**
@@ -361,7 +357,7 @@ public class COSString extends COSBase
             //outside the ASCII range.
             outsideASCII = bytes[i] <0;
         }
-        if ((!outsideASCII || forceLiteralForm) && !forceHexForm)
+        if (!outsideASCII && !forceHexForm)
         {
             output.write(STRING_OPEN);
             for( int i=0; i<length; i++ )
@@ -444,8 +440,9 @@ public class COSString extends COSBase
     {
         if (obj instanceof COSString)
         {
-            obj = ((COSString) obj).getString();
-            return this.getString().equals(obj);
+            COSString strObj = (COSString) obj;
+            return this.getString().equals(strObj.getString()) 
+                && this.forceHexForm == strObj.forceHexForm;
         }
         return false;
     }
@@ -456,6 +453,7 @@ public class COSString extends COSBase
     @Override
     public int hashCode()
     {
-        return getString().hashCode();
+        int result = getString().hashCode();
+        return result += forceHexForm ? 17 : 0; 
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSStandardOutputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSStandardOutputStream.java?rev=1140753&r1=1140752&r2=1140753&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSStandardOutputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSStandardOutputStream.java Tue Jun 28 17:53:39 2011
@@ -208,7 +208,7 @@ public class COSStandardOutputStream ext
     
     private void checkPos() throws IOException 
     {
-        if(fileChannel.position() != getPos())
+        if(fileChannel!=null && fileChannel.position() != getPos())
             throw new IOException("OutputStream has an invalid position");
     }
 

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java?rev=1140753&r1=1140752&r2=1140753&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java Tue Jun 28 17:53:39 2011
@@ -20,6 +20,8 @@ import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.pdfbox.cos.TestCOSFloat;
+import org.apache.pdfbox.cos.TestCOSInteger;
 import org.apache.pdfbox.cos.TestCOSString;
 import org.apache.pdfbox.filter.TestFilters;
 import org.apache.pdfbox.pdmodel.TestFDF;
@@ -73,6 +75,8 @@ public class TestAll extends TestCase
         suite.addTest( TestFDF.suite() );
         suite.addTest( TestFields.suite() );
         suite.addTest( TestCOSString.suite() );
+        suite.addTest( TestCOSInteger.suite() );
+        suite.addTest( TestCOSFloat.suite() );
         suite.addTestSuite( TestPDDocumentCatalog.class );
         suite.addTestSuite( TestPDDocumentInformation.class );
         suite.addTestSuite( org.apache.pdfbox.pdmodel.graphics.optionalcontent.TestOptionalContentGroups.class );

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSBase.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSBase.java?rev=1140753&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSBase.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSBase.java Tue Jun 28 17:53:39 2011
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.pdfbox.cos;
+
+import junit.framework.TestCase;
+
+import org.apache.pdfbox.filter.FilterManager;
+
+/**
+ * Test class for {@link COSBase}.
+ */
+public abstract class TestCOSBase extends TestCase
+{
+    /** The COSBase abstraction of the object being tested. */
+    protected COSBase testCOSBase;
+
+    /**
+     * Tests getFilterManager() - test that a filter manager is obtained... Not much to test here.
+     */
+    public void testGetFilterManager()
+    {
+        assertTrue(testCOSBase.getFilterManager() instanceof FilterManager);
+    }
+
+    /**
+     * Tests getCOSObject() - tests that the underlying object is returned.
+     */
+    public void testGetCOSObject()
+    {
+        assertEquals(testCOSBase, testCOSBase.getCOSObject());
+    }
+
+    /**
+     * Test accept() - tests the interface for visiting a document at the COS level.
+     */
+    public abstract void testAccept();
+
+    /**
+     * Tests isNeedToBeUpdate() and setNeedToBeUpdate() - tests the getter/setter methods.
+     */
+    public void testIsSetNeedToBeUpdate()
+    {
+        testCOSBase.setNeedToBeUpdate(true);
+        assertTrue(testCOSBase.isNeedToBeUpdate());
+        testCOSBase.setNeedToBeUpdate(false);
+        assertFalse(testCOSBase.isNeedToBeUpdate());
+    }
+
+    /**
+     * Tests isDirect() and setDirect() - tests the getter/setter methods.
+     */
+    public void testIsSetDirect()
+    {
+        testCOSBase.setDirect(true);
+        assertTrue(testCOSBase.isDirect());
+        testCOSBase.setDirect(false);
+        assertFalse(testCOSBase.isDirect());
+    }
+
+    /**
+     * A simple utility function to compare two byte arrays.
+     * @param str1 the expected byte array
+     * @param str2 the byte array being compared
+     */
+    protected void testByteArrays(byte[] byteArr1, byte[] byteArr2)
+    {
+        assertEquals(byteArr1.length, byteArr1.length);
+        for (int i = 0; i < byteArr1.length; i++)
+        {
+            assertEquals(byteArr1[i], byteArr2[i]);
+        }
+    }
+}

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java?rev=1140753&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java Tue Jun 28 17:53:39 2011
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.pdfbox.cos;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.util.Random;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.pdfbox.pdfwriter.COSWriter;
+
+/**
+ * Tests {@link COSFloat}.
+ */
+public class TestCOSFloat extends TestCOSNumber
+{
+    // Use random number to ensure various float values are expressed in the test
+    private Random rnd;
+    private DecimalFormat formatDecimal;
+    {
+        formatDecimal = (DecimalFormat) NumberFormat.getNumberInstance();
+        formatDecimal.setMaximumFractionDigits(10);
+        formatDecimal.setGroupingUsed(false);
+        DecimalFormatSymbols symbols = formatDecimal.getDecimalFormatSymbols();
+        symbols.setDecimalSeparator('.');
+        formatDecimal.setDecimalFormatSymbols(symbols);
+    }
+
+    public void setUp()
+    {
+        rnd = new Random();
+        try
+        {
+            testCOSBase = COSNumber.get("1.1");
+        }
+        catch (IOException e)
+        {
+            fail("Failed to create a COSNumber in setUp()");
+        }
+    }
+
+    /**
+     * Tests equals() - ensures that the Object.equals() contract is obeyed. These are tested over
+     * a range of arbitrary values to ensure Consistency, Reflexivity, Symmetry, Transitivity and
+     * non-nullity.
+     */
+    public void testEquals()
+    {
+        // Consistency
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat test1 = new COSFloat(num);
+            COSFloat test2 = new COSFloat(num);
+            COSFloat test3 = new COSFloat(num);
+            // Reflexive (x == x)
+            assertTrue(test1.equals(test1));
+            // Symmetric is preserved ( x==y then y==x)
+            assertTrue(test2.equals(test1));
+            assertTrue(test1.equals(test2));
+            // Transitive (if x==y && y==z then x==z)
+            assertTrue(test1.equals(test2));
+            assertTrue(test2.equals(test3));
+            assertTrue(test1.equals(test3));
+            // Non-nullity
+            assertFalse(test1.equals(null));
+            assertFalse(test2.equals(null));
+            assertFalse(test3.equals(null));
+            
+            COSFloat test4 = new COSFloat(num + 0.01f);
+            assertFalse(test4.equals(test1));
+        }
+    }
+
+    /**
+     * Tests hashCode() - ensures that the Object.hashCode() contract is obeyed over a range of
+     * arbitrary values.
+     */
+    public void testHashCode()
+    {
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat test1 = new COSFloat(num);
+            COSFloat test2 = new COSFloat(num);
+            assertEquals(test1.hashCode(), test2.hashCode());
+            
+            COSFloat test3 = new COSFloat(num + 0.01f);
+            assertFalse(test3.equals(test1));
+        }
+    }
+
+    @Override
+    public void testFloatValue()
+    {
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat testFloat = new COSFloat(num);
+            assertEquals(num, testFloat.floatValue());
+        }
+    }
+
+    @Override
+    public void testDoubleValue()
+    {
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat testFloat = new COSFloat(num);
+            assertEquals((double) num, testFloat.doubleValue());
+        }
+    }
+
+    @Override
+    public void testIntValue()
+    {
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat testFloat = new COSFloat(num);
+            assertEquals((int) num, testFloat.intValue());
+        }
+    }
+
+    @Override
+    public void testLongValue()
+    {
+        for (int i = -100000; i < 300000; i += 20000)
+        {
+            float num = i * rnd.nextFloat();
+            COSFloat testFloat = new COSFloat(num);
+            assertEquals((long) num, testFloat.longValue());
+        }
+    }
+
+    @Override
+    public void testAccept()
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        COSWriter visitor = new COSWriter(outStream);
+        float num = 0;
+        try
+        {
+            for (int i = -100000; i < 300000; i += 20000)
+            {
+                num = i * rnd.nextFloat();
+                COSFloat cosFloat = new COSFloat(num);
+                cosFloat.accept(visitor);
+                testByteArrays(formatDecimal.format(num).getBytes("ISO-8859-1"),
+                        outStream.toByteArray());
+                outStream.reset();
+            }
+        }
+        catch (Exception e)
+        {
+            fail("Failed to write " + num + " exception: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Tests writePDF() - this method takes an {@link OutputStream} and writes this object to it.
+     */
+    public void testWritePDF()
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        float num = 0;
+        try
+        {
+            for (int i = -1000; i < 3000; i += 200)
+            {
+                num = i * rnd.nextFloat();
+                COSFloat cosFloat = new COSFloat(num);
+                cosFloat.writePDF(outStream);
+                testByteArrays(formatDecimal.format(num).getBytes("ISO-8859-1"),
+                        outStream.toByteArray());
+                outStream.reset();
+            }
+        }
+        catch (IOException e)
+        {
+            fail("Failed to write " + num + " exception: " + e.getMessage());
+        }
+    }
+
+    /**
+     * This will get the suite of test that this class holds.
+     *
+     * @return All of the tests that this class holds.
+     */
+    public static Test suite()
+    {
+        return new TestSuite(TestCOSFloat.class);
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSInteger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSInteger.java?rev=1140753&r1=1140752&r2=1140753&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSInteger.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSInteger.java Tue Jun 28 17:53:39 2011
@@ -16,23 +16,173 @@
  */
 package org.apache.pdfbox.cos;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.pdfbox.pdfwriter.COSWriter;
 
 /**
  * A test case for COSInteger
- * 
+ *
  * @author Koch
  */
-public class TestCOSInteger extends TestCase
+public class TestCOSInteger extends TestCOSNumber
 {
+    public void setUp()
+    {
+        try
+        {
+            testCOSBase = COSNumber.get("0");
+        }
+        catch (IOException e)
+        {
+            fail("Failed to create a COSNumber in setUp()");
+        }
+    }
+
+    /**
+     * Tests equals() - ensures that the Object.equals() contract is obeyed. These are tested over
+     * a range of arbitrary values to ensure Consistency, Reflexivity, Symmetry, Transitivity and
+     * non-nullity.
+     */
+    public void testEquals()
+    {
+        // Consistency
+        for (int i = -1000; i < 3000; i += 200)
+        {
+            COSInteger test1 = COSInteger.get(i);
+            COSInteger test2 = COSInteger.get(i);
+            COSInteger test3 = COSInteger.get(i);
+            // Reflexive (x == x)
+            assertTrue(test1.equals(test1));
+            // Symmetric is preserved ( x==y then y===x)
+            assertTrue(test2.equals(test1));
+            assertTrue(test1.equals(test2));
+            // Transitive (if x==y && y==z then x===z)
+            assertTrue(test1.equals(test2));
+            assertTrue(test2.equals(test3));
+            assertTrue(test1.equals(test3));
+            // Non-nullity
+            assertFalse(test1.equals(null));
+            assertFalse(test2.equals(null));
+            assertFalse(test3.equals(null));
+            
+            COSInteger test4 = COSInteger.get(i + 1);
+            assertFalse(test4.equals(test1));
+        }
+    }
 
-    public void testGet()
+    /**
+     * Tests hashCode() - ensures that the Object.hashCode() contract is obeyed over a range of
+     * arbitrary values.
+     */
+    public void testHashCode()
     {
-        for (int i = -200; i <= 300; i++)
+        for (int i = -1000; i < 3000; i += 200)
         {
-            Assert.assertEquals(i, COSInteger.get(i).intValue());
+            COSInteger test1 = COSInteger.get(i);
+            COSInteger test2 = COSInteger.get(i);
+            assertEquals(test1.hashCode(), test2.hashCode());
+            
+            COSInteger test3 = COSInteger.get(i + 1);
+            assertFalse(test3.hashCode() == test1.hashCode());
         }
     }
 
+    @Override
+    public void testFloatValue()
+    {
+        for (int i = -1000; i < 3000; i += 200)
+        {
+            assertEquals((float) i, COSInteger.get(i).floatValue());
+        }
+    }
+
+    @Override
+    public void testDoubleValue()
+    {
+        for (int i = -1000; i < 3000; i += 200)
+        {
+            assertEquals((double) i, COSInteger.get(i).doubleValue());
+        }
+    }
+
+    @Override
+    public void testIntValue()
+    {
+        for (int i = -1000; i < 3000; i += 200)
+        {
+            assertEquals(i, COSInteger.get(i).intValue());
+        }
+    }
+
+    @Override
+    public void testLongValue()
+    {
+        for (int i = -1000; i < 3000; i += 200)
+        {
+            assertEquals((long) i, COSInteger.get(i).longValue());
+        }
+    }
+
+    @Override
+    public void testAccept()
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        COSWriter visitor = new COSWriter(outStream);
+        int index = 0;
+        try
+        {
+            for (int i = -1000; i < 3000; i += 200)
+            {
+                index = i;
+                COSInteger cosInt = COSInteger.get(i);
+                cosInt.accept(visitor);
+                testByteArrays(String.valueOf(i).getBytes("ISO-8859-1"), outStream.toByteArray());
+                outStream.reset();
+            }
+        }
+        catch (Exception e)
+        {
+            fail("Failed to write " + index + " exception: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Tests writePDF() - this method takes an {@link OutputStream} and writes this object to it.
+     */
+    public void testWritePDF()
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        int index = 0;
+        try
+        {
+            for (int i = -1000; i < 3000; i += 200)
+            {
+                index = i;
+                COSInteger cosInt = COSInteger.get(i);
+                cosInt.writePDF(outStream);
+                testByteArrays(String.valueOf(i).getBytes("ISO-8859-1"), outStream.toByteArray());
+                outStream.reset();
+            }
+        }
+        catch (Exception e)
+        {
+            fail("Failed to write " + index + " exception: " + e.getMessage());
+        }
+    }
+
+    /**
+     * This will get the suite of test that this class holds.
+     *
+     * @return All of the tests that this class holds.
+     */
+    public static Test suite()
+    {
+        return new TestSuite(TestCOSInteger.class);
+    }
 }
\ No newline at end of file

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSNumber.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSNumber.java?rev=1140753&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSNumber.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSNumber.java Tue Jun 28 17:53:39 2011
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.pdfbox.cos;
+
+import java.io.IOException;
+
+/**
+ * Test class for {@link COSNumber}
+ */
+public abstract class TestCOSNumber extends TestCOSBase
+{
+    /**
+     * Test floatValue() - test that the correct float value is returned.
+     */
+    public abstract void testFloatValue();
+
+    /**
+     * Test doubleValue() - test that the correct double value is returned.
+     */
+    public abstract void testDoubleValue();
+
+    /**
+     * Test intValue() - test that the correct int value is returned.
+     */
+    public abstract void testIntValue();
+
+    /**
+     * Test longValue() - test that the correct long value is returned.
+     */
+    public abstract void testLongValue();
+
+    /**
+     * Tests get() - tests a static constructor for COSNumber classes.
+     */
+    public void testGet()
+    {
+        try
+        {
+            // Ensure the basic static numbers are recognized
+            assertEquals(COSInteger.ZERO, COSNumber.get("0"));
+            assertEquals(COSInteger.ONE, COSNumber.get("1"));
+            assertEquals(COSInteger.TWO, COSNumber.get("2"));
+            assertEquals(COSInteger.THREE, COSNumber.get("3"));
+            // Test some arbitrary ints
+            assertEquals(COSInteger.get(100), COSNumber.get("100"));
+            assertEquals(COSInteger.get(256), COSNumber.get("256"));
+            assertEquals(COSInteger.get(-1000), COSNumber.get("-1000"));
+            // Some arbitrary floats
+            assertEquals(new COSFloat(1.1f), COSNumber.get("1.1"));
+            assertEquals(new COSFloat(100f), COSNumber.get("100.0"));
+            assertEquals(new COSFloat(-100.001f), COSNumber.get("-100.001"));
+            try
+            {
+                assertEquals("Null Value...", COSNumber.get(null));
+                fail("Failed to throw a NullPointerException");
+            }
+            catch (NullPointerException e)
+            {
+                // PASS
+            }
+
+        }
+        catch (IOException e)
+        {
+            fail("Failed to convert a number " + e.getMessage());
+        }
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSString.java?rev=1140753&r1=1140752&r2=1140753&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSString.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSString.java Tue Jun 28 17:53:39 2011
@@ -16,10 +16,14 @@
  */
 package org.apache.pdfbox.cos;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdfwriter.COSWriter;
 
 import junit.framework.Test;
-import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
 /**
@@ -28,18 +32,12 @@ import junit.framework.TestSuite;
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision$
  */
-public class TestCOSString extends TestCase
+public class TestCOSString extends TestCOSBase
 {
-
-    /**
-     * Constructor.
-     *
-     * @param name The name of the test to run.
-     */
-    public TestCOSString( String name )
-    {
-        super( name );
-    }
+    private final static String ESC_CHAR_STRING =
+            "( test#some) escaped< \\chars>!~1239857 ";
+    private final static String ESC_CHAR_STRING_PDF_FORMAT =
+            "\\( test#some\\) escaped< \\\\chars>!~1239857 ";
 
     /**
      * This will get the suite of test that this class holds.
@@ -48,7 +46,12 @@ public class TestCOSString extends TestC
      */
     public static Test suite()
     {
-        return new TestSuite( TestCOSString.class );
+        return new TestSuite(TestCOSString.class);
+    }
+
+    public void setUp()
+    {
+        testCOSBase = new COSString("test cos string");
     }
 
     /**
@@ -56,10 +59,251 @@ public class TestCOSString extends TestC
      *
      * @param args The command line arguments.
      */
-    public static void main( String[] args )
+    public static void main(String[] args)
+    {
+        String[] arg = {TestCOSString.class.getName()};
+        junit.textui.TestRunner.main(arg);
+    }
+
+    /**
+     * Tests the public static members within the class that are purely PDF format string objects 
+     * like open/closing strings, escape characters etc...
+     */
+    public void testStaticMembers()
+    {
+        stringByteArrayComparison("(", COSString.STRING_OPEN);
+        stringByteArrayComparison(")", COSString.STRING_CLOSE);
+        stringByteArrayComparison("<", COSString.HEX_STRING_OPEN);
+        stringByteArrayComparison(">", COSString.HEX_STRING_CLOSE);
+        stringByteArrayComparison("\\", COSString.ESCAPE);
+        stringByteArrayComparison("\\r", COSString.CR_ESCAPE);
+        stringByteArrayComparison("\\n", COSString.LF_ESCAPE);
+        stringByteArrayComparison("\\t", COSString.HT_ESCAPE);
+        stringByteArrayComparison("\\b", COSString.BS_ESCAPE);
+        stringByteArrayComparison("\\f", COSString.FF_ESCAPE);
+    }
+
+    /**
+     * Helper method for comparing a string to it's PDF byte array.
+     * 
+     * @param expected the String expected
+     * @param member the byte array being tested
+     */
+    private void stringByteArrayComparison(String expected, byte[] member)
+    {
+        byte[] expectedBytes = null;
+        try
+        {
+            expectedBytes = expected.getBytes("ISO-8859-1");
+        }
+        catch (UnsupportedEncodingException e)
+        {
+            fail("ISO-8859-1 encoding threw an exception: " + e.getMessage());
+        }
+        testByteArrays(expectedBytes, member);
+    }
+
+    /**
+     * Test setForceHexForm() and setForceLiteralForm() - tests these two methods do enforce the
+     * different String output forms within PDF. 
+     */
+    public void testSetForceHexLiteralForm()
+    {
+        String inputString = "Test with a text and a few numbers 1, 2 and 3";
+        String pdfLiteral = "(" + inputString + ")";
+        String pdfHex = "<" + createHex(inputString) + ">";
+        COSString cosStr = new COSString(inputString);
+        cosStr.setForceLiteralForm(true);
+        writePDFTests(pdfLiteral, cosStr);
+        cosStr.setForceHexForm(true);
+        writePDFTests(pdfHex, cosStr);
+        cosStr.setForceLiteralForm(true);
+        writePDFTests(pdfLiteral, cosStr);
+
+        COSString escStr = new COSString(ESC_CHAR_STRING);
+        writePDFTests("(" + ESC_CHAR_STRING_PDF_FORMAT + ")", escStr);
+        escStr.setForceHexForm(true);
+        // Escape characters not escaped in hex version
+        writePDFTests("<" + createHex(ESC_CHAR_STRING) + ">", escStr);
+    }
+
+    /**
+     * Helper method for testing writePDF().
+     * 
+     * @param expected the String expected when writePDF() is invoked
+     * @param testSubj the test subject
+     */
+    private void writePDFTests(String expected, COSString testSubj)
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        try
+        {
+            testSubj.writePDF(outStream);
+        }
+        catch (IOException e)
+        {
+            fail("IOException: " + e.getMessage());
+        }
+        assertEquals(expected, outStream.toString());
+    }
+
+    /**
+     * Test createFromHexString() - tests that the proper String is created from a hex string input.
+     */
+    public void testCreateFromHexString()
+    {
+        String expected = "Quick and simple test";
+        String hexForm = createHex(expected);
+        try
+        {
+            COSString test1 = COSString.createFromHexString(hexForm);
+            writePDFTests("(" + expected + ")", test1);
+            COSString test2 = COSString.createFromHexString(createHex(ESC_CHAR_STRING));
+            writePDFTests("(" + ESC_CHAR_STRING_PDF_FORMAT + ")", test2);
+            COSString test3 = COSString.createFromHexString(hexForm + "xx", true);
+            writePDFTests("(" + expected + "?)", test3);
+        }
+        catch (IOException e)
+        {
+            fail("IOException thrown: " + e.getMessage());
+        }
+        try
+        {
+            COSString test4 = COSString.createFromHexString(hexForm + "xx", false);
+            fail("Should have thrown an IOException here");
+        }
+        catch (IOException e)
+        {
+            // PASS
+        }
+    }
+
+    private String createHex(String str)
+    {
+        StringBuilder sb = new StringBuilder();
+        for (char c : str.toCharArray())
+        {
+            sb.append(Integer.toString(c, 16));
+        }
+        return sb.toString().toUpperCase();
+    }
+
+    /**
+     * Tests getHex() - ensure the hex String returned is properly formatted.
+     */
+    public void testGetHex()
+    {
+        String expected = "Test subject for testing getHex";
+        COSString test1 = new COSString(expected);
+        String hexForm = createHex(expected);
+        assertEquals(hexForm, test1.getHexString());
+        test1.setForceLiteralForm(true);
+        assertEquals(hexForm, test1.getHexString());
+        COSString escCS = new COSString(ESC_CHAR_STRING);
+        // Not sure whether the escaped characters should be escaped or not, presumably since 
+        // writePDF() gives you the proper formatted text, getHex() should ONLY convert to hex. 
+        assertEquals(createHex(ESC_CHAR_STRING), escCS.getHexString());
+    }
+
+    /**
+     * Test getString() - ensure string are returned in the correct format.
+     */
+    public void testGetString()
+    {
+        COSString nullStr = new COSString();
+        assertEquals("", nullStr.getString());
+        try
+        {
+            String testStr = "Test subject for getString()";
+            COSString test1 = new COSString(testStr);
+            assertEquals(testStr, test1.getString());
+
+            String appendedStr = "appended text";
+            test1.append(appendedStr.getBytes());
+            assertEquals(testStr + appendedStr, test1.getString());
+
+            test1.append(ESC_CHAR_STRING.getBytes());
+            assertEquals(testStr + appendedStr + ESC_CHAR_STRING, test1.getString());
+
+            COSString hexStr = COSString.createFromHexString(createHex(testStr));
+            assertEquals(testStr, hexStr.getString());
+
+            COSString escapedString = new COSString(ESC_CHAR_STRING);
+            assertEquals(ESC_CHAR_STRING, escapedString.getString());
+        }
+        catch (IOException e)
+        {
+            fail("IOException thrown: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Test append(int) and append(byte[]) - test both code paths. 
+     */
+    public void testAppend()
+    {
+        try
+        {
+            // Mostly tested in testGetString()
+            COSString testSubj = new COSString();
+            StringBuilder sb = new StringBuilder();
+            assertEquals(sb.toString(), testSubj.getString());
+            // Arbitrary int but makes it easy to test
+            testSubj.append('a');
+            sb.append("a");
+            assertEquals(sb.toString(), testSubj.getString());
+            testSubj.append(ESC_CHAR_STRING.getBytes());
+            sb.append(ESC_CHAR_STRING);
+            assertEquals(sb.toString(), testSubj.getString());
+            try
+            {
+                testSubj.append(null);
+                assertEquals(sb.toString(), testSubj.getString());
+                fail("NullPointerException not thrown.");
+            }
+            catch (NullPointerException e)
+            {
+                // PASS
+            }
+        }
+        catch (IOException e)
+        {
+            fail("IOException thrown: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Test reset() - tests that the internal buffer is reset. Not a great deal to test here...
+     */
+    public void testReset()
+    {
+        String str = "This string is going to be reset";
+        COSString testSubj = new COSString(str);
+        assertEquals(str, testSubj.getString());
+        testSubj.reset();
+        assertEquals("", testSubj.getString());
+    }
+
+    /**
+     * Test getBytes() - again not much to test, just ensure the proper byte array is returned.
+     */
+    public void testGetBytes()
     {
-        String[] arg = {TestCOSString.class.getName() };
-        junit.textui.TestRunner.main( arg );
+        COSString str = new COSString(ESC_CHAR_STRING);
+        testByteArrays(ESC_CHAR_STRING.getBytes(), str.getBytes());
+    }
+
+    /**
+     * Tests writePDF() - tests that the string is in PDF format.
+     */
+    public void testWritePDF()
+    {
+        // This has been tested quite thorougly above but do a couple tests anyway
+        COSString testSubj = new COSString(ESC_CHAR_STRING);
+        writePDFTests("(" + ESC_CHAR_STRING_PDF_FORMAT + ")", testSubj);
+        String textString = "This is just an arbitrary piece of text for testing";
+        COSString testSubj2 = new COSString(textString);
+        writePDFTests("(" + textString + ")", testSubj2);
     }
 
     /**
@@ -70,7 +314,90 @@ public class TestCOSString extends TestC
     public void testUnicode() throws IOException
     {
         String theString = "\u4e16";
-        COSString string = new COSString( theString );
-        assertTrue( string.getString().equals( theString ) );
+        COSString string = new COSString(theString);
+        assertTrue(string.getString().equals(theString));
+    }
+
+    @Override
+    public void testAccept()
+    {
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        ICOSVisitor visitor = new COSWriter(outStream);
+        COSString testSubj = new COSString(ESC_CHAR_STRING);
+        try
+        {
+            testSubj.accept(visitor);
+            assertEquals("(" + ESC_CHAR_STRING_PDF_FORMAT + ")", outStream.toString());
+            outStream.reset();
+            testSubj.setForceHexForm(true);
+            testSubj.accept(visitor);
+            assertEquals("<" + createHex(ESC_CHAR_STRING) + ">", outStream.toString());
+        }
+        catch (COSVisitorException e)
+        {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Tests equals(Object) - ensure that the Object.equals() contract is obeyed.
+     */
+    public void testEquals()
+    {
+        // Check all these several times for consistency
+        for (int i = 0; i < 10; i++)
+        {
+            // Reflexive
+            COSString x1 = new COSString("Test");
+            assertTrue(x1.equals(x1));
+
+            // Symmetry i.e. if x == y then y == x
+            COSString y1 = new COSString("Test");
+            assertTrue(x1.equals(y1));
+            assertTrue(y1.equals(x1));
+            COSString x2 = new COSString("Test");
+            x2.setForceHexForm(true);
+            // also if x != y then y != x
+            assertFalse(x1.equals(x2));
+            assertFalse(x2.equals(x1));
+
+            // Transitive if x == y && y == z then x == z
+            COSString z1 = new COSString("Test");
+            assertTrue(x1.equals(y1));
+            assertTrue(y1.equals(z1));
+            assertTrue(x1.equals(z1));
+            // Test the negative as well if x1 == y1 && y1 != x2 then x1 != x2
+            assertTrue(x1.equals(y1));
+            assertFalse(y1.equals(x2));
+            assertFalse(x1.equals(x2));
+
+            // Non-nullity
+            assertFalse(x1.equals(null));
+            assertFalse(y1.equals(null));
+            assertFalse(z1.equals(null));
+            assertFalse(x2.equals(null));
+
+            // Also check other state
+            COSString y2 = new COSString("Test");
+            y2.setForceLiteralForm(true);
+            assertFalse(y2.equals(x2));
+            assertTrue(y2.equals(x1));
+        }
+    }
+
+    /**
+     * Test hashCode() - tests that the Object.hashCode() contract is obeyed.
+     */
+    public void testHashCode()
+    {
+        COSString str1 = new COSString("Test1");
+        COSString str2 = new COSString("Test2");
+        assertFalse(str1.hashCode() == str2.hashCode());
+        COSString str3 = new COSString("Test1");
+        assertTrue(str1.hashCode() == str3.hashCode());
+        str3.setForceHexForm(true);
+        assertFalse(str1.hashCode() == str3.hashCode());
+        str3.setForceLiteralForm(true);
+        assertTrue(str1.hashCode() == str3.hashCode());
     }
 }