1/*
2 * Copyright (c) 2013, 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/**
25 * @test
26 * @bug 5077522
27 * @summary test INDETERMINATE relations
28 * @run main DurationComparison
29 */
30import javax.xml.datatype.DatatypeConfigurationException;
31import javax.xml.datatype.DatatypeConstants;
32import javax.xml.datatype.DatatypeFactory;
33import javax.xml.datatype.Duration;
34
35/**
36 *
37 * This is a JCK failure. The implementation fails with a list of
38 * INDETERMINATE comparisons
39 *
40 *
41 * @author Joe Wang <huizhe.wang@oracle.com>
42 */
43public class DurationComparison {
44    static String errMsg;
45    int passed = 0, failed = 0;
46
47    public static void main(String[] args) {
48        DurationComparison test = new DurationComparison();
49        test.testCompareWithInderterminateRelation();
50        test.testVerifyOtherRelations();
51        test.tearDown();
52    }
53
54    /**
55     * See JDK-5077522, Duration.compare returns equal for INDETERMINATE
56     * comparisons
57     */
58    public void testCompareWithInderterminateRelation() {
59
60        final String [][] partialOrder = { // partialOrder
61           {"P1Y", "<>", "P365D"},
62           {"P1Y", "<>", "P366D"},
63           {"P1M", "<>", "P28D"},
64           {"P1M", "<>", "P29D"},
65           {"P1M", "<>", "P30D"},
66           {"P1M", "<>", "P31D"},
67           {"P5M", "<>", "P150D"},
68           {"P5M", "<>", "P151D"},
69           {"P5M", "<>", "P152D"},
70           {"P5M", "<>", "P153D"},
71           {"PT2419200S", "<>", "P1M"},
72           {"PT2678400S", "<>", "P1M"},
73           {"PT31536000S", "<>", "P1Y"},
74           {"PT31622400S", "<>", "P1Y"},
75           {"PT525600M", "<>", "P1Y"},
76           {"PT527040M", "<>", "P1Y"},
77           {"PT8760H", "<>", "P1Y"},
78           {"PT8784H", "<>", "P1Y"},
79           {"P365D", "<>", "P1Y"},
80        };
81
82        DatatypeFactory df = null;
83        try {
84            df = DatatypeFactory.newInstance();
85        } catch (DatatypeConfigurationException ex) {
86            ex.printStackTrace();
87            fail(ex.toString());
88        }
89
90        for (int valueIndex = 0; valueIndex < partialOrder.length; ++valueIndex) {
91            Duration duration1 = df.newDuration(partialOrder[valueIndex][0]);
92            Duration duration2 = df.newDuration(partialOrder[valueIndex][2]);
93            int cmp = duration1.compare(duration2);
94            int expected = ">".equals(partialOrder[valueIndex][1])
95                     ? DatatypeConstants.GREATER
96                     : "<".equals(partialOrder[valueIndex][1])
97                     ? DatatypeConstants.LESSER
98                     : "==".equals(partialOrder[valueIndex][1])
99                     ? DatatypeConstants.EQUAL
100                     : DatatypeConstants.INDETERMINATE;
101
102            if (expected != cmp) {
103                fail("returned " + cmp2str(cmp)
104                    + " for durations \'" + duration1 + "\' and "
105                    + duration2 + "\', but expected " + cmp2str(expected));
106            } else {
107                success("Comparing " + duration1 + " and " + duration2 +
108                        ": INDETERMINATE");
109            }
110        }
111    }
112
113    /**
114     * Verify cases around the INDETERMINATE relations
115     */
116    public void testVerifyOtherRelations() {
117
118        final String [][] partialOrder = { // partialOrder
119           {"P1Y", ">", "P364D"},
120           {"P1Y", "<", "P367D"},
121           {"P1Y2D", ">", "P366D"},
122           {"P1M", ">", "P27D"},
123           {"P1M", "<", "P32D"},
124           {"P1M", "<", "P31DT1H"},
125           {"P5M", ">", "P149D"},
126           {"P5M", "<", "P154D"},
127           {"P5M", "<", "P153DT1H"},
128           {"PT2419199S", "<", "P1M"},  //PT2419200S -> P28D
129           {"PT2678401S", ">", "P1M"},  //PT2678400S -> P31D
130           {"PT31535999S", "<", "P1Y"}, //PT31536000S -> P365D
131           {"PT31622401S", ">", "P1Y"}, //PT31622400S -> P366D
132           {"PT525599M59S", "<", "P1Y"},  //PT525600M -> P365D
133           {"PT527040M1S", ">", "P1Y"},  //PT527040M -> P366D
134           {"PT8759H59M59S", "<", "P1Y"},    //PT8760H -> P365D
135           {"PT8784H1S", ">", "P1Y"},    //PT8784H -> P366D
136        };
137
138        DatatypeFactory df = null;
139        try {
140            df = DatatypeFactory.newInstance();
141        } catch (DatatypeConfigurationException ex) {
142            ex.printStackTrace();
143            fail(ex.toString());
144        }
145
146        for (int valueIndex = 0; valueIndex < partialOrder.length; ++valueIndex) {
147            Duration duration1 = df.newDuration(partialOrder[valueIndex][0]);
148            Duration duration2 = df.newDuration(partialOrder[valueIndex][2]);
149            int cmp = duration1.compare(duration2);
150            int expected = ">".equals(partialOrder[valueIndex][1])
151                     ? DatatypeConstants.GREATER
152                     : "<".equals(partialOrder[valueIndex][1])
153                     ? DatatypeConstants.LESSER
154                     : "==".equals(partialOrder[valueIndex][1])
155                     ? DatatypeConstants.EQUAL
156                     : DatatypeConstants.INDETERMINATE;
157
158            if (expected != cmp) {
159                fail("returned " + cmp2str(cmp)
160                    + " for durations \'" + duration1 + "\' and "
161                    + duration2 + "\', but expected " + cmp2str(expected));
162            } else {
163                success("Comparing " + duration1 + " and " + duration2 +
164                        ": expected: " + cmp2str(expected) +
165                        " actual: " + cmp2str(cmp));
166            }
167        }
168    }
169    public static String cmp2str(int cmp) {
170        return cmp == DatatypeConstants.LESSER ? "LESSER"
171             : cmp == DatatypeConstants.GREATER ? "GREATER"
172             : cmp == DatatypeConstants.EQUAL ? "EQUAL"
173             : cmp == DatatypeConstants.INDETERMINATE ? "INDETERMINATE"
174             : "UNDEFINED";
175    }
176
177    public void tearDown() {
178
179        System.out.println("\nNumber of tests passed: " + passed);
180        System.out.println("Number of tests failed: " + failed + "\n");
181
182        if (errMsg != null ) {
183            throw new RuntimeException(errMsg);
184        }
185    }
186    void fail(String msg) {
187        if (errMsg == null) {
188            errMsg = msg;
189        } else {
190            errMsg = errMsg + "\n" + msg;
191        }
192        failed++;
193    }
194
195    void success(String msg) {
196        passed++;
197        System.out.println(msg);
198    }
199
200}
201