001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.collections.test;
027:
028: import org.jicarilla.collections.AbstractXMLNodeHandler;
029: import org.jicarilla.collections.DefaultNode;
030: import org.jicarilla.collections.DefaultXMLNodeHandler;
031: import org.jicarilla.collections.Node;
032: import org.jicarilla.collections.NodeBuilder;
033: import junit.framework.TestCase;
034:
035: import javax.xml.parsers.SAXParser;
036: import javax.xml.parsers.SAXParserFactory;
037: import java.io.StringBufferInputStream;
038:
039: /**
040: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
041: * @version $Id: NodeBuilderTestCase.java,v 1.3 2004/02/26 16:51:54 lsimons Exp $
042: */
043: public class NodeBuilderTestCase extends TestCase {
044: public void testConstructor() throws Throwable {
045: Throwable t = null;
046: try {
047:
048: new NodeBuilder(null, new DefaultXMLNodeHandler());
049: } catch (AssertionError th) {
050: t = th;
051: }
052: assertNotNull(t);
053:
054: t = null;
055: try {
056: final SAXParser p = SAXParserFactory.newInstance()
057: .newSAXParser();
058: new NodeBuilder(p, null);
059: } catch (AssertionError th) {
060: t = th;
061: }
062: assertNotNull(t);
063:
064: final SAXParser p = SAXParserFactory.newInstance()
065: .newSAXParser();
066: assertNotNull(new NodeBuilder(p, new DefaultXMLNodeHandler()));
067: }
068:
069: public void testGetParser() throws Throwable {
070: final SAXParser p = SAXParserFactory.newInstance()
071: .newSAXParser();
072: final Builder b = new Builder(p, new DefaultXMLNodeHandler());
073: assertEquals(p, b.getParser());
074: }
075:
076: public void testGetHandler() throws Throwable {
077: final SAXParser p = SAXParserFactory.newInstance()
078: .newSAXParser();
079: final DefaultXMLNodeHandler h = new DefaultXMLNodeHandler();
080: final Builder b = new Builder(p, h);
081: assertEquals(h, b.getHandler());
082: }
083:
084: public void testTreeFromXML() throws Throwable {
085: final SAXParser p = SAXParserFactory.newInstance()
086: .newSAXParser();
087: final Handler h = new Handler();
088: final Builder b = new Builder(p, h);
089:
090: final String xml = "<?xml version=\"1.0\"?>\n"
091: + "<html><head><title>Blah</title></head></html>";
092: final StringBufferInputStream s = new StringBufferInputStream(
093: xml);
094:
095: final Node n = b.treeFromXML(s);
096: assertNotNull(n);
097: assertTrue(h.getNodeCalled);
098: assertTrue(h.recycleCalled);
099: }
100:
101: public final static class Builder extends NodeBuilder {
102: public Builder(final SAXParser parser,
103: final AbstractXMLNodeHandler handler) {
104: super (parser, handler);
105: }
106:
107: public SAXParser getParser() {
108: return super .getParser();
109: }
110:
111: public AbstractXMLNodeHandler getHandler() {
112: return super .getHandler();
113: }
114: }
115:
116: public final static class Handler extends DefaultXMLNodeHandler {
117: public boolean recycleCalled = false;
118: public boolean getNodeCalled = false;
119:
120: public void recycle() {
121: recycleCalled = true;
122: super .recycle();
123: }
124:
125: public DefaultNode getNode() {
126: getNodeCalled = true;
127: return super.getNode();
128: }
129: }
130: }
|