KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > ExportRssTest


1 /*
2  * Created on Mar 25, 2004
3  */

4 package org.roller.presentation.velocity;
5
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.List JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 import org.roller.model.WeblogManager;
17 import org.roller.pojos.UserData;
18 import org.roller.presentation.StrutsActionTestBase;
19
20 import com.mockrunner.mock.web.MockServletContext;
21
22 /**
23  * Not really a servlet test, the ExportRss class does require
24  * that RollerContext has been loaded and initialized.
25  *
26  * @author lance.lavandowska
27  */

28 public class ExportRssTest extends StrutsActionTestBase
29 {
30     private String JavaDoc oldUploadPath;
31     
32     /**
33      * Not sure how to test that the file was generated, so if
34      * there was no Exception we passed.
35      */

36     public void testExportRecent() throws Exception JavaDoc
37     {
38         getRoller().begin(UserData.SYSTEM_USER);
39         ExportRss exporter = new ExportRss(mWebsite);
40         
41         //List entries = getRoller().getWeblogManager().getAllRecentWeblogEntries(new Date(), 5);
42

43         List JavaDoc entries = getRoller().getWeblogManager().getWeblogEntries(
44                         null, // userName
45
null, // startDate
46
new Date JavaDoc(), // endDate
47
null, // catName
48
WeblogManager.ALL, // status
49
new Integer JavaDoc(5)); // maxEntries
50

51         try
52         {
53             // test RSS output
54
exporter.exportEntries(entries, "export-test.rss.xml");
55
56             // test Atom output
57
exporter.setExportAtom(true);
58             exporter.exportEntries(entries, "export-test.atom.xml");
59         }
60         catch (Exception JavaDoc e)
61         {
62             mLogger.error(e);
63             // I'm not sure how best to test the output!
64
// I guess no exceptions will have to do.
65
fail("Find a better way to test than checking Exceptions, bozo!");
66         }
67         finally
68         {
69             getRoller().begin(UserData.SYSTEM_USER);
70         }
71     }
72     
73     /*
74      * Need to change the UploadPath location for testing.
75      * @see junit.framework.TestCase#setUp()
76      */

77     public void setUp() throws Exception JavaDoc
78     {
79         super.setUp();
80
81         getRoller().begin(UserData.SYSTEM_USER);
82         //RollerConfigData rollerConfig = rollerContext.getRollerConfig();
83
//oldUploadPath = rollerConfig.getUploadPath();
84
//rollerConfig.setUploadPath("build/junk");
85
//rollerConfig.save();
86
getRoller().commit();
87
88         MockServletContext mContext = getMockFactory().getMockServletContext();
89         mContext.setRealPath("build/junk", "./build/junk");
90         
91         setupVelocityProperties(mContext);
92     }
93     
94     /**
95      * ExportRss needs to load velocity.properties from ServletContext.
96      * For the mock implementation we need to set the 'resource stream'.
97      * @param mContext
98      * @throws IOException
99      */

100     private void setupVelocityProperties(MockServletContext mContext) throws IOException JavaDoc
101     {
102         InputStream JavaDoc is = this.getClass().getClassLoader().getResourceAsStream("velocity.properties");
103         if (is == null) fail("Unable to find velocity.properties");
104         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is, 1);
105         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
106         int buf = bis.read();
107         do
108         {
109             bos.write(buf);
110             buf = bis.read();
111         }
112         while(buf != -1);
113         
114         byte[] bytes = bos.toByteArray();
115         mContext.setResourceAsStream("/WEB-INF/velocity.properties", bytes);
116         try
117         {
118             bos.close();
119             bis.close();
120         }
121         catch (IOException JavaDoc e)
122         {
123             e.printStackTrace();
124         }
125     }
126
127     /*
128      * Need to reset the UploadPath after testing.
129      * @see junit.framework.TestCase#tearDown()
130      */

131     public void tearDown() throws Exception JavaDoc
132     {
133         //getRoller().begin(UserData.SYSTEM_USER);
134
//RollerConfigData rollerConfig = rollerContext.getRollerConfig();
135
//rollerConfig.setUploadPath(oldUploadPath);
136
//rollerConfig.save();
137
//getRoller().commit();
138

139         super.tearDown();
140     }
141
142     public static Test suite()
143     {
144         return new TestSuite(ExportRssTest.class);
145     }
146 }
147
Popular Tags