PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/enhanced/buildtest/trunk/tests/functional/src/test/functional/org/apache/harmony/test/func/api/javax/naming/provider/dns/DnsTest1.java

https://bitbucket.org/varialus/harmony
Java | 357 lines | 173 code | 21 blank | 163 comment | 21 complexity | 54967c13d530668516ea991b5263e27b MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. *
  19. */
  20. package org.apache.harmony.test.func.api.javax.naming.provider.dns;
  21. import java.util.Hashtable;
  22. import javax.naming.ConfigurationException;
  23. import javax.naming.Context;
  24. import javax.naming.NamingException;
  25. import javax.naming.directory.Attribute;
  26. import javax.naming.directory.Attributes;
  27. import javax.naming.directory.InitialDirContext;
  28. import org.apache.harmony.test.func.api.javax.naming.share.MultiCaseUtil;
  29. import org.apache.harmony.share.Result;
  30. /**
  31. * This test can test localhost name server. To do this uncomment methods
  32. * getCtx1(), testGetAttributes2N();
  33. *
  34. */
  35. public class DnsTest1 extends MultiCaseUtil {
  36. /**
  37. * DNS Service Provider class name.
  38. */
  39. public static String factory;
  40. static {
  41. try {
  42. String className = "org.apache.harmony.jndi.provider.dns.DNSContextFactory";
  43. Class.forName(className);
  44. factory = className;
  45. } catch (ClassNotFoundException e) {
  46. factory = "com.sun.jndi.dns.DnsContextFactory";
  47. }
  48. }
  49. /**
  50. * DNS server host. This host is uses if command line argument PROVIDER_URL
  51. * is not specified.
  52. */
  53. private String host = "default.example.com";
  54. /**
  55. * Initial dir context.
  56. */
  57. private InitialDirContext ctx;
  58. /**
  59. * Create initial context.
  60. */
  61. public void setUp() {
  62. try {
  63. String str = getArg("PROVIDER_URL");
  64. if (str != null && !str.startsWith("$")) {
  65. host = str.substring(6, str.indexOf("/", 6));
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. host = "default.example.com";
  70. }
  71. echo("Server host: " + host);
  72. }
  73. /**
  74. * Get attributes, associated with the host example.com. getAttributes()
  75. * method should retrieve a=A: 198.175.96.33
  76. *
  77. * @return
  78. */
  79. public Result testGetAttributes11() {
  80. try {
  81. getCtx1();
  82. } catch (NamingException e) {
  83. e.printStackTrace();
  84. return failed(e.toString());
  85. }
  86. return checkAttributes1();
  87. }
  88. /**
  89. * Get attributes, associated with the host example.com. getAttributes()
  90. * method should retrieve A, NS and SOA attributes.
  91. *
  92. * @return
  93. */
  94. public Result testGetAttributes12() {
  95. try {
  96. getCtx1();
  97. } catch (NamingException e) {
  98. e.printStackTrace();
  99. return failed(e.toString());
  100. }
  101. return checkAttributes2();
  102. }
  103. /**
  104. * Get attributes, associated with the host1.example.com. getAttributes()
  105. * method should retrieve txt=TXT: "test", a=A: 10.0.0.1, hinfo=HINFO: OS
  106. * Windows XP, OS Linux SuSe 9.2, CPU Pentium 3.0 GHz, mx=MX: 10
  107. * host1.example.com.
  108. *
  109. * @return
  110. */
  111. public Result testGetAttributes13() {
  112. try {
  113. getCtx1();
  114. } catch (NamingException e) {
  115. e.printStackTrace();
  116. return failed(e.toString());
  117. }
  118. return checkAttributes3();
  119. }
  120. /**
  121. * Get attributes, associated with the host example.com. getAttributes()
  122. * method should retrieve a=A: 198.175.96.33
  123. *
  124. * @return
  125. */
  126. // public Result testGetAttributes21() {
  127. // try {
  128. // getCtx2();
  129. // } catch (NamingException e) {
  130. // e.printStackTrace();
  131. // return failed(e.toString());
  132. // }
  133. // return checkAttributes1();
  134. // }
  135. /**
  136. * Get attributes, associated with the host example.com. getAttributes()
  137. * method should retrieve A, NS and SOA attributes.
  138. *
  139. * @return
  140. */
  141. // public Result testGetAttributes22() {
  142. // try {
  143. // getCtx2();
  144. // } catch (NamingException e) {
  145. // e.printStackTrace();
  146. // return failed(e.toString());
  147. // }
  148. // return checkAttributes2();
  149. // }
  150. /**
  151. * Get attributes, associated with the host1.example.com. getAttributes()
  152. * method should retrieve txt=TXT: "test", a=A: 10.0.0.1, hinfo=HINFO: OS
  153. * Windows XP, OS Linux SuSe 9.2, CPU Pentium 3.0 GHz, mx=MX: 10
  154. * host1.example.com.
  155. *
  156. * @return
  157. */
  158. // public Result testGetAttributes23() {
  159. // try {
  160. // getCtx2();
  161. // } catch (NamingException e) {
  162. // e.printStackTrace();
  163. // return failed(e.toString());
  164. // }
  165. // return checkAttributes3();
  166. // }
  167. /**
  168. * Create initial context with faulty the configuration:
  169. * (DirContext.PROVIDER_URL, "dns://127.0.0.1:5353
  170. * dns://127.0.0.1:53/example.com"). In this case ConfigurationException
  171. * should be thrown.
  172. *
  173. * @return
  174. */
  175. public Result testFaultyConfiguration() {
  176. try {
  177. ctx = new InitialDirContext();
  178. ctx.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, factory);
  179. ctx.addToEnvironment(Context.PROVIDER_URL, "dns://" + host
  180. + ":5353 dns://" + host + "/example.com");
  181. } catch (Exception e) {
  182. if (e instanceof ConfigurationException) {
  183. return passed();
  184. }
  185. }
  186. return failed("ConfigurationException should be thwrown");
  187. }
  188. /**
  189. * Get attributes, associated with the host example.com. getAttributes()
  190. * method should retrieve a=A: 198.175.96.33
  191. *
  192. * @return
  193. */
  194. private Result checkAttributes1() {
  195. try {
  196. Attributes attrs = ctx.getAttributes("example.com");
  197. if (!compareAttrs(attrs, "A", "198.175.96.33")) {
  198. return failed("FAILED\n Command: "
  199. + "getAttributes(\"example.com\")\n Retrieved: " + attrs
  200. + "\n Required: {a=A: 198.175.96.33}\n");
  201. }
  202. } catch (NamingException e) {
  203. e.printStackTrace();
  204. return failed(e.toString());
  205. }
  206. return passed();
  207. }
  208. /**
  209. * Get attributes, associated with the host example.com. getAttributes()
  210. * method should retrieve A, NS and SOA attributes.
  211. *
  212. * @return
  213. */
  214. private Result checkAttributes2() {
  215. try {
  216. Attributes attrs = ctx.getAttributes("example.com");
  217. if (!(compareAttrs(attrs, "A", "127.0.0.1")
  218. && compareAttrs(attrs, "NS", "example.com.") && compareAttrs(
  219. attrs, "SOA", "host1.example.com. hostmaster.example.com."
  220. + " 200412105 36000 3600 604800 86400"))) {
  221. return failed("FAILED\n Command: "
  222. + "getAttributes(\"example.com\")\n Retrieved: " + attrs
  223. + "\n Required: {a=A: 127.0.0.1, ns=NS: example.com.,"
  224. + " soa=SOA: host1.example.com. hostmaster.example.com. "
  225. + "200412105 36000 3600 604800 86400} \n");
  226. }
  227. } catch (NamingException e) {
  228. e.printStackTrace();
  229. return failed(e.toString());
  230. }
  231. return passed();
  232. }
  233. /**
  234. * Get attributes, associated with host1.example.com. getAttributes() method
  235. * should retrieve txt=TXT: "test", a=A: 10.0.0.1, hinfo=HINFO: OS Windows
  236. * XP, OS Linux SuSe 9.2, CPU Pentium 3.0 GHz, mx=MX: 10 host1.example.com.
  237. *
  238. * @return
  239. */
  240. private Result checkAttributes3() {
  241. try {
  242. Attributes attrs = ctx.getAttributes("host1.example.com");
  243. if (!(compareAttrs(attrs, "txt", "test host1")
  244. && compareAttrs(attrs, "MX", "10 host1.example.com.") && compareAttrs(
  245. attrs, "HINFO",
  246. "CPU Pentium 3.0 GHz, OS Linux SuSe 9.2, OS Windows XP"))) {
  247. return failed("FAILED\n Command: "
  248. + "getAttributes(\"host1.example.com\")\n Retrieved: "
  249. + attrs
  250. + "\n Required: {txt=TXT: test host1, a=A: 10.0.0.1, "
  251. + "hinfo=HINFO: OS Linux SuSe 9.2, CPU Pentium 3.0 GHz, "
  252. + "OS Windows XP, mx=MX: 10 host1.example.com.}\n" + "\n");
  253. }
  254. } catch (NamingException e) {
  255. e.printStackTrace();
  256. return failed(e.toString());
  257. }
  258. return passed();
  259. }
  260. /**
  261. * Compares the Attributes object with its string representation.
  262. *
  263. * @param attrs Attributes object
  264. * @param name comma separated attribute names
  265. * @param values comma separated attribute values.
  266. * @return
  267. */
  268. private boolean compareAttrs(Attributes attrs, String name, String values) {
  269. String[] val = getCommaSeparatedTokens(values);
  270. Attribute attr = attrs.get(name);
  271. if (attr == null || attr.size() != val.length) {
  272. return false;
  273. }
  274. try {
  275. for (int i = 0; i < attr.size(); i++) {
  276. String attrVal = (String)attr.get(i);
  277. boolean b = true;
  278. for (int n = 0; n < val.length; n++) {
  279. if (val[n].equals(attrVal)) {
  280. b = false;
  281. break;
  282. }
  283. }
  284. if (b) {
  285. return false;
  286. }
  287. }
  288. } catch (NamingException e) {
  289. e.printStackTrace();
  290. return false;
  291. }
  292. return true;
  293. }
  294. /**
  295. * Create new initial context.
  296. *
  297. * @return
  298. * @throws NamingException
  299. */
  300. private InitialDirContext getCtx1() throws NamingException {
  301. Hashtable env = new Hashtable();
  302. env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
  303. env.put(Context.PROVIDER_URL, "dns://" + host + ":5353 dns://" + host);
  304. ctx = new InitialDirContext(env);
  305. return ctx;
  306. }
  307. /**
  308. * @return
  309. * @throws NamingException
  310. */
  311. // private InitialDirContext getCtx2() throws NamingException {
  312. // Hashtable env = new Hashtable();
  313. // env.put(DirContext.INITIAL_CONTEXT_FACTORY, factory);
  314. // env.put(DirContext.PROVIDER_URL, "dns://:5353 dns://:53");
  315. // ctx = new InitialDirContext(env);
  316. // return ctx;
  317. // }
  318. /**
  319. * Close initial context.
  320. */
  321. public void tearDown() {
  322. try {
  323. if (ctx != null) {
  324. ctx.close();
  325. }
  326. } catch (NamingException e) {
  327. // e.printStackTrace();
  328. }
  329. }
  330. /**
  331. * @param args
  332. */
  333. public static void main(String[] args) {
  334. System.exit(new DnsTest1().test(args));
  335. }
  336. }