package wstxtest.msv;

import javax.xml.stream.*;

import org.codehaus.stax2.*;
import org.codehaus.stax2.validation.*;

import wstxtest.vstream.BaseValidationTest;

/**
 * Simple testing of W3C Schema datatypes.
 * Added to test [WSTX-210].
 *
 * @author Tatu Saloranta
 */
public class TestW3CSchemaTypes
    extends BaseValidationTest
{
    final static String SCHEMA_INT =
        "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n"
        +"<xsd:element name='price' type='xsd:int' />"
        +"\n</xsd:schema>"
        ;

    final static String SCHEMA_FLOAT =
        "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n"
        +"<xsd:element name='price' type='xsd:float' />"
        +"\n</xsd:schema>"
        ;

    // // // First 'int' datatype

    public void testSimpleValidInt() throws Exception
    {
        XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT);
        XMLStreamReader2 sr = getReader("<price>129</price>");
        sr.validateAgainst(schema);
        streamThrough(sr);
    }

    public void testSimpleInvalidInt() throws Exception
    {
        XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT);
        verifyFailure("<price>abc</price>", schema, "invalid 'int' value",
                      "does not satisfy the \"int\" type");
    }

    // // // Then 'float' datatype

    public void testSimpleValidFloat() throws Exception
    {
        XMLValidationSchema schema = parseW3CSchema(SCHEMA_FLOAT);
        XMLStreamReader2 sr = getReader("<price>1.00</price>");
        sr.validateAgainst(schema);
        streamThrough(sr);
    }

    public void testSimpleInvalidFloat() throws Exception
    {
        XMLValidationSchema schema = parseW3CSchema(SCHEMA_FLOAT);
        verifyFailure("<price>abc</price>", schema, "invalid 'float' value",
                      "does not satisfy the \"float\" type");
    }

    /*
    ///////////////////////////////////////////////////////////////////////
    // Helper
    ///////////////////////////////////////////////////////////////////////
    */

    XMLStreamReader2 getReader(String contents) throws XMLStreamException
    {
        XMLInputFactory2 f = getInputFactory();
        setValidating(f, false);
        return constructStreamReader(f, contents);
    }
}

@@ -159,16 +159,46 @@
 nTest;%0A%0A
+import java.io.StringWriter;%0A%0A
 /**%0A * S
@@ -1901,24 +1901,586 @@
 e%22);%0A    %7D%0A%0A
+    // // // Writing%0A%0A    public void testValdiationWhenWritingCharactersFromArray() throws Exception%0A    %7B%0A        XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT);%0A%0A        XMLOutputFactory2 f = getOutputFactory();%0A        XMLStreamWriter2 sw = (XMLStreamWriter2)f.createXMLStreamWriter(new StringWriter());%0A        sw.validateAgainst(schema);%0A%0A        String xml = %22%3Cprice%3E129%3C/price%3E%22;%0A%0A        sw.writeStartElement(%22price%22);%0A        sw.writeCharacters(xml.toCharArray(), xml.indexOf(%221%22), 3);%0A        sw.writeEndElement();%0A        sw.flush();%0A    %7D%0A%0A
     /*%0A    /

Check that validation doesn't fail when writing to an XMLStreamWriter using writeCharacters(char[], int, int)