View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.edl.impl;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.property.Config;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.core.api.impex.xml.XmlIngestionException;
23  import org.kuali.rice.core.api.style.Style;
24  import org.kuali.rice.core.api.style.StyleService;
25  import org.kuali.rice.core.api.util.xml.XmlJotter;
26  import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
27  import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
28  import org.kuali.rice.edl.impl.service.EDocLiteService;
29  import org.kuali.rice.edl.impl.service.EdlServiceLocator;
30  import org.kuali.rice.kew.test.KEWTestCase;
31  import org.kuali.rice.kew.test.TestUtilities;
32  import org.kuali.rice.test.BaselineTestCase;
33  import org.w3c.dom.Element;
34  
35  import javax.xml.transform.Templates;
36  import java.io.ByteArrayInputStream;
37  import java.io.FileNotFoundException;
38  import java.util.List;
39  import java.util.Map;
40  
41  import static org.junit.Assert.*;
42  
43  /**
44   * Tests EDocLiteServiceImpl
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
48  public class EDocLiteServiceImplTest extends KEWTestCase {
49  
50  	@Test public void testLoadXML() throws FileNotFoundException {
51          loadXmlFile("EDocLiteContent.xml");
52          loadXmlFile("edlstyle.xml");
53  
54          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
55          StyleService styleService = CoreApiServiceLocator.getStyleService();
56          //edls.loadXml(new FileInputStream("conf/examples/xml/EDocLiteContent.xml"));
57          assertTrue("Definition not found", edls.getEDocLiteDefinitions().contains("profile"));
58          Style defaultStyle = styleService.getStyle("Default");
59          assertNotNull("Style not found", defaultStyle);
60          assertEquals(1, edls.getEDocLiteAssociations().size());
61          EDocLiteDefinition def = edls.getEDocLiteDefinition("profile");
62          assertNotNull("'profile' definition not found", def);
63          assertEquals("profile", def.getName());
64          assertNotNull(def.getActiveInd());
65          assertTrue(def.getActiveInd().booleanValue());
66          Style style = styleService.getStyle("Default");
67          assertNotNull("'Default' style not found", style);
68          assertEquals("Default", style.getName());
69          assertTrue(style.isActive());
70          assertNotNull(style.getXmlContent());
71      }
72  
73      @Test public void testLoadBadDefinition() throws FileNotFoundException {
74          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
75          try {
76              edls.loadXml(TestUtilities.loadResource(getClass(), "BadDefinition.xml"), null);
77              fail("BadDefinition was successfully parsed.");
78          } catch (XmlIngestionException re) {
79              // should probably use type system to detect type of error, not just message string...
80              // maybe we need general parsing or "semantic" validation exception
81              assertTrue("Wrong exception occurred", re.getMessage().contains("EDocLite definition contains references to non-existent attributes"));
82          }
83      }
84  
85      @Test public void testStoreDefinition() {
86          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
87          String defXml = "<edl></edl>";
88          try {
89              edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
90              fail("Storing edl with no name succeeded");
91          } catch (XmlIngestionException wsee) {
92              // expected due to lack of name
93          }
94          defXml = "<edl name=\"test\"></edl>";
95          edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
96          EDocLiteDefinition def = edls.getEDocLiteDefinition("test");
97          assertNotNull(def);
98          assertEquals("test", def.getName());
99      }
100 
101     @Test public void testStoreAssociation() {
102         EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
103         String assocXml = "<association></association>";
104         try {
105             edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
106             fail("Storing association with no docType succeeded");
107         } catch (XmlIngestionException wsee) {
108             // expected due to lack of doctype
109         }
110         assocXml = "<association><docType></docType></association>";
111         try {
112             edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
113             fail("Storing association with empty docType succeeded");
114         } catch (XmlIngestionException wsee) {
115             // expected due to emtpy doctype value
116         }
117         assocXml = "<association><docType>foobar</docType></association>";
118         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
119         EDocLiteAssociation assoc = edls.getEDocLiteAssociation("foobar");
120         assertNull("Inactive Association was found", assoc);
121 
122         assocXml = "<association><docType>foobar</docType><active>true</active></association>";
123         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
124         assoc = edls.getEDocLiteAssociation("foobar");
125         assertNotNull("Association was not found", assoc);
126         assertEquals("foobar", assoc.getEdlName());
127         assertNull(assoc.getDefinition());
128         assertNull(assoc.getStyle());
129 
130         List<EDocLiteAssociation> assocs = edls.getEDocLiteAssociations();
131         assertEquals(1, assocs.size());
132         assoc = assocs.get(0);
133         assertEquals("foobar", assoc.getEdlName());
134         assertNull(assoc.getDefinition());
135         assertNull(assoc.getStyle());
136         assertNotNull(assoc.getActiveInd());
137         assertTrue(assoc.getActiveInd().booleanValue());
138 
139         assocXml = "<association><style>style name</style><definition>definition name</definition><docType>foobar</docType><active>true</active></association>";
140         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
141         assoc = edls.getEDocLiteAssociation("foobar");
142         assertNotNull("Association was not found", assoc);
143         assertEquals("foobar", assoc.getEdlName());
144         assertEquals("definition name", assoc.getDefinition());
145         assertEquals("style name", assoc.getStyle());
146 
147         assocs = edls.getEDocLiteAssociations();
148         assertEquals(1, assocs.size());
149         assoc = (EDocLiteAssociation) assocs.get(0);
150         assertNotNull("Association was not found", assoc);
151         assertEquals("foobar", assoc.getEdlName());
152         assertEquals("definition name", assoc.getDefinition());
153         assertEquals("style name", assoc.getStyle());
154         assertNotNull(assoc.getActiveInd());
155         assertTrue(assoc.getActiveInd().booleanValue());
156     }
157 
158     /**
159      * Tests the caching of "styles" in EDocLiteServiceImpl.
160      *
161      * The style cache is really a cache of java.xml.transform.Templates objects which represent
162      * the "compiled" stylesheets.
163      */
164     @Test public void testStyleCaching() throws Exception {
165     	ConfigContext.getCurrentContextConfig().putProperty(Config.EDL_CONFIG_LOCATION, "classpath:org/kuali/rice/kew/edl/TestEDLConfig.xml");
166 
167     	loadXmlFile("EDocLiteContent.xml");
168         loadXmlFile("edlstyle.xml");
169         loadXmlFile("widgets.xml");
170 
171         // try to grab the templates out of the cache, it shouldn't be cached yet
172 //        Templates cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
173 //        assertNull("The default style template should not be cached yet.", cachedTemplates);
174 
175         // fetch the Templates object from the service
176         EDocLiteAssociation association = EdlServiceLocator.getEDocLiteService().getEDocLiteAssociation("EDocLiteDocType");
177         assertNull("We should be using the Default style.", association.getStyle());
178         Templates templates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
179         assertNotNull("Templates should not be null.", templates);
180 
181         // the Templates should now be cached
182 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
183 //        assertNotNull("Templates should now be cached.", cachedTemplates);
184 
185 //        // the cached Templates should be the same as the Templates we fetched from the service
186 //        assertEquals("Templates should be the same.", templates, cachedTemplates);
187 
188         // now re-import the style and the templates should no longer be cached
189         loadXmlFile("edlstyle.xml");
190 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
191 //        assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
192 
193         // re-fetch the templates from the service and verify they are in the cache
194         Templates newTemplates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
195         assertNotNull("Templates should not be null.", templates);
196 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
197 //        assertNotNull("Templates should now be cached.", cachedTemplates);
198 
199         // lastly, check that the newly cached templates are not the same as the original templates
200         assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
201 
202     }
203 }