PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.remote/test/unit/src/org/netbeans/modules/cnd/remote/mapper/MappingsTestCase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 328 lines | 230 code | 37 blank | 61 comment | 63 complexity | 7e2a16b6b1181862827385aef71ae569 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 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.cnd.remote.mapper;
  43. import java.io.StringReader;
  44. import java.util.Map;
  45. import java.util.SortedSet;
  46. import java.util.TreeMap;
  47. import java.util.TreeSet;
  48. import junit.framework.Test;
  49. import org.netbeans.modules.cnd.remote.test.RemoteDevelopmentTest;
  50. import org.netbeans.modules.cnd.remote.test.RemoteTestBase;
  51. import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
  52. import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
  53. import org.netbeans.modules.nativeexecution.api.util.HostInfoUtils;
  54. import org.netbeans.modules.nativeexecution.test.ForAllEnvironments;
  55. import org.netbeans.modules.nativeexecution.test.NativeExecutionTestSupport;
  56. import org.netbeans.modules.nativeexecution.test.RcFile;
  57. /**
  58. *
  59. * @author Sergey Grinev
  60. */
  61. public class MappingsTestCase extends RemoteTestBase {
  62. @ForAllEnvironments
  63. public void testAnalyzer() throws Exception {
  64. ExecutionEnvironment execEnv = getTestExecutionEnvironment();
  65. HostMappingsAnalyzer ham = new HostMappingsAnalyzer(execEnv); //sg155630@elif
  66. final Map<String, String> mappings = ham.getMappings();
  67. assert mappings != null;
  68. String sortedMappings = toSortedString(mappings);
  69. String referenceMappings = getReferenceMappingSortedString();
  70. if (referenceMappings != null && !referenceMappings.isEmpty()) {
  71. System.err.printf("\nMappings for %s differ.\nReference mappins are:%s\nActual mappings are:%s\n",
  72. execEnv, referenceMappings, sortedMappings);
  73. if (!referenceMappings.equals(sortedMappings)) {
  74. assertTrue("Mappings differ", false);
  75. }
  76. } else {
  77. System.err.printf("Mappings for %s:%s\n", execEnv, sortedMappings);
  78. }
  79. }
  80. private String getReferenceMappingSortedString() throws Exception {
  81. RcFile rcFile = NativeExecutionTestSupport.getRcFile();
  82. String mspec = NativeExecutionTestSupport.getMspec(getTestExecutionEnvironment());
  83. String section = "remote." + mspec + ".pathMappings";
  84. SortedSet<String> sortedKeySet = new TreeSet<String>(rcFile.getKeys(section));
  85. if (sortedKeySet.isEmpty()) {
  86. return null;
  87. }
  88. StringBuilder sb = new StringBuilder();
  89. for (String key : sortedKeySet) {
  90. String value = rcFile.get(section, key);
  91. key = expandMacro(key);
  92. value = expandMacro(value);
  93. sb.append("\n\t").append(key).append('=').append(value);
  94. }
  95. return sb.toString();
  96. }
  97. private String expandMacro(String text) throws Exception {
  98. String localHostName = HostInfoUtils.getHostInfo(ExecutionEnvironmentFactory.getLocal()).getHostname();
  99. return text.replace("${LOCALHOST}", localHostName);
  100. }
  101. private String toSortedString(Map<String, String> mappings) {
  102. TreeMap<String, String> sortedMap = new TreeMap<String, String>();
  103. sortedMap.putAll(mappings);
  104. StringBuilder sb = new StringBuilder();
  105. for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
  106. sb.append("\n\t").append(entry.getKey()).append('=').append(entry.getValue());
  107. }
  108. return sb.toString();
  109. }
  110. public void testHostMappingProviderWindows_English() throws Exception {
  111. StringBuilder sb = new StringBuilder();
  112. sb.append("New connections will not be remembered.\n");
  113. sb.append("\n");
  114. sb.append("\n");
  115. sb.append("Status Local Remote Network\n");
  116. sb.append("\n");
  117. sb.append("-------------------------------------------------------------------------------\n");
  118. sb.append("OK P: \\\\serverOne\\pub Microsoft Windows Network\n");
  119. sb.append("Disconnected Y: \\\\sErvEr_22_\\long name Microsoft Windows Network\n");
  120. sb.append("OK Z: \\\\name.domen.domen2.zone\\sg155630 Microsoft Windows Network\n");
  121. sb.append("The command completed successfully.\n");
  122. Map<String, String> map;
  123. map = HostMappingProviderWindows.parseNetUseOutput("serverOne", new StringReader(sb.toString()));
  124. assert map != null && map.size() == 1 && "p:".equals(map.get("pub"));
  125. map = HostMappingProviderWindows.parseNetUseOutput("sErvEr_22_", new StringReader(sb.toString()));
  126. assert map != null && map.size() == 1 && "y:".equals(map.get("long name"));
  127. map = HostMappingProviderWindows.parseNetUseOutput("name.domen.domen2.zone", new StringReader(sb.toString()));
  128. assert map != null && map.size() == 1 && "z:".equals(map.get("sg155630"));
  129. }
  130. public void testHostMappingProviderWindows_English2() throws Exception {
  131. // Bug #214622
  132. StringBuilder sb = new StringBuilder();
  133. sb.append("New connections will be remembered.\n");
  134. sb.append("\n");
  135. sb.append("\n");
  136. sb.append("Status Local Remote Network\n");
  137. sb.append("\n");
  138. sb.append("-------------------------------------------------------------------------------\n");
  139. sb.append("Disconnected K: \\\\amkar.russia.sun.com\\home\n");
  140. sb.append(" Microsoft Windows Network\n");
  141. sb.append(" X: \\\\vboxsvr\\cnd-main VirtualBox Shared Folders\n");
  142. sb.append(" Z: \\\\vboxsvr\\exchange VirtualBox Shared Folders\n");
  143. sb.append("The command completed successfully.\n");
  144. Map<String, String> map;
  145. map = HostMappingProviderWindows.parseNetUseOutput("amkar.russia.sun.com", new StringReader(sb.toString()));
  146. assert map != null && map.size() == 1 && "k:".equals(map.get("home"));
  147. map = HostMappingProviderWindows.parseNetUseOutput("vboxsvr", new StringReader(sb.toString()));
  148. assert map != null && map.size() == 2;
  149. }
  150. public void testHostMappingProviderWindows_English3() throws Exception {
  151. // Bug #213365
  152. StringBuilder sb = new StringBuilder();
  153. sb.append("New connections will be remembered.\n");
  154. sb.append("\n");
  155. sb.append("\n");
  156. sb.append("Status Local Remote Network\n");
  157. sb.append("\n");
  158. sb.append("-------------------------------------------------------------------------------\n");
  159. sb.append("Disconnected K: \\\\amkar.rus.sun.com\\home\n");
  160. sb.append(" Microsoft Windows Network\n");
  161. sb.append(" X: \\\\vboxsvr\\cnd-main VirtualBox Shared Folders\n");
  162. sb.append(" Z: \\\\vboxsvr\\exchange VirtualBox Shared Folders\n");
  163. sb.append("The command completed successfully.\n");
  164. Map<String, String> map;
  165. map = HostMappingProviderWindows.parseNetUseOutput("amkar.rus.sun.com", new StringReader(sb.toString()));
  166. assert map != null && map.size() == 1 && "k:".equals(map.get("home"));
  167. map = HostMappingProviderWindows.parseNetUseOutput("vboxsvr", new StringReader(sb.toString()));
  168. assert map != null && map.size() == 2;
  169. }
  170. public void testHostMappingProviderWindows_Russian() throws Exception {
  171. StringBuilder sb = new StringBuilder();
  172. sb.append("Новые подключения не будут запомнены.\n");
  173. sb.append("\n");
  174. sb.append("\n");
  175. sb.append("Состояние Локальный Удаленный Сеть\n");
  176. sb.append("\n");
  177. sb.append("-------------------------------------------------------------------------------\n");
  178. sb.append("OK P: \\\\serverOne\\pub Microsoft Windows Network\n");
  179. sb.append("Нет доступа Y: \\\\sErvEr_22_\\long name Microsoft Windows Network\n");
  180. sb.append("OK Z: \\\\name.domen.domen2.zone\\sg155630 Microsoft Windows Network\n");
  181. sb.append("Команда выполнена успешно.\n");
  182. Map<String, String> map;
  183. map = HostMappingProviderWindows.parseNetUseOutput("serverOne", new StringReader(sb.toString()));
  184. assert map != null && map.size() == 1 && "p:".equals(map.get("pub"));
  185. map = HostMappingProviderWindows.parseNetUseOutput("sErvEr_22_", new StringReader(sb.toString()));
  186. assert map != null && map.size() == 1 && "y:".equals(map.get("long name"));
  187. map = HostMappingProviderWindows.parseNetUseOutput("name.domen.domen2.zone", new StringReader(sb.toString()));
  188. assert map != null && map.size() == 1 && "z:".equals(map.get("sg155630"));
  189. }
  190. public void testHostMappingProviderWindows_German() throws Exception {
  191. StringBuilder sb = new StringBuilder();
  192. sb.append("Neue Verbindungen werden gespeichert.\n");
  193. sb.append("\n");
  194. sb.append("\n");
  195. sb.append("Status Lokal Remote Netzwerk\n");
  196. sb.append("\n");
  197. sb.append("-------------------------------------------------------------------------------\n");
  198. sb.append("OK P: \\\\serverOne\\pub Microsoft Windows-Netzwerk\n");
  199. sb.append("Nicht verfügbar Y: \\\\sErvEr_22_\\long name Microsoft Windows-Netzwerk\n");
  200. sb.append("OK Z: \\\\name.domen.domen2.zone\\sg155630 Microsoft Windows-Netzwerk\n");
  201. sb.append("Der Befehl wurde erfolgreich ausgeführt.\n");
  202. Map<String, String> map;
  203. map = HostMappingProviderWindows.parseNetUseOutput("serverOne", new StringReader(sb.toString()));
  204. assert map != null && map.size() == 1 && "p:".equals(map.get("pub"));
  205. map = HostMappingProviderWindows.parseNetUseOutput("sErvEr_22_", new StringReader(sb.toString()));
  206. assert map != null && map.size() == 1 && "y:".equals(map.get("long name"));
  207. map = HostMappingProviderWindows.parseNetUseOutput("name.domen.domen2.zone", new StringReader(sb.toString()));
  208. assert map != null && map.size() == 1 && "z:".equals(map.get("sg155630"));
  209. }
  210. public void testHostMappingProviderWindows_Unexpected() throws Exception {
  211. // Test for error processing.
  212. // In the case net use prints something unexpected no exceptions should be thrown
  213. // 1. No header
  214. String netUseOutput_1 =
  215. "-------------------------------------------------------------------\n" +
  216. "OK P: \\\\server_1\\pub Microsoft Windows-Netzwerk\n";
  217. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(netUseOutput_1));
  218. // 2. No "-----------------" line
  219. String netUseOutput_2 =
  220. "Status Local Remote Network\n" +
  221. "OK P: \\\\server_1\\pub Microsoft Windows-Netzwerk\n";
  222. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(netUseOutput_2));
  223. // 3. short host
  224. String netUseOutput_3 =
  225. "\n" +
  226. "Status Local Remote Network\n" +
  227. "-------------------------------------------------------------------\n" +
  228. "OK P: xx Microsoft Windows-Netzwerk\n";
  229. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(netUseOutput_3));
  230. // 4. server without "\\"
  231. String netUseOutput_4 =
  232. "\n" +
  233. "Status Local Remote Network\n" +
  234. "-------------------------------------------------------------------\n" +
  235. "OK P: server_1_pub Microsoft Windows-Netzwerk\n";
  236. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(netUseOutput_4));
  237. // 5. empty line
  238. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(""));
  239. // 6. just some crap
  240. String netUseOutput_6 = "qwe\nasd\n---------------\nzxc\n123\n456\n\n\n";
  241. HostMappingProviderWindows.parseNetUseOutput("server_1", new StringReader(netUseOutput_6));
  242. }
  243. public void testHostMappingProviderSamba() throws Exception {
  244. Map<String, String> map;
  245. map = HostMappingProviderSamba.parseOutput(new StringReader(getConfigFile().toString()));
  246. assert map != null && map.size() == 1 && "/export/pub".equals(map.get("pub"));
  247. }
  248. private static StringBuilder getConfigFile() {
  249. StringBuilder sb = new StringBuilder();
  250. sb.append("[global]\n");
  251. sb.append("\n");
  252. sb.append(" workgroup = staff\n");
  253. sb.append(" server string = Eaglet-SR Samba Server\n");
  254. sb.append(" log file = /var/adm/samba_log.%m\n");
  255. sb.append(" security = SHARE\n");
  256. sb.append("\n");
  257. sb.append("[pub]\n");
  258. sb.append("\n");
  259. sb.append(" comment = pub\n");
  260. sb.append(" path = /export/pub\n");
  261. sb.append(" force user = tester\n");
  262. sb.append(" force group = other\n");
  263. sb.append(" read only = No\n");
  264. sb.append(" guest ok = No\n");
  265. return sb;
  266. }
  267. public void testSimpleConfigParser() {
  268. SimpleConfigParser p = new SimpleConfigParser();
  269. StringBuilder sb = getConfigFile();
  270. sb.insert(0, "orphan=orphanValue\n");
  271. p.parse(new StringReader(sb.toString()));
  272. assert p.getSections().contains("global");
  273. assert p.getOrphanAttributes().get("orphan").equals("orphanValue");
  274. assert p.getAttributes("pub").get("path").equals("/export/pub");
  275. }
  276. // public void testMappingsValidation() {
  277. // if (canTestRemote()) {
  278. // RemotePathMap.validateMapping(getHKey(), rpath, "/net/endif/export");
  279. // }
  280. // }
  281. // we need this since some methods are without @ForAllEnvironments
  282. public MappingsTestCase(String testName) {
  283. super(testName);
  284. }
  285. public MappingsTestCase(String testName, ExecutionEnvironment execEnv) {
  286. super(testName, execEnv);
  287. }
  288. public static Test suite() {
  289. return new RemoteDevelopmentTest(MappingsTestCase.class);
  290. }
  291. }