users@jaxb.java.net

Re: (feature request/bug) -- ValidatorImpl constructor doesn't initialize DatatypeConverter

From: jon gold <dev_at_samizdatdigital.org>
Date: Tue, 07 Sep 2004 14:25:44 -0700

jon gold wrote:

> Ryan Shoemaker - JavaSoft East wrote:
>
>> jon gold wrote:
>>
>> > if there's a need for a use case, here it is (and the reason i
>> ended up
>> > finding all of this out): i have a swing app which my users use to
>> fill
>> > in various values. unbeknownst to them, the values are actually turned
>> > into a jaxb object. when they hit save, they're marshalling, but i
>> want
>> > to validate first, or course. thus, i'm creating jaxb objects from
>> scratch,
>> > and calling validate before ever trying to marshal or unmarshal.
>>
>> This sounds like a bug to me, but I haven't been able to reproduce it.
>>
>> I modified $JAXB_HOME/samples/create-marshal to call validate before
>> marshal, but it seemed to work fine.
>>
>> *** 101,109 ****
>>
>> // set the required Items list
>> po.setItems( items );
>> !
>> ! jc.createValidator().validate( po );
>> !
>> // create a Marshaller and marshal to System.out
>> Marshaller m = jc.createMarshaller();
>> m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT,
>> Boolean.TRUE );
>> --- 101,107 ----
>>
>> // set the required Items list
>> po.setItems( items );
>> !
>> // create a Marshaller and marshal to System.out
>> Marshaller m = jc.createMarshaller();
>> m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT,
>> Boolean.TRUE );
>>
>> It would be very helpful if you could put together a small test case
>> that reproduces the problem. Also, let me know which version of the
>> RI you are running ("% xjc -version").
>>
>> It might be a bug that the javadoc doesn't state that the datatype
>> converter must be initialized before validation. I'll check with
>> the spec guys.
>>
>> Thanks,
>
>
> sadly, i can't reproduce it this morning (and for some reason, my
> changes to ValidatorImpl seem to have been reverted or something). i'm
> awfully perplexed that it's working now, but cest la vie. it'll pop up
> again, i'm sure.
>
> jon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>
i have good news and bad news, depending on whether you're the one
sending this message, or the one reading it. the good news is that i
reproduced the problem and have attached a testcase. the bad news is
that i reproduced the problem and have attached a testcase. i haven't
tried all datatypes yet, but it seems to have something to do with
doubles: i can validate a string-based object, but not one with a double
that has a restriction. there are a lot of variables, but i'll at least
give yall this as a starting point.

jon


import com.somewhere.test.xml.bind.SimpleString;
import com.somewhere.test.xml.bind.RestrictedDouble;
import com.somewhere.test.xml.bind.ObjectFactory;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import javax.xml.bind.Validator;

// this is a test case to point out that, if a validation is requested
before a marshal or an unmarshalling occurs, then
// we get exceptions (notably due to the fact that ValidationImpl
constructor doesn't call
// DatatypeConverter.setDatatypeConverter, as do Marshallers and
Unmarshallers). interestingly enough, this only seems to
// be the case with doubles, not strings (haven't tried any other types).
//
// here's the schema for the tests below:
//
//
// <?xml version="1.0"?>
//
// <xsd:schema targetNamespace="http://somewhere.com/test/xml/bind"
// xmlns:test="http://somewhere.com/test/xml/bind"
// xmlns:xsd="http://www.w3.org/2001/XMLSchema">
//
// <xsd:complexType name="SimpleStringType">
// <xsd:sequence>
// <xsd:element name="Value" type="xsd:string"/>
// </xsd:sequence>
// </xsd:complexType>
//
// <xsd:element name="SimpleString" type="test:SimpleStringType"/>
//
// <xsd:complexType name="RestrictedDoubleType">
// <xsd:sequence>
// <xsd:element name="Value">
// <xsd:simpleType>
// <xsd:restriction base="xsd:double">
// <xsd:minInclusive value="0"/>
// <xsd:maxInclusive value="200"/>
// </xsd:restriction>
// </xsd:simpleType>
// </xsd:element>
// </xsd:sequence>
// </xsd:complexType>
//
//
// <xsd:element name="RestrictedDouble"
type="test:RestrictedDoubleType"/>
//
// </xsd:schema>
//
// all of this is done using java 1.4, jwsdp 1.4 on a powerbook. output
from xjc.sh -version:
//
// JavaTM Architecture for XML Binding(JAXB) Reference
Implementation, (build 1.0.3-b18-fcs)
//

public class ValidateFirstTestCase extends TestCase {

    public ValidateFirstTestCase()
    {
        super();
    }

    public static Test suite()
    {
        return new TestSuite( ValidateFirstTestCase.class );
    }

    // this will succeed.
    public void testValidateString() throws Exception
    {
        ObjectFactory of = new ObjectFactory();
        SimpleString ss = of.createSimpleString();

        ss.setValue( "hello there" );

        Validator validator = of.createValidator();
        assertTrue( validator.validate( ss ) );
    }

    // this won't.
    public void testValidateDouble() throws Exception
    {
        ObjectFactory of = new ObjectFactory();

        RestrictedDouble dbl = of.createRestrictedDouble();
        dbl.setValue( 120 );

        Validator validator = of.createValidator();
        assertTrue( validator.validate( dbl ) );
    }

    public void testValidateDoubleAfterMarshallerConstructor() throws
Exception
    {
        ObjectFactory of = new ObjectFactory();
        of.createMarshaller(); // just to execute the necessary line in
the constructor.

        // now, the above test will work.
        testValidateDouble();
    }

    public static void main( String[] args )
    {
        junit.textui.TestRunner.run( suite() );
    }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net