1 22 package org.objectweb.petals.jbi.messaging; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 29 import javax.activation.DataHandler ; 30 import javax.activation.FileDataSource ; 31 import javax.jbi.messaging.MessagingException; 32 import javax.security.auth.Subject ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 import junit.framework.TestCase; 37 38 import org.easymock.classextension.EasyMock; 39 import org.objectweb.petals.util.SourceHelper; 40 41 46 public class NormalizedMessageImplTest extends TestCase { 47 48 private NormalizedMessageImpl normalizedMessageImpl; 49 50 private FileDataSource source; 51 52 private String 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 (new File (baseDir + "src" + File.separator 59 + "test-data" + File.separator + "filesource.txt")); 60 normalizedMessageImpl = new NormalizedMessageImpl(); 61 } 62 63 public void testAddAttachment() throws IOException { 64 try { 65 normalizedMessageImpl 66 .addAttachment("test", new DataHandler (source)); 67 } catch (MessagingException e) { 68 fail(); 69 } 70 } 71 72 public void testGetAttachment() throws IOException { 73 testAddAttachment(); 74 DataHandler dataHandler = normalizedMessageImpl.getAttachment("test"); 75 String content = (String ) dataHandler.getContent(); 76 assertEquals(content, "File used to test the normalized message"); 77 } 78 79 public void testGetContent() throws IOException , MessagingException { 80 normalizedMessageImpl = new NormalizedMessageImpl(); 81 Source xml = new StreamSource (new File (baseDir + "src" + File.separator 82 + "test-data" + File.separator + "descriptors" + File.separator 83 + "sharedLibraryInstallationDescriptor.xml")); 84 normalizedMessageImpl.setContent(xml); 85 StreamSource source = (StreamSource ) normalizedMessageImpl.getContent(); 86 assertEquals(source, xml); 87 } 88 89 public void testGetAttachmentNames() throws IOException { 90 testAddAttachment(); 91 assertEquals(normalizedMessageImpl.getAttachmentNames().iterator() 92 .next(), "test"); 93 } 94 95 public void testRemoveAttachment() throws IOException , MessagingException { 96 testAddAttachment(); 97 normalizedMessageImpl.removeAttachment("test"); 98 assertNull(normalizedMessageImpl.getAttachment("test")); 99 } 100 101 public void testRemoveAttachment1() throws IOException , 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 { 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 subject = new Subject (); 120 normalizedMessageImpl.setSecuritySubject(subject); 121 assertEquals(normalizedMessageImpl.getSecuritySubject(), subject); 122 } 123 124 public void testSetProperty() { 125 normalizedMessageImpl.properties.put("test", EasyMock 126 .createMock(DataHandler .class)); 127 normalizedMessageImpl.setProperty("test", null); 128 assertNull(normalizedMessageImpl.attachments.get("test")); 129 } 130 131 public void testReadObjectDelegate() throws Exception { 132 ObjectInputStream objectInputStream = EasyMock 133 .createMock(ObjectInputStream .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 { 148 normalizedMessageImpl.content = SourceHelper.createSource("<foo/>"); 149 ObjectOutputStream objectOutputStream = EasyMock 150 .createMock(ObjectOutputStream .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 |