KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > AttachmentTest


1 /*
2  * This file is part of "SnipSnap Radeox Rendering Engine".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://radeox.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48
49 /**
50  * Test for the attachment examples
51  *
52  * @author Stephan J. Schmidt
53  * @version $Id: AttachmentTest.java 1606 2004-05-17 10:56:18Z leo $
54  */

55
56 public class AttachmentTest extends TestCase {
57   private StringWriter JavaDoc writer;
58
59   public AttachmentTest(String JavaDoc name) {
60     super(name);
61   }
62
63   protected void setUp() throws Exception JavaDoc {
64     writer = new StringWriter JavaDoc();
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 // cut:start-2
79
AttachmentStorage attachmentStorage = (AttachmentStorage)
80           Components.getComponent(AttachmentStorage.class);
81       OutputStream JavaDoc out =
82           attachmentStorage.getOutputStream(attachment);
83 // cut:end-2
84
assertNotNull("OutputStream not null.", out);
85     } catch (IOException JavaDoc e) {
86       e.printStackTrace();
87     }
88
89   }
90
91   public void testReadAttachment() {
92
93     Snip snip = new SnipImpl("HelloSnip","HelloSnip");
94
95     try {
96 // cut:start-1
97
Attachments attachments = snip.getAttachments();
98       Attachment attachment =
99           attachments.getAttachment("MyAttachment.txt");
100       AttachmentStorage attachmentStorage = (AttachmentStorage)
101           Components.getComponent(AttachmentStorage.class);
102       InputStream JavaDoc in = attachmentStorage.getInputStream(attachment);
103 // cut:end-1
104
assertNotNull("InputStream not null.", in);
105     } catch (IOException JavaDoc 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 JavaDoc e) {
121       e.printStackTrace();
122
123     } catch (IOException JavaDoc e) {
124       e.printStackTrace();
125     }
126     assertEquals("Attachments are written.", "hello", writer.toString());
127   }
128 }
129
Popular Tags