KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > messaging > NormalizedMessageImplTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: NormalizedMessageImplTest.java 10:12:25 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.messaging;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import javax.activation.DataHandler JavaDoc;
30 import javax.activation.FileDataSource JavaDoc;
31 import javax.jbi.messaging.MessagingException;
32 import javax.security.auth.Subject JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 import junit.framework.TestCase;
37
38 import org.easymock.classextension.EasyMock;
39 import org.objectweb.petals.util.SourceHelper;
40
41 /**
42  * Test of the NormalizedMessageImpl
43  *
44  * @author ddesjardins - eBMWebsourcing
45  */

46 public class NormalizedMessageImplTest extends TestCase {
47
48     private NormalizedMessageImpl normalizedMessageImpl;
49
50     private FileDataSource JavaDoc source;
51
52     private String JavaDoc baseDir;
53
54     public void setUp() {
55         baseDir = this.getClass().getResource(".").toString();
56         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
57         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
58         source = new FileDataSource JavaDoc(new File JavaDoc(baseDir + "src" + File.separator
59             + "test-data" + File.separator + "filesource.txt"));
60         normalizedMessageImpl = new NormalizedMessageImpl();
61     }
62
63     public void testAddAttachment() throws IOException JavaDoc {
64         try {
65             normalizedMessageImpl
66                 .addAttachment("test", new DataHandler JavaDoc(source));
67         } catch (MessagingException e) {
68             fail();
69         }
70     }
71
72     public void testGetAttachment() throws IOException JavaDoc {
73         testAddAttachment();
74         DataHandler JavaDoc dataHandler = normalizedMessageImpl.getAttachment("test");
75         String JavaDoc content = (String JavaDoc) dataHandler.getContent();
76         assertEquals(content, "File used to test the normalized message");
77     }
78
79     public void testGetContent() throws IOException JavaDoc, MessagingException {
80         normalizedMessageImpl = new NormalizedMessageImpl();
81         Source JavaDoc xml = new StreamSource JavaDoc(new File JavaDoc(baseDir + "src" + File.separator
82             + "test-data" + File.separator + "descriptors" + File.separator
83             + "sharedLibraryInstallationDescriptor.xml"));
84         normalizedMessageImpl.setContent(xml);
85         StreamSource JavaDoc source = (StreamSource JavaDoc) normalizedMessageImpl.getContent();
86         assertEquals(source, xml);
87     }
88
89     public void testGetAttachmentNames() throws IOException JavaDoc {
90         testAddAttachment();
91         assertEquals(normalizedMessageImpl.getAttachmentNames().iterator()
92             .next(), "test");
93     }
94
95     public void testRemoveAttachment() throws IOException JavaDoc, MessagingException {
96         testAddAttachment();
97         normalizedMessageImpl.removeAttachment("test");
98         assertNull(normalizedMessageImpl.getAttachment("test"));
99     }
100
101     public void testRemoveAttachment1() throws IOException JavaDoc, MessagingException {
102         testAddAttachment();
103         try {
104             normalizedMessageImpl.removeAttachment("foo");
105             fail();
106         } catch (MessagingException e) {
107
108         }
109     }
110
111     public void testGetPropertyNames() throws IOException JavaDoc {
112         normalizedMessageImpl = new NormalizedMessageImpl();
113         normalizedMessageImpl.setProperty("test", "value");
114         assertEquals(
115             normalizedMessageImpl.getPropertyNames().iterator().next(), "test");
116     }
117
118     public void testGetSecuritySubject() {
119         Subject JavaDoc subject = new Subject JavaDoc();
120         normalizedMessageImpl.setSecuritySubject(subject);
121         assertEquals(normalizedMessageImpl.getSecuritySubject(), subject);
122     }
123
124     public void testSetProperty() {
125         normalizedMessageImpl.properties.put("test", EasyMock
126             .createMock(DataHandler JavaDoc.class));
127         normalizedMessageImpl.setProperty("test", null);
128         assertNull(normalizedMessageImpl.attachments.get("test"));
129     }
130
131     public void testReadObjectDelegate() throws Exception JavaDoc {
132         ObjectInputStream JavaDoc objectInputStream = EasyMock
133             .createMock(ObjectInputStream JavaDoc.class);
134
135         objectInputStream.defaultReadObject();
136         EasyMock.expect(objectInputStream.available()).andReturn(4);
137         EasyMock.expect(objectInputStream.readUTF()).andReturn("<test/>");
138         EasyMock.expect(objectInputStream.readInt()).andReturn(0);
139
140         EasyMock.replay(objectInputStream);
141
142         normalizedMessageImpl.readObjectDelegate(objectInputStream);
143         assertEquals(SourceHelper.createString(normalizedMessageImpl.content),
144             "<test/>");
145     }
146
147     public void testWriteObjectDelegate() throws IOException JavaDoc {
148         normalizedMessageImpl.content = SourceHelper.createSource("<foo/>");
149         ObjectOutputStream JavaDoc objectOutputStream = EasyMock
150             .createMock(ObjectOutputStream JavaDoc.class);
151
152         objectOutputStream.defaultWriteObject();
153         objectOutputStream.writeUTF("<foo/>");
154         objectOutputStream.writeInt(0);
155         
156         EasyMock.replay(objectOutputStream);
157         
158         normalizedMessageImpl.writeObjectDelegate(objectOutputStream);
159     }
160 }
161
Popular Tags