01: package org.araneaframework.tests.servlet.extension.resources;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.InputStream;
06: import java.util.ArrayList;
07: import java.util.Iterator;
08: import java.util.List;
09: import java.util.Map;
10: import javax.xml.parsers.SAXParserFactory;
11: import junit.framework.TestCase;
12: import org.araneaframework.http.extension.ExternalResource;
13: import org.araneaframework.http.extension.ExternalResourceConfigurationHandler;
14: import org.xml.sax.InputSource;
15: import org.xml.sax.XMLReader;
16:
17: public class ResourceConfigurationParsingTest extends TestCase {
18: private ExternalResource struct;
19:
20: private static final List availableGroups = new ArrayList();
21: static {
22: availableGroups.add("common-styles");
23: availableGroups.add("common-js");
24: }
25:
26: private static final List availableFilesInGroup = new ArrayList();
27: static {
28: availableFilesInGroup.add("js/mce.js");
29: availableFilesInGroup.add("js/calendar.js");
30: availableFilesInGroup.add("js/prototype.js");
31:
32: availableFilesInGroup.add("js/behaviour.js");
33: }
34:
35: private static final List allFiles = new ArrayList();
36: static {
37: allFiles.add("gfx/alfa.gif");
38: allFiles.add("gfx/beta.gif");
39: allFiles.add("gfx/deta.gif");
40: allFiles.add("css/print.css");
41: allFiles.add("css/screen.css");
42: allFiles.add("css/sexy.css");
43: allFiles.add("js/mce_helper.js");
44: allFiles.add("js/mce_popup.js");
45: allFiles.add("js/mce_debug.js");
46:
47: allFiles.addAll(availableFilesInGroup);
48: }
49:
50: public void setUp() throws Exception {
51: XMLReader xr = SAXParserFactory.newInstance().newSAXParser()
52: .getXMLReader();
53: ExternalResourceConfigurationHandler handler = new ExternalResourceConfigurationHandler();
54:
55: xr.setContentHandler(handler);
56: xr.setErrorHandler(handler);
57:
58: String s = new File(".").getAbsolutePath();
59: InputStream stream = new FileInputStream(s
60: + "/etc/extensions/resources/sample.xml");
61: xr.parse(new InputSource(stream));
62:
63: struct = handler.getResource();
64: }
65:
66: public void testGetGroups() throws Exception {
67: for (Iterator ite = struct.getGroupNames().iterator(); ite
68: .hasNext();) {
69: String groupName = (String) ite.next();
70: if (availableGroups.indexOf(groupName) == -1)
71: fail("Unknown group extracted");
72: }
73: }
74:
75: public void testGetContentsUnion() {
76: Map map = struct.getGroupByName("common-js");
77:
78: for (Iterator ite = map.entrySet().iterator(); ite.hasNext();) {
79: String file = (String) ((Map.Entry) (ite.next())).getKey();
80:
81: if (availableFilesInGroup.indexOf(file) == -1)
82: fail("Unknown file in a group");
83: }
84: }
85:
86: public void testAllowedFilesContains() {
87: for (Iterator iter = allFiles.iterator(); iter.hasNext();) {
88: String file = (String) iter.next();
89: if (!struct.isAllowedFile(file))
90: fail("File is reported not allowed although in allowed list");
91: }
92: }
93:
94: public void testNotInAllowedFiles() {
95: if (struct.isAllowedFile("nonexistent"))
96: fail("Non existent file is reported to be allowed");
97: }
98: }
|