2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 package com.ibm.icu.dev.test.util;
12 import com.ibm.icu.dev.test.TestFmwk;
13 import com.ibm.icu.util.VersionInfo;
17 * Testing class for VersionInfo
18 * @author Syn Wee Quek
19 * @since release 2.1 March 01 2002
21 public final class VersionInfoTest extends TestFmwk
23 // constructor ---------------------------------------------------
28 public VersionInfoTest()
32 // public methods -----------------------------------------------
34 public static void main(String arg[])
36 VersionInfoTest test = new VersionInfoTest();
39 } catch (Exception e) {
40 test.errln("Error testing VersionInfo");
45 * Test that the instantiation works
47 public void TestInstance()
49 for (int i = 0; i < INSTANCE_INVALID_STRING_.length; i ++) {
51 VersionInfo.getInstance(INSTANCE_INVALID_STRING_[i]);
52 errln("\"" + INSTANCE_INVALID_STRING_[i] +
53 "\" should produce an exception");
54 } catch (RuntimeException e) {
55 logln("PASS: \"" + INSTANCE_INVALID_STRING_[i] +
56 "\" failed as expected");
59 for (int i = 0; i < INSTANCE_VALID_STRING_.length; i ++) {
61 VersionInfo.getInstance(INSTANCE_VALID_STRING_[i]);
62 } catch (RuntimeException e) {
63 errln("\"" + INSTANCE_VALID_STRING_[i] +
64 "\" should produce an valid version");
67 for (int i = 0; i < INSTANCE_INVALID_INT_.length; i ++) {
69 getInstance(INSTANCE_INVALID_INT_[i]);
70 errln("invalid ints should produce an exception");
71 } catch (RuntimeException e) {
72 logln("PASS: \"" + INSTANCE_INVALID_INT_[i] +
73 "\" failed as expected");
76 for (int i = 0; i < INSTANCE_VALID_INT_.length; i ++) {
78 getInstance(INSTANCE_VALID_INT_[i]);
79 } catch (RuntimeException e) {
80 errln("valid ints should not produce an exception");
86 * Test that the comparison works
88 public void TestCompare()
90 for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
92 VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
94 VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
95 if (v1.compareTo(v2) == 0) {
96 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
97 COMPARE_NOT_EQUAL_STRING_[i + 1]);
100 for (int i = 0; i < COMPARE_NOT_EQUAL_INT_.length; i += 2) {
101 VersionInfo v1 = getInstance(COMPARE_NOT_EQUAL_INT_[i]);
102 VersionInfo v2 = getInstance(COMPARE_NOT_EQUAL_INT_[i + 1]);
103 if (v1.compareTo(v2) == 0) {
104 errln(COMPARE_NOT_EQUAL_INT_[i] + " should not equal " +
105 COMPARE_NOT_EQUAL_INT_[i + 1]);
108 for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
110 VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
112 VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
113 if (v1.compareTo(v2) != 0) {
114 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
115 COMPARE_EQUAL_STRING_[i + 1]);
118 for (int i = 0; i < COMPARE_EQUAL_INT_.length - 1; i ++) {
119 VersionInfo v1 = getInstance(COMPARE_EQUAL_INT_[i]);
120 VersionInfo v2 = getInstance(COMPARE_EQUAL_INT_[i + 1]);
121 if (v1.compareTo(v2) != 0) {
122 errln(COMPARE_EQUAL_INT_[i] + " should equal " +
123 COMPARE_EQUAL_INT_[i + 1]);
126 for (int i = 0; i < COMPARE_LESS_.length - 1; i ++) {
127 VersionInfo v1 = VersionInfo.getInstance(COMPARE_LESS_[i]);
128 VersionInfo v2 = VersionInfo.getInstance(COMPARE_LESS_[i + 1]);
129 if (v1.compareTo(v2) >= 0) {
130 errln(COMPARE_LESS_[i] + " should be less than " +
131 COMPARE_LESS_[i + 1]);
133 if (v2.compareTo(v1) <= 0) {
134 errln(COMPARE_LESS_[i + 1] + " should be greater than " +
141 * Test that the getter function works
143 public void TestGetter()
145 for (int i = 0; i < GET_STRING_.length; i ++) {
146 VersionInfo v = VersionInfo.getInstance(GET_STRING_[i]);
147 if (v.getMajor() != GET_RESULT_[i << 2] ||
148 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
149 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
150 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
151 errln(GET_STRING_[i] + " should return major=" +
152 GET_RESULT_[i << 2] + " minor=" +
153 GET_RESULT_[(i << 2) + 1] + " milli=" +
154 GET_RESULT_[(i << 2) + 2] + " micro=" +
155 GET_RESULT_[(i << 2) + 3]);
157 v = getInstance(GET_INT_[i]);
158 if (v.getMajor() != GET_RESULT_[i << 2] ||
159 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
160 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
161 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
162 errln(GET_STRING_[i] + " should return major=" +
163 GET_RESULT_[i << 2] + " minor=" +
164 GET_RESULT_[(i << 2) + 1] + " milli=" +
165 GET_RESULT_[(i << 2) + 2] + " micro=" +
166 GET_RESULT_[(i << 2) + 3]);
174 public void TesttoString()
176 for (int i = 0; i < TOSTRING_STRING_.length; i ++) {
177 VersionInfo v = VersionInfo.getInstance(TOSTRING_STRING_[i]);
178 if (!v.toString().equals(TOSTRING_RESULT_[i])) {
179 errln("toString() for " + TOSTRING_STRING_[i] +
180 " should produce " + TOSTRING_RESULT_[i]);
182 v = getInstance(TOSTRING_INT_[i]);
183 if (!v.toString().equals(TOSTRING_RESULT_[i])) {
184 errln("toString() for " + TOSTRING_INT_[i] +
185 " should produce " + TOSTRING_RESULT_[i]);
191 * Test Comparable interface
193 public void TestComparable() {
194 for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
195 VersionInfo v1 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
196 VersionInfo v2 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
197 if (v1.compareTo(v2) == 0) {
198 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
199 COMPARE_NOT_EQUAL_STRING_[i + 1]);
202 for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
203 VersionInfo v1 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
204 VersionInfo v2 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
205 if (v1.compareTo(v2) != 0) {
206 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
207 COMPARE_EQUAL_STRING_[i + 1]);
211 // private methods --------------------------------------------------
214 * int array versioninfo creation
216 private static VersionInfo getInstance(int data[])
218 switch (data.length) {
220 return VersionInfo.getInstance(data[0]);
222 return VersionInfo.getInstance(data[0], data[1]);
224 return VersionInfo.getInstance(data[0], data[1], data[2]);
226 return VersionInfo.getInstance(data[0], data[1], data[2],
231 // private data members --------------------------------------------
236 private static final String INSTANCE_INVALID_STRING_[] = {
254 private static final String INSTANCE_VALID_STRING_[] = {
260 private static final int INSTANCE_INVALID_INT_[][] = {
276 private static final int INSTANCE_VALID_INT_[][] = {
286 private static final String COMPARE_NOT_EQUAL_STRING_[] = {
289 private static final int COMPARE_NOT_EQUAL_INT_[][] = {
290 {2, 0, 0, 0}, {3, 0, 0, 0}
292 private static final String COMPARE_EQUAL_STRING_[] = {
293 "2.0.0.0", "2.0.0", "2.0", "2"
295 private static final int COMPARE_EQUAL_INT_[][] = {
296 {2}, {2, 0}, {2, 0, 0}, {2, 0, 0, 0}
298 private static final String COMPARE_LESS_[] = {
299 "0", "0.0.0.1", "0.0.1", "0.1", "1", "2", "2.1", "2.1.1", "2.1.1.1"
305 private static final String GET_STRING_[] = {
311 private static final int GET_INT_[][] = {
317 private static final int GET_RESULT_[] = {
327 private static final String TOSTRING_STRING_[] = {
333 private static final int TOSTRING_INT_[][] = {
339 private static final String TOSTRING_RESULT_[] = {
347 * Test case for multi-threading problem reported by ticket#7880
349 public void TestMultiThread() {
350 final int numThreads = 20;
351 GetInstanceWorker[] workers = new GetInstanceWorker[numThreads];
352 VersionInfo[][] results = new VersionInfo[numThreads][255];
355 for (int i = 0; i < workers.length; i++) {
356 workers[i] = new GetInstanceWorker(i, results[i]);
360 for (int i = 0; i < workers.length; i++) {
364 // Wait for the completion
365 for (int i = 0; i < workers.length; i++) {
368 } catch (InterruptedException e) {
369 errln("A problem in thread execution. " + e.getMessage());
373 // Check if singleton for each
374 for (int i = 1; i < results.length; i++) {
375 for (int j = 0; j < results[0].length; j++) {
376 if (results[0][j] != results[i][j]) {
377 errln("Different instance at index " + j + " Thread#" + i);
383 private class GetInstanceWorker extends Thread {
384 private VersionInfo[] results;
386 GetInstanceWorker(int serialNumber, VersionInfo[] results) {
387 super("GetInstnaceWorker#" + serialNumber);
388 this.results = results;
392 for (int i = 0; i < results.length; i++) {
393 results[i] = VersionInfo.getInstance(i);