1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "jvmaccess/javainfo.hxx"
25 
26 #include "osl/file.hxx"
27 #include "osl/thread.h"
28 #include "rtl/ustring.hxx"
29 
30 #include <stdio.h>
31 #include <vector>
32 
33 using namespace rtl;
34 using namespace std;
35 using namespace osl;
36 
37 using jvmaccess::JavaInfo;
38 
39 #define OUSTR( x )  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
40 
41 #define JAVA_VERSION "1.4.1_01"
42 
43 bool test_constructor1();
44 bool test_constructor2();
45 bool test_createAllInfo();
46 bool test_compareVersions();
47 bool test_createAllInfo();
48 bool test_createBestInfo();
49 bool test_isEqual();
50 bool test_getJarFilePath();
51 
52 void printInfo(const JavaInfo& info);
53 
main(int argc,char * argv[],char * envp[])54 int main( int argc, char * argv[], char * envp[])
55 {
56     const int arSize= 20;
57     bool arRet[arSize];
58     int i =0;
59     arRet[i++]= test_createAllInfo();
60     arRet[i++]= test_constructor1();
61     arRet[i++]= test_constructor2();
62 
63     arRet[i++]= test_compareVersions();
64     arRet[i++]= test_createBestInfo();
65     arRet[i++]= test_isEqual();
66     arRet[i++]= test_getJarFilePath();
67     bool error= true;
68     for(int j= 0; j < i; j++)
69         error &= arRet[j];
70 
71     if( error == false)
72         printf("Errors occurred\n");
73     return 0;
74 }
75 
test_constructor1()76 bool test_constructor1()
77 {
78     printf("\ntest JavaInfo::JavaInfo(const OUString& usJavaHome\n" \
79            "!Check output for correctness\n\n");
80     try{
81     JavaInfo info(OUSTR("file:///d:/java/j2sdk1.4.1_01"));
82 //    JavaInfo info(OUSTR("file:///local/jl/java/j2sdk1.4.0"));
83 //    JavaInfo info(OUSTR("file:///usr/local2/jl/java/j2sdk1.4.0"));
84 
85     printInfo(info);
86     }catch( JavaInfo::InitException& )
87     {
88         return false;
89     }
90 
91     return true;
92 }
93 
test_constructor2()94 bool test_constructor2()
95 {
96     printf("\ntest JavaInfo::JavaInfo(const OUString& usVersion,  int requirements)\n" \
97            "!Check output for correctness\n\n");
98     bool arRet[20];
99     int i= 0;
100 
101     JavaInfo a(OUSTR("file:///d:/java/j2sdk1.4.1_01"));
102 //    -----------------------------------------------------
103 //    JavaInfo a(OUSTR("file:///usr/local2/jl/java/j2sdk1.4.0"));
104 //------------------------------------------------------------
105 //    JavaInfo a(OUSTR("file:///local/jl/java/j2sdk1.4.0"));
106     try{
107         JavaInfo info= JavaInfo(OUString(), 0);
108         arRet[i++]= true;
109     }
110     catch( ...) {
111         arRet[i++]= false;
112     }
113     try{
114         // make sure it supports accessibility
115         JavaInfo info= JavaInfo(OUString(), JavaInfo::Accessibility);
116         arRet[i++]= info.supportsAccessibility();
117     }
118     catch( ...) {
119         arRet[i++]= false;
120     }
121 
122     try{
123         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::EqualVersion);
124         arRet[i++]= info.compareVersions(a) == 0;
125     }
126     catch( ...) {
127         arRet[i++]= false;
128     }
129     try{
130         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), 0);
131         arRet[i++]= info.compareVersions(a) == 0;
132     }
133     catch( ...) {
134         arRet[i++]= false;
135     }
136 
137     try{
138         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::EqualVersion | JavaInfo::GreaterVersion);
139         arRet[i++]= info.compareVersions(a) >= 0;
140     }
141     catch( ...) {
142         arRet[i++]= false;
143     }
144     try{
145         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::EqualVersion | JavaInfo::SmallerVersion);
146         arRet[i++]= info.compareVersions(a) <= 0;
147     }
148     catch( ...) {
149         arRet[i++]= false;
150     }
151     try{
152         JavaInfo info(OUString(OUSTR(JAVA_VERSION)),  JavaInfo::SmallerVersion);
153         arRet[i++]= info.compareVersions(a) < 0;
154     }
155     catch( ...) {
156         arRet[i++]= false;
157     }
158     try{
159         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::GreaterVersion);
160         arRet[i++]= info.compareVersions(a) > 0;
161     }
162     catch( ...) {
163         arRet[i++]= false;
164     }
165 
166     try{
167         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::EqualVersion | JavaInfo::SmallerVersion
168              | JavaInfo::Accessibility);
169         arRet[i++]= info.compareVersions(a) <= 0 && info.supportsAccessibility();
170     }
171     catch( ...) {
172         arRet[i++]= false;
173     }
174     try{
175         JavaInfo info(OUString(OUSTR(JAVA_VERSION)), JavaInfo::GreaterVersion | JavaInfo::Accessibility);
176         arRet[i++]= info.compareVersions(a) > 0 && info.supportsAccessibility();
177     }
178     catch( ...) {
179         arRet[i++]= false;
180     }
181 
182 
183     bool err= true;
184     for(int j= 0; j < i; j++)
185         err &= arRet[j];
186     if( err)
187         printf("ok\n");
188     else
189         printf("failed\n");
190     return err;
191 
192 }
193 
test_createAllInfo()194 bool test_createAllInfo()
195 {
196     printf("\ntest JavaInfo::createAllInfo\n" \
197            "! Compare output with values in registry, PATH, JAVA_HOME, LD_LIBRARY_PATH !\n\n");
198     vector<JavaInfo> vec;
199     JavaInfo::createAllInfo(&vec);
200     for(vector<JavaInfo>::size_type i= 0; i < vec.size(); i++)
201         printInfo(vec[i]);
202     return true;
203 }
204 
test_compareVersions()205 bool test_compareVersions()
206 {
207     bool ret= false;
208     printf("\ntest JavaInfo::compareVersions \n" \
209            "! Check output for correctness\n\n");
210 
211 
212     JavaInfo a(OUSTR("file:///C:/Program%20Files/JavaSoft/JRE/1.3.1"));
213     JavaInfo b(OUSTR("file:///C:/Program%20Files/JavaSoft/JRE/1.3.1_04"));
214     JavaInfo c(OUSTR("file:///C:/Program%20Files/Java/j2re1.4.0_03"));
215     JavaInfo d(OUSTR("file:///C:/Program%20Files/Java/j2re1.4.2_04"));
216 //     JavaInfo e(OUSTR("file:///d:/java/j2sdk1.4.0_01"));
217 //     JavaInfo f(OUSTR("file:///d:/java/j2sdk1.4.0_02"));
218 //     JavaInfo g(OUSTR("file:///d:/java/j2sdk1.4.1"));
219 //     JavaInfo h(OUSTR("file:///d:/java/j2sdk1.4.1_01"));
220 
221 //     JavaInfo a(OUSTR("file:///usr/local2/jl/java/j2re1_3_1_02"));
222 //     JavaInfo b(OUSTR("file:///usr/local2/jl/java/j2sdk1.4.0"));
223 //     JavaInfo c(OUSTR("file:///usr/local2/jl/java/j2sdk1.4.1"));
224 //     JavaInfo d(OUSTR("file:///usr/local2/jl/java/j2re1.4.1_01"));
225 
226 //     JavaInfo a(OUSTR("file:///local/jl/java/jre1.3.1"));
227 //     JavaInfo b(OUSTR("file:///local/jl/java/jdk1.3.1_04"));
228 //     JavaInfo c(OUSTR("file:///local/jl/java/j2sdk1.4.0"));
229 //     JavaInfo d(OUSTR("file:///local/jl/java/j2sdk1.4.1"));
230 //     JavaInfo e(OUSTR("file:///local/jl/java/j2re1.4.1_01"));
231 //     fprintf(stderr,"###ok1");
232 
233     if( (a.compareVersions(a) == 0
234          && a.compareVersions(b) < 0
235          && a.compareVersions(c) < 0
236 //         && a.compareVersions(d) < 0
237 //         && a.compareVersions(e) < 0
238 //         && a.compareVersions(h) < 0
239             )
240         &&
241         (a.compareVersions(a)== 0))
242 
243         ret= true;
244 
245     if(ret &&
246      (b.compareVersions(a) > 0
247          && b.compareVersions(b) == 0
248          && b.compareVersions(c) < 0
249          && b.compareVersions(d) < 0
250 //      && b.compareVersions(e) < 0
251 //         && b.compareVersions(h) < 0))
252          ))
253         ret= true;
254 
255     if(ret &&
256        (   d.compareVersions(a) > 0
257            && d.compareVersions(b) > 0
258            && d.compareVersions(c) > 0
259            && d.compareVersions(d) == 0
260 //           && d.compareVersions(e) < 0
261 //        && d.compareVersions(f) < 0
262 //        && d.compareVersions(g) < 0
263 //        && d.compareVersions(h) < 0))
264            ))
265         ret= true;
266 
267 //     if(ret
268 //        && e.compareVersions(a) > 0
269 //        && e.compareVersions(b) > 0
270 //        && e.compareVersions(c) > 0
271 //        && e.compareVersions(d) > 0
272 //        && e.compareVersions(e) == 0
273 //         )
274 
275 
276 //     if(ret &&
277 //        (f.compareVersions(a) > 0
278 //         && f.compareVersions(c) > 0
279 //         && f.compareVersions(d) > 0
280 //         && f.compareVersions(g) < 0
281 //         && f.compareVersions(h) < 0))
282 //         ret= true;
283 
284     if( ! ret)
285         printf("failed\n");
286     else
287         printf("ok\n");
288 
289     return ret;
290 }
291 
test_createBestInfo()292 bool test_createBestInfo()
293 {
294     printf("\ntest JavaInfo::createBestInfo\n" \
295            "! Check output for correctness\n\n");
296     JavaInfo info= JavaInfo::createBestInfo(false);
297     printInfo(info);
298     return true;
299 }
300 
test_isEqual()301 bool test_isEqual()
302 {
303     printf("\ntest JavaInfo::isEqual\n");
304     JavaInfo a(OUSTR(JAVA_VERSION)); // accessible
305     JavaInfo b(OUSTR(JAVA_VERSION));
306 
307     JavaInfo c(OUSTR("file:///c:/local/r/j2sdk1.4.0"));// not Accessible
308     JavaInfo d(OUSTR("file:///d:/java/copy_j2sdk1.4.0"));
309 //-------------------------------------------------------------------
310 //     JavaInfo a(OUSTR("file:///usr/local2/jl/java/j2re1_3_1_02"));
311 //     JavaInfo b(OUSTR("file:///usr/local2/jl/java/j2re1_3_1_02"));
312 
313 //     JavaInfo c(OUSTR("file:///usr/local2/jl/java/j2sdk1.4.1"));
314 //     JavaInfo d(OUSTR("file:///usr/local2/jl/java/copyj2sdk1.4.1"));
315 
316 //     JavaInfo a(OUSTR("file:///local/jl/java/jre1.3.1"));
317 //     JavaInfo b(OUSTR("file:///local/jl/java/jre1.3.1"));
318 //     JavaInfo c(OUSTR("file:///local/jl/java/j2sdk1.4.1"));
319 //     JavaInfo d(OUSTR("file:///local/jl/java/copyj2sdk1.4.1"));
320 
321     bool arRet[20];
322     int i= 0;
323     arRet[i++]= a.isEqual(b);
324     arRet[i++]= ! a.isEqual(c); //must fail
325     arRet[i++]= a.isEqual(d);
326 
327     bool err= true;
328     for(int j= 0; j < i; j++)
329         err &= arRet[j];
330     if( arRet == false)
331         printf("failed\n");
332     else
333         printf("ok\n");
334     return err;
335 }
336 
test_getJarFilePath()337 bool test_getJarFilePath()
338 {
339     printf("\ntest JavaInfo::getJarFilePath\n");
340     JavaInfo info(OUString(OUSTR("1.3.1")), JavaInfo::GreaterVersion | JavaInfo::EqualVersion );
341     OUString s= info.getJarFilePath(OUSTR("javaplugin.jar"));
342 
343     printf("check if this URL is correct:\n");
344     OString oPath= OUStringToOString(s, osl_getThreadTextEncoding());
345     printf("%s\n", oPath.getStr());
346     return true;
347 }
348 
printInfo(const JavaInfo & info)349 void printInfo(const JavaInfo& info)
350 {
351     OUString usVersion= info.getVersion();
352     OString sVersion= OUStringToOString( usVersion, osl_getThreadTextEncoding());
353     OUString usHome= info.getInstallationLocation();
354     OString sHome= OUStringToOString( usHome, osl_getThreadTextEncoding());
355     OUString usType= info.getType();
356     OString sType= OUStringToOString(usType, osl_getThreadTextEncoding());
357     OUString usLib= info.getRuntimeLibLocation();
358     OString sLib= OUStringToOString(usLib, osl_getThreadTextEncoding());
359     OUString usLibLocation= info.getLibLocations();
360     OString sLibLocation= OUStringToOString(usLibLocation, osl_getThreadTextEncoding());
361     sal_Bool baccess= info.supportsAccessibility();
362 
363     printf("%s %s\n",sType.getStr(), sVersion.getStr());
364     printf("\t%s \n",sHome.getStr());
365     printf("\t%s \n",sLib.getStr());
366     printf("\tLibDir: %s \n", sLibLocation.getStr());
367     printf("\t%s\n", baccess ? "accessible" : "not accessible");
368 }
369