1/*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package datatype;
25
26import javax.xml.datatype.DatatypeConfigurationException;
27import javax.xml.datatype.DatatypeConstants;
28import javax.xml.datatype.DatatypeFactory;
29import javax.xml.datatype.XMLGregorianCalendar;
30
31import org.testng.Assert;
32import org.testng.annotations.BeforeMethod;
33import org.testng.annotations.Listeners;
34import org.testng.annotations.Test;
35
36/*
37 * @test
38 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
39 * @run testng/othervm -DrunSecMngr=true datatype.XMLGregorianCalendarTest
40 * @run testng/othervm datatype.XMLGregorianCalendarTest
41 * @summary Test XMLGregorianCalendar.
42 */
43@Listeners({jaxp.library.BasePolicy.class})
44public class XMLGregorianCalendarTest {
45
46    private static final boolean DEBUG = false;
47
48    private static final int TEST_VALUE_FAIL = 0;
49
50    private static final int TEST_VALUE_PASS = 1;
51
52    private XMLGregorianCalendar calendar;
53
54    @BeforeMethod
55    public void setUp() {
56        try {
57            calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
58        } catch (DatatypeConfigurationException dce) {
59            dce.printStackTrace();
60            Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage());
61        }
62    }
63
64    @Test
65    public final void testSetTime() {
66
67        /**
68         * Hour, minute, second values to test and expected result.
69         */
70        final int[] TEST_VALUES = { 24, 0, 0, TEST_VALUE_PASS, 24, 1, 0, TEST_VALUE_FAIL, 24, 0, 1, TEST_VALUE_FAIL, 24, DatatypeConstants.FIELD_UNDEFINED, 0,
71                TEST_VALUE_FAIL, 24, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL };
72
73        // create DatatypeFactory
74        DatatypeFactory datatypeFactory = null;
75        try {
76            datatypeFactory = DatatypeFactory.newInstance();
77        } catch (DatatypeConfigurationException datatypeConfigurationException) {
78            Assert.fail(datatypeConfigurationException.toString());
79        }
80
81        if (DEBUG) {
82            System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
83        }
84
85        // create XMLGregorianCalendar
86        XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar();
87
88        // test each value
89        for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 4) {
90
91            if (DEBUG) {
92                System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
93                        + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 3]);
94            }
95
96            try {
97                // set time
98                xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]);
99
100                if (DEBUG) {
101                    System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\"");
102                }
103
104                // was this expected to fail?
105                if (TEST_VALUES[onTestValue + 3] == TEST_VALUE_FAIL) {
106                    Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
107                            + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString() + "\"");
108                }
109            } catch (Exception exception) {
110
111                if (DEBUG) {
112                    System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\"");
113                }
114
115                // was this expected to succed?
116                if (TEST_VALUES[onTestValue + 3] == TEST_VALUE_PASS) {
117                    Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
118                            + ") are valid yet it failed with \"" + exception.toString() + "\"");
119                }
120                // expected failure
121            }
122        }
123    }
124
125    @Test
126    public final void testSetHour() {
127
128        /**
129         * Hour values to test and expected result.
130         */
131        final int[] TEST_VALUES = {
132                // setTime(H, M, S), hour override, expected result
133                0, 0, 0, 0, TEST_VALUE_PASS, 0, 0, 0, 23, TEST_VALUE_PASS, 0, 0, 0, 24, TEST_VALUE_PASS,
134                // creates invalid state
135                0, 0, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL,
136                // violates Schema Errata
137                0, 0, 1, 24, TEST_VALUE_FAIL };
138
139        // create DatatypeFactory
140        DatatypeFactory datatypeFactory = null;
141        try {
142            datatypeFactory = DatatypeFactory.newInstance();
143        } catch (DatatypeConfigurationException datatypeConfigurationException) {
144            Assert.fail(datatypeConfigurationException.toString());
145        }
146
147        if (DEBUG) {
148            System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
149        }
150
151        // create XMLGregorianCalendar
152        XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar();
153
154        // test each value
155        for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 5) {
156
157            if (DEBUG) {
158                System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
159                        + ", " + TEST_VALUES[onTestValue + 3] + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 4]);
160            }
161
162            try {
163                // set time to known valid value
164                xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]);
165                // now explicitly set hour
166                xmlGregorianCalendar.setHour(TEST_VALUES[onTestValue + 3]);
167
168                if (DEBUG) {
169                    System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\"");
170                }
171
172                // was this expected to fail?
173                if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_FAIL) {
174                    Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
175                            + TEST_VALUES[onTestValue + 3] + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString()
176                            + "\"");
177                }
178            } catch (Exception exception) {
179
180                if (DEBUG) {
181                    System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\"");
182                }
183
184                // was this expected to succed?
185                if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_PASS) {
186                    Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
187                            + TEST_VALUES[onTestValue + 3] + ") are valid yet it failed with \"" + exception.toString() + "\"");
188                }
189                // expected failure
190            }
191        }
192    }
193
194    @Test
195    public void testEqualsWithDifferentObjectParam() {
196
197        Assert.assertFalse(calendar.equals(new Integer(0)), "equals method should return false for any object other" + " than XMLGregorianCalendar");
198    }
199
200    @Test
201    public void testEqualsWithNullObjectParam() {
202
203        Assert.assertFalse(calendar.equals(null), "equals method should return false for null parameter");
204    }
205
206    @Test
207    public void testEqualsWithEqualObjectParam() {
208
209        try {
210            Assert.assertTrue(calendar.equals(DatatypeFactory.newInstance().newXMLGregorianCalendar()), "equals method is expected to return true");
211        } catch (DatatypeConfigurationException dce) {
212            dce.printStackTrace();
213            Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage());
214        }
215    }
216
217    @Test
218    public void testToString() {
219        try {
220            String inputDateTime = "2006-10-23T22:15:01.000000135+08:00";
221            DatatypeFactory factory = DatatypeFactory.newInstance();
222            XMLGregorianCalendar calendar = factory.newXMLGregorianCalendar(inputDateTime);
223            String toStr = calendar.toString();
224            Assert.assertTrue(toStr.indexOf("E") == -1, "String value cannot contain exponent");
225        } catch (DatatypeConfigurationException dce) {
226            dce.printStackTrace();
227            Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage());
228        }
229    }
230}
231