0001: /*
0002: * Copyright 2001-2005 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.time;
0017:
0018: import java.io.ByteArrayInputStream;
0019: import java.io.ByteArrayOutputStream;
0020: import java.io.ObjectInputStream;
0021: import java.io.ObjectOutputStream;
0022: import java.util.Arrays;
0023: import java.util.Date;
0024: import java.util.Locale;
0025:
0026: import junit.framework.TestCase;
0027: import junit.framework.TestSuite;
0028:
0029: import org.joda.time.chrono.BuddhistChronology;
0030: import org.joda.time.chrono.CopticChronology;
0031: import org.joda.time.chrono.ISOChronology;
0032: import org.joda.time.format.DateTimeFormat;
0033: import org.joda.time.format.DateTimeFormatter;
0034:
0035: /**
0036: * This class is a Junit unit test for Partial.
0037: *
0038: * @author Stephen Colebourne
0039: */
0040: public class TestPartial_Basics extends TestCase {
0041:
0042: private static final DateTimeZone PARIS = DateTimeZone
0043: .forID("Europe/Paris");
0044: private static final DateTimeZone LONDON = DateTimeZone
0045: .forID("Europe/London");
0046: private static final DateTimeZone TOKYO = DateTimeZone
0047: .forID("Asia/Tokyo");
0048: private static final int OFFSET = 1;
0049: private static final Chronology COPTIC_PARIS = CopticChronology
0050: .getInstance(PARIS);
0051: private static final Chronology COPTIC_LONDON = CopticChronology
0052: .getInstance(LONDON);
0053: private static final Chronology COPTIC_TOKYO = CopticChronology
0054: .getInstance(TOKYO);
0055: private static final Chronology COPTIC_UTC = CopticChronology
0056: .getInstanceUTC();
0057: private static final Chronology ISO_PARIS = ISOChronology
0058: .getInstance(PARIS);
0059: private static final Chronology ISO_LONDON = ISOChronology
0060: .getInstance(LONDON);
0061: private static final Chronology ISO_TOKYO = ISOChronology
0062: .getInstance(TOKYO);
0063: private static final Chronology ISO_UTC = ISOChronology
0064: .getInstanceUTC();
0065: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
0066: .getInstance(PARIS);
0067: private static final Chronology BUDDHIST_LONDON = BuddhistChronology
0068: .getInstance(LONDON);
0069: private static final Chronology BUDDHIST_TOKYO = BuddhistChronology
0070: .getInstance(TOKYO);
0071: private static final Chronology BUDDHIST_UTC = BuddhistChronology
0072: .getInstanceUTC();
0073:
0074: private long TEST_TIME_NOW = 10L
0075: * DateTimeConstants.MILLIS_PER_HOUR + 20L
0076: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
0077: * DateTimeConstants.MILLIS_PER_SECOND + 40L;
0078:
0079: private long TEST_TIME1 = 1L * DateTimeConstants.MILLIS_PER_HOUR
0080: + 2L * DateTimeConstants.MILLIS_PER_MINUTE + 3L
0081: * DateTimeConstants.MILLIS_PER_SECOND + 4L;
0082:
0083: private long TEST_TIME2 = 1L * DateTimeConstants.MILLIS_PER_DAY
0084: + 5L * DateTimeConstants.MILLIS_PER_HOUR + 6L
0085: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
0086: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0087:
0088: private DateTimeZone zone = null;
0089:
0090: public static void main(String[] args) {
0091: junit.textui.TestRunner.run(suite());
0092: }
0093:
0094: public static TestSuite suite() {
0095: return new TestSuite(TestPartial_Basics.class);
0096: }
0097:
0098: public TestPartial_Basics(String name) {
0099: super (name);
0100: }
0101:
0102: protected void setUp() throws Exception {
0103: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0104: zone = DateTimeZone.getDefault();
0105: DateTimeZone.setDefault(LONDON);
0106: }
0107:
0108: protected void tearDown() throws Exception {
0109: DateTimeUtils.setCurrentMillisSystem();
0110: DateTimeZone.setDefault(zone);
0111: zone = null;
0112: }
0113:
0114: //-----------------------------------------------------------------------
0115: public void testGet() {
0116: Partial test = createHourMinPartial();
0117: assertEquals(10, test.get(DateTimeFieldType.hourOfDay()));
0118: assertEquals(20, test.get(DateTimeFieldType.minuteOfHour()));
0119: try {
0120: test.get(null);
0121: fail();
0122: } catch (IllegalArgumentException ex) {
0123: }
0124: try {
0125: test.get(DateTimeFieldType.secondOfMinute());
0126: fail();
0127: } catch (IllegalArgumentException ex) {
0128: }
0129: }
0130:
0131: public void testSize() {
0132: Partial test = createHourMinPartial();
0133: assertEquals(2, test.size());
0134: }
0135:
0136: public void testGetFieldType() {
0137: Partial test = createHourMinPartial();
0138: assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0));
0139: assertSame(DateTimeFieldType.minuteOfHour(), test
0140: .getFieldType(1));
0141: try {
0142: test.getFieldType(-1);
0143: } catch (IndexOutOfBoundsException ex) {
0144: }
0145: try {
0146: test.getFieldType(2);
0147: } catch (IndexOutOfBoundsException ex) {
0148: }
0149: }
0150:
0151: public void testGetFieldTypes() {
0152: Partial test = createHourMinPartial();
0153: DateTimeFieldType[] fields = test.getFieldTypes();
0154: assertEquals(2, fields.length);
0155: assertSame(DateTimeFieldType.hourOfDay(), fields[0]);
0156: assertSame(DateTimeFieldType.minuteOfHour(), fields[1]);
0157: assertNotSame(test.getFieldTypes(), test.getFieldTypes());
0158: }
0159:
0160: public void testGetField() {
0161: Partial test = createHourMinPartial(COPTIC_PARIS);
0162: assertSame(CopticChronology.getInstanceUTC().hourOfDay(), test
0163: .getField(0));
0164: assertSame(CopticChronology.getInstanceUTC().minuteOfHour(),
0165: test.getField(1));
0166: try {
0167: test.getField(-1);
0168: } catch (IndexOutOfBoundsException ex) {
0169: }
0170: try {
0171: test.getField(5);
0172: } catch (IndexOutOfBoundsException ex) {
0173: }
0174: }
0175:
0176: public void testGetFields() {
0177: Partial test = createHourMinPartial(COPTIC_PARIS);
0178: DateTimeField[] fields = test.getFields();
0179: assertEquals(2, fields.length);
0180: assertSame(CopticChronology.getInstanceUTC().hourOfDay(),
0181: fields[0]);
0182: assertSame(CopticChronology.getInstanceUTC().minuteOfHour(),
0183: fields[1]);
0184: assertNotSame(test.getFields(), test.getFields());
0185: }
0186:
0187: public void testGetValue() {
0188: Partial test = createHourMinPartial(COPTIC_PARIS);
0189: assertEquals(10, test.getValue(0));
0190: assertEquals(20, test.getValue(1));
0191: try {
0192: test.getValue(-1);
0193: } catch (IndexOutOfBoundsException ex) {
0194: }
0195: try {
0196: test.getValue(2);
0197: } catch (IndexOutOfBoundsException ex) {
0198: }
0199: }
0200:
0201: public void testGetValues() {
0202: Partial test = createHourMinPartial(COPTIC_PARIS);
0203: int[] values = test.getValues();
0204: assertEquals(2, values.length);
0205: assertEquals(10, values[0]);
0206: assertEquals(20, values[1]);
0207: assertNotSame(test.getValues(), test.getValues());
0208: }
0209:
0210: public void testIsSupported() {
0211: Partial test = createHourMinPartial(COPTIC_PARIS);
0212: assertEquals(true, test.isSupported(DateTimeFieldType
0213: .hourOfDay()));
0214: assertEquals(true, test.isSupported(DateTimeFieldType
0215: .minuteOfHour()));
0216: assertEquals(false, test.isSupported(DateTimeFieldType
0217: .secondOfMinute()));
0218: assertEquals(false, test.isSupported(DateTimeFieldType
0219: .millisOfSecond()));
0220: assertEquals(false, test.isSupported(DateTimeFieldType
0221: .dayOfMonth()));
0222: }
0223:
0224: public void testEqualsHashCode() {
0225: Partial test1 = createHourMinPartial(COPTIC_PARIS);
0226: Partial test2 = createHourMinPartial(COPTIC_PARIS);
0227: assertEquals(true, test1.equals(test2));
0228: assertEquals(true, test2.equals(test1));
0229: assertEquals(true, test1.equals(test1));
0230: assertEquals(true, test2.equals(test2));
0231: assertEquals(true, test1.hashCode() == test2.hashCode());
0232: assertEquals(true, test1.hashCode() == test1.hashCode());
0233: assertEquals(true, test2.hashCode() == test2.hashCode());
0234:
0235: Partial test3 = createHourMinPartial2(COPTIC_PARIS);
0236: assertEquals(false, test1.equals(test3));
0237: assertEquals(false, test2.equals(test3));
0238: assertEquals(false, test3.equals(test1));
0239: assertEquals(false, test3.equals(test2));
0240: assertEquals(false, test1.hashCode() == test3.hashCode());
0241: assertEquals(false, test2.hashCode() == test3.hashCode());
0242:
0243: assertEquals(false, test1.equals("Hello"));
0244: assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
0245: assertEquals(new TimeOfDay(10, 20, 30, 40),
0246: createTODPartial(ISO_UTC));
0247: }
0248:
0249: //-----------------------------------------------------------------------
0250: public void testCompareTo() {
0251: Partial test1 = createHourMinPartial();
0252: Partial test1a = createHourMinPartial();
0253: assertEquals(0, test1.compareTo(test1a));
0254: assertEquals(0, test1a.compareTo(test1));
0255: assertEquals(0, test1.compareTo(test1));
0256: assertEquals(0, test1a.compareTo(test1a));
0257:
0258: Partial test2 = createHourMinPartial2(ISO_UTC);
0259: assertEquals(-1, test1.compareTo(test2));
0260: assertEquals(+1, test2.compareTo(test1));
0261:
0262: Partial test3 = createHourMinPartial2(COPTIC_UTC);
0263: assertEquals(-1, test1.compareTo(test3));
0264: assertEquals(+1, test3.compareTo(test1));
0265: assertEquals(0, test3.compareTo(test2));
0266:
0267: assertEquals(0, new TimeOfDay(10, 20, 30, 40)
0268: .compareTo(createTODPartial(ISO_UTC)));
0269:
0270: try {
0271: test1.compareTo(null);
0272: fail();
0273: } catch (NullPointerException ex) {
0274: }
0275: try {
0276: test1.compareTo(new Date());
0277: fail();
0278: } catch (ClassCastException ex) {
0279: }
0280: try {
0281: test1.compareTo(new YearMonthDay());
0282: fail();
0283: } catch (ClassCastException ex) {
0284: }
0285: try {
0286: createTODPartial(ISO_UTC).without(
0287: DateTimeFieldType.hourOfDay()).compareTo(
0288: new YearMonthDay());
0289: fail();
0290: } catch (ClassCastException ex) {
0291: }
0292: }
0293:
0294: //-----------------------------------------------------------------------
0295: public void testIsEqual_TOD() {
0296: Partial test1 = createHourMinPartial();
0297: Partial test1a = createHourMinPartial();
0298: assertEquals(true, test1.isEqual(test1a));
0299: assertEquals(true, test1a.isEqual(test1));
0300: assertEquals(true, test1.isEqual(test1));
0301: assertEquals(true, test1a.isEqual(test1a));
0302:
0303: Partial test2 = createHourMinPartial2(ISO_UTC);
0304: assertEquals(false, test1.isEqual(test2));
0305: assertEquals(false, test2.isEqual(test1));
0306:
0307: Partial test3 = createHourMinPartial2(COPTIC_UTC);
0308: assertEquals(false, test1.isEqual(test3));
0309: assertEquals(false, test3.isEqual(test1));
0310: assertEquals(true, test3.isEqual(test2));
0311:
0312: try {
0313: createHourMinPartial().isEqual(null);
0314: fail();
0315: } catch (IllegalArgumentException ex) {
0316: }
0317: }
0318:
0319: //-----------------------------------------------------------------------
0320: public void testIsBefore_TOD() {
0321: Partial test1 = createHourMinPartial();
0322: Partial test1a = createHourMinPartial();
0323: assertEquals(false, test1.isBefore(test1a));
0324: assertEquals(false, test1a.isBefore(test1));
0325: assertEquals(false, test1.isBefore(test1));
0326: assertEquals(false, test1a.isBefore(test1a));
0327:
0328: Partial test2 = createHourMinPartial2(ISO_UTC);
0329: assertEquals(true, test1.isBefore(test2));
0330: assertEquals(false, test2.isBefore(test1));
0331:
0332: Partial test3 = createHourMinPartial2(COPTIC_UTC);
0333: assertEquals(true, test1.isBefore(test3));
0334: assertEquals(false, test3.isBefore(test1));
0335: assertEquals(false, test3.isBefore(test2));
0336:
0337: try {
0338: createHourMinPartial().isBefore(null);
0339: fail();
0340: } catch (IllegalArgumentException ex) {
0341: }
0342: }
0343:
0344: //-----------------------------------------------------------------------
0345: public void testIsAfter_TOD() {
0346: Partial test1 = createHourMinPartial();
0347: Partial test1a = createHourMinPartial();
0348: assertEquals(false, test1.isAfter(test1a));
0349: assertEquals(false, test1a.isAfter(test1));
0350: assertEquals(false, test1.isAfter(test1));
0351: assertEquals(false, test1a.isAfter(test1a));
0352:
0353: Partial test2 = createHourMinPartial2(ISO_UTC);
0354: assertEquals(false, test1.isAfter(test2));
0355: assertEquals(true, test2.isAfter(test1));
0356:
0357: Partial test3 = createHourMinPartial2(COPTIC_UTC);
0358: assertEquals(false, test1.isAfter(test3));
0359: assertEquals(true, test3.isAfter(test1));
0360: assertEquals(false, test3.isAfter(test2));
0361:
0362: try {
0363: createHourMinPartial().isAfter(null);
0364: fail();
0365: } catch (IllegalArgumentException ex) {
0366: }
0367: }
0368:
0369: //-----------------------------------------------------------------------
0370: public void testWithChronologyRetainFields_Chrono() {
0371: Partial base = createHourMinPartial(COPTIC_PARIS);
0372: Partial test = base.withChronologyRetainFields(BUDDHIST_TOKYO);
0373: check(base, 10, 20);
0374: assertEquals(COPTIC_UTC, base.getChronology());
0375: check(test, 10, 20);
0376: assertEquals(BUDDHIST_UTC, test.getChronology());
0377: }
0378:
0379: public void testWithChronologyRetainFields_sameChrono() {
0380: Partial base = createHourMinPartial(COPTIC_PARIS);
0381: Partial test = base.withChronologyRetainFields(COPTIC_TOKYO);
0382: assertSame(base, test);
0383: }
0384:
0385: public void testWithChronologyRetainFields_nullChrono() {
0386: Partial base = createHourMinPartial(COPTIC_PARIS);
0387: Partial test = base.withChronologyRetainFields(null);
0388: check(base, 10, 20);
0389: assertEquals(COPTIC_UTC, base.getChronology());
0390: check(test, 10, 20);
0391: assertEquals(ISO_UTC, test.getChronology());
0392: }
0393:
0394: //-----------------------------------------------------------------------
0395: public void testWith1() {
0396: Partial test = createHourMinPartial();
0397: Partial result = test.with(DateTimeFieldType.hourOfDay(), 15);
0398: check(test, 10, 20);
0399: check(result, 15, 20);
0400: }
0401:
0402: public void testWith2() {
0403: Partial test = createHourMinPartial();
0404: try {
0405: test.with(null, 6);
0406: fail();
0407: } catch (IllegalArgumentException ex) {
0408: }
0409: check(test, 10, 20);
0410: }
0411:
0412: public void testWith3a() {
0413: Partial test = createHourMinPartial();
0414: Partial result = test.with(DateTimeFieldType.secondOfMinute(),
0415: 15);
0416: check(test, 10, 20);
0417: assertEquals(3, result.size());
0418: assertEquals(true, result.isSupported(DateTimeFieldType
0419: .hourOfDay()));
0420: assertEquals(true, result.isSupported(DateTimeFieldType
0421: .minuteOfHour()));
0422: assertEquals(true, result.isSupported(DateTimeFieldType
0423: .secondOfMinute()));
0424: assertEquals(DateTimeFieldType.hourOfDay(), result
0425: .getFieldType(0));
0426: assertEquals(DateTimeFieldType.minuteOfHour(), result
0427: .getFieldType(1));
0428: assertEquals(DateTimeFieldType.secondOfMinute(), result
0429: .getFieldType(2));
0430: assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
0431: assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
0432: assertEquals(15, result.get(DateTimeFieldType.secondOfMinute()));
0433: }
0434:
0435: public void testWith3b() {
0436: Partial test = createHourMinPartial();
0437: Partial result = test.with(DateTimeFieldType.minuteOfDay(), 15);
0438: check(test, 10, 20);
0439: assertEquals(3, result.size());
0440: assertEquals(true, result.isSupported(DateTimeFieldType
0441: .hourOfDay()));
0442: assertEquals(true, result.isSupported(DateTimeFieldType
0443: .minuteOfDay()));
0444: assertEquals(true, result.isSupported(DateTimeFieldType
0445: .minuteOfHour()));
0446: assertEquals(DateTimeFieldType.hourOfDay(), result
0447: .getFieldType(0));
0448: assertEquals(DateTimeFieldType.minuteOfDay(), result
0449: .getFieldType(1));
0450: assertEquals(DateTimeFieldType.minuteOfHour(), result
0451: .getFieldType(2));
0452: assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
0453: assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
0454: assertEquals(15, result.get(DateTimeFieldType.minuteOfDay()));
0455: }
0456:
0457: public void testWith3c() {
0458: Partial test = createHourMinPartial();
0459: Partial result = test.with(DateTimeFieldType.dayOfMonth(), 15);
0460: check(test, 10, 20);
0461: assertEquals(3, result.size());
0462: assertEquals(true, result.isSupported(DateTimeFieldType
0463: .dayOfMonth()));
0464: assertEquals(true, result.isSupported(DateTimeFieldType
0465: .hourOfDay()));
0466: assertEquals(true, result.isSupported(DateTimeFieldType
0467: .minuteOfHour()));
0468: assertEquals(DateTimeFieldType.dayOfMonth(), result
0469: .getFieldType(0));
0470: assertEquals(DateTimeFieldType.hourOfDay(), result
0471: .getFieldType(1));
0472: assertEquals(DateTimeFieldType.minuteOfHour(), result
0473: .getFieldType(2));
0474: assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
0475: assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
0476: assertEquals(15, result.get(DateTimeFieldType.dayOfMonth()));
0477: }
0478:
0479: public void testWith3d() {
0480: Partial test = new Partial(DateTimeFieldType.year(), 2005);
0481: Partial result = test.with(DateTimeFieldType.monthOfYear(), 6);
0482: assertEquals(2, result.size());
0483: assertEquals(2005, result.get(DateTimeFieldType.year()));
0484: assertEquals(6, result.get(DateTimeFieldType.monthOfYear()));
0485: }
0486:
0487: public void testWith3e() {
0488: Partial test = new Partial(DateTimeFieldType.era(), 1);
0489: Partial result = test.with(DateTimeFieldType.halfdayOfDay(), 0);
0490: assertEquals(2, result.size());
0491: assertEquals(1, result.get(DateTimeFieldType.era()));
0492: assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay()));
0493: assertEquals(0, result.indexOf(DateTimeFieldType.era()));
0494: assertEquals(1, result
0495: .indexOf(DateTimeFieldType.halfdayOfDay()));
0496: }
0497:
0498: public void testWith3f() {
0499: Partial test = new Partial(DateTimeFieldType.halfdayOfDay(), 0);
0500: Partial result = test.with(DateTimeFieldType.era(), 1);
0501: assertEquals(2, result.size());
0502: assertEquals(1, result.get(DateTimeFieldType.era()));
0503: assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay()));
0504: assertEquals(0, result.indexOf(DateTimeFieldType.era()));
0505: assertEquals(1, result
0506: .indexOf(DateTimeFieldType.halfdayOfDay()));
0507: }
0508:
0509: public void testWith4() {
0510: Partial test = createHourMinPartial();
0511: Partial result = test.with(DateTimeFieldType.hourOfDay(), 10);
0512: assertSame(test, result);
0513: }
0514:
0515: //-----------------------------------------------------------------------
0516: public void testWithout1() {
0517: Partial test = createHourMinPartial();
0518: Partial result = test.without(DateTimeFieldType.year());
0519: check(test, 10, 20);
0520: check(result, 10, 20);
0521: }
0522:
0523: public void testWithout2() {
0524: Partial test = createHourMinPartial();
0525: Partial result = test.without((DateTimeFieldType) null);
0526: check(test, 10, 20);
0527: check(result, 10, 20);
0528: }
0529:
0530: public void testWithout3() {
0531: Partial test = createHourMinPartial();
0532: Partial result = test.without(DateTimeFieldType.hourOfDay());
0533: check(test, 10, 20);
0534: assertEquals(1, result.size());
0535: assertEquals(false, result.isSupported(DateTimeFieldType
0536: .hourOfDay()));
0537: assertEquals(true, result.isSupported(DateTimeFieldType
0538: .minuteOfHour()));
0539: assertEquals(DateTimeFieldType.minuteOfHour(), result
0540: .getFieldType(0));
0541: }
0542:
0543: public void testWithout4() {
0544: Partial test = createHourMinPartial();
0545: Partial result = test.without(DateTimeFieldType.minuteOfHour());
0546: check(test, 10, 20);
0547: assertEquals(1, result.size());
0548: assertEquals(true, result.isSupported(DateTimeFieldType
0549: .hourOfDay()));
0550: assertEquals(false, result.isSupported(DateTimeFieldType
0551: .minuteOfHour()));
0552: assertEquals(DateTimeFieldType.hourOfDay(), result
0553: .getFieldType(0));
0554: }
0555:
0556: public void testWithout5() {
0557: Partial test = new Partial(DateTimeFieldType.hourOfDay(), 12);
0558: Partial result = test.without(DateTimeFieldType.hourOfDay());
0559: assertEquals(0, result.size());
0560: assertEquals(false, result.isSupported(DateTimeFieldType
0561: .hourOfDay()));
0562: }
0563:
0564: //-----------------------------------------------------------------------
0565: public void testWithField1() {
0566: Partial test = createHourMinPartial();
0567: Partial result = test.withField(DateTimeFieldType.hourOfDay(),
0568: 15);
0569: check(test, 10, 20);
0570: check(result, 15, 20);
0571: }
0572:
0573: public void testWithField2() {
0574: Partial test = createHourMinPartial();
0575: try {
0576: test.withField(null, 6);
0577: fail();
0578: } catch (IllegalArgumentException ex) {
0579: }
0580: check(test, 10, 20);
0581: }
0582:
0583: public void testWithField3() {
0584: Partial test = createHourMinPartial();
0585: try {
0586: test.withField(DateTimeFieldType.dayOfMonth(), 6);
0587: fail();
0588: } catch (IllegalArgumentException ex) {
0589: }
0590: check(test, 10, 20);
0591: }
0592:
0593: public void testWithField4() {
0594: Partial test = createHourMinPartial();
0595: Partial result = test.withField(DateTimeFieldType.hourOfDay(),
0596: 10);
0597: assertSame(test, result);
0598: }
0599:
0600: //-----------------------------------------------------------------------
0601: public void testWithFieldAdded1() {
0602: Partial test = createHourMinPartial();
0603: Partial result = test.withFieldAdded(DurationFieldType.hours(),
0604: 6);
0605:
0606: assertEquals(createHourMinPartial(), test);
0607: check(test, 10, 20);
0608: check(result, 16, 20);
0609: }
0610:
0611: public void testWithFieldAdded2() {
0612: Partial test = createHourMinPartial();
0613: try {
0614: test.withFieldAdded(null, 0);
0615: fail();
0616: } catch (IllegalArgumentException ex) {
0617: }
0618: check(test, 10, 20);
0619: }
0620:
0621: public void testWithFieldAdded3() {
0622: Partial test = createHourMinPartial();
0623: try {
0624: test.withFieldAdded(null, 6);
0625: fail();
0626: } catch (IllegalArgumentException ex) {
0627: }
0628: check(test, 10, 20);
0629: }
0630:
0631: public void testWithFieldAdded4() {
0632: Partial test = createHourMinPartial();
0633: Partial result = test.withFieldAdded(DurationFieldType.hours(),
0634: 0);
0635: assertSame(test, result);
0636: }
0637:
0638: public void testWithFieldAdded5() {
0639: Partial test = createHourMinPartial();
0640: try {
0641: test.withFieldAdded(DurationFieldType.days(), 6);
0642: fail();
0643: } catch (IllegalArgumentException ex) {
0644: }
0645: check(test, 10, 20);
0646: }
0647:
0648: public void testWithFieldAdded6() {
0649: Partial test = createHourMinPartial();
0650: try {
0651: test.withFieldAdded(DurationFieldType.hours(), 16);
0652: fail();
0653: } catch (IllegalArgumentException ex) {
0654: // expected
0655: }
0656: check(test, 10, 20);
0657: }
0658:
0659: public void testWithFieldAdded7() {
0660: Partial test = createHourMinPartial(23, 59, ISO_UTC);
0661: try {
0662: test.withFieldAdded(DurationFieldType.minutes(), 1);
0663: fail();
0664: } catch (IllegalArgumentException ex) {
0665: // expected
0666: }
0667: check(test, 23, 59);
0668:
0669: test = createHourMinPartial(23, 59, ISO_UTC);
0670: try {
0671: test.withFieldAdded(DurationFieldType.hours(), 1);
0672: fail();
0673: } catch (IllegalArgumentException ex) {
0674: // expected
0675: }
0676: check(test, 23, 59);
0677: }
0678:
0679: public void testWithFieldAdded8() {
0680: Partial test = createHourMinPartial(0, 0, ISO_UTC);
0681: try {
0682: test.withFieldAdded(DurationFieldType.minutes(), -1);
0683: fail();
0684: } catch (IllegalArgumentException ex) {
0685: // expected
0686: }
0687: check(test, 0, 0);
0688:
0689: test = createHourMinPartial(0, 0, ISO_UTC);
0690: try {
0691: test.withFieldAdded(DurationFieldType.hours(), -1);
0692: fail();
0693: } catch (IllegalArgumentException ex) {
0694: // expected
0695: }
0696: check(test, 0, 0);
0697: }
0698:
0699: //-----------------------------------------------------------------------
0700: public void testWithFieldAddWrapped1() {
0701: Partial test = createHourMinPartial();
0702: Partial result = test.withFieldAddWrapped(DurationFieldType
0703: .hours(), 6);
0704:
0705: assertEquals(createHourMinPartial(), test);
0706: check(test, 10, 20);
0707: check(result, 16, 20);
0708: }
0709:
0710: public void testWithFieldAddWrapped2() {
0711: Partial test = createHourMinPartial();
0712: try {
0713: test.withFieldAddWrapped(null, 0);
0714: fail();
0715: } catch (IllegalArgumentException ex) {
0716: }
0717: check(test, 10, 20);
0718: }
0719:
0720: public void testWithFieldAddWrapped3() {
0721: Partial test = createHourMinPartial();
0722: try {
0723: test.withFieldAddWrapped(null, 6);
0724: fail();
0725: } catch (IllegalArgumentException ex) {
0726: }
0727: check(test, 10, 20);
0728: }
0729:
0730: public void testWithFieldAddWrapped4() {
0731: Partial test = createHourMinPartial();
0732: Partial result = test.withFieldAddWrapped(DurationFieldType
0733: .hours(), 0);
0734: assertSame(test, result);
0735: }
0736:
0737: public void testWithFieldAddWrapped5() {
0738: Partial test = createHourMinPartial();
0739: try {
0740: test.withFieldAddWrapped(DurationFieldType.days(), 6);
0741: fail();
0742: } catch (IllegalArgumentException ex) {
0743: }
0744: check(test, 10, 20);
0745: }
0746:
0747: public void testWithFieldAddWrapped6() {
0748: Partial test = createHourMinPartial();
0749: Partial result = test.withFieldAddWrapped(DurationFieldType
0750: .hours(), 16);
0751:
0752: assertEquals(createHourMinPartial(), test);
0753: check(test, 10, 20);
0754: check(result, 2, 20);
0755: }
0756:
0757: public void testWithFieldAddWrapped7() {
0758: Partial test = createHourMinPartial(23, 59, ISO_UTC);
0759: Partial result = test.withFieldAddWrapped(DurationFieldType
0760: .minutes(), 1);
0761: check(test, 23, 59);
0762: check(result, 0, 0);
0763:
0764: test = createHourMinPartial(23, 59, ISO_UTC);
0765: result = test.withFieldAddWrapped(DurationFieldType.hours(), 1);
0766: check(test, 23, 59);
0767: check(result, 0, 59);
0768: }
0769:
0770: public void testWithFieldAddWrapped8() {
0771: Partial test = createHourMinPartial(0, 0, ISO_UTC);
0772: Partial result = test.withFieldAddWrapped(DurationFieldType
0773: .minutes(), -1);
0774: check(test, 0, 0);
0775: check(result, 23, 59);
0776:
0777: test = createHourMinPartial(0, 0, ISO_UTC);
0778: result = test
0779: .withFieldAddWrapped(DurationFieldType.hours(), -1);
0780: check(test, 0, 0);
0781: check(result, 23, 0);
0782: }
0783:
0784: //-----------------------------------------------------------------------
0785: public void testPlus_RP() {
0786: Partial test = createHourMinPartial(BUDDHIST_LONDON);
0787: Partial result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8));
0788: check(test, 10, 20);
0789: check(result, 15, 26);
0790:
0791: result = test.plus((ReadablePeriod) null);
0792: assertSame(test, result);
0793: }
0794:
0795: //-----------------------------------------------------------------------
0796: public void testMinus_RP() {
0797: Partial test = createHourMinPartial(BUDDHIST_LONDON);
0798: Partial result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
0799: check(test, 10, 20);
0800: check(result, 9, 19);
0801:
0802: result = test.minus((ReadablePeriod) null);
0803: assertSame(test, result);
0804: }
0805:
0806: //-----------------------------------------------------------------------
0807: public void testToDateTime_RI() {
0808: Partial base = createHourMinPartial(COPTIC_PARIS);
0809: DateTime dt = new DateTime(0L); // LONDON zone
0810: assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
0811:
0812: DateTime test = base.toDateTime(dt);
0813: check(base, 10, 20);
0814: assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
0815: assertEquals("1970-01-01T10:20:00.000+01:00", test.toString());
0816: }
0817:
0818: public void testToDateTime_nullRI() {
0819: Partial base = createHourMinPartial(1, 2, ISO_UTC);
0820: DateTimeUtils.setCurrentMillisFixed(TEST_TIME2);
0821:
0822: DateTime test = base.toDateTime((ReadableInstant) null);
0823: check(base, 1, 2);
0824: assertEquals("1970-01-02T01:02:07.008+01:00", test.toString());
0825: }
0826:
0827: //-----------------------------------------------------------------------
0828: public void testProperty() {
0829: Partial test = createHourMinPartial();
0830: assertNotNull(test.property(DateTimeFieldType.hourOfDay()));
0831: assertNotNull(test.property(DateTimeFieldType.minuteOfHour()));
0832: try {
0833: test.property(DateTimeFieldType.secondOfDay());
0834: fail();
0835: } catch (IllegalArgumentException ex) {
0836: }
0837: try {
0838: test.property(null);
0839: fail();
0840: } catch (IllegalArgumentException ex) {
0841: }
0842: }
0843:
0844: //-----------------------------------------------------------------------
0845: public void testSerialization() throws Exception {
0846: Partial test = createHourMinPartial(COPTIC_PARIS);
0847:
0848: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0849: ObjectOutputStream oos = new ObjectOutputStream(baos);
0850: oos.writeObject(test);
0851: byte[] bytes = baos.toByteArray();
0852: oos.close();
0853:
0854: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0855: ObjectInputStream ois = new ObjectInputStream(bais);
0856: Partial result = (Partial) ois.readObject();
0857: ois.close();
0858:
0859: assertEquals(test, result);
0860: assertTrue(Arrays.equals(test.getValues(), result.getValues()));
0861: assertTrue(Arrays.equals(test.getFields(), result.getFields()));
0862: assertEquals(test.getChronology(), result.getChronology());
0863: }
0864:
0865: //-----------------------------------------------------------------------
0866: public void testGetFormatter1() {
0867: Partial test = new Partial(DateTimeFieldType.year(), 2005);
0868: assertEquals("2005", test.getFormatter().print(test));
0869:
0870: test = test.with(DateTimeFieldType.monthOfYear(), 6);
0871: assertEquals("2005-06", test.getFormatter().print(test));
0872:
0873: test = test.with(DateTimeFieldType.dayOfMonth(), 25);
0874: assertEquals("2005-06-25", test.getFormatter().print(test));
0875:
0876: test = test.without(DateTimeFieldType.monthOfYear());
0877: assertEquals("2005--25", test.getFormatter().print(test));
0878: }
0879:
0880: public void testGetFormatter2() {
0881: Partial test = new Partial();
0882: assertEquals(null, test.getFormatter());
0883:
0884: test = test.with(DateTimeFieldType.era(), 1);
0885: assertEquals(null, test.getFormatter());
0886:
0887: test = test.with(DateTimeFieldType.halfdayOfDay(), 0);
0888: assertEquals(null, test.getFormatter());
0889: }
0890:
0891: public void testGetFormatter3() {
0892: Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5);
0893: assertEquals("-W-5", test.getFormatter().print(test));
0894:
0895: // contrast with testToString5
0896: test = test.with(DateTimeFieldType.dayOfMonth(), 13);
0897: assertEquals("---13", test.getFormatter().print(test));
0898: }
0899:
0900: //-----------------------------------------------------------------------
0901: public void testToString1() {
0902: Partial test = createHourMinPartial();
0903: assertEquals("10:20", test.toString());
0904: }
0905:
0906: public void testToString2() {
0907: Partial test = new Partial();
0908: assertEquals("[]", test.toString());
0909: }
0910:
0911: public void testToString3() {
0912: Partial test = new Partial(DateTimeFieldType.year(), 2005);
0913: assertEquals("2005", test.toString());
0914:
0915: test = test.with(DateTimeFieldType.monthOfYear(), 6);
0916: assertEquals("2005-06", test.toString());
0917:
0918: test = test.with(DateTimeFieldType.dayOfMonth(), 25);
0919: assertEquals("2005-06-25", test.toString());
0920:
0921: test = test.without(DateTimeFieldType.monthOfYear());
0922: assertEquals("2005--25", test.toString());
0923: }
0924:
0925: public void testToString4() {
0926: Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5);
0927: assertEquals("-W-5", test.toString());
0928:
0929: test = test.with(DateTimeFieldType.dayOfMonth(), 13);
0930: assertEquals("[dayOfMonth=13, dayOfWeek=5]", test.toString());
0931: }
0932:
0933: public void testToString5() {
0934: Partial test = new Partial(DateTimeFieldType.era(), 1);
0935: assertEquals("[era=1]", test.toString());
0936:
0937: test = test.with(DateTimeFieldType.halfdayOfDay(), 0);
0938: assertEquals("[era=1, halfdayOfDay=0]", test.toString());
0939: }
0940:
0941: //-----------------------------------------------------------------------
0942: public void testToString_String() {
0943: Partial test = createHourMinPartial();
0944: assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test
0945: .toString("yyyy HH"));
0946: assertEquals("10:20", test.toString((String) null));
0947: }
0948:
0949: //-----------------------------------------------------------------------
0950: public void testToString_String_Locale() {
0951: Partial test = createHourMinPartial();
0952: assertEquals("10 20", test.toString("H m", Locale.ENGLISH));
0953: assertEquals("10:20", test.toString(null, Locale.ENGLISH));
0954: assertEquals("10 20", test.toString("H m", null));
0955: assertEquals("10:20", test.toString(null, null));
0956: }
0957:
0958: //-----------------------------------------------------------------------
0959: public void testToString_DTFormatter() {
0960: Partial test = createHourMinPartial();
0961: assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test
0962: .toString(DateTimeFormat.forPattern("yyyy HH")));
0963: assertEquals("10:20", test.toString((DateTimeFormatter) null));
0964: }
0965:
0966: //-----------------------------------------------------------------------
0967: private Partial createHourMinPartial() {
0968: return createHourMinPartial(ISO_UTC);
0969: }
0970:
0971: private Partial createHourMinPartial(Chronology chrono) {
0972: return createHourMinPartial(10, 20, chrono);
0973: }
0974:
0975: private Partial createHourMinPartial2(Chronology chrono) {
0976: return createHourMinPartial(15, 20, chrono);
0977: }
0978:
0979: private Partial createHourMinPartial(int hour, int min,
0980: Chronology chrono) {
0981: return new Partial(new DateTimeFieldType[] {
0982: DateTimeFieldType.hourOfDay(),
0983: DateTimeFieldType.minuteOfHour() }, new int[] { hour,
0984: min }, chrono);
0985: }
0986:
0987: private Partial createTODPartial(Chronology chrono) {
0988: return new Partial(new DateTimeFieldType[] {
0989: DateTimeFieldType.hourOfDay(),
0990: DateTimeFieldType.minuteOfHour(),
0991: DateTimeFieldType.secondOfMinute(),
0992: DateTimeFieldType.millisOfSecond() }, new int[] { 10,
0993: 20, 30, 40 }, chrono);
0994: }
0995:
0996: private void check(Partial test, int hour, int min) {
0997: assertEquals(test.toString(), hour, test.get(DateTimeFieldType
0998: .hourOfDay()));
0999: assertEquals(test.toString(), min, test.get(DateTimeFieldType
1000: .minuteOfHour()));
1001: }
1002: }
|