PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/websvc.manager/test/unit/src/org/netbeans/modules/websvc/manager/test/SetupUtil.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 265 lines | 183 code | 45 blank | 37 comment | 16 complexity | 1ba4d292fcb19f4acce4e7ec1c720bd3 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-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. * Contributor(s):
  28. *
  29. * Portions Copyrighted 2007 Sun Microsystems, Inc.
  30. */
  31. package org.netbeans.modules.websvc.manager.test;
  32. import java.io.File;
  33. import java.io.FileFilter;
  34. import java.io.FileInputStream;
  35. import java.io.FileOutputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.net.URL;
  39. import java.nio.channels.FileChannel;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. import java.util.Properties;
  43. import java.util.jar.Manifest;
  44. import org.netbeans.junit.MockServices;
  45. import org.netbeans.modules.websvc.manager.WebServiceManager;
  46. import org.openide.DialogDisplayer;
  47. import org.openide.filesystems.FileSystem;
  48. import org.openide.filesystems.FileUtil;
  49. import org.openide.filesystems.LocalFileSystem;
  50. import org.openide.filesystems.MultiFileSystem;
  51. import org.openide.filesystems.Repository;
  52. import org.openide.filesystems.XMLFileSystem;
  53. import org.openide.modules.InstalledFileLocator;
  54. import org.openide.util.Lookup;
  55. import org.openide.util.LookupEvent;
  56. import org.openide.util.LookupListener;
  57. import org.openide.util.NbCollections;
  58. /**
  59. *
  60. * @author quynguyen
  61. */
  62. public class SetupUtil {
  63. private static final String WORKDIR_SPACES = "user directory";
  64. private static final String WORKDIR = "userdirectory";
  65. private static final String TEST_WSDL = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
  66. private static final String TEST_CATALOG = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/catalog.xml";
  67. private static final String CATALOG_FILE = "uszip-asmx-catalog/catalog.xml";
  68. private static final String WSDL_FILE = "uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
  69. private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
  70. private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
  71. public static SetupData commonSetUp(File workingDir) throws Exception {
  72. SetupData data = new SetupData();
  73. File userDir = new File(workingDir.getParentFile(),
  74. System.getProperty("os.name").startsWith("Windows") ? WORKDIR_SPACES : WORKDIR);
  75. System.getProperties().setProperty("netbeans.user", userDir.getAbsolutePath());
  76. File websvcHome = new File(WebServiceManager.WEBSVC_HOME);
  77. data.setWebsvcHome(websvcHome);
  78. File websvcUserDir = new File(WebServiceManager.WEBSVC_HOME);
  79. websvcUserDir.mkdirs();
  80. File wsdlFile = new File(websvcUserDir, WSDL_FILE);
  81. File catalogFile = new File(websvcUserDir, CATALOG_FILE);
  82. retrieveURL(wsdlFile, SetupUtil.class.getResource(TEST_WSDL));
  83. retrieveURL(catalogFile, SetupUtil.class.getResource(TEST_CATALOG));
  84. copy(wsdlFile, workingDir);
  85. data.setLocalWsdlFile(wsdlFile);
  86. data.setLocalCatalogFile(catalogFile);
  87. data.setLocalOriginalWsdl(new File(workingDir, wsdlFile.getName()));
  88. MainFS fs = new MainFS();
  89. fs.setConfigRootDir(websvcHome.getParentFile());
  90. TestRepository.defaultFileSystem = fs;
  91. MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
  92. InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
  93. locator.setUserConfigRoot(websvcHome.getParentFile());
  94. File targetBuildProperties = new File(websvcUserDir.getParentFile().getParentFile(), "build.properties");
  95. generatePropertiesFile(targetBuildProperties);
  96. return data;
  97. }
  98. public static void commonTearDown() throws Exception {
  99. DialogDisplayer dd = DialogDisplayer.getDefault();
  100. if (dd instanceof DialogDisplayerNotifier) {
  101. ((DialogDisplayerNotifier)dd).removeAllListeners();
  102. }
  103. MockServices.setServices();
  104. }
  105. public static void copy(File src, File target) throws Exception {
  106. if (src.isFile()) {
  107. File targetFile = new File(target, src.getName());
  108. FileInputStream is = new FileInputStream(src);
  109. FileOutputStream os = new FileOutputStream(targetFile);
  110. FileChannel inputChannel = is.getChannel();
  111. FileChannel outputChannel = os.getChannel();
  112. inputChannel.transferTo(0, inputChannel.size(), outputChannel);
  113. inputChannel.close();
  114. outputChannel.close();
  115. }else {
  116. File newDir = new File(target, src.getName());
  117. newDir.mkdirs();
  118. File[] dirFiles = src.listFiles();
  119. if (dirFiles != null) {
  120. for (int i = 0; i < dirFiles.length; i++) {
  121. copy(dirFiles[i], newDir);
  122. }
  123. }
  124. }
  125. }
  126. public static void retrieveURL(File targetFile, URL url) throws IOException {
  127. targetFile.getParentFile().mkdirs();
  128. FileOutputStream fos = new FileOutputStream(targetFile);
  129. byte[] readBuffer = new byte[1024];
  130. InputStream is = url.openStream();
  131. int bytesRead = 0;
  132. while ( (bytesRead = is.read(readBuffer, 0, 1024)) > 0) {
  133. fos.write(readBuffer, 0, bytesRead);
  134. }
  135. fos.flush();
  136. fos.close();
  137. is.close();
  138. }
  139. private static void generatePropertiesFile(File target) throws IOException {
  140. String separator = System.getProperty("path.separator");
  141. File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
  142. File jaxWsBase = apiBase.getParentFile();
  143. FileFilter jarFilter = new FileFilter() {
  144. public boolean accept(File pathname) {
  145. return pathname.isFile() && pathname.getName().endsWith(".jar");
  146. }
  147. };
  148. File[] apiJars = apiBase.listFiles(jarFilter);
  149. File[] implJars = jaxWsBase.listFiles(jarFilter);
  150. Properties result = new Properties();
  151. StringBuffer classpath = new StringBuffer();
  152. for (int i = 0; i < apiJars.length; i++) {
  153. String pathElement = apiJars[i].getAbsolutePath() + separator;
  154. classpath.append(pathElement);
  155. }
  156. for (int i = 0; i < implJars.length; i++) {
  157. classpath.append(implJars[i].getAbsolutePath());
  158. if (i != implJars.length - 1) {
  159. classpath.append(separator);
  160. }
  161. }
  162. result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
  163. FileOutputStream fos = new FileOutputStream(target);
  164. result.store(fos, "build.properties file");
  165. }
  166. public static final class TestRepository extends Repository {
  167. static FileSystem defaultFileSystem = null;
  168. public TestRepository() {
  169. super(defaultFileSystem);
  170. }
  171. }
  172. // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
  173. // loaded into the default filesystem (since core/startup is in the classpath
  174. // and registers a default Repository that we do not want)
  175. public static final class MainFS extends MultiFileSystem implements LookupListener {
  176. private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
  177. private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
  178. private final XMLFileSystem layers = new XMLFileSystem();
  179. private final LocalFileSystem configRoot = new LocalFileSystem();
  180. public void setConfigRootDir(File root) throws Exception {
  181. configRoot.setRootDirectory(root);
  182. }
  183. public MainFS() {
  184. ALL.addLookupListener(this);
  185. List<URL> layerUrls = new ArrayList<URL>();
  186. ClassLoader l = Thread.currentThread().getContextClassLoader();
  187. try {
  188. for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
  189. InputStream is = manifest.openStream();
  190. try {
  191. Manifest mani = new Manifest(is);
  192. String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
  193. if (layerLoc != null) {
  194. URL layer = l.getResource(layerLoc);
  195. if (layer != null) {
  196. layerUrls.add(layer);
  197. }
  198. }
  199. } finally {
  200. is.close();
  201. }
  202. }
  203. layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
  204. } catch (Exception x) {
  205. }
  206. resultChanged(null); // run after add listener - see PN1 in #26338
  207. }
  208. private FileSystem[] computeDelegates() {
  209. List<FileSystem> arr = new ArrayList<FileSystem>();
  210. arr.add(MEMORY);
  211. arr.add(layers);
  212. arr.add(configRoot);
  213. arr.addAll(ALL.allInstances());
  214. return arr.toArray(new FileSystem[0]);
  215. }
  216. public void resultChanged(LookupEvent ev) {
  217. setDelegates(computeDelegates());
  218. }
  219. }
  220. }