blob: 084cd06df4b94bc2d2021adc3b582238d9b05af0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6545058 6611182
27 * @summary validate and test -version, -fullversion, and internal
28 * @compile VersionCheck.java
29 * @run main VersionCheck
30 */
31
32import java.lang.*;
33import java.io.File;
34import java.io.BufferedReader;
35import java.io.InputStream;
36import java.io.InputStreamReader;
37import java.io.Reader;
38import java.util.Map;
39import java.util.ArrayList;
40import java.util.List;
41import java.util.StringTokenizer;
42
43public class VersionCheck {
44
45 private static String javaBin;
46
47 // A known set of programs we know for sure will behave correctly.
48 private static String[] programs = new String[]{
49 "appletviewer",
50 "extcheck",
51 "idlj",
52 "jar",
53 "jarsigner",
54 "javac",
55 "javadoc",
56 "javah",
57 "javap",
58 "jconsole",
59 "jdb",
60 "jhat",
61 "jinfo",
62 "jmap",
63 "jps",
64 "jstack",
65 "jstat",
66 "jstatd",
67 "keytool",
68 "native2ascii",
69 "orbd",
70 "pack200",
71 "policytool",
72 "rmic",
73 "rmid",
74 "rmiregistry",
75 "schemagen",
76 "serialver",
77 "servertool",
78 "tnameserv",
79 "wsgen",
80 "wsimport",
81 "xjc"
82 };
83
84 // expected reference strings
85 static String refVersion;
86 static String refFullVersion;
87
88 private static List<String> getProcessStreamAsList(boolean javaDebug,
89 String... argv) {
90 List<String> out = new ArrayList<String>();
91 List<String> javaCmds = new ArrayList<String>();
92
93 String prog = javaBin + File.separator + argv[0];
94 if (System.getProperty("os.name").startsWith("Windows")) {
95 prog = prog.concat(".exe");
96 }
97 javaCmds.add(prog);
98 for (int i = 1; i < argv.length; i++) {
99 javaCmds.add(argv[i]);
100 }
101
102 ProcessBuilder pb = new ProcessBuilder(javaCmds);
103 Map<String, String> env = pb.environment();
104 if (javaDebug) {
105 env.put("_JAVA_LAUNCHER_DEBUG", "true");
106 }
107 try {
108 Process p = pb.start();
109 BufferedReader r = (javaDebug) ?
110 new BufferedReader(new InputStreamReader(p.getInputStream())) :
111 new BufferedReader(new InputStreamReader(p.getErrorStream())) ;
112
113 String s = r.readLine();
114 while (s != null) {
115 out.add(s.trim());
116 s = r.readLine();
117 }
118 p.waitFor();
119 p.destroy();
120 } catch (Exception ex) {
121 ex.printStackTrace();
122 throw new RuntimeException(ex.getMessage());
123 }
124 return out;
125 }
126
127 static String getVersion(String... argv) {
128 List<String> alist = getProcessStreamAsList(false, argv);
129 if (alist.size() == 0) {
130 throw new AssertionError("unexpected process returned null");
131 }
132 StringBuilder out = new StringBuilder();
133 // remove the HotSpot line
134 for (String x : alist) {
135 if (!x.contains("HotSpot")) {
136 out = out.append(x + "\n");
137 }
138 }
139 return out.toString();
140 }
141
142 static boolean compareVersionStrings() {
143 int failcount = 0;
144 for (String x : programs) {
145 String testStr;
146
147 testStr = getVersion(x, "-J-version");
148 if (refVersion.compareTo(testStr) != 0) {
149 failcount++;
150 System.out.println("Error: " + x +
151 " fails -J-version comparison");
152 System.out.println("Expected:");
153 System.out.print(refVersion);
154 System.out.println("Actual:");
155 System.out.print(testStr);
156 }
157
158 testStr = getVersion(x, "-J-fullversion");
159 if (refFullVersion.compareTo(testStr) != 0) {
160 failcount++;
161 System.out.println("Error: " + x +
162 " fails -J-fullversion comparison");
163 System.out.println("Expected:");
164 System.out.print(refFullVersion);
165 System.out.println("Actual:");
166 System.out.print(testStr);
167 }
168 }
169 System.out.println("Version Test: " + failcount);
170 return failcount == 0;
171 }
172
173 static boolean compareInternalStrings() {
174 int failcount = 0;
175 String bStr = refVersion.substring(refVersion.lastIndexOf("build") +
176 "build".length() + 1,
177 refVersion.lastIndexOf(")"));
178
179 String[] vStr = bStr.split("\\.|-|_");
180 String jdkMajor = vStr[0];
181 String jdkMinor = vStr[1];
182 String jdkMicro = vStr[2];
183 String jdkBuild = vStr[vStr.length - 1];
184
185 String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
186 String expectedFullVersion = "fullversion:" + bStr;
187
188 List<String> alist = getProcessStreamAsList(true, "java", "-version");
189
190 if (!alist.contains(expectedDotVersion)) {
191 System.out.println("Error: could not find " + expectedDotVersion);
192 failcount++;
193 }
194
195 if (!alist.contains(expectedFullVersion)) {
196 System.out.println("Error: could not find " + expectedFullVersion);
197 failcount++;
198 }
199 System.out.println("Internal Strings Test: " + failcount);
200 return failcount == 0;
201 }
202
203 // Initialize
204 static void init() {
205 String javaHome = System.getProperty("java.home");
206 if (javaHome.endsWith("jre")) {
207 javaHome = new File(javaHome).getParent();
208 }
209 javaBin = javaHome + File.separator + "bin";
210 refVersion = getVersion("java", "-version");
211 refFullVersion = getVersion("java", "-fullversion");
212 }
213
214 public static void main(String[] args) {
215 init();
216 if (compareVersionStrings() && compareInternalStrings()) {
217 System.out.println("All Version string comparisons: PASS");
218 } else {
219 throw new AssertionError("Some tests failed");
220 }
221 }
222}