james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r746247 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/message/Entity.java test/java/org/apache/james/mime4j/message/EntityTest.java
Date Fri, 20 Feb 2009 14:17:00 GMT
Author: mwiederkehr
Date: Fri Feb 20 14:17:00 2009
New Revision: 746247

URL: http://svn.apache.org/viewvc?rev=746247&view=rev
Log:
MIME4J-119: added Entity.removeBody()

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java?rev=746247&r1=746246&r2=746247&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java Fri Feb 20
14:17:00 2009
@@ -126,13 +126,35 @@
      * Sets the body of this entity.
      * 
      * @param body the body.
+     * @throws IllegalStateException if the body has already been set.
      */
     public void setBody(Body body) {
+        if (this.body != null)
+            throw new IllegalStateException("body already set");
+
         this.body = body;
         body.setParent(this);
     }
 
     /**
+     * Removes and returns the body of this entity. The removed body may be
+     * attached to another entity. If it is no longer needed it should be
+     * {@link Disposable#dispose() disposed} of.
+     * 
+     * @return the removed body or <code>null</code> if no body was set.
+     */
+    public Body removeBody() {
+        if (body == null)
+            return null;
+
+        Body body = this.body;
+        this.body = null;
+        body.setParent(null);
+
+        return body;
+    }
+
+    /**
      * Sets the specified message as body of this entity and the content type to
      * &quot;message/rfc822&quot;. A <code>Header</code> is created if
this
      * entity does not already have one.

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java?rev=746247&r1=746246&r2=746247&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java Fri Feb
20 14:17:00 2009
@@ -26,6 +26,44 @@
 
 public class EntityTest extends TestCase {
 
+    public void testSetBody() throws Exception {
+        Entity entity = new BodyPart();
+        assertNull(entity.getBody());
+
+        Body body = new BodyFactory().textBody("test");
+        assertNull(body.getParent());
+
+        entity.setBody(body);
+        assertSame(body, entity.getBody());
+        assertSame(entity, body.getParent());
+    }
+
+    public void testSetBodyTwice() throws Exception {
+        Entity entity = new BodyPart();
+
+        Body b1 = new BodyFactory().textBody("foo");
+        Body b2 = new BodyFactory().textBody("bar");
+
+        entity.setBody(b1);
+        try {
+            entity.setBody(b2);
+            fail();
+        } catch (IllegalStateException expected) {
+        }
+    }
+
+    public void testRemoveBody() throws Exception {
+        Entity entity = new BodyPart();
+        Body body = new BodyFactory().textBody("test");
+        entity.setBody(body);
+
+        Body removed = entity.removeBody();
+        assertSame(body, removed);
+
+        assertNull(entity.getBody());
+        assertNull(removed.getParent());
+    }
+
     public void testGetDispositionType() throws Exception {
         Entity entity = new BodyPart();
 



Mime
View raw message