1 4 package com.tc.aspectwerkz.definition; 5 6 import org.w3c.dom.Document ; 7 import org.w3c.dom.Element ; 8 import org.w3c.dom.NodeList ; 9 import org.xml.sax.InputSource ; 10 11 import com.tc.aspectwerkz.DeploymentModel; 12 13 import java.io.InputStream ; 14 import java.io.StringReader ; 15 import java.net.URL ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 22 import junit.framework.TestCase; 23 24 27 public class DocumentParserTest extends TestCase { 28 29 public void testParse() throws Exception { 30 URL resource = getClass().getResource("DocumentParserTest.xml"); 32 assertNotNull(resource); 33 InputStream is = resource.openStream(); 34 35 Set definitions = DocumentParser.parse(getClass().getClassLoader(), XmlParser.createDocument(new InputSource (is))); 36 37 assertTrue(!definitions.isEmpty()); 38 } 39 40 public void testParseAspectDefinition() { 41 Class c = FooAspect.class; 42 Class cc = com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy.class; 43 44 String 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 { 59 String 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 classNames = DocumentParser.parseAspectClassNames(getDocument(new InputSource (new StringReader (xml)))); 76 assertEquals("Error: " + classNames, 6, classNames.size()); 77 } 78 79 public void testGetText() throws Exception { 80 String xml = "<aspectwerkz> " + 81 " <system> aaaa </system>" + 82 "</aspectwerkz>"; 83 84 Document document = getDocument(new InputSource (new StringReader (xml))); 85 NodeList elements = document.getElementsByTagName("system"); 86 String text = DocumentParser.getText((Element ) elements.item(0)); 87 assertEquals("aaaa", text.trim()); 88 } 89 90 private static Document getDocument(InputSource is) throws Exception { 91 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 92 DocumentBuilder builder = factory.newDocumentBuilder(); 93 return builder.parse(is); 94 } 95 96 97 public static class FooAspect { 98 public Object addRequestTag() { 99 return null; 100 } 101 } 102 103 } 104 | Popular Tags |