1 25 26 package examples; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 import org.radeox.macro.Macro; 32 import org.radeox.api.engine.context.RenderContext; 33 import org.snipsnap.render.context.SnipRenderContext; 34 import org.snipsnap.render.macro.parameter.SnipMacroParameter; 35 import org.snipsnap.snip.Snip; 36 import org.snipsnap.snip.SnipImpl; 37 import org.snipsnap.snip.SnipSpace; 38 import org.snipsnap.snip.attachment.Attachments; 39 import org.snipsnap.snip.attachment.Attachment; 40 import org.snipsnap.snip.attachment.storage.AttachmentStorage; 41 import org.snipsnap.test.mock.MockSnipSpace; 42 import org.snipsnap.container.Components; 43 44 import java.io.IOException ; 45 import java.io.StringWriter ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 49 55 56 public class AttachmentTest extends TestCase { 57 private StringWriter writer; 58 59 public AttachmentTest(String name) { 60 super(name); 61 } 62 63 protected void setUp() throws Exception { 64 writer = new StringWriter (); 65 super.setUp(); 66 } 67 68 public static Test suite() { 69 return new TestSuite(AttachmentTest.class); 70 } 71 72 public void testWriteAttachment() { 73 Snip snip = new SnipImpl("HelloSnip","HelloSnip"); 74 75 Attachment attachment = null; 76 77 try { 78 AttachmentStorage attachmentStorage = (AttachmentStorage) 80 Components.getComponent(AttachmentStorage.class); 81 OutputStream out = 82 attachmentStorage.getOutputStream(attachment); 83 assertNotNull("OutputStream not null.", out); 85 } catch (IOException e) { 86 e.printStackTrace(); 87 } 88 89 } 90 91 public void testReadAttachment() { 92 93 Snip snip = new SnipImpl("HelloSnip","HelloSnip"); 94 95 try { 96 Attachments attachments = snip.getAttachments(); 98 Attachment attachment = 99 attachments.getAttachment("MyAttachment.txt"); 100 AttachmentStorage attachmentStorage = (AttachmentStorage) 101 Components.getComponent(AttachmentStorage.class); 102 InputStream in = attachmentStorage.getInputStream(attachment); 103 assertNotNull("InputStream not null.", in); 105 } catch (IOException e) { 106 e.printStackTrace(); 107 } 108 } 109 110 public void testShowAttachments() { 111 SnipSpace space = new MockSnipSpace(); 112 Snip snip = new SnipImpl("HelloSnip","HelloSnip"); 113 114 RenderContext context = new SnipRenderContext(snip, space); 115 SnipMacroParameter parameter = new SnipMacroParameter(context); 116 117 Macro macro = new ShowAttachmentsMacro(); 118 try { 119 macro.execute(writer, parameter); 120 } catch (IllegalArgumentException e) { 121 e.printStackTrace(); 122 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } 126 assertEquals("Attachments are written.", "hello", writer.toString()); 127 } 128 } 129 | Popular Tags |