001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.dev.test.util;
009:
010: import com.ibm.icu.dev.test.TestFmwk;
011: import com.ibm.icu.util.VersionInfo;
012:
013: /**
014: * Testing class for VersionInfo
015: * @author Syn Wee Quek
016: * @since release 2.1 March 01 2002
017: */
018: public final class VersionInfoTest extends TestFmwk {
019: // constructor ---------------------------------------------------
020:
021: /**
022: * Constructor
023: */
024: public VersionInfoTest() {
025: }
026:
027: // public methods -----------------------------------------------
028:
029: public static void main(String arg[]) {
030: VersionInfoTest test = new VersionInfoTest();
031: try {
032: test.run(arg);
033: } catch (Exception e) {
034: test.errln("Error testing VersionInfo");
035: }
036: }
037:
038: /**
039: * Test that the instantiation works
040: */
041: public void TestInstance() {
042: for (int i = 0; i < INSTANCE_INVALID_STRING_.length; i++) {
043: try {
044: VersionInfo version = VersionInfo
045: .getInstance(INSTANCE_INVALID_STRING_[i]);
046: errln("\"" + INSTANCE_INVALID_STRING_[i]
047: + "\" should produce an exception");
048: } catch (RuntimeException e) {
049: logln("PASS: \"" + INSTANCE_INVALID_STRING_[i]
050: + "\" failed as expected");
051: }
052: }
053: for (int i = 0; i < INSTANCE_VALID_STRING_.length; i++) {
054: try {
055: VersionInfo version = VersionInfo
056: .getInstance(INSTANCE_VALID_STRING_[i]);
057: } catch (RuntimeException e) {
058: errln("\"" + INSTANCE_VALID_STRING_[i]
059: + "\" should produce an valid version");
060: }
061: }
062: for (int i = 0; i < INSTANCE_INVALID_INT_.length; i++) {
063: try {
064: VersionInfo version = getInstance(INSTANCE_INVALID_INT_[i]);
065: errln("invalid ints should produce an exception");
066: } catch (RuntimeException e) {
067: logln("PASS: \"" + INSTANCE_INVALID_INT_[i]
068: + "\" failed as expected");
069: }
070: }
071: for (int i = 0; i < INSTANCE_VALID_INT_.length; i++) {
072: try {
073: VersionInfo version = getInstance(INSTANCE_VALID_INT_[i]);
074: } catch (RuntimeException e) {
075: errln("valid ints should not produce an exception");
076: }
077: }
078: }
079:
080: /**
081: * Test that the comparison works
082: */
083: public void TestCompare() {
084: for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
085: VersionInfo v1 = VersionInfo
086: .getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
087: VersionInfo v2 = VersionInfo
088: .getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
089: if (v1.compareTo(v2) == 0) {
090: errln(COMPARE_NOT_EQUAL_STRING_[i]
091: + " should not equal "
092: + COMPARE_NOT_EQUAL_STRING_[i + 1]);
093: }
094: }
095: for (int i = 0; i < COMPARE_NOT_EQUAL_INT_.length; i += 2) {
096: VersionInfo v1 = getInstance(COMPARE_NOT_EQUAL_INT_[i]);
097: VersionInfo v2 = getInstance(COMPARE_NOT_EQUAL_INT_[i + 1]);
098: if (v1.compareTo(v2) == 0) {
099: errln(COMPARE_NOT_EQUAL_INT_[i] + " should not equal "
100: + COMPARE_NOT_EQUAL_INT_[i + 1]);
101: }
102: }
103: for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i++) {
104: VersionInfo v1 = VersionInfo
105: .getInstance(COMPARE_EQUAL_STRING_[i]);
106: VersionInfo v2 = VersionInfo
107: .getInstance(COMPARE_EQUAL_STRING_[i + 1]);
108: if (v1.compareTo(v2) != 0) {
109: errln(COMPARE_EQUAL_STRING_[i] + " should equal "
110: + COMPARE_EQUAL_STRING_[i + 1]);
111: }
112: }
113: for (int i = 0; i < COMPARE_EQUAL_INT_.length - 1; i++) {
114: VersionInfo v1 = getInstance(COMPARE_EQUAL_INT_[i]);
115: VersionInfo v2 = getInstance(COMPARE_EQUAL_INT_[i + 1]);
116: if (v1.compareTo(v2) != 0) {
117: errln(COMPARE_EQUAL_INT_[i] + " should equal "
118: + COMPARE_EQUAL_INT_[i + 1]);
119: }
120: }
121: for (int i = 0; i < COMPARE_LESS_.length - 1; i++) {
122: VersionInfo v1 = VersionInfo.getInstance(COMPARE_LESS_[i]);
123: VersionInfo v2 = VersionInfo
124: .getInstance(COMPARE_LESS_[i + 1]);
125: if (v1.compareTo(v2) >= 0) {
126: errln(COMPARE_LESS_[i] + " should be less than "
127: + COMPARE_LESS_[i + 1]);
128: }
129: if (v2.compareTo(v1) <= 0) {
130: errln(COMPARE_LESS_[i + 1] + " should be greater than "
131: + COMPARE_LESS_[i]);
132: }
133: }
134: }
135:
136: /**
137: * Test that the getter function works
138: */
139: public void TestGetter() {
140: for (int i = 0; i < GET_STRING_.length; i++) {
141: VersionInfo v = VersionInfo.getInstance(GET_STRING_[i]);
142: if (v.getMajor() != GET_RESULT_[i << 2]
143: || v.getMinor() != GET_RESULT_[(i << 2) + 1]
144: || v.getMilli() != GET_RESULT_[(i << 2) + 2]
145: || v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
146: errln(GET_STRING_[i] + " should return major="
147: + GET_RESULT_[i << 2] + " minor="
148: + GET_RESULT_[(i << 2) + 1] + " milli="
149: + GET_RESULT_[(i << 2) + 2] + " micro="
150: + GET_RESULT_[(i << 2) + 3]);
151: }
152: v = getInstance(GET_INT_[i]);
153: if (v.getMajor() != GET_RESULT_[i << 2]
154: || v.getMinor() != GET_RESULT_[(i << 2) + 1]
155: || v.getMilli() != GET_RESULT_[(i << 2) + 2]
156: || v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
157: errln(GET_STRING_[i] + " should return major="
158: + GET_RESULT_[i << 2] + " minor="
159: + GET_RESULT_[(i << 2) + 1] + " milli="
160: + GET_RESULT_[(i << 2) + 2] + " micro="
161: + GET_RESULT_[(i << 2) + 3]);
162: }
163: }
164: }
165:
166: /**
167: * Test toString()
168: */
169: public void TesttoString() {
170: for (int i = 0; i < TOSTRING_STRING_.length; i++) {
171: VersionInfo v = VersionInfo
172: .getInstance(TOSTRING_STRING_[i]);
173: if (!v.toString().equals(TOSTRING_RESULT_[i])) {
174: errln("toString() for " + TOSTRING_STRING_[i]
175: + " should produce " + TOSTRING_RESULT_[i]);
176: }
177: v = getInstance(TOSTRING_INT_[i]);
178: if (!v.toString().equals(TOSTRING_RESULT_[i])) {
179: errln("toString() for " + TOSTRING_INT_[i]
180: + " should produce " + TOSTRING_RESULT_[i]);
181: }
182: }
183: }
184:
185: // private methods --------------------------------------------------
186:
187: /**
188: * int array versioninfo creation
189: */
190: private static VersionInfo getInstance(int data[]) {
191: switch (data.length) {
192: case 1:
193: return VersionInfo.getInstance(data[0]);
194: case 2:
195: return VersionInfo.getInstance(data[0], data[1]);
196: case 3:
197: return VersionInfo.getInstance(data[0], data[1], data[2]);
198: default:
199: return VersionInfo.getInstance(data[0], data[1], data[2],
200: data[3]);
201: }
202: }
203:
204: // private data members --------------------------------------------
205:
206: /**
207: * Test instance data
208: */
209: private static final String INSTANCE_INVALID_STRING_[] = { "a",
210: "-1", "-1.0", "-1.0.0", "-1.0.0.0", "0.-1", "0.0.-1",
211: "0.0.0.-1", "256", "256.0", "256.0.0", "256.0.0.0",
212: "0.256", "0.0.256", "0.0.0.256", "1.2.3.4.5" };
213: private static final String INSTANCE_VALID_STRING_[] = { "255",
214: "255.255", "255.255.255", "255.255.255.255" };
215: private static final int INSTANCE_INVALID_INT_[][] = { { -1 },
216: { -1, 0 }, { -1, 0, 0 }, { -1, 0, 0, 0 }, { 0, -1 },
217: { 0, 0, -1 }, { 0, 0, 0, -1 }, { 256 }, { 256, 0 },
218: { 256, 0, 0 }, { 256, 0, 0, 0 }, { 0, 256 }, { 0, 0, 256 },
219: { 0, 0, 0, 256 }, };
220: private static final int INSTANCE_VALID_INT_[][] = { { 255 },
221: { 255, 255 }, { 255, 255, 255 }, { 255, 255, 255, 255 } };
222:
223: /**
224: * Test compare data
225: */
226: private static final String COMPARE_NOT_EQUAL_STRING_[] = {
227: "2.0.0.0", "3.0.0.0" };
228: private static final int COMPARE_NOT_EQUAL_INT_[][] = {
229: { 2, 0, 0, 0 }, { 3, 0, 0, 0 } };
230: private static final String COMPARE_EQUAL_STRING_[] = { "2.0.0.0",
231: "2.0.0", "2.0", "2" };
232: private static final int COMPARE_EQUAL_INT_[][] = { { 2 },
233: { 2, 0 }, { 2, 0, 0 }, { 2, 0, 0, 0 } };
234: private static final String COMPARE_LESS_[] = { "0", "0.0.0.1",
235: "0.0.1", "0.1", "1", "2", "2.1", "2.1.1", "2.1.1.1" };
236:
237: /**
238: * Test Getter data
239: */
240: private static final String GET_STRING_[] = { "0", "1.1",
241: "2.1.255", "3.1.255.100" };
242: private static final int GET_INT_[][] = { { 0 }, { 1, 1 },
243: { 2, 1, 255 }, { 3, 1, 255, 100 } };
244: private static final int GET_RESULT_[] = { 0, 0, 0, 0, 1, 1, 0, 0,
245: 2, 1, 255, 0, 3, 1, 255, 100 };
246:
247: /**
248: * Test toString data
249: */
250: private static final String TOSTRING_STRING_[] = { "0", "1.1",
251: "2.1.255", "3.1.255.100" };
252: private static final int TOSTRING_INT_[][] = { { 0 }, { 1, 1 },
253: { 2, 1, 255 }, { 3, 1, 255, 100 } };
254: private static final String TOSTRING_RESULT_[] = { "0.0.0.0",
255: "1.1.0.0", "2.1.255.0", "3.1.255.100" };
256: }
|