1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package libcore.java.time.chrono;
17 
18 import org.junit.Test;
19 import java.time.chrono.AbstractChronology;
20 import java.time.chrono.ChronoLocalDate;
21 import java.time.chrono.Chronology;
22 import java.time.chrono.Era;
23 import java.time.chrono.IsoChronology;
24 import java.time.temporal.ChronoField;
25 import java.time.temporal.TemporalAccessor;
26 import java.time.temporal.ValueRange;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Set;
30 
31 import static org.junit.Assert.assertEquals;
32 
33 /**
34  * Additional tests for {@link Chronology}.
35  *
36  * @see tck.java.time.chrono.TCKChronology
37  */
38 public class ChronologyTest {
39 
40     @Test
test_compareTo()41     public void test_compareTo() {
42         Set<Chronology> chronologies = new LinkedHashSet<>(Chronology.getAvailableChronologies());
43         chronologies.add(new FakeChronology("aaa", "z aaa"));
44         chronologies.add(new FakeChronology("zzz", "a zzz"));
45 
46         // Check for comparison of each chronology with each other (including itself).
47         for (Chronology c1 : chronologies) {
48             for (Chronology c2 : chronologies) {
49                 assertComparesAccordingToId(c1, c2);
50             }
51         }
52     }
53 
assertComparesAccordingToId(Chronology c1, Chronology c2)54     private static void assertComparesAccordingToId(Chronology c1, Chronology c2) {
55         int chronologyResult = c1.compareTo(c2);
56         int idResult = c1.getId().compareTo(c2.getId());
57         // note that this message is not strictly true: if two chronologies with the same id but
58         // different parameters exist, then they should return non-zero for compareTo(). That is not
59         // possible with any of the chronologies we currently ship (as of early 2017), though.
60         assertEquals("compareTo() must match getId().compareTo()",
61                 (int) Math.signum(chronologyResult), (int) Math.signum(idResult));
62         assertEquals(c1 + " and " + c2 + " compare as equal.",
63                 chronologyResult == 0, c1.equals(c2));
64     }
65 
66     @Test(expected = NullPointerException.class)
test_compareTo_null()67     public void test_compareTo_null() {
68         IsoChronology.INSTANCE.compareTo(null);
69     }
70 
71     /** Fake chronology that supports only returning an id and a type. */
72     private static class FakeChronology extends AbstractChronology {
73 
74         private final String id;
75 
76         private final String type;
77 
FakeChronology(String id, String type)78         public FakeChronology(String id, String type) {
79             this.id = id;
80             this.type = type;
81         }
82 
83 
84         @Override
getId()85         public String getId() {
86             return id;
87         }
88 
89         @Override
getCalendarType()90         public String getCalendarType() {
91             return type;
92         }
93 
94         @Override
date(int prolepticYear, int month, int dayOfMonth)95         public ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth) {
96             throw new UnsupportedOperationException();
97         }
98 
99         @Override
dateYearDay(int prolepticYear, int dayOfYear)100         public ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear) {
101             throw new UnsupportedOperationException();
102         }
103 
104         @Override
dateEpochDay(long epochDay)105         public ChronoLocalDate dateEpochDay(long epochDay) {
106             throw new UnsupportedOperationException();
107         }
108 
109         @Override
date(TemporalAccessor temporal)110         public ChronoLocalDate date(TemporalAccessor temporal) {
111             throw new UnsupportedOperationException();
112         }
113 
114         @Override
isLeapYear(long prolepticYear)115         public boolean isLeapYear(long prolepticYear) {
116             throw new UnsupportedOperationException();
117         }
118 
119         @Override
prolepticYear(Era era, int yearOfEra)120         public int prolepticYear(Era era, int yearOfEra) {
121             throw new UnsupportedOperationException();
122         }
123 
124         @Override
eraOf(int eraValue)125         public Era eraOf(int eraValue) {
126             throw new UnsupportedOperationException();
127         }
128 
129         @Override
eras()130         public List<Era> eras() {
131             throw new UnsupportedOperationException();
132         }
133 
134         @Override
range(ChronoField field)135         public ValueRange range(ChronoField field) {
136             throw new UnsupportedOperationException();
137         }
138     }
139 }
140