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