/*   Copyright 2004 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package xmlcursor.xpath.complex.detailed;

import org.apache.xmlbeans.*;
import org.junit.Before;
import org.junit.Test;

import java.time.OffsetDateTime;
import java.util.Calendar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TypesTest {
    private XmlObject o;
    private XmlObject[] res;

    @Before
    public void setUp()
        throws Exception {
        o = XmlObject.Factory.parse("<a/>");
    }

    @Test
    public void testDate() {
        boolean negativeOffset = OffsetDateTime.now().getOffset().getTotalSeconds() < 0;
        res = o.selectPath("xs:date(\"2000-01-01\")");
        assertEquals(1, res.length);
        Calendar d = ((XmlDate) res[0]).getCalendarValue();
        if (negativeOffset) {
            assertEquals(1999, d.get(Calendar.YEAR));
            assertEquals(11, d.get(Calendar.MONTH));
            assertEquals(31, d.get(Calendar.DAY_OF_MONTH));
        } else {
            assertEquals(2000, d.get(Calendar.YEAR));
            assertEquals(0, d.get(Calendar.MONTH));
            assertEquals(1, d.get(Calendar.DAY_OF_MONTH));
        }
    }

    @Test
    public void testDecimal() {
        res = o.selectPath(
            "seconds-from-dateTime(xs:dateTime('1997-07-16T19:20:30+01:00'))");
        assertEquals(1, res.length);
        XmlDecimal dec = ((XmlDecimal) res[0]);
        assertEquals("<xml-fragment>30</xml-fragment>", dec.xmlText());
    }

    //Saxon returns string here, though the string is a valid duration
    //representation
    @Test
    public void testDuration() {
        res = o.selectPath("xs:dayTimeDuration(\"PT12H\")*4");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType());
        String s = res[0].xmlText();
        System.out.println(s);
        int i = s.indexOf("(\"");
        int j = s.indexOf("\")");
        assertTrue(0 < i);
        assertTrue(i < j);
        String duration = s.substring(i + 2, j);
        System.out.println(duration);
        GDurationSpecification gDur = new GDurationBuilder(duration);
        System.out.println(gDur.getDay());
        assertEquals(2, gDur.getDay());
    }

    @Test
    public void testTypes() throws Exception {
        XmlObject o = XmlObject.Factory.parse(
            "<a xml:base='abc'>foo<b>bar</b></a>");
        XmlObject[] res = null;

        //Long
        res = o.selectPath("hours-from-dateTime(" +
                           "current-dateTime()) cast as xs:integer");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType());
        XmlLong xl = ((XmlLong) res[0]);
        System.out.println(xl.xmlText());

        //Java type is string...
        res = o.selectPath("current-time()");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType());
        System.out.println(res[0].xmlText());
        XmlTime time = XmlTime.Factory.parse(res[0].xmlText());
        System.out.println(time.xmlText());

        /*
        res = o.selectPath("subtract-dateTimes-yielding-dayTimeDuration(" +
            "current-dateTime()," +
            "current-dateTime())");
        assertEquals(1, res.length);
        XmlDuration dur = ((XmlDuration) res[0]);
        System.out.println(dur.xmlText());
        */
        //Java type is long--is query right?
        res = o.selectPath("xs:byte(3)");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType()); //xs:long
        //XmlByte b = ((XmlByte) res[0]);
        //System.out.println(b.xmlText());

        //Java type is string
        res = o.selectPath("base-uri(/a)");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType()); //xs:string
        XmlAnyURI u = XmlAnyURI.Factory.parse(res[0].xmlText());
        System.out.println(u.xmlText());

        //java type is Date
        res = o.selectPath("current-dateTime()");
        assertEquals(1, res.length);
        System.out.println(res[0].schemaType());
        XmlDate dt = ((XmlDate) res[0]);
        System.out.println(dt.xmlText());
    }

}

@@ -1811,32 +1811,727 @@
     %7D%0D%0A    %7D%0D%0A%0D%0A
+    @Test%0D%0A    public void testZDate() %7B%0D%0A        boolean negativeOffset = OffsetDateTime.now().getOffset().getTotalSeconds() %3C 0;%0D%0A        res = o.selectPath(%22xs:date(%5C%222000-01-01Z%5C%22)%22);%0D%0A        assertEquals(1, res.length);%0D%0A        Calendar d = ((XmlDate) res%5B0%5D).getCalendarValue();%0D%0A        if (negativeOffset) %7B%0D%0A            assertEquals(1999, d.get(Calendar.YEAR));%0D%0A            assertEquals(11, d.get(Calendar.MONTH));%0D%0A            assertEquals(31, d.get(Calendar.DAY_OF_MONTH));%0D%0A        %7D else %7B%0D%0A            assertEquals(2000, d.get(Calendar.YEAR));%0D%0A            assertEquals(0, d.get(Calendar.MONTH));%0D%0A            assertEquals(1, d.get(Calendar.DAY_OF_MONTH));%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A
     @Test%0D%0A    p

try to make date test more robust