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