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.io.StringReader;
022: import java.io.StringWriter;
023: import java.io.Writer;
024:
025: import javax.xml.transform.Templates;
026: import javax.xml.transform.TransformerConfigurationException;
027: import javax.xml.transform.TransformerException;
028: import javax.xml.transform.stream.StreamResult;
029: import javax.xml.transform.stream.StreamSource;
030:
031: import junit.framework.AssertionFailedError;
032:
033: import org.junit.Test;
034: import org.kuali.workflow.test.WorkflowTestCase;
035:
036: import edu.iu.uis.eden.KEWServiceLocator;
037: import edu.iu.uis.eden.WorkflowServiceErrorException;
038: import edu.iu.uis.eden.test.TestUtilities;
039:
040: /**
041: * Tests StyleServiceImpl
042: * @author Aaron Hamid (arh14 at cornell dot edu)
043: */
044: public class StyleServiceImplTest extends WorkflowTestCase {
045:
046: @Test
047: public void testLoadXML() throws FileNotFoundException {
048: loadXmlFile("style.xml");
049:
050: StyleService styleService = KEWServiceLocator.getStyleService();
051: assertNotNull("Style 'an_arbitrary_style' not found",
052: styleService.getStyle("an_arbitrary_style"));
053:
054: assertTrue("Style not found among all styles", styleService
055: .getStyleNames().contains("an_arbitrary_style"));
056:
057: EDocLiteStyle style = styleService
058: .getStyle("an_arbitrary_style");
059: assertNotNull("'an_arbitrary_style' style not found", style);
060: assertEquals("an_arbitrary_style", style.getName());
061: assertNotNull(style.getActiveInd());
062: assertTrue(style.getActiveInd().booleanValue());
063: assertNotNull(style.getXmlContent());
064: }
065:
066: @Test
067: public void testInclusions() throws FileNotFoundException,
068: TransformerConfigurationException, TransformerException {
069: loadXmlFile("style.xml");
070:
071: StyleService styleService = KEWServiceLocator.getStyleService();
072:
073: // ignoring the duplicate definition via inclusion test as the behavior seems
074: // unspecified
075: // XML.com claims it is an "error": http://www.xml.com/pub/a/2000/11/01/xslt/index.html
076: // XLST 1.0 spec doesn't seem to specify anything regarding this: http://www.w3.org/TR/xslt
077: // Michael Kay's XSLT Programmer's Reference states "...it is implementation-defined
078: // whether an XSLT processor will report duplicate declarations as an error , so
079: // the behavior may vary from on product to another
080: // (although it is not clear to me whether he is speaking specifically of identical
081: // literal definitions introduced by re-inclusion of the same exact stylesheet twice, or
082: // "logical" duplication of template match criteria)
083: /*Templates t = styleService.getStyleAsTranslet("test_includer");
084: StringWriter w = new StringWriter();
085: StreamResult result = new StreamResult(w);
086: try {
087: t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
088: System.err.println(w.toString());
089: fail("Exception not thrown on ambiguous template defs");
090: } catch (Exception e) {
091: // expected
092: }*/
093:
094: Writer w = new StringWriter();
095: StreamResult result = new StreamResult(w);
096: Templates t = styleService.getStyleAsTranslet("test_includer2");
097: t.newTransformer().transform(
098: new StreamSource(new StringReader("<a/>")), result);
099: assertEquals("oneoneoneoneone", w.toString());
100:
101: w = new StringWriter();
102: result = new StreamResult(w);
103: t.newTransformer().transform(
104: new StreamSource(new StringReader("<b/>")), result);
105: assertEquals("22222", w.toString());
106:
107: w = new StringWriter();
108: result = new StreamResult(w);
109: t = styleService.getStyleAsTranslet("test_importer");
110: t.newTransformer().transform(
111: new StreamSource(new StringReader("<a/>")), result);
112: assertEquals("aaaaa", w.toString());
113:
114: w = new StringWriter();
115: result = new StreamResult(w);
116: t.newTransformer().transform(
117: new StreamSource(new StringReader("<b/>")), result);
118: assertEquals("BBBBB", w.toString());
119:
120: w = new StringWriter();
121: result = new StreamResult(w);
122: t.newTransformer().transform(
123: new StreamSource(new StringReader("<c/>")), result);
124: assertEquals("CCCCC", w.toString());
125: }
126:
127: @Test
128: public void testLoadBadDefinition() throws FileNotFoundException {
129: StyleService styleService = KEWServiceLocator.getStyleService();
130: try {
131: styleService.loadXml(TestUtilities.loadResource(getClass(),
132: "badstyle.xml"), null);
133: fail("BadDefinition was successfully parsed.");
134: } catch (RuntimeException re) {
135: // should probably use type system to detect type of error, not just message string...
136: // maybe we need general parsing or "semantic" validation exception
137: assertTrue(
138: "Wrong exception occurred: " + re,
139: re
140: .getMessage()
141: .indexOf(
142: "Style 'style' element must contain a 'xsl:stylesheet' child element") != -1);
143: }
144: }
145:
146: @Test
147: public void testStoreStyle() {
148: StyleService styleService = KEWServiceLocator.getStyleService();
149: String styleXml = "<style></style>";
150: try {
151: styleService.saveStyle(new ByteArrayInputStream(styleXml
152: .getBytes()));
153: throw new AssertionFailedError(
154: "Storing style with no name succeeded");
155: } catch (WorkflowServiceErrorException wsee) {
156: // expected due to lack of name
157: }
158: styleXml = "<style name=\"test\"></style>";
159: try {
160: styleService.saveStyle(new ByteArrayInputStream(styleXml
161: .getBytes()));
162: throw new AssertionFailedError(
163: "Storing style with no xsl:stylesheet element succeeded");
164: } catch (WorkflowServiceErrorException wsee) {
165: // expected due to lack of stylesheet content
166: }
167: styleXml = "<style name=\"test\"><xsl:stylesheet></xsl:stylesheet></style>";
168: styleService.saveStyle(new ByteArrayInputStream(styleXml
169: .getBytes()));
170: EDocLiteStyle style = styleService.getStyle("test");
171: assertNotNull(style);
172: assertEquals("test", style.getName());
173: assertNotNull(style);
174: assertNotNull(style.getXmlContent());
175: }
176:
177: /**
178: * Tests the caching of "styles" in StyleServiceImpl.
179: *
180: * The style cache is really a cache of java.xml.transform.Templates objects which represent
181: * the "compiled" stylesheets.
182: */
183: @Test
184: public void testStyleCaching() throws Exception {
185: loadXmlFile("style.xml");
186:
187: // try to grab the templates out of the cache, it shouldn't be cached yet
188: Templates cachedTemplates = new StyleServiceImpl()
189: .fetchTemplatesFromCache("an_arbitrary_style");
190: assertNull(
191: "The default style template should not be cached yet.",
192: cachedTemplates);
193:
194: // fetch the Templates object from the service
195: Templates templates = KEWServiceLocator.getStyleService()
196: .getStyleAsTranslet("an_arbitrary_style");
197: assertNotNull("Templates should not be null.", templates);
198: templates = KEWServiceLocator.getStyleService()
199: .getStyleAsTranslet("an_arbitrary_style");
200: assertNotNull("Templates should not be null.", templates);
201:
202: // the Templates should now be cached
203: cachedTemplates = new StyleServiceImpl()
204: .fetchTemplatesFromCache("an_arbitrary_style");
205: assertNotNull("Templates should now be cached.",
206: cachedTemplates);
207:
208: // the cached Templates should be the same as the Templates we fetched from the service
209: assertEquals("Templates should be the same.", templates,
210: cachedTemplates);
211:
212: // now re-import the style and the templates should no longer be cached
213: loadXmlFile("style.xml");
214: cachedTemplates = new StyleServiceImpl()
215: .fetchTemplatesFromCache("an_arbitrary_style");
216: assertNull(
217: "After re-import, the Default style Templates should no longer be cached.",
218: cachedTemplates);
219:
220: // re-fetch the templates from the service and verify they are in the cache
221: Templates newTemplates = KEWServiceLocator.getStyleService()
222: .getStyleAsTranslet("an_arbitrary_style");
223: assertNotNull("Templates should not be null.", templates);
224: newTemplates = KEWServiceLocator.getStyleService()
225: .getStyleAsTranslet("an_arbitrary_style");
226: assertNotNull("Templates should not be null.", templates);
227:
228: cachedTemplates = new StyleServiceImpl()
229: .fetchTemplatesFromCache("an_arbitrary_style");
230: assertNotNull("Templates should now be cached.",
231: cachedTemplates);
232:
233: // lastly, check that the newly cached templates are not the same as the original templates
234: assertFalse(
235: "Old Templates should be different from new Templates.",
236: templates.equals(newTemplates));
237:
238: }
239: }
|