You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2011/07/31 09:30:05 UTC

svn commit: r1152555 - /webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java

Author: veithen
Date: Sun Jul 31 07:30:05 2011
New Revision: 1152555

URL: http://svn.apache.org/viewvc?rev=1152555&view=rev
Log:
Increased the unit test coverage of Attachments.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java?rev=1152555&r1=1152554&r2=1152555&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java Sun Jul 31 07:30:05 2011
@@ -32,6 +32,7 @@ import org.apache.axiom.om.AbstractTestC
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.TestConstants;
 import org.apache.axiom.testutils.io.IOTestUtils;
+import org.apache.axiom.util.UIDGenerator;
 
 public class AttachmentsTest extends AbstractTestCase {
     String img1FileName = "mtom/img/test.jpg";
@@ -59,6 +60,12 @@ public class AttachmentsTest extends Abs
         assertNull(dh);
     }
 
+    public void testGetDataHandlerNonExistingMIMEPartWithoutStream() throws Exception {
+        Attachments attachments = new Attachments();
+        attachments.addDataHandler("id@apache.org", new DataHandler("test", "text/plain"));
+        assertNull(attachments.getDataHandler("non-existing@apache.org"));
+    }
+
     public void testGetAllContentIDs() throws Exception {
         InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
         Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
@@ -70,6 +77,20 @@ public class AttachmentsTest extends Abs
         assertEquals("2.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org", contentIDs[2]);
     }
     
+    public void testGetAllContentIDsWithoutStream() {
+        Attachments attachments = new Attachments();
+        // The choice of content IDs here makes sure that we test that the attachments are returned
+        // in the order in which they have been added (instead of sorted by content ID as in
+        // earlier Axiom versions)
+        attachments.addDataHandler("contentB@apache.org", new DataHandler("content1", "text/plain"));
+        attachments.addDataHandler("contentA@apache.org", new DataHandler("content2", "text/plain"));
+        
+        String[] contentIDs = attachments.getAllContentIDs();
+        assertEquals(2, contentIDs.length);
+        assertEquals("contentB@apache.org", contentIDs[0]);
+        assertEquals("contentA@apache.org", contentIDs[1]);
+    }
+    
     public void testGetContentIDSet() {
         InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
         Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
@@ -80,6 +101,18 @@ public class AttachmentsTest extends Abs
         assertTrue(idSet.contains("1.urn:uuid:A3ADBAEE51A1A87B2A11443668160943@apache.org"));
     }
     
+    public void testGetContentIDSetWithoutStream() {
+        Attachments attachments = new Attachments();
+        attachments.addDataHandler("id1@apache.org", new DataHandler("content1", "text/plain"));
+        attachments.addDataHandler("id2@apache.org", new DataHandler("content2", "text/plain"));
+        attachments.addDataHandler("id3@apache.org", new DataHandler("content3", "text/plain"));
+        
+        Set idSet = attachments.getContentIDSet();
+        assertTrue(idSet.contains("id1@apache.org"));
+        assertTrue(idSet.contains("id2@apache.org"));
+        assertTrue(idSet.contains("id3@apache.org"));
+    }
+    
     public void testGetContentLength() throws IOException {
         InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
         Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
@@ -89,6 +122,18 @@ public class AttachmentsTest extends Abs
         long fileSize = IOUtils.getStreamAsByteArray(getTestResource(TestConstants.MTOM_MESSAGE)).length;
         assertEquals("Return value of getContentLength()", fileSize, length);
     }
+    
+    /**
+     * Tests that {@link Attachments#getContentLength()} returns <code>-1</code> if the object is
+     * not stream backed.
+     * 
+     * @throws IOException
+     */
+    public void testGetContentLengthWithoutStream() throws IOException {
+        Attachments attachments = new Attachments();
+        attachments.addDataHandler(UIDGenerator.generateContentId(), new DataHandler("test", "text/plain"));
+        assertEquals(-1, attachments.getContentLength());
+    }
 
     private void testGetSOAPPartContentID(String contentTypeStartParam, String contentId)
             throws Exception {
@@ -284,7 +329,7 @@ public class AttachmentsTest extends Abs
         }
     }
 
-    public void testRemoveDataHandlerWithStream() throws Exception {
+    public void testRemoveDataHandlerAfterParsing() {
         InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
         Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
 
@@ -304,6 +349,43 @@ public class AttachmentsTest extends Abs
         assertTrue(list2.contains("2.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org"));
     }
 
+    /**
+     * Tests the behavior of {@link Attachments#removeDataHandler(String)} for a MIME part that has
+     * not yet been processed.
+     */
+    public void testRemoveDataHandlerBeforeParsing() {
+        InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
+        Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
+        attachments.removeDataHandler("1.urn:uuid:A3ADBAEE51A1A87B2A11443668160943@apache.org");
+        Set idSet = attachments.getContentIDSet();
+        assertEquals(2, idSet.size());
+        assertTrue(idSet.contains("0.urn:uuid:A3ADBAEE51A1A87B2A11443668160702@apache.org"));
+        assertFalse(idSet.contains("1.urn:uuid:A3ADBAEE51A1A87B2A11443668160943@apache.org"));
+        assertTrue(idSet.contains("2.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org"));
+    }
+    
+    /**
+     * Tests that {@link Attachments#removeDataHandler(String)} returns silently if the message
+     * doesn't contain a MIME part with the specified content ID.
+     */
+    public void testRemoveDataHandlerNonExistingWithStream() {
+        InputStream inStream = getTestResource(TestConstants.MTOM_MESSAGE);
+        Attachments attachments = new Attachments(inStream, TestConstants.MTOM_MESSAGE_CONTENT_TYPE);
+        attachments.removeDataHandler("non-existing@apache.org");
+        assertEquals(3, attachments.getContentIDSet().size());
+    }
+
+    /**
+     * Tests that {@link Attachments#removeDataHandler(String)} returns silently if no data handler
+     * with the given content ID has been added to the object.
+     */
+    public void testRemoveDataHandlerNonExistingWithoutStream() {
+        Attachments attachments = new Attachments();
+        attachments.addDataHandler("id@apache.org", new DataHandler("test", "text/plain"));
+        attachments.removeDataHandler("non-existing@apache.org");
+        assertEquals(1, attachments.getContentIDSet().size());
+    }
+
     public void testCachedFilesExpired() throws Exception {
         
         // Set file expiration to 10 seconds