KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > DocumentParserTest


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.definition;
5
6 import org.w3c.dom.Document JavaDoc;
7 import org.w3c.dom.Element JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9 import org.xml.sax.InputSource JavaDoc;
10
11 import com.tc.aspectwerkz.DeploymentModel;
12
13 import java.io.InputStream JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @author Eugene Kuleshov
26  */

27 public class DocumentParserTest extends TestCase {
28
29   public void testParse() throws Exception JavaDoc {
30     // URL resource = getClass().getResource(getClass().getName().replace('.', '/') + ".xml");
31
URL JavaDoc resource = getClass().getResource("DocumentParserTest.xml");
32     assertNotNull(resource);
33     InputStream JavaDoc is = resource.openStream();
34     
35     Set JavaDoc definitions = DocumentParser.parse(getClass().getClassLoader(), XmlParser.createDocument(new InputSource JavaDoc(is)));
36     
37     assertTrue(!definitions.isEmpty());
38   }
39   
40   public void testParseAspectDefinition() {
41     Class JavaDoc c = FooAspect.class;
42     Class JavaDoc cc = com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy.class;
43
44     String JavaDoc xmlDef = "<aspect class=\"" + c.getName() + "\" "
45         + "name=\"foo\" "
46         + "deployment-model=\"perJVM\" "
47         + "container=\"" + cc.getName() + "\"></aspect>";
48     
49     SystemDefinition systemDef = SystemDefinitionContainer.getVirtualDefinitionFor(getClass().getClassLoader());
50     AspectDefinition definition = DocumentParser.parseAspectDefinition(xmlDef, systemDef, c);
51     
52     assertEquals(c.getName(), definition.getClassName());
53     assertEquals(cc.getName(), definition.getContainerClassName());
54     assertEquals("foo", definition.getName());
55     assertEquals(DeploymentModel.PER_JVM, definition.getDeploymentModel());
56   }
57   
58   public void testParseAspectClassNames() throws Exception JavaDoc {
59     String JavaDoc xml = "<aspectwerkz>" +
60         " <system base-package=\"bla.bla.bla\"> </system>" +
61         "<system>" +
62         " <aspect class=\"aaa1\"/>" +
63         " <aspect class=\"aaa2\"/>" +
64         "</system> " +
65         "<!-- test -->" +
66         "<system>" +
67         " <aspect class=\"aaa3\"/>" +
68         " <package><aspect class=\"ppp1\"/></package>" +
69         "</system> " +
70         "<system>" +
71         " <package><aspect class=\"ppp2\"/></package>" +
72         "</system> " +
73         "</aspectwerkz>";
74     
75     List JavaDoc classNames = DocumentParser.parseAspectClassNames(getDocument(new InputSource JavaDoc(new StringReader JavaDoc(xml))));
76     assertEquals("Error: " + classNames, 6, classNames.size());
77   }
78   
79   public void testGetText() throws Exception JavaDoc {
80     String JavaDoc xml = "<aspectwerkz> " +
81     " <system> aaaa </system>" +
82     "</aspectwerkz>";
83
84     Document JavaDoc document = getDocument(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
85     NodeList JavaDoc elements = document.getElementsByTagName("system");
86     String JavaDoc text = DocumentParser.getText((Element JavaDoc) elements.item(0));
87     assertEquals("aaaa", text.trim());
88   }
89   
90   private static Document JavaDoc getDocument(InputSource JavaDoc is) throws Exception JavaDoc {
91     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
92     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
93     return builder.parse(is);
94   }
95   
96   
97   public static class FooAspect {
98     public Object JavaDoc addRequestTag() {
99       return null;
100     }
101   }
102
103 }
104
Popular Tags