2 *******************************************************************************
3 * Copyright (C) 1996-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 package com.ibm.icu.dev.test.util;
12 import java.util.Arrays;
14 import com.ibm.icu.dev.test.TestFmwk;
15 import com.ibm.icu.util.VersionInfo;
19 * Testing class for VersionInfo
20 * @author Syn Wee Quek
21 * @since release 2.1 March 01 2002
23 public final class VersionInfoTest extends TestFmwk
25 // constructor ---------------------------------------------------
30 public VersionInfoTest()
34 // public methods -----------------------------------------------
36 public static void main(String arg[])
38 VersionInfoTest test = new VersionInfoTest();
41 } catch (Exception e) {
42 test.errln("Error testing VersionInfo");
47 * Test that the instantiation works
49 public void TestInstance()
51 for (int i = 0; i < INSTANCE_INVALID_STRING_.length; i ++) {
53 VersionInfo.getInstance(INSTANCE_INVALID_STRING_[i]);
54 errln("\"" + INSTANCE_INVALID_STRING_[i] +
55 "\" should produce an exception");
56 } catch (RuntimeException e) {
57 logln("PASS: \"" + INSTANCE_INVALID_STRING_[i] +
58 "\" failed as expected");
61 for (int i = 0; i < INSTANCE_VALID_STRING_.length; i ++) {
63 VersionInfo.getInstance(INSTANCE_VALID_STRING_[i]);
64 } catch (RuntimeException e) {
65 errln("\"" + INSTANCE_VALID_STRING_[i] +
66 "\" should produce an valid version");
69 for (int i = 0; i < INSTANCE_INVALID_INT_.length; i ++) {
71 getInstance(INSTANCE_INVALID_INT_[i]);
72 errln("invalid ints should produce an exception");
73 } catch (RuntimeException e) {
74 logln("PASS: \"" + INSTANCE_INVALID_INT_[i] +
75 "\" failed as expected");
78 for (int i = 0; i < INSTANCE_VALID_INT_.length; i ++) {
80 getInstance(INSTANCE_VALID_INT_[i]);
81 } catch (RuntimeException e) {
82 errln("valid ints should not produce an exception");
88 * Test that the comparison works
90 public void TestCompare()
92 for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
94 VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
96 VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
97 if (v1.compareTo(v2) == 0) {
98 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
99 COMPARE_NOT_EQUAL_STRING_[i + 1]);
102 for (int i = 0; i < COMPARE_NOT_EQUAL_INT_.length; i += 2) {
103 VersionInfo v1 = getInstance(COMPARE_NOT_EQUAL_INT_[i]);
104 VersionInfo v2 = getInstance(COMPARE_NOT_EQUAL_INT_[i + 1]);
105 if (v1.compareTo(v2) == 0) {
106 errln(COMPARE_NOT_EQUAL_INT_[i] + " should not equal " +
107 COMPARE_NOT_EQUAL_INT_[i + 1]);
110 for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
112 VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
114 VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
115 if (v1.compareTo(v2) != 0) {
116 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
117 COMPARE_EQUAL_STRING_[i + 1]);
120 for (int i = 0; i < COMPARE_EQUAL_INT_.length - 1; i ++) {
121 VersionInfo v1 = getInstance(COMPARE_EQUAL_INT_[i]);
122 VersionInfo v2 = getInstance(COMPARE_EQUAL_INT_[i + 1]);
123 if (v1.compareTo(v2) != 0) {
124 errln(Arrays.toString(COMPARE_EQUAL_INT_[i]) + " should equal " +
125 Arrays.toString(COMPARE_EQUAL_INT_[i + 1]));
128 for (int i = 0; i < COMPARE_LESS_.length - 1; i ++) {
129 VersionInfo v1 = VersionInfo.getInstance(COMPARE_LESS_[i]);
130 VersionInfo v2 = VersionInfo.getInstance(COMPARE_LESS_[i + 1]);
131 if (v1.compareTo(v2) >= 0) {
132 errln(COMPARE_LESS_[i] + " should be less than " +
133 COMPARE_LESS_[i + 1]);
135 if (v2.compareTo(v1) <= 0) {
136 errln(COMPARE_LESS_[i + 1] + " should be greater than " +
143 * Test that the getter function works
145 public void TestGetter()
147 for (int i = 0; i < GET_STRING_.length; i ++) {
148 VersionInfo v = VersionInfo.getInstance(GET_STRING_[i]);
149 if (v.getMajor() != GET_RESULT_[i << 2] ||
150 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
151 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
152 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
153 errln(GET_STRING_[i] + " should return major=" +
154 GET_RESULT_[i << 2] + " minor=" +
155 GET_RESULT_[(i << 2) + 1] + " milli=" +
156 GET_RESULT_[(i << 2) + 2] + " micro=" +
157 GET_RESULT_[(i << 2) + 3]);
159 v = getInstance(GET_INT_[i]);
160 if (v.getMajor() != GET_RESULT_[i << 2] ||
161 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
162 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
163 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
164 errln(GET_STRING_[i] + " should return major=" +
165 GET_RESULT_[i << 2] + " minor=" +
166 GET_RESULT_[(i << 2) + 1] + " milli=" +
167 GET_RESULT_[(i << 2) + 2] + " micro=" +
168 GET_RESULT_[(i << 2) + 3]);
176 public void TesttoString()
178 for (int i = 0; i < TOSTRING_STRING_.length; i ++) {
179 VersionInfo v = VersionInfo.getInstance(TOSTRING_STRING_[i]);
180 if (!v.toString().equals(TOSTRING_RESULT_[i])) {
181 errln("toString() for " + TOSTRING_STRING_[i] +
182 " should produce " + TOSTRING_RESULT_[i]);
184 v = getInstance(TOSTRING_INT_[i]);
185 if (!v.toString().equals(TOSTRING_RESULT_[i])) {
186 errln("toString() for " + Arrays.toString(TOSTRING_INT_[i]) +
187 " should produce " + TOSTRING_RESULT_[i]);
193 * Test Comparable interface
195 public void TestComparable() {
196 for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
197 VersionInfo v1 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
198 VersionInfo v2 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
199 if (v1.compareTo(v2) == 0) {
200 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
201 COMPARE_NOT_EQUAL_STRING_[i + 1]);
204 for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
205 VersionInfo v1 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
206 VersionInfo v2 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
207 if (v1.compareTo(v2) != 0) {
208 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
209 COMPARE_EQUAL_STRING_[i + 1]);
213 // private methods --------------------------------------------------
216 * int array versioninfo creation
218 private static VersionInfo getInstance(int data[])
220 switch (data.length) {
222 return VersionInfo.getInstance(data[0]);
224 return VersionInfo.getInstance(data[0], data[1]);
226 return VersionInfo.getInstance(data[0], data[1], data[2]);
228 return VersionInfo.getInstance(data[0], data[1], data[2],
233 // private data members --------------------------------------------
238 private static final String INSTANCE_INVALID_STRING_[] = {
256 private static final String INSTANCE_VALID_STRING_[] = {
262 private static final int INSTANCE_INVALID_INT_[][] = {
278 private static final int INSTANCE_VALID_INT_[][] = {
288 private static final String COMPARE_NOT_EQUAL_STRING_[] = {
291 private static final int COMPARE_NOT_EQUAL_INT_[][] = {
292 {2, 0, 0, 0}, {3, 0, 0, 0}
294 private static final String COMPARE_EQUAL_STRING_[] = {
295 "2.0.0.0", "2.0.0", "2.0", "2"
297 private static final int COMPARE_EQUAL_INT_[][] = {
298 {2}, {2, 0}, {2, 0, 0}, {2, 0, 0, 0}
300 private static final String COMPARE_LESS_[] = {
301 "0", "0.0.0.1", "0.0.1", "0.1", "1", "2", "2.1", "2.1.1", "2.1.1.1"
307 private static final String GET_STRING_[] = {
313 private static final int GET_INT_[][] = {
319 private static final int GET_RESULT_[] = {
329 private static final String TOSTRING_STRING_[] = {
335 private static final int TOSTRING_INT_[][] = {
341 private static final String TOSTRING_RESULT_[] = {
349 * Test case for multi-threading problem reported by ticket#7880
351 public void TestMultiThread() {
352 final int numThreads = 20;
353 GetInstanceWorker[] workers = new GetInstanceWorker[numThreads];
354 VersionInfo[][] results = new VersionInfo[numThreads][255];
357 for (int i = 0; i < workers.length; i++) {
358 workers[i] = new GetInstanceWorker(i, results[i]);
362 for (int i = 0; i < workers.length; i++) {
366 // Wait for the completion
367 for (int i = 0; i < workers.length; i++) {
370 } catch (InterruptedException e) {
371 errln("A problem in thread execution. " + e.getMessage());
375 // Check if singleton for each
376 for (int i = 1; i < results.length; i++) {
377 for (int j = 0; j < results[0].length; j++) {
378 if (results[0][j] != results[i][j]) {
379 errln("Different instance at index " + j + " Thread#" + i);
385 private class GetInstanceWorker extends Thread {
386 private VersionInfo[] results;
388 GetInstanceWorker(int serialNumber, VersionInfo[] results) {
389 super("GetInstnaceWorker#" + serialNumber);
390 this.results = results;
394 for (int i = 0; i < results.length; i++) {
395 results[i] = VersionInfo.getInstance(i);