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

RNGValidatorTest

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

Fields Summary
Constructors Summary
public RNGValidatorTest(String name)

        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] args)

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

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

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

    
protected voidtearDown()

    
public voidtestBasic()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<empty/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";

        final String source ="<?xml version='1.0'?> <top></top>";
        StringWriter out = new StringWriter();
        r.validate(new InputSource(new StringReader(schema)),
                   new InputSource(new StringReader(source)),
                   out);
        assertEquals("", ""+out);
                     
    
public voidtestBasicError()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<empty/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";

        final String source ="<?xml version='1.0'?> <top><bottom/></top>";
        StringWriter out = new StringWriter();
        r.validate(new InputSource(new StringReader(schema)),
                   new InputSource(new StringReader(source)),
                   out);
        assertEquals("Error: URI=null Line=1: element \"bottom\" was found where no element may occur\n", ""+out);
                     
    
public voidtestBasicWithSubstitution()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<ref name='system-property'/>"+
        "<attribute name='id'>"+
        "<value>VALUE</value>"+
        "</attribute>"+
        "</element>"+
        "</define>"+
        "<define name='system-property'>"+
        "<element name='system-property'>"+
        "<empty/>"+
        "<attribute name='name'/>"+
        "<attribute name='value'/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";

        final String source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='VALUE'/></top>";
        StringWriter out = new StringWriter();
        VariableResolver vr = new VariableResolver();
        r.validate(new InputSource(new StringReader(schema)),
                   new InputSource(new StringReader(source)),
                   vr,
                   out);
        assertEquals("", ""+out);
                     
    
public voidtestBasicWithSubstitutionError()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<ref name='system-property'/>"+
        "<attribute name='id'>"+
        "<value>VALUE</value>"+
        "</attribute>"+
        "</element>"+
        "</define>"+
        "<define name='system-property'>"+
        "<element name='system-property'>"+
        "<empty/>"+
        "<attribute name='name'/>"+
        "<attribute name='value'/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";

        final String source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='Not Expected Value'/></top>";
        StringWriter out = new StringWriter();
        VariableResolver vr = new VariableResolver();
        r.validate(new InputSource(new StringReader(schema)),
                   new InputSource(new StringReader(source)),
                   vr,
                   out);
        assertEquals("Error: URI=null Line=1: attribute \"id\" has a bad value\n", ""+out);
                     
    
public voidtestEntityResolver()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<empty/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";

        final String dtd = ""+
        "<?xml version='1.0' encoding='UTF-8'?>"+
        "<!ELEMENT top (bottom)>"+
        "<!ELEMENT bottom EMPTY>";

        final String source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>";
        VariableResolver vr = new VariableResolver();
        StringWriter out = new StringWriter();
        
        vr.setEntityResolver(new MyEntityResolver(dtd));
        r.validate(new InputSource(new StringReader(schema)),
                   new InputSource(new StringReader(source)),
                   vr,
                   out);
        assertEquals("", out+"");
    
public voidtestEntityResolverWithError()

        RNGValidator r = new RNGValidator();
        final String schema =""+
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+
        "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+
        "<define name='top'>"+
        "<element name='top'>"+
        "<empty/>"+
        "</element>"+
        "</define>"+
        "<start>"+
        "<ref name='top'/>"+
        "</start>"+
        "</grammar>";


        final String source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>";
        VariableResolver vr = new VariableResolver();
        StringWriter out = new StringWriter();
        
        vr.setEntityResolver(new MyEntityResolver());
        try {
            
            r.validate(new InputSource(new StringReader(schema)),
                       new InputSource(new StringReader(source)),
                       vr,
                       out);
            fail("Expected a SAX Exception indicating problems with the external entity");
        }
        catch (SAXParseException spe){
            assertEquals("External parameter entity \"%[dtd];\" has characters after markup.", spe.getMessage());
        }
    
public voidtestacceptanceTest()

        final RNGValidator r = new RNGValidator();
        final VariableResolver vr = new VariableResolver();
        vr.setEntityResolver(new MyEntityResolver(new File ("sun-domain_1_1.dtd")));
        final InputStream schema = this.getClass().getResourceAsStream("/domain.rng");
        if (null ==schema ){
            throw new IOException("couldn't get resource: /domain.rng");
        }
        StringWriter out = new StringWriter();
        r.validate(new InputSource(schema),
                   new InputSource("domain.xml"),
                   vr,
                   out);
        assertEquals("Error: URI=file:/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml Line=11: attribute \"load-order\" has a bad value: \"ABC\" does not satisfy the \"positiveInteger\" type\n", ""+out);
    
public voidtestacceptanceTestNoEntityFile()

        final RNGValidator r = new RNGValidator();
        final VariableResolver vr = new VariableResolver();
        vr.setEntityResolver(new MyEntityResolver());
        final InputStream schema = this.getClass().getResourceAsStream("/domain.rng");
        if (null ==schema ){
            throw new IOException("couldn't get resource: /domain.rng");
        }
        StringWriter out = new StringWriter();
        try {
            r.validate(new InputSource(schema),
                       new InputSource("domain.xml"),
                       vr,
                       out);
            fail("Expected Error indicating no file");
        }
        catch (SAXParseException spe){
            assertEquals("External entity not found: \"http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd\".", spe.getMessage());
        }
        
    
public voidtestacceptanceTestNoInputFile()

        final RNGValidator r = new RNGValidator();
        final VariableResolver vr = new VariableResolver();
        vr.setEntityResolver(new MyEntityResolver(new File ("sun-domain_1_1.dtd")));
        final InputStream schema = this.getClass().getResourceAsStream("/domain.rng");
        if (null ==schema ){
            throw new IOException("couldn't get resource: /domain.rng");
        }
        StringWriter out = new StringWriter();
        try {
            r.validate(new InputSource(schema),
                       new InputSource("domain.xml.notHere"),
                       vr,
                       out);
            fail("Expected Error indicating no file");
        }
        catch (IOException ioe){
            assertEquals("/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml.notHere (No such file or directory)", ioe.getMessage());
        }