KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > manage > FileSystemPageManagerTest


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.page.manage;
14 import java.io.File JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.StringWriter JavaDoc;
18
19 import com.openedit.BaseTestCase;
20 import com.openedit.page.Page;
21 import com.openedit.util.OutputFiller;
22
23
24 /**
25  * DOCUMENT ME!
26  *
27  * @author Eric Galluzzo
28  */

29 public class FileSystemPageManagerTest extends BaseTestCase
30 {
31     public static final String JavaDoc PATH_WITH_CONFIG = "withconfig.html";
32     public static final String JavaDoc PATH_WITH_DEFAULT = "/withdefault/index.html";
33     public static final String JavaDoc PATH_WITH_MERGE = "/withdefault/merge.html";
34     public static final String JavaDoc PATH_WITH_SITE = "/withdefault/site/merge.html";
35     public static final String JavaDoc PATH_WITHOUT_CONFIG = "withoutconfig.html";
36     public static final String JavaDoc NONEXISTENT_PATH = "this/does/not/exist.html";
37     public static final String JavaDoc NEW_CONFIG = "<page><hi-mom/></page>";
38
39
40     public FileSystemPageManagerTest(String JavaDoc inName)
41     {
42         super(inName);
43     }
44
45     /**
46      * DOCUMENT ME!
47      *
48      * @throws Exception
49      */

50     public void testDirectoryWithDefaultConfig() throws Exception JavaDoc
51     {
52         Page page = getPage(PATH_WITH_DEFAULT);
53         assertNotNull(page);
54         assertEquals( PATH_WITH_DEFAULT, page.getPath() );
55     }
56
57 /** getConfigAsString() method was thoroughly tested but never used!
58
59     public void testGetConfigAsString_Exists() throws Exception
60     {
61         assertTrue(fieldPageManager.getConfigAsString(PATH_WITH_CONFIG).length() > 0);
62     }
63
64     public void testGetConfigAsString_NoConfig() throws Exception
65     {
66         assertNull(fieldPageManager.getConfigAsString(PATH_WITHOUT_CONFIG));
67     }
68
69     public void testGetConfigAsString_NoSuchPath() throws Exception
70     {
71         assertNull(fieldPageManager.getConfigAsString(NONEXISTENT_PATH));
72     }
73 */

74     /**
75      * DOCUMENT ME!
76      *
77      * @throws Exception
78      */

79     public void testGetPage_Exists() throws Exception JavaDoc
80     {
81         Page page = getPage(PATH_WITH_CONFIG);
82         assertTrue( page.exists() );
83         assertEquals("/" + PATH_WITH_CONFIG,page.getPath() );
84     }
85
86     /**
87      * DOCUMENT ME!
88      *
89      * @throws Exception
90      */

91     public void testGetPage_NoConfig() throws Exception JavaDoc
92     {
93         assertNotNull(getPage(PATH_WITHOUT_CONFIG));
94     }
95
96     /**
97      * DOCUMENT ME!
98      *
99      * @throws Exception
100      */

101     public void testGetPage_NoSuchPath() throws Exception JavaDoc
102     {
103         assertNotNull(getPage(NONEXISTENT_PATH));
104     }
105
106     /**
107      * DOCUMENT ME!
108      *
109      * @throws Exception
110      */

111     public void testMergeConfig() throws Exception JavaDoc
112     {
113         Page page = getPage(PATH_WITH_MERGE);
114         assertNotNull(page);
115         assertEquals( PATH_WITH_MERGE, page.getPath() );
116     }
117     public void testSiteXconf() throws Exception JavaDoc
118     {
119         Page page = getPage(PATH_WITH_SITE);
120         assertNotNull(page);
121         assertEquals("subsales", page.get("section"));
122         assertEquals("Open Edit",page.get("productName"));
123         
124         assertEquals( PATH_WITH_SITE, page.getPath() );
125     }
126     public void testRemovePage_NoConfig() throws Exception JavaDoc
127     {
128         PageManager pageManager = getFixture().getPageManager();
129         pageManager.removePage(getPage(PATH_WITHOUT_CONFIG));
130         assertTrue(!getConfigFile(PATH_WITHOUT_CONFIG).exists());
131     }
132
133
134     public void testRemovePage_NoSuchPath() throws Exception JavaDoc
135     {
136         PageManager pageManager = getFixture().getPageManager();
137         pageManager.removePage(getPage(NONEXISTENT_PATH));
138         assertTrue(!getConfigFile(NONEXISTENT_PATH).exists());
139     }
140
141 /** writeConfig() method was tested but never used!
142  * It seems like we could have getPageConfiguration() and putPageConfiguration()
143  * methods on the PageManager instead of getPageConfigurationReader() and
144  * getPageConfigurationWriter() methods.
145  *
146     public void testRemovePage_Exists() throws Exception
147     {
148         fieldPageManager.writeConfig(PATH_WITHOUT_CONFIG, NEW_CONFIG);
149         fieldPageManager.removePage(fieldPageManager.getPage(PATH_WITHOUT_CONFIG));
150         assertTrue(!getConfigFile(PATH_WITHOUT_CONFIG).exists());
151     }
152
153     public void testWriteConfig_Exists() throws Exception
154     {
155         File configFile = getConfigFile(PATH_WITH_CONFIG);
156         String oldContents = getFileContents(configFile);
157
158         try
159         {
160             fieldPageManager.writeConfig(PATH_WITH_CONFIG, NEW_CONFIG);
161             assertEquals(getFileContents(configFile), NEW_CONFIG);
162         }
163         finally
164         {
165             FileWriter writer = new FileWriter(configFile);
166             writer.write(oldContents);
167             writer.close();
168         }
169     }
170
171     public void testWriteConfig_NoConfig() throws Exception
172     {
173         File configFile = getConfigFile(PATH_WITHOUT_CONFIG);
174
175         try
176         {
177             fieldPageManager.writeConfig(PATH_WITHOUT_CONFIG, NEW_CONFIG);
178             assertEquals(getFileContents(configFile), NEW_CONFIG);
179         }
180         finally
181         {
182             configFile.delete();
183         }
184     }
185
186     public void testWriteConfig_NoSuchPath() throws Exception
187     {
188         File configFile = getConfigFile(NONEXISTENT_PATH);
189
190         try
191         {
192             fieldPageManager.writeConfig(NONEXISTENT_PATH, NEW_CONFIG);
193             assertEquals(getFileContents(configFile), NEW_CONFIG);
194         }
195         finally
196         {
197             configFile.delete();
198         }
199     }
200 */

201
202     public void testSiteConfig() throws Exception JavaDoc
203     {
204         Page withSite = getPage(PATH_WITH_CONFIG);
205         assertNotNull(withSite);
206         assertEquals("Open Edit",withSite.get("productName"));
207     }
208
209     protected File JavaDoc getConfigFile(String JavaDoc inPath) throws Exception JavaDoc
210     {
211         String JavaDoc baseName = inPath;
212         int dotPos = inPath.lastIndexOf(".");
213
214         if (dotPos >= 0)
215         {
216             baseName = inPath.substring(0, dotPos);
217         }
218
219         return new File JavaDoc(getRoot(), baseName + ".xconf");
220     }
221
222     protected String JavaDoc getFileContents(File JavaDoc inFile) throws IOException JavaDoc
223     {
224         FileReader JavaDoc reader = new FileReader JavaDoc(inFile);
225         StringWriter JavaDoc writer = new StringWriter JavaDoc();
226
227         try
228         {
229             new OutputFiller().fill(reader, writer);
230         }
231         finally
232         {
233             reader.close();
234             writer.close();
235         }
236
237         return writer.toString();
238     }
239 }
240
Popular Tags