xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/Installer.java (revision 43a102b2d160fdbc9485ed1f9c40936b61a06131)
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 package org.openoffice.test.common;
22 
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.Method;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.text.MessageFormat;
29 import java.util.logging.Level;
30 
31 /**
32  * Install openoffice from installation package before running test
33  *
34  */
35 public class Installer implements Runnable {
36     private static Logger log = Logger.getLogger(Installer.class);
37     File downloadDir = Testspace.getFile("download");
38     File downloadUrl = Testspace.getFile("download/url");
39     File installDir = Testspace.getFile("install");
40     File installTempDir = Testspace.getFile("install_temp");
41 
42     @Override
43     public void run() {
44         String prop = System.getProperty("singleton");
45         if ("true".equalsIgnoreCase(prop) || "yes".equalsIgnoreCase(prop)) {
46             if (SystemUtil.findProcesses(".*org\\.openoffice\\.test\\.common\\.Installer.*").size() > 1) {
47                 throw new RuntimeException("Only allow one running test instance!");
48             }
49         }
50         if ((prop = System.getProperty("openoffice.pack")) != null) {
51             String onlyNewProp = System.getProperty("only.new");
52             File packFile = null;
53             if (FileUtil.isUrl(prop)) {
54                 log.log(Level.INFO, MessageFormat.format("Try to download {0}...", prop));
55                 String url = FileUtil.readFileAsString(downloadUrl);
56                 if (!prop.equals(url)) {
57                     FileUtil.deleteFile(downloadDir);
58                     downloadDir.mkdirs();
59                     packFile = FileUtil.download(prop, downloadDir);
60                     if (packFile == null)
61                         throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
62                     FileUtil.writeStringToFile(downloadUrl, prop);
63                 } else {
64                     boolean[] skipped = {false};
65                     packFile = FileUtil.download(prop, downloadDir, true, skipped);
66                     if (packFile == null)
67                         throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
68                     if (("true".equalsIgnoreCase(onlyNewProp) || "yes".equalsIgnoreCase(onlyNewProp)) && skipped[0])
69                         throw new RuntimeException(MessageFormat.format("{0} is old. Test is allowed only on new build.", prop));
70                 }
71             } else {
72                 packFile = new File(prop);
73                 if (!packFile.isFile())
74                     throw new RuntimeException(MessageFormat.format("{0} does not exists or is not a file!", prop));
75             }
76 
77             try {
78                 FileUtil.deleteFile(installDir);
79                 FileUtil.deleteFile(installTempDir);
80                 installTempDir.mkdirs();
81                 if (packFile.getName().endsWith(".gz")) {
82                     StringBuffer output = new StringBuffer();
83                     if (SystemUtil.exec(new String[]{"tar", "-zxpf", packFile.getAbsolutePath(), "-C", installTempDir.getAbsolutePath()}, output) != 0)
84                         throw new RuntimeException(MessageFormat.format("{0} can not be installed! Cause: {1}" , packFile, output));
85                 } else {
86                     if (!FileUtil.unzip(packFile, installTempDir))
87                         throw new RuntimeException(MessageFormat.format("{0} can not be installed!", packFile));
88                 }
89                 // On windows, if path is too long, openoffice can not be started.
90                 File[] files = installTempDir.listFiles();
91                 if (files != null && files.length == 1 && files[0].isDirectory()) {
92                     files[0].renameTo(installDir);
93                 }
94                 File sofficeBin = FileUtil.findFile(installDir, "soffice.bin", false);
95                 if (sofficeBin == null)
96                     throw new RuntimeException(MessageFormat.format("{0} is not a valid openoffice installation package!" , packFile));
97                 try {
98                     String openofficeHome = sofficeBin.getParentFile().getParentFile().getCanonicalPath();
99                     log.log(Level.INFO, MessageFormat.format("{0} is installed to {1}", prop, openofficeHome));
100                     System.setProperty("openoffice.home", openofficeHome);
101                 } catch (IOException e) {
102                     //ignore, never occurs
103                 }
104 
105             } finally {
106                 FileUtil.deleteFile(installTempDir);
107             }
108         }
109 
110          prop = System.getProperty("openoffice.home");
111          if (!FileUtil.fileExists(prop)) {
112              throw new RuntimeException("No OpenOffice is found!");
113          }
114 
115          String[] jars = {"juh.jar", "unoil.jar", "ridl.jar", "jurt.jar"};
116          for (String jar : jars) {
117              File file = FileUtil.findFile(prop, jar);
118              if (file != null)
119                  addToClassPath(file);
120          }
121 
122     }
123 
124     public static boolean addToClassPath(File file) {
125         try {
126             URL url = file.toURI().toURL();
127             URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
128             Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
129             method.setAccessible(true);
130             method.invoke(classLoader, new Object[] { url });
131             return true;
132         } catch (Throwable t) {
133             t.printStackTrace();
134         }
135 
136         return false;
137     }
138 
139     public static void main(String... args) {
140         new Installer().run();
141     }
142 }
143