001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.edl;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.FileNotFoundException;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.transform.Templates;
025:
026: import junit.framework.AssertionFailedError;
027:
028: import org.junit.Test;
029: import org.kuali.rice.config.Config;
030: import org.kuali.rice.core.Core;
031: import org.kuali.workflow.test.WorkflowTestCase;
032: import org.w3c.dom.Element;
033:
034: import edu.iu.uis.eden.KEWServiceLocator;
035: import edu.iu.uis.eden.WorkflowServiceErrorException;
036: import edu.iu.uis.eden.test.TestUtilities;
037: import edu.iu.uis.eden.util.XmlHelper;
038:
039: /**
040: * Tests EDocLiteServiceImpl
041: * @author Aaron Hamid (arh14 at cornell dot edu)
042: */
043: public class EDocLiteServiceImplTest extends WorkflowTestCase {
044:
045: @Test
046: public void testLoadXML() throws FileNotFoundException {
047: loadXmlFile("EDocLiteContent.xml");
048: loadXmlFile("edlstyle.xml");
049:
050: EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
051: //edls.loadXml(new FileInputStream("conf/examples/xml/EDocLiteContent.xml"));
052: assertTrue("Definition not found", edls
053: .getEDocLiteDefinitions().contains("profile"));
054: assertTrue("Style not found", edls.getEDocLiteStyles()
055: .contains("Default"));
056: assertEquals(1, edls.getEDocLiteAssociations().size());
057: EDocLiteDefinition def = edls.getEDocLiteDefinition("profile");
058: assertNotNull("'profile' definition not found", def);
059: assertEquals("profile", def.getName());
060: assertNotNull(def.getActiveInd());
061: assertTrue(def.getActiveInd().booleanValue());
062: EDocLiteStyle style = edls.getEDocLiteStyle("Default");
063: assertNotNull("'Default' style not found", style);
064: assertEquals("Default", style.getName());
065: assertNotNull(style.getActiveInd());
066: assertTrue(style.getActiveInd().booleanValue());
067: assertNotNull(style.getXmlContent());
068: }
069:
070: @Test
071: public void testLoadBadDefinition() throws FileNotFoundException {
072: EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
073: try {
074: edls.loadXml(TestUtilities.loadResource(getClass(),
075: "BadDefinition.xml"), null);
076: fail("BadDefinition was successfully parsed.");
077: } catch (RuntimeException re) {
078: // should probably use type system to detect type of error, not just message string...
079: // maybe we need general parsing or "semantic" validation exception
080: assertTrue(
081: "Wrong exception occurred",
082: re
083: .getMessage()
084: .indexOf(
085: "EDocLite definition contains references to non-existent attributes") != -1);
086: }
087: }
088:
089: @Test
090: public void testStoreDefinition() {
091: EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
092: String defXml = "<edl></edl>";
093: try {
094: edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml
095: .getBytes()));
096: throw new AssertionFailedError(
097: "Storing edl with no name succeeded");
098: } catch (WorkflowServiceErrorException wsee) {
099: // expected due to lack of name
100: }
101: defXml = "<edl name=\"test\"></edl>";
102: edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml
103: .getBytes()));
104: EDocLiteDefinition def = edls.getEDocLiteDefinition("test");
105: assertNotNull(def);
106: assertEquals("test", def.getName());
107: }
108:
109: @Test
110: public void testStoreStyle() {
111: EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
112: String styleXml = "<style></style>";
113: try {
114: edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml
115: .getBytes()));
116: throw new AssertionFailedError(
117: "Storing style with no name succeeded");
118: } catch (WorkflowServiceErrorException wsee) {
119: // expected due to lack of name
120: }
121: styleXml = "<style name=\"test\"></style>";
122: try {
123: edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml
124: .getBytes()));
125: throw new AssertionFailedError(
126: "Storing style with no xsl:stylesheet element succeeded");
127: } catch (WorkflowServiceErrorException wsee) {
128: // expected due to lack of stylesheet content
129: }
130: styleXml = "<style name=\"test\"><xsl:stylesheet></xsl:stylesheet></style>";
131: edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml
132: .getBytes()));
133: EDocLiteStyle style = edls.getEDocLiteStyle("test");
134: assertNotNull(style);
135: assertEquals("test", style.getName());
136: assertNotNull(style);
137: assertNotNull(style.getXmlContent());
138: }
139:
140: @Test
141: public void testStoreAssociation() {
142: EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
143: String assocXml = "<association></association>";
144: try {
145: edls.saveEDocLiteAssociation(new ByteArrayInputStream(
146: assocXml.getBytes()));
147: throw new AssertionFailedError(
148: "Storing association with no docType succeeded");
149: } catch (WorkflowServiceErrorException wsee) {
150: // expected due to lack of doctype
151: }
152: assocXml = "<association><docType></docType></association>";
153: try {
154: edls.saveEDocLiteAssociation(new ByteArrayInputStream(
155: assocXml.getBytes()));
156: throw new AssertionFailedError(
157: "Storing association with empty docType succeeded");
158: } catch (WorkflowServiceErrorException wsee) {
159: // expected due to emtpy doctype value
160: }
161: assocXml = "<association><docType>foobar</docType></association>";
162: edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml
163: .getBytes()));
164: EDocLiteAssociation assoc = edls
165: .getEDocLiteAssociation("foobar");
166: assertNull("Inactive Association was found", assoc);
167:
168: assocXml = "<association><docType>foobar</docType><active>true</active></association>";
169: edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml
170: .getBytes()));
171: assoc = edls.getEDocLiteAssociation("foobar");
172: assertNotNull("Association was not found", assoc);
173: assertEquals("foobar", assoc.getEdlName());
174: assertNull(assoc.getDefinition());
175: assertNull(assoc.getStyle());
176:
177: List assocs = edls.getEDocLiteAssociations();
178: assertEquals(1, assocs.size());
179: assoc = (EDocLiteAssociation) assocs.get(0);
180: assertEquals("foobar", assoc.getEdlName());
181: assertNull(assoc.getDefinition());
182: assertNull(assoc.getStyle());
183: assertNotNull(assoc.getActiveInd());
184: assertTrue(assoc.getActiveInd().booleanValue());
185:
186: assocXml = "<association><style>style name</style><definition>definition name</definition><docType>foobar</docType><active>true</active></association>";
187: edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml
188: .getBytes()));
189: assoc = edls.getEDocLiteAssociation("foobar");
190: assertNotNull("Association was not found", assoc);
191: assertEquals("foobar", assoc.getEdlName());
192: assertEquals("definition name", assoc.getDefinition());
193: assertEquals("style name", assoc.getStyle());
194:
195: assocs = edls.getEDocLiteAssociations();
196: assertEquals(1, assocs.size());
197: assoc = (EDocLiteAssociation) assocs.get(0);
198: assertNotNull("Association was not found", assoc);
199: assertEquals("foobar", assoc.getEdlName());
200: assertEquals("definition name", assoc.getDefinition());
201: assertEquals("style name", assoc.getStyle());
202: assertNotNull(assoc.getActiveInd());
203: assertTrue(assoc.getActiveInd().booleanValue());
204: }
205:
206: /**
207: * Tests the caching behavior of configs in EDocLiteServiceImpl. The config cache is a
208: * map of XML org.w3c.dom.Element to config classname mappings. This cache is, in-reality, maintained
209: * by the EDLControllerFactory.
210: */
211: @Test
212: public void testConfigCaching() throws Exception {
213: Core.getCurrentContextConfig().overrideProperty(
214: Config.EDL_CONFIG_LOCATION,
215: "classpath:edu/iu/uis/eden/edl/TestEDLConfig.xml");
216:
217: loadXmlFile("EDocLiteContent.xml");
218: loadXmlFile("edlstyle.xml");
219: loadXmlFile("widgets.xml");
220:
221: Map config = EDLControllerFactory
222: .fetchConfigFromCache("profile");
223: assertNull("Config should not be cached initially.", config);
224:
225: // fetch the edl controller which should result in caching
226: EDLController edlController = KEWServiceLocator
227: .getEDocLiteService().getEDLController(
228: "EDocLiteDocType");
229: assertNotNull(edlController);
230:
231: config = EDLControllerFactory.fetchConfigFromCache("profile");
232: assertNotNull("Config should now be cached.", config);
233:
234: // compare the config in the cache with the config on the EDLController
235: assertEquals("Config processors should be the same.",
236: edlController.getConfigProcessors().size(), config
237: .size());
238: assertEquals(1, config.size());
239: Element key1 = (Element) edlController.getConfigProcessors()
240: .keySet().iterator().next();
241: Element key2 = (Element) config.keySet().iterator().next();
242: assertEquals("Key values should be the same", XmlHelper
243: .getTextContent(key1), XmlHelper.getTextContent(key2));
244: assertEquals("Values should be the same", edlController
245: .getConfigProcessors().get(key1), config.get(key2));
246:
247: // now import the EDocLite again and it should be cleared from the cache
248: loadXmlFile("EDocLiteContent.xml");
249: config = EDLControllerFactory.fetchConfigFromCache("profile");
250: assertNull("Config should no longer be cached.", config);
251:
252: // fetch again and we should be back in action
253: edlController = KEWServiceLocator.getEDocLiteService()
254: .getEDLController("EDocLiteDocType");
255: assertNotNull(edlController);
256: config = EDLControllerFactory.fetchConfigFromCache("profile");
257: assertNotNull("Config should now be cached.", config);
258: }
259:
260: /**
261: * Tests the caching of "styles" in EDocLiteServiceImpl.
262: *
263: * The style cache is really a cache of java.xml.transform.Templates objects which represent
264: * the "compiled" stylesheets.
265: */
266: @Test
267: public void testStyleCaching() throws Exception {
268: Core.getCurrentContextConfig().overrideProperty(
269: Config.EDL_CONFIG_LOCATION,
270: "classpath:edu/iu/uis/eden/edl/TestEDLConfig.xml");
271:
272: loadXmlFile("EDocLiteContent.xml");
273: loadXmlFile("edlstyle.xml");
274: loadXmlFile("widgets.xml");
275:
276: // try to grab the templates out of the cache, it shouldn't be cached yet
277: // Templates cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
278: // assertNull("The default style template should not be cached yet.", cachedTemplates);
279:
280: // fetch the Templates object from the service
281: EDocLiteAssociation association = KEWServiceLocator
282: .getEDocLiteService().getEDocLiteAssociation(
283: "EDocLiteDocType");
284: assertNull("We should be using the Default style.", association
285: .getStyle());
286: Templates templates = KEWServiceLocator.getEDocLiteService()
287: .getStyleAsTranslet(association.getStyle());
288: assertNotNull("Templates should not be null.", templates);
289:
290: // the Templates should now be cached
291: // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
292: // assertNotNull("Templates should now be cached.", cachedTemplates);
293:
294: // // the cached Templates should be the same as the Templates we fetched from the service
295: // assertEquals("Templates should be the same.", templates, cachedTemplates);
296:
297: // now re-import the style and the templates should no longer be cached
298: loadXmlFile("edlstyle.xml");
299: // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
300: // assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
301:
302: // re-fetch the templates from the service and verify they are in the cache
303: Templates newTemplates = KEWServiceLocator.getEDocLiteService()
304: .getStyleAsTranslet(association.getStyle());
305: assertNotNull("Templates should not be null.", templates);
306: // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
307: // assertNotNull("Templates should now be cached.", cachedTemplates);
308:
309: // lastly, check that the newly cached templates are not the same as the original templates
310: assertFalse(
311: "Old Templates should be different from new Templates.",
312: templates.equals(newTemplates));
313:
314: }
315: }
|