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

VariableExpanderTest

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

Fields Summary
private FrameHolder
frameHolder
private VariableExpander
ve
private StringWriter
out
Constructors Summary
public VariableExpanderTest(String name)

        super(name);
    
Methods Summary
private voidconstructComplexFrames()

        Frame domain, config1, config2, cluster, server1, server2, server3;
        String d = "d";
        String dv1 = "dv1";
        String c1 = "c1";
        String cv1 = "cv1";
        String c2 = "c2";
        String cv2 = "cv2";
        String cl1 = "cl1";
        String clv1 = "clv1";
        String s1 = "s1";
        String sv1 = "sv1";
        String s2 = "s2";
        String sv2 = "sv2";
        String s3 = "s3";
        String sv3 = "sv3";
        domain = frameHolder.getDomainFrame();
        domain.put(d, dv1);
        config1 = frameHolder.getConfigFrame(c1);
        config1.put(c1, cv1);
        config1.inheritFrom(domain);
        config2 = frameHolder.getConfigFrame(c2);
        config2.put(c2, cv2);
        config2.inheritFrom(domain);
        server1 = frameHolder.getServerFrame(s1);
        server1.put(s1, sv1);
        server1.inheritFrom(config1);
        server2 = frameHolder.getServerFrame(s2);
        server2.put(s2, sv2);
        server3 = frameHolder.getServerFrame(s3);
        server3.put(s3, sv3);
        server3.inheritFrom(config2);
        cluster = frameHolder.getClusterFrame(cl1);
        cluster.put(cl1, clv1);
        cluster.inheritFrom(config1);
        server2.inheritFrom(cluster);
    
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(VariableExpanderTest.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 VariableExpanderTest(args[i]));
        }
        return ts;
    
private voidnyi()

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

        frameHolder = new FrameHolder();
        out = new StringWriter();
        ve = new VariableExpander(new Framer(frameHolder));
        ve.setParent(getXMLReader());
        OutputFormat of = new OutputFormat();
        of.setOmitXMLDeclaration(true);        
        Serializer ts = new XMLSerializer(of);
        ts.setOutputCharStream(out);
        ve.setContentHandler(ts.asContentHandler());
    
protected voidtearDown()

    
public voidtestAttributeAndCharacterHandling()

        frameHolder.getDomainFrame().put("k", "v");
        frameHolder.getDomainFrame().put("k1", "v1");
        ve.parse(getInputSource("<foo attr1='fee' attr2='${k}'>foo far ${k} ${k1}</foo>"));
        assertEquals("<foo attr1=\"fee\" attr2=\"v\">foo far v v1</foo>", ""+out);
    
public voidtestAttributeHandling()

        frameHolder.getDomainFrame().put("k", "v");
        ve.parse(getInputSource("<foo attr1='fee' attr2='${k}'/>"));
        assertEquals("<foo attr1=\"fee\" attr2=\"v\"/>", ""+out);
    
public voidtestCharactersComeBeforeChild()

        final String input = "<r>"+
        "<parent>"+
        "some child characters that should appear before the child element"+
        "<child>this should come after the preceding characters</child>"+
        "some child characters that should come after the child element"+
        "</parent>"+
        "</r>";
        ve.parse(getInputSource(input));
        assertEquals(input, ""+out);
    
public voidtestConfigCase()

        frameHolder.getDomainFrame().put("domain", "domain_new");
        frameHolder.getConfigFrame("config1").put("config1", "config1_v");
        ve.parse(getInputSource("<foo><system-property name='config1' value='sys-property'/><config name='config1'>${config1}</config></foo>"));
        assertEquals("<foo><system-property name=\"config1\" value=\"sys-property\"/><config name=\"config1\">config1_v</config></foo>", out.toString());
    
public voidtestGeneralCharacterHandling()

        constructComplexFrames();
       final String doc = "<doc>" +
        "<config name=\"c1\" att1=\"${d}\" att2=\"att2\">foo far ${c1} ${d}</config>"+
        "<server name=\"s2\">gargle ${s2} ${d} ${cl1}</server>"+
        "</doc>";
        final String expected = "<doc>" +
        "<config name=\"c1\" att1=\"dv1\" att2=\"att2\">foo far cv1 dv1</config>"+
        "<server name=\"s2\">gargle sv2 dv1 clv1</server>"+
        "</doc>";
        ve.parse(getInputSource(doc));
        assertEquals(expected, ""+out);
    
public voidtestInvalidReference()

        Frame f = Frame.newFrame();
        f.put("domain", "domain_new");
        assertEquals("${foo}", ve.eval("${foo}", f));
    
public voidtestMultipleVariables()

        Frame f = frameHolder.getDomainFrame();
        f.put("k1", "v1");
        f.put("k2", "v2");
        assertEquals("far lar lar v1 foo far v2", ve.eval("far lar lar ${k1} foo far ${k2}", f));
    
public voidtestNestedReference()

        Frame f = Frame.newFrame();
        f.put("k", "v");
        assertEquals("${fo${k}}", ve.eval("${fo${k}}", f));
        
    
public voidtestPartialReference()

        Frame f = Frame.newFrame();
        f.put("domain", "domain_new");
        assertEquals("${fo", ve.eval("${fo", f));
        
    
public voidtestSetFramer()

        frameHolder.getDomainFrame().put("domain", "domain from original frame holder");
        FrameHolder newFH = new FrameHolder();
        newFH.getDomainFrame().put("domain", "new");
        Framer framer = new Framer(newFH);
        ve.setFramer(framer);
        ve.parse(getInputSource("<foo a=\"${domain}\"/>"));
        assertEquals("<foo a=\"new\"/>", ""+out);
    
public voidtestSetFramerBasic()

        VariableExpander ve = new VariableExpander();
        ve.setFramer(new Framer());
    
public voidtestSimpleCase()

        Frame f = Frame.newFrame();
        f.put("domain", "domain_new");
        assertEquals("domain_new", ve.eval("${domain}", f));