001: package org.tanukisoftware.wrapper;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: */
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests the conversion of system time into ticks.
032: *
033: * @author Leif Mortenson <leif@tanukisoftware.com>
034: */
035: public class WrapperManagerSystemTimeTestCase extends TestCase {
036: private final int TICK_MS = 100;
037:
038: /*---------------------------------------------------------------
039: * Constructor
040: *-------------------------------------------------------------*/
041: public WrapperManagerSystemTimeTestCase(String name) {
042: super (name);
043: }
044:
045: /*---------------------------------------------------------------
046: * Methods
047: *-------------------------------------------------------------*/
048: private int getSystemTicks(long time) {
049: // Calculate a tick count using the current system time. The
050: // conversion from a long in ms, to an int in TICK_MS increments
051: // will result in data loss, but the loss of bits and resulting
052: // overflow is expected and Ok.
053: return (int) (time / TICK_MS);
054: }
055:
056: private long getTickAge(int start, int end) {
057: // Important to cast the first value so that negative values are correctly
058: // cast to negative long values.
059: return (long) (end - start) * TICK_MS;
060: }
061:
062: public void doTest(long time, int expectedTicks, int negTicks,
063: int posTicks) {
064: int ticks = getSystemTicks(time);
065: assertEquals("getSystemTicks( " + time + " ) failed",
066: expectedTicks, ticks);
067:
068: long posAge = getTickAge(posTicks, expectedTicks);
069: assertEquals("getTickAge( " + posTicks + ", " + expectedTicks
070: + " )", 1000L, posAge);
071:
072: long negAge = getTickAge(negTicks, expectedTicks);
073: assertEquals("getTickAge( " + negTicks + ", " + expectedTicks
074: + " )", -1000L, negAge);
075: }
076:
077: /*---------------------------------------------------------------
078: * Test Cases
079: *-------------------------------------------------------------*/
080: public void testSimple() {
081: doTest(0L, 0, 10, -10);
082: doTest(1L, 0, 10, -10);
083: doTest(2L, 0, 10, -10);
084: doTest(99L, 0, 10, -10);
085: doTest(100L, 1, 11, -9);
086: doTest(101L, 1, 11, -9);
087: doTest(199L, 1, 11, -9);
088: doTest(200L, 2, 12, -8);
089: }
090:
091: public void testOverflow() {
092: doTest(214748363700L, 2147483637, 2147483647, 2147483627);
093: doTest(214748363800L, 2147483638, -2147483648, 2147483628);
094: doTest(214748363900L, 2147483639, -2147483647, 2147483629);
095: doTest(214748364000L, 2147483640, -2147483646, 2147483630);
096: doTest(214748364100L, 2147483641, -2147483645, 2147483631);
097: doTest(214748364200L, 2147483642, -2147483644, 2147483632);
098: doTest(214748364300L, 2147483643, -2147483643, 2147483633);
099: doTest(214748364400L, 2147483644, -2147483642, 2147483634);
100: doTest(214748364500L, 2147483645, -2147483641, 2147483635);
101: doTest(214748364600L, 2147483646, -2147483640, 2147483636);
102: doTest(214748364700L, 2147483647, -2147483639, 2147483637);
103: doTest(214748364800L, -2147483648, -2147483638, 2147483638);
104: doTest(214748364900L, -2147483647, -2147483637, 2147483639);
105: doTest(214748365000L, -2147483646, -2147483636, 2147483640);
106: doTest(214748365100L, -2147483645, -2147483635, 2147483641);
107: doTest(214748365200L, -2147483644, -2147483634, 2147483642);
108: doTest(214748365300L, -2147483643, -2147483633, 2147483643);
109: doTest(214748365400L, -2147483642, -2147483632, 2147483644);
110: doTest(214748365500L, -2147483641, -2147483631, 2147483645);
111: doTest(214748365600L, -2147483640, -2147483630, 2147483646);
112: doTest(214748365700L, -2147483639, -2147483629, 2147483647);
113: doTest(214748365800L, -2147483638, -2147483628, -2147483648);
114:
115: doTest(429496729300L, -3, 7, -13);
116: doTest(429496729400L, -2, 8, -12);
117: doTest(429496729500L, -1, 9, -11);
118: doTest(429496729600L, 0, 10, -10);
119: doTest(429496729700L, 1, 11, -9);
120: doTest(429496729800L, 2, 12, -8);
121: doTest(429496729900L, 3, 13, -7);
122: }
123: }
|