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