KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > WrapperManagerSystemTimeTestCase


1 package org.tanukisoftware.wrapper;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */

27
28 import junit.framework.TestCase;
29
30 /**
31  * Tests the conversion of system time into ticks.
32  *
33  * @author Leif Mortenson <leif@tanukisoftware.com>
34  */

35 public class WrapperManagerSystemTimeTestCase
36     extends TestCase
37 {
38     private final int TICK_MS = 100;
39     
40     /*---------------------------------------------------------------
41      * Constructor
42      *-------------------------------------------------------------*/

43     public WrapperManagerSystemTimeTestCase( String JavaDoc name )
44     {
45         super( name );
46     }
47     
48     /*---------------------------------------------------------------
49      * Methods
50      *-------------------------------------------------------------*/

51     private int getSystemTicks( long time )
52     {
53         // Calculate a tick count using the current system time. The
54
// conversion from a long in ms, to an int in TICK_MS increments
55
// will result in data loss, but the loss of bits and resulting
56
// overflow is expected and Ok.
57
return (int)( time / TICK_MS );
58     }
59     
60     private long getTickAge( int start, int end )
61     {
62         // Important to cast the first value so that negative values are correctly
63
// cast to negative long values.
64
return (long)( end - start ) * TICK_MS;
65     }
66     
67     public void doTest( long time, int expectedTicks, int negTicks, int posTicks )
68     {
69         int ticks = getSystemTicks( time );
70         assertEquals( "getSystemTicks( " + time + " ) failed", expectedTicks, ticks );
71         
72         long posAge = getTickAge( posTicks, expectedTicks );
73         assertEquals( "getTickAge( " + posTicks + ", " + expectedTicks + " )", 1000L, posAge );
74         
75         long negAge = getTickAge( negTicks, expectedTicks );
76         assertEquals( "getTickAge( " + negTicks + ", " + expectedTicks + " )", -1000L, negAge );
77     }
78     
79     /*---------------------------------------------------------------
80      * Test Cases
81      *-------------------------------------------------------------*/

82     public void testSimple()
83     {
84         doTest( 0L, 0, 10, -10 );
85         doTest( 1L, 0, 10, -10 );
86         doTest( 2L, 0, 10, -10 );
87         doTest( 99L, 0, 10, -10 );
88         doTest( 100L, 1, 11, -9 );
89         doTest( 101L, 1, 11, -9 );
90         doTest( 199L, 1, 11, -9 );
91         doTest( 200L, 2, 12, -8 );
92     }
93     
94     public void testOverflow()
95     {
96         doTest( 214748363700L, 2147483637, 2147483647, 2147483627 );
97         doTest( 214748363800L, 2147483638, -2147483648, 2147483628 );
98         doTest( 214748363900L, 2147483639, -2147483647, 2147483629 );
99         doTest( 214748364000L, 2147483640, -2147483646, 2147483630 );
100         doTest( 214748364100L, 2147483641, -2147483645, 2147483631 );
101         doTest( 214748364200L, 2147483642, -2147483644, 2147483632 );
102         doTest( 214748364300L, 2147483643, -2147483643, 2147483633 );
103         doTest( 214748364400L, 2147483644, -2147483642, 2147483634 );
104         doTest( 214748364500L, 2147483645, -2147483641, 2147483635 );
105         doTest( 214748364600L, 2147483646, -2147483640, 2147483636 );
106         doTest( 214748364700L, 2147483647, -2147483639, 2147483637 );
107         doTest( 214748364800L, -2147483648, -2147483638, 2147483638 );
108         doTest( 214748364900L, -2147483647, -2147483637, 2147483639 );
109         doTest( 214748365000L, -2147483646, -2147483636, 2147483640 );
110         doTest( 214748365100L, -2147483645, -2147483635, 2147483641 );
111         doTest( 214748365200L, -2147483644, -2147483634, 2147483642 );
112         doTest( 214748365300L, -2147483643, -2147483633, 2147483643 );
113         doTest( 214748365400L, -2147483642, -2147483632, 2147483644 );
114         doTest( 214748365500L, -2147483641, -2147483631, 2147483645 );
115         doTest( 214748365600L, -2147483640, -2147483630, 2147483646 );
116         doTest( 214748365700L, -2147483639, -2147483629, 2147483647 );
117         doTest( 214748365800L, -2147483638, -2147483628, -2147483648 );
118         
119         doTest( 429496729300L, -3, 7, -13 );
120         doTest( 429496729400L, -2, 8, -12 );
121         doTest( 429496729500L, -1, 9, -11 );
122         doTest( 429496729600L, 0, 10, -10 );
123         doTest( 429496729700L, 1, 11, -9 );
124         doTest( 429496729800L, 2, 12, -8 );
125         doTest( 429496729900L, 3, 13, -7 );
126     }
127 }
128
Popular Tags