FileDocCategorySizeDatePackage
VariableResolverTest.javaAPI DocGlassfish v2 API9823Fri May 04 22:24:40 BST 2007com.sun.enterprise.config.serverbeans.validation

VariableResolverTest

public class VariableResolverTest extends TestCase
author
Toby H Ferguson
version
$Revision: 1.4 $

Fields Summary
VariableResolver
vr
StringWriter
out
Constructors Summary
public VariableResolverTest(String name)

        super(name);
    
Methods Summary
private org.xml.sax.ContentHandlergetContentHandler(java.io.StringWriter w)

        ToXMLStream xml = new ToXMLStream();
        xml.setWriter(w);
        xml.setOmitXMLDeclaration(true);
        return xml.asContentHandler();
    
private org.xml.sax.InputSourcegetInputSource(java.lang.String s)

        return new InputSource(new StringReader(s));
    
private org.xml.sax.XMLReadergetXMLReader()

        final XMLReader xr =  SAXParserFactory.newInstance().newSAXParser().getXMLReader();
        xr.setFeature("http://xml.org/sax/features/namespaces", true);
        xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
        return xr;
    
public static voidmain(java.lang.String[] args)

        if (args.length == 0){
            junit.textui.TestRunner.run(VariableResolverTest.class);
        } else {
            junit.textui.TestRunner.run(makeSuite(args));
        }
    
private static junit.framework.TestSuitemakeSuite(java.lang.String[] args)

        final TestSuite ts = new TestSuite();
        for (int i = 0; i < args.length; i++){
            ts.addTest(new VariableResolverTest(args[i]));
        }
        return ts;
    
private voidnyi()

        fail("Not Yet Implemented");
    
protected voidsetUp()

        vr = new VariableResolver();
        vr.setParent(getXMLReader());
        out = new StringWriter();
        vr.setContentHandler(getContentHandler(out));
    
protected voidtearDown()

    
public voidtestAComplexExample()

        String input = "<r>"+
        "<foo><bar><gargle/>la di da</bar><bar>hick</bar>"+
        "<system-property name=\"k1\" value=\"this is the value of k1\"/>"+
        "A reference to k1 yields: \"${k1}\""+
        "</foo>"+
        "<config name=\"config1\">" +
        "k1: \"${k1}\""+
        "<system-property name=\"k1\" value=\"K1 VALUE\"/>"+
        "</config>"+
        "<config name=\"config2\">"+
        "k1: \"${k1}\""+
        "</config>"+
        "</r>";
        String expected = "<r>"+
        "<foo><bar><gargle/>la di da</bar><bar>hick</bar>"+
        "<system-property name=\"k1\" value=\"this is the value of k1\"/>"+
        "A reference to k1 yields: \"this is the value of k1\""+
        "</foo>"+
        "<config name=\"config1\">" +
        "k1: \"K1 VALUE\""+
        "<system-property name=\"k1\" value=\"K1 VALUE\"/>"+
        "</config>"+
        "<config name=\"config2\">"+
        "k1: \"this is the value of k1\""+
        "</config>"+
        "</r>";
        vr.parse(getInputSource(input));
        assertEquals(expected, ""+out);
    
public voidtestBasicOperation()

        String doc = "<foo/>";
        vr.parse(getInputSource(doc));
        out.flush();
        assertEquals("<foo/>", ""+out);
    
public voidtestBasicSystemVariable()

        String input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>";
        String expected = "<foo att=\"value\"><system-property name=\"var\" value=\"value\"/></foo>";
        vr.parse(getInputSource(input));
        assertEquals(expected, ""+out);
    
public voidtestConfigStuff()

        String input = "<r>"+
        "<config name=\"c1\" att=\"${k1}\">"+
        "<child>${k2}</child>"+
        "<system-property name=\"k2\" value=\"v2\"/>"+
        "</config>"+
        "<system-property name=\"k1\" value=\"v1\"/>"+
        "</r>";
        String expected = "<r>"+
        "<config name=\"c1\" att=\"v1\">"+
        "<child>v2</child>"+
        "<system-property name=\"k2\" value=\"v2\"/>"+
        "</config>"+
        "<system-property name=\"k1\" value=\"v1\"/>"+
        "</r>";
        vr.parse(getInputSource(input));
        assertEquals(expected, ""+out);
    
public voidtestFile()

        vr.parse(new InputSource("test.xml"));
        
        String expected = "<r>\n"+
        "  <foo>\n"+
        "    <bar>\n"+
        "      <gargle>la di da</gargle>\n"+
        "    </bar>\n"+
        "    <bar>\n"+
        "      hick\n"+
        "    </bar>\n"+
        "    <system-property name=\"k1\" value=\"this is the value of k1\"/>\n"+
        "    A reference to k1 yields: \"this is the value of k1\"\n"+
        "  </foo>\n"+
        "  <config name=\"config1\">\n"+
        "    k1: \"K1 VALUE\"\n"+
        "    <system-property name=\"k1\" value=\"K1 VALUE\"/>\n"+
        "  </config>\n"+
        "  <config name=\"config2\">\n"+
        "    K1: \"this is the value of k1\"\n"+
        "  </config>\n"+
        "</r>";
        assertEquals(expected, ""+out);
    
public voidtestSimpleExpansion()

        String input = "<foo>${path.separator}</foo>";
        vr.parse(getInputSource(input));
        assertEquals("<foo>"+System.getProperty("path.separator")+"</foo>", ""+out);
    
public voidtestURI()

        final String uri = new File ("test.xml").toURI().toURL().toString();
        vr.parse(new InputSource(uri));
        
        String expected = "<r>\n"+
        "  <foo>\n"+
        "    <bar>\n"+
        "      <gargle>la di da</gargle>\n"+
        "    </bar>\n"+
        "    <bar>\n"+
        "      hick\n"+
        "    </bar>\n"+
        "    <system-property name=\"k1\" value=\"this is the value of k1\"/>\n"+
        "    A reference to k1 yields: \"this is the value of k1\"\n"+
        "  </foo>\n"+
        "  <config name=\"config1\">\n"+
        "    k1: \"K1 VALUE\"\n"+
        "    <system-property name=\"k1\" value=\"K1 VALUE\"/>\n"+
        "  </config>\n"+
        "  <config name=\"config2\">\n"+
        "    K1: \"this is the value of k1\"\n"+
        "  </config>\n"+
        "</r>";
        assertEquals(expected, ""+out);
    
public voidtestUseOfDefaultReader()

        VariableResolver myVr = new VariableResolver();
        myVr.setContentHandler(getContentHandler(out));
        String input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>";
        String expected = "<foo att=\"value\"><system-property name=\"var\" value=\"value\"/></foo>";
        myVr.parse(getInputSource(input));
        assertEquals(expected, ""+out);
    
public voidtestXMLSerialization()

        StringWriter w = new StringWriter();
        ToXMLStream xml = new ToXMLStream();
        xml.setWriter(w);
        xml.setOmitXMLDeclaration(true);
        xml.startElement("uri", "localName", "name", null);
        xml.endElement("uri", "localName", "name");
        assertEquals("<name xmlns=\"uri\"/>", w.toString());