1 // -*- Java -*- 2 // 3 // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu> 4 // Copyright (c) 2005, University of Colorado at Boulder 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions are 9 // met: 10 // 11 // * Redistributions of source code must retain the above copyright 12 // notice, this list of conditions and the following disclaimer. 13 // 14 // * Redistributions in binary form must reproduce the above copyright 15 // notice, this list of conditions and the following disclaimer in the 16 // documentation and/or other materials provided with the distribution. 17 // 18 // * Neither the name of the University of Colorado at Boulder nor the 19 // names of its contributors may be used to endorse or promote 20 // products derived from this software without specific prior written 21 // permission. 22 // 23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 // 35 package org.xbill.DNS; 36 37 import java.util.Date; 38 import java.util.Calendar; 39 import java.util.GregorianCalendar; 40 import java.util.TimeZone; 41 import junit.framework.TestCase; 42 import org.xbill.DNS.FormattedTime; 43 44 public class FormattedTimeTest extends TestCase 45 { test_format()46 public void test_format() 47 { 48 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 49 cal.set(2005, 2, 19, 4, 4, 5); 50 String out = FormattedTime.format(cal.getTime()); 51 assertEquals("20050319040405", out); 52 } 53 test_parse()54 public void test_parse() throws TextParseException 55 { 56 // have to make sure to clear out the milliseconds since there 57 // is occasionally a difference between when cal and cal2 are 58 // instantiated. 59 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 60 cal.set(2005, 2, 19, 4, 4, 5); 61 cal.set(Calendar.MILLISECOND, 0); 62 63 Date out = FormattedTime.parse("20050319040405"); 64 GregorianCalendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 65 cal2.setTimeInMillis(out.getTime()); 66 cal2.set(Calendar.MILLISECOND, 0); 67 assertEquals(cal, cal2); 68 } 69 test_parse_invalid()70 public void test_parse_invalid() 71 { 72 try { 73 FormattedTime.parse("2004010101010"); 74 fail("TextParseException not thrown"); 75 } 76 catch( TextParseException e ){ 77 } 78 try { 79 FormattedTime.parse("200401010101010"); 80 fail("TextParseException not thrown"); 81 } 82 catch( TextParseException e ){ 83 } 84 try { 85 FormattedTime.parse("2004010101010A"); 86 fail("TextParseException not thrown"); 87 } 88 catch( TextParseException e ){ 89 } 90 } 91 } 92