1 /* 2 * Copyright (c) 2014, 2015, 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 24 package datatype; 25 26 import javax.xml.datatype.DatatypeConfigurationException; 27 import javax.xml.datatype.DatatypeConstants; 28 import javax.xml.datatype.DatatypeFactory; 29 import javax.xml.datatype.XMLGregorianCalendar; 30 31 import org.testng.Assert; 32 import org.testng.annotations.BeforeMethod; 33 import org.testng.annotations.Test; 34 35 /* 36 * @summary Test XMLGregorianCalendar. 37 */ 38 public class XMLGregorianCalendarTest { 39 40 private static final boolean DEBUG = false; 41 42 private static final int TEST_VALUE_FAIL = 0; 43 44 private static final int TEST_VALUE_PASS = 1; 45 46 private XMLGregorianCalendar calendar; 47 48 @BeforeMethod 49 protected void setUp() { 50 try { 51 calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(); 52 } catch (DatatypeConfigurationException dce) { 53 dce.printStackTrace(); 54 Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage()); 55 } 56 } 57 58 @Test 59 public final void testSetTime() { 60 61 /** 62 * Hour, minute, second values to test and expected result. 63 */ 64 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, 65 TEST_VALUE_FAIL, 24, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL }; 66 67 // create DatatypeFactory 68 DatatypeFactory datatypeFactory = null; 69 try { 70 datatypeFactory = DatatypeFactory.newInstance(); 71 } catch (DatatypeConfigurationException datatypeConfigurationException) { 72 Assert.fail(datatypeConfigurationException.toString()); 73 } 74 75 if (DEBUG) { 76 System.err.println("DatatypeFactory created: " + datatypeFactory.toString()); 77 } 78 79 // create XMLGregorianCalendar 80 XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(); 81 82 // test each value 83 for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 4) { 84 85 if (DEBUG) { 86 System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] 87 + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 3]); 88 } 89 90 try { 91 // set time 92 xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]); 93 94 if (DEBUG) { 95 System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\""); 96 } 97 98 // was this expected to fail? 99 if (TEST_VALUES[onTestValue + 3] == TEST_VALUE_FAIL) { 100 Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] 101 + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString() + "\""); 102 } 103 } catch (Exception exception) { 104 105 if (DEBUG) { 106 System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\""); 107 } 108 109 // was this expected to succed? 110 if (TEST_VALUES[onTestValue + 3] == TEST_VALUE_PASS) { 111 Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] 112 + ") are valid yet it failed with \"" + exception.toString() + "\""); 113 } 114 // expected failure 115 } 116 } 117 } 118 119 @Test 120 public final void testSetHour() { 121 122 /** 123 * Hour values to test and expected result. 124 */ 125 final int[] TEST_VALUES = { 126 // setTime(H, M, S), hour override, expected result 127 0, 0, 0, 0, TEST_VALUE_PASS, 0, 0, 0, 23, TEST_VALUE_PASS, 0, 0, 0, 24, TEST_VALUE_PASS, 128 // creates invalid state 129 0, 0, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL, 130 // violates Schema Errata 131 0, 0, 1, 24, TEST_VALUE_FAIL }; 132 133 // create DatatypeFactory 134 DatatypeFactory datatypeFactory = null; 135 try { 136 datatypeFactory = DatatypeFactory.newInstance(); 137 } catch (DatatypeConfigurationException datatypeConfigurationException) { 138 Assert.fail(datatypeConfigurationException.toString()); 139 } 140 141 if (DEBUG) { 142 System.err.println("DatatypeFactory created: " + datatypeFactory.toString()); 143 } 144 145 // create XMLGregorianCalendar 146 XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(); 147 148 // test each value 149 for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 5) { 150 151 if (DEBUG) { 152 System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] 153 + ", " + TEST_VALUES[onTestValue + 3] + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 4]); 154 } 155 156 try { 157 // set time to known valid value 158 xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]); 159 // now explicitly set hour 160 xmlGregorianCalendar.setHour(TEST_VALUES[onTestValue + 3]); 161 162 if (DEBUG) { 163 System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\""); 164 } 165 166 // was this expected to fail? 167 if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_FAIL) { 168 Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", " 169 + TEST_VALUES[onTestValue + 3] + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString() 170 + "\""); 171 } 172 } catch (Exception exception) { 173 174 if (DEBUG) { 175 System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\""); 176 } 177 178 // was this expected to succed? 179 if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_PASS) { 180 Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", " 181 + TEST_VALUES[onTestValue + 3] + ") are valid yet it failed with \"" + exception.toString() + "\""); 182 } 183 // expected failure 184 } 185 } 186 } 187 188 @Test 189 public void testEqualsWithDifferentObjectParam() { 190 191 Assert.assertFalse(calendar.equals(new Integer(0)), "equals method should return false for any object other" + " than XMLGregorianCalendar"); 192 } 193 194 @Test 195 public void testEqualsWithNullObjectParam() { 196 197 Assert.assertFalse(calendar.equals(null), "equals method should return false for null parameter"); 198 } 199 200 @Test 201 public void testEqualsWithEqualObjectParam() { 202 203 try { 204 Assert.assertTrue(calendar.equals(DatatypeFactory.newInstance().newXMLGregorianCalendar()), "equals method is expected to return true"); 205 } catch (DatatypeConfigurationException dce) { 206 dce.printStackTrace(); 207 Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage()); 208 } 209 } 210 211 @Test 212 public void testToString() { 213 try { 214 String inputDateTime = "2006-10-23T22:15:01.000000135+08:00"; 215 DatatypeFactory factory = DatatypeFactory.newInstance(); 216 XMLGregorianCalendar calendar = factory.newXMLGregorianCalendar(inputDateTime); 217 String toStr = calendar.toString(); 218 Assert.assertTrue(toStr.indexOf("E") == -1, "String value cannot contain exponent"); 219 } catch (DatatypeConfigurationException dce) { 220 dce.printStackTrace(); 221 Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage()); 222 } 223 } 224 }