Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.netbeans.modules.autoupdate.services; |
42 |
|
43 |
import java.io.File; |
44 |
import java.io.IOException; |
45 |
import java.io.InputStream; |
46 |
import java.lang.reflect.Method; |
47 |
import java.util.HashMap; |
48 |
import java.util.Map; |
49 |
import org.netbeans.api.autoupdate.UpdateUnit; |
50 |
import org.netbeans.spi.autoupdate.UpdateItem; |
51 |
import org.netbeans.spi.autoupdate.UpdateProvider; |
52 |
|
53 |
/** |
54 |
* |
55 |
* @author Dmitry Lipin |
56 |
*/ |
57 |
public class ExecutablePermissionsTest extends NbmAdvancedTestCase { |
58 |
|
59 |
private UpdateProvider p = null; |
60 |
private String testModuleVersion = "1.0"; |
61 |
private String testModuleName = "org.yourorghere.executable.permissions"; |
62 |
|
63 |
public ExecutablePermissionsTest(String name) { |
64 |
super(name); |
65 |
} |
66 |
|
67 |
@Override |
68 |
protected void setUp() throws IOException, Exception { |
69 |
super.setUp(); |
70 |
} |
71 |
|
72 |
@Override |
73 |
public boolean canRun() { |
74 |
return super.canRun() && !System.getProperty("os.name").startsWith("Windows"); |
75 |
} |
76 |
|
77 |
private String generateExecutablePermissionsModuleElement() { |
78 |
String res = "\n<module codenamebase=\"" + testModuleName + "\" " + |
79 |
"homepage=\"http://www.netbeans.org/\" distribution=\"nbresloc:/org/netbeans/api/autoupdate/data/org-yourorghere-executable-permissions.nbm\" " + |
80 |
"license=\"standard-nbm-license.txt\" downloadsize=\"5122\" " + |
81 |
"needsrestart=\"false\" moduleauthor=\"\" " + |
82 |
"eager=\"false\" " + |
83 |
"releasedate=\"2006/02/23\">\n"; |
84 |
res += "<manifest OpenIDE-Module=\"" + testModuleName + "\" " + |
85 |
"OpenIDE-Module-Name=\"" + testModuleName + "\" " + |
86 |
"AutoUpdate-Show-In-Client=\"true\" " + |
87 |
"OpenIDE-Module-Specification-Version=\"" + testModuleVersion + "\"/>\n"; |
88 |
res += "</module>"; |
89 |
return res; |
90 |
} |
91 |
|
92 |
public void testExecutablePermissionsModule() throws IOException { |
93 |
String os = !org.openide.util.Utilities.isUnix() ? "Windows" : "Unix"; |
94 |
String catalog = generateCatalog(generateExecutablePermissionsModuleElement()); |
95 |
|
96 |
p = createUpdateProvider(catalog); |
97 |
p.refresh(true); |
98 |
|
99 |
Map<String, UpdateUnit> unitImpls = new HashMap<String, UpdateUnit>(); |
100 |
Map<String, UpdateItem> updates = p.getUpdateItems(); |
101 |
assertNotNull("Some modules are installed.", updates); |
102 |
assertFalse("Some modules are installed.", updates.isEmpty()); |
103 |
assertTrue(testModuleName + " found in parsed items.", updates.keySet().contains(testModuleName + "_" + testModuleVersion)); |
104 |
|
105 |
Map<String, UpdateUnit> newImpls = UpdateUnitFactory.getDefault().appendUpdateItems(unitImpls, p); |
106 |
assertNotNull("Some units found.", newImpls); |
107 |
assertFalse("Some units found.", newImpls.isEmpty()); |
108 |
|
109 |
UpdateUnit u1 = newImpls.get(testModuleName); |
110 |
installUpdateUnit(u1); |
111 |
File f = new File(userDir, "bin/start.sh"); |
112 |
assertTrue("File " + f + " should exist after module installation", f.exists()); |
113 |
if (System.getProperty("java.version").startsWith("1.5")) { |
114 |
File ls = new File("/bin/ls"); |
115 |
if (!ls.isFile()) { |
116 |
ls = new File("/usr/bin/ls"); |
117 |
} |
118 |
if (ls.isFile()) { |
119 |
String output = readCommandOutput(ls.getAbsolutePath(), "-la", f.getAbsolutePath()).trim(); |
120 |
int index = output.indexOf(" "); |
121 |
assertFalse("Can`t read permissions from output:\n" + output, index == -1); |
122 |
String permissions = output.substring(0, index); |
123 |
assertTrue("File " + f + " does not have executable permissions after installation, actual perms : " + permissions, |
124 |
permissions.matches(".*x.*x.*x.*")); |
125 |
} |
126 |
} else { |
127 |
Method canExecuteMethod = null; |
128 |
try { |
129 |
canExecuteMethod = File.class.getMethod("canExecute", new Class[]{}); |
130 |
} catch (Exception e) { |
131 |
assertTrue("java.io.File.canExecute method is not accessible", false); |
132 |
} |
133 |
boolean canExecute = false; |
134 |
try { |
135 |
canExecute = (Boolean) canExecuteMethod.invoke(f); |
136 |
} catch (Exception e) { |
137 |
assertTrue("File " + f + " is not executable after module installation", canExecute); |
138 |
e.printStackTrace(); |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
private String readCommandOutput(String... command) { |
144 |
ProcessBuilder builder = new ProcessBuilder(command); |
145 |
boolean doRun = true; |
146 |
StringBuilder sb = new StringBuilder(); |
147 |
byte[] bytes = new byte[8192]; |
148 |
int c = 0; |
149 |
|
150 |
try { |
151 |
Process process = builder.start(); |
152 |
while (doRun) { |
153 |
try { |
154 |
Thread.sleep(20); |
155 |
} catch (InterruptedException e) { |
156 |
} |
157 |
try { |
158 |
process.exitValue(); |
159 |
doRun = false; |
160 |
} catch (IllegalThreadStateException e) { |
161 |
; // do nothing - the process is still running |
162 |
} |
163 |
InputStream is = process.getInputStream(); |
164 |
while ((c = is.read(bytes)) != -1) { |
165 |
sb.append(new String(bytes, 0, c)); |
166 |
} |
167 |
} |
168 |
return sb.toString(); |
169 |
} catch (IOException e) { |
170 |
e.printStackTrace(); |
171 |
return new String(); |
172 |
} |
173 |
} |
174 |
} |