PageRenderTime 95ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/nbbuild/test/unit/src/org/netbeans/nbbuild/TestBase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 332 lines | 235 code | 31 blank | 66 comment | 10 complexity | 4fe9c43e88f5b166788153a330509cfe MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2010 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.nbbuild;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.File;
  46. import java.io.FileDescriptor;
  47. import java.io.FileOutputStream;
  48. import java.io.InputStream;
  49. import java.io.PrintStream;
  50. import java.io.PrintWriter;
  51. import java.net.InetAddress;
  52. import java.net.URL;
  53. import java.security.Permission;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import java.util.List;
  57. import java.util.jar.Manifest;
  58. import junit.framework.AssertionFailedError;
  59. import org.netbeans.junit.NbTestCase;
  60. /**
  61. * Predefines commonly used utilities.
  62. */
  63. abstract class TestBase extends NbTestCase {
  64. protected TestBase(String name) {
  65. super(name);
  66. }
  67. protected @Override void setUp() throws Exception {
  68. clearWorkDir();
  69. }
  70. // XXX look for other copy-pasted utility methods, like createNewJarFile
  71. protected final String readFile(java.io.File f) throws java.io.IOException {
  72. int s = (int) f.length();
  73. byte[] data = new byte[s];
  74. assertEquals("Read all data", s, new java.io.FileInputStream(f).read(data));
  75. return new String(data);
  76. }
  77. protected final Manifest createManifest() {
  78. Manifest m = new Manifest();
  79. m.getMainAttributes().putValue(java.util.jar.Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
  80. return m;
  81. }
  82. protected final File extractString(String res) throws Exception {
  83. File f = File.createTempFile("res", ".xml", getWorkDir());
  84. FileOutputStream os = new FileOutputStream(f);
  85. InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
  86. for (;;) {
  87. int ch = is.read();
  88. if (ch == -1) {
  89. break;
  90. }
  91. os.write(ch);
  92. }
  93. os.close();
  94. return f;
  95. }
  96. protected final File extractResource(String res) throws Exception {
  97. File f = File.createTempFile("res", ".xml", getWorkDir());
  98. extractResource(f, res);
  99. return f;
  100. }
  101. protected final void extractResource(File f, String res) throws Exception {
  102. URL u = getClass().getResource(res);
  103. assertNotNull("Resource should be found " + res, u);
  104. FileOutputStream os = new FileOutputStream(f);
  105. InputStream is = u.openStream();
  106. for (;;) {
  107. int ch = is.read();
  108. if (ch == -1) {
  109. break;
  110. }
  111. os.write(ch);
  112. }
  113. os.close();
  114. }
  115. protected final void execute(String res, String... args) throws Exception {
  116. execute(extractResource(res), args);
  117. }
  118. private static ByteArrayOutputStream out, err;
  119. protected final String getStdOut() {
  120. return out.toString();
  121. }
  122. protected final String getStdErr() {
  123. return err.toString();
  124. }
  125. protected final void execute(File f, String[] args) throws Exception {
  126. // we need security manager to prevent System.exit
  127. if (!(System.getSecurityManager() instanceof MySecMan)) {
  128. out = new java.io.ByteArrayOutputStream();
  129. err = new java.io.ByteArrayOutputStream();
  130. System.setOut(new java.io.PrintStream(out));
  131. System.setErr(new java.io.PrintStream(err));
  132. System.setSecurityManager(new MySecMan());
  133. }
  134. MySecMan sec = (MySecMan) System.getSecurityManager();
  135. // Jesse claims that this is not the right way how the execution
  136. // of an ant script should be invoked:
  137. //
  138. // better IMHO to just run the task directly
  139. // (setProject() and similar, configure its bean properties, and call
  140. // execute()), or just make a new Project and initialize it.
  141. // ant.Main.main is not intended for embedded use. Then you could get rid
  142. // of the SecurityManager stuff, would be cleaner I think.
  143. //
  144. // If I had to write this once again, I would try to follow the
  145. // "just make a new Project and initialize it", but as this works
  146. // for me now, I leave it for the time when somebody really
  147. // needs that...
  148. List<String> arr = new ArrayList<String>();
  149. arr.add("-f");
  150. arr.add(f.toString());
  151. arr.addAll(Arrays.asList(args));
  152. out.reset();
  153. err.reset();
  154. try {
  155. sec.setActive(true);
  156. org.apache.tools.ant.Main.main(arr.toArray(new String[0]));
  157. } catch (MySecExc ex) {
  158. assertNotNull("The only one to throw security exception is MySecMan and should set exitCode", sec.exitCode);
  159. ExecutionError.assertExitCode(
  160. "Execution has to finish without problems",
  161. sec.exitCode.intValue());
  162. } finally {
  163. sec.setActive(false);
  164. }
  165. }
  166. protected static final class ExecutionError extends AssertionFailedError {
  167. public final int exitCode;
  168. public ExecutionError(String msg, int e) {
  169. super(msg);
  170. this.exitCode = e;
  171. }
  172. private static void assertExitCode(String msg, int e) {
  173. if (e != 0) {
  174. throw new ExecutionError(
  175. msg + " was: " + e + "\nOutput: " + out.toString()
  176. + "\nError: " + err.toString(),
  177. e);
  178. }
  179. }
  180. }
  181. private static class MySecExc extends SecurityException {
  182. @Override
  183. public void printStackTrace() {
  184. }
  185. @Override
  186. public void printStackTrace(PrintStream ps) {
  187. }
  188. @Override
  189. public void printStackTrace(PrintWriter ps) {
  190. }
  191. }
  192. private static class MySecMan extends SecurityManager {
  193. public Integer exitCode;
  194. private boolean active;
  195. @Override
  196. public void checkExit(int status) {
  197. if (active) {
  198. exitCode = new Integer(status);
  199. throw new MySecExc();
  200. }
  201. }
  202. @Override
  203. public void checkPermission(Permission perm, Object context) {
  204. }
  205. @Override
  206. public void checkPermission(Permission perm) {
  207. /*
  208. if (perm instanceof RuntimePermission) {
  209. if (perm.getName ().equals ("setIO")) {
  210. throw new MySecExc ();
  211. }
  212. }
  213. */
  214. }
  215. @Override
  216. public void checkMulticast(InetAddress maddr) {
  217. }
  218. @Override
  219. public void checkAccess(ThreadGroup g) {
  220. }
  221. @Override
  222. public void checkWrite(String file) {
  223. }
  224. @Override
  225. public void checkLink(String lib) {
  226. }
  227. @Override
  228. public void checkExec(String cmd) {
  229. }
  230. @Override
  231. public void checkDelete(String file) {
  232. }
  233. @Override
  234. public void checkPackageAccess(String pkg) {
  235. }
  236. @Override
  237. public void checkPackageDefinition(String pkg) {
  238. }
  239. @Override
  240. public void checkPropertyAccess(String key) {
  241. }
  242. @Override
  243. public void checkRead(String file) {
  244. }
  245. @Override
  246. public void checkSecurityAccess(String target) {
  247. }
  248. @Override
  249. public void checkWrite(FileDescriptor fd) {
  250. }
  251. @Override
  252. public void checkListen(int port) {
  253. }
  254. @Override
  255. public void checkRead(FileDescriptor fd) {
  256. }
  257. @SuppressWarnings("deprecation")
  258. @Override
  259. public void checkMulticast(InetAddress maddr, byte ttl) {
  260. }
  261. @Override
  262. public void checkAccess(Thread t) {
  263. }
  264. @Override
  265. public void checkConnect(String host, int port, Object context) {
  266. }
  267. @Override
  268. public void checkRead(String file, Object context) {
  269. }
  270. @Override
  271. public void checkConnect(String host, int port) {
  272. }
  273. @Override
  274. public void checkAccept(String host, int port) {
  275. }
  276. @Override
  277. public void checkMemberAccess(Class<?> clazz, int which) {
  278. }
  279. @Override
  280. public void checkSystemClipboardAccess() {
  281. }
  282. @Override
  283. public void checkSetFactory() {
  284. }
  285. @Override
  286. public void checkCreateClassLoader() {
  287. }
  288. @Override
  289. public void checkAwtEventQueueAccess() {
  290. }
  291. @Override
  292. public void checkPrintJobAccess() {
  293. }
  294. @Override
  295. public void checkPropertiesAccess() {
  296. }
  297. void setActive(boolean b) {
  298. active = b;
  299. }
  300. } // end of MySecMan
  301. }