001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.definition;
005:
006: import org.w3c.dom.Document;
007: import org.w3c.dom.Element;
008: import org.w3c.dom.NodeList;
009: import org.xml.sax.InputSource;
010:
011: import com.tc.aspectwerkz.DeploymentModel;
012:
013: import java.io.InputStream;
014: import java.io.StringReader;
015: import java.net.URL;
016: import java.util.List;
017: import java.util.Set;
018:
019: import javax.xml.parsers.DocumentBuilder;
020: import javax.xml.parsers.DocumentBuilderFactory;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * @author Eugene Kuleshov
026: */
027: public class DocumentParserTest extends TestCase {
028:
029: public void testParse() throws Exception {
030: // URL resource = getClass().getResource(getClass().getName().replace('.', '/') + ".xml");
031: URL resource = getClass().getResource("DocumentParserTest.xml");
032: assertNotNull(resource);
033: InputStream is = resource.openStream();
034:
035: Set definitions = DocumentParser.parse(getClass()
036: .getClassLoader(), XmlParser
037: .createDocument(new InputSource(is)));
038:
039: assertTrue(!definitions.isEmpty());
040: }
041:
042: public void testParseAspectDefinition() {
043: Class c = FooAspect.class;
044: Class cc = com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy.class;
045:
046: String xmlDef = "<aspect class=\"" + c.getName() + "\" "
047: + "name=\"foo\" " + "deployment-model=\"perJVM\" "
048: + "container=\"" + cc.getName() + "\"></aspect>";
049:
050: SystemDefinition systemDef = SystemDefinitionContainer
051: .getVirtualDefinitionFor(getClass().getClassLoader());
052: AspectDefinition definition = DocumentParser
053: .parseAspectDefinition(xmlDef, systemDef, c);
054:
055: assertEquals(c.getName(), definition.getClassName());
056: assertEquals(cc.getName(), definition.getContainerClassName());
057: assertEquals("foo", definition.getName());
058: assertEquals(DeploymentModel.PER_JVM, definition
059: .getDeploymentModel());
060: }
061:
062: public void testParseAspectClassNames() throws Exception {
063: String xml = "<aspectwerkz>"
064: + " <system base-package=\"bla.bla.bla\"> </system>"
065: + "<system>" + " <aspect class=\"aaa1\"/>"
066: + " <aspect class=\"aaa2\"/>" + "</system> "
067: + "<!-- test -->" + "<system>"
068: + " <aspect class=\"aaa3\"/>"
069: + " <package><aspect class=\"ppp1\"/></package>"
070: + "</system> " + "<system>"
071: + " <package><aspect class=\"ppp2\"/></package>"
072: + "</system> " + "</aspectwerkz>";
073:
074: List classNames = DocumentParser
075: .parseAspectClassNames(getDocument(new InputSource(
076: new StringReader(xml))));
077: assertEquals("Error: " + classNames, 6, classNames.size());
078: }
079:
080: public void testGetText() throws Exception {
081: String xml = "<aspectwerkz> " + " <system> aaaa </system>"
082: + "</aspectwerkz>";
083:
084: Document document = getDocument(new InputSource(
085: new StringReader(xml)));
086: NodeList elements = document.getElementsByTagName("system");
087: String text = DocumentParser
088: .getText((Element) elements.item(0));
089: assertEquals("aaaa", text.trim());
090: }
091:
092: private static Document getDocument(InputSource is)
093: throws Exception {
094: DocumentBuilderFactory factory = DocumentBuilderFactory
095: .newInstance();
096: DocumentBuilder builder = factory.newDocumentBuilder();
097: return builder.parse(is);
098: }
099:
100: public static class FooAspect {
101: public Object addRequestTag() {
102: return null;
103: }
104: }
105:
106: }
|