001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS;
036:
037: import junit.framework.TestCase;
038:
039: public class TTLTest extends TestCase {
040: private final long S = 1;
041: private final long M = 60 * S;
042: private final long H = 60 * M;
043: private final long D = 24 * H;
044: private final long W = 7 * D;
045:
046: public void test_parseTTL() {
047: assertEquals(9876, TTL.parseTTL("9876"));
048:
049: assertEquals(0, TTL.parseTTL("0S"));
050: assertEquals(0, TTL.parseTTL("0M"));
051: assertEquals(0, TTL.parseTTL("0H"));
052: assertEquals(0, TTL.parseTTL("0D"));
053: assertEquals(0, TTL.parseTTL("0W"));
054:
055: assertEquals(S, TTL.parseTTL("1s"));
056: assertEquals(M, TTL.parseTTL("1m"));
057: assertEquals(H, TTL.parseTTL("1h"));
058: assertEquals(D, TTL.parseTTL("1d"));
059: assertEquals(W, TTL.parseTTL("1w"));
060:
061: assertEquals(98 * S, TTL.parseTTL("98S"));
062: assertEquals(76 * M, TTL.parseTTL("76M"));
063: assertEquals(54 * H, TTL.parseTTL("54H"));
064: assertEquals(32 * D, TTL.parseTTL("32D"));
065: assertEquals(10 * W, TTL.parseTTL("10W"));
066:
067: assertEquals(98 * S + 11 * M + 1234 * H + 2 * D + W, TTL
068: .parseTTL("98S11M1234H2D01W"));
069: }
070:
071: public void test_parseTTL_invalid() {
072: try {
073: TTL.parseTTL(null);
074: fail("NumberFormatException not throw");
075: } catch (NumberFormatException e) {
076: }
077:
078: try {
079: TTL.parseTTL("");
080: fail("NumberFormatException not throw");
081: } catch (NumberFormatException e) {
082: }
083:
084: try {
085: TTL.parseTTL("S");
086: fail("NumberFormatException not throw");
087: } catch (NumberFormatException e) {
088: }
089:
090: try {
091: TTL.parseTTL("10S4B");
092: fail("NumberFormatException not throw");
093: } catch (NumberFormatException e) {
094: }
095:
096: try {
097: TTL.parseTTL("1S" + 0xFFFFFFFFL + "S");
098: fail("NumberFormatException not throw");
099: } catch (NumberFormatException e) {
100: }
101:
102: try {
103: TTL.parseTTL("" + 0x100000000L);
104: fail("NumberFormatException not throw");
105: } catch (NumberFormatException e) {
106: }
107: }
108:
109: public void test_format() {
110: assertEquals("0S", TTL.format(0));
111: assertEquals("1S", TTL.format(1));
112: assertEquals("59S", TTL.format(59));
113: assertEquals("1M", TTL.format(60));
114: assertEquals("59M", TTL.format(59 * M));
115: assertEquals("1M33S", TTL.format(M + 33));
116: assertEquals("59M59S", TTL.format(59 * M + 59 * S));
117: assertEquals("1H", TTL.format(H));
118: assertEquals("10H1M21S", TTL.format(10 * H + M + 21));
119: assertEquals("23H59M59S", TTL.format(23 * H + 59 * M + 59));
120: assertEquals("1D", TTL.format(D));
121: assertEquals("4D18H45M30S", TTL.format(4 * D + 18 * H + 45 * M
122: + 30));
123: assertEquals("6D23H59M59S", TTL.format(6 * D + 23 * H + 59 * M
124: + 59));
125: assertEquals("1W", TTL.format(W));
126: assertEquals("10W4D1H21M29S", TTL.format(10 * W + 4 * D + H
127: + 21 * M + 29));
128: assertEquals("3550W5D3H14M7S", TTL.format(0x7FFFFFFFL));
129: }
130:
131: public void test_format_invalid() {
132: try {
133: TTL.format(-1);
134: fail("InvalidTTLException not thrown");
135: } catch (InvalidTTLException e) {
136: }
137:
138: try {
139: TTL.format(0x100000000L);
140: fail("InvalidTTLException not thrown");
141: } catch (InvalidTTLException e) {
142: }
143: }
144: }
|