PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jboss-5.1.0/varia/src/tests/org/jboss/test/services/binding/test/ServiceBindingManagerUnitTestCase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 475 lines | 349 code | 51 blank | 75 comment | 0 complexity | c4f92b31338957298c048b56e67db021 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2006, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.test.services.binding.test;
  23. import java.beans.PropertyEditor;
  24. import java.beans.PropertyEditorManager;
  25. import java.io.File;
  26. import java.net.InetAddress;
  27. import java.net.URL;
  28. import java.util.Collections;
  29. import org.jboss.services.binding.ServiceBinding;
  30. import org.jboss.services.binding.ServiceBindingManager;
  31. import org.jboss.services.binding.ServiceBindingMetadata;
  32. import org.w3c.dom.Element;
  33. /**
  34. * Tests of ServiceBindingManager.
  35. *
  36. * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
  37. * @version $Revision: 85945 $
  38. */
  39. public class ServiceBindingManagerUnitTestCase extends ServiceBindingTestBase
  40. {
  41. private static final String SERVER = "server";
  42. private static final String INPUT = "${host}";
  43. private ServiceBindingManager testee;
  44. private ServiceBindingMetadata noNameMetadata;
  45. private ServiceBinding noNameBinding;
  46. private MockServiceBindingStore mockStore;
  47. private InetAddress address;
  48. /**
  49. * Create a new ServiceBindingManagerUnitTestCase.
  50. *
  51. * @param arg0
  52. */
  53. public ServiceBindingManagerUnitTestCase(String arg0)
  54. {
  55. super(arg0);
  56. }
  57. protected void setUp() throws Exception
  58. {
  59. super.setUp();
  60. mockStore = new MockServiceBindingStore(binding, SERVER);
  61. testee = new ServiceBindingManager(SERVER, mockStore);
  62. noNameMetadata = new ServiceBindingMetadata(SVC_NAME, null, null, PORT);
  63. noNameBinding = new ServiceBinding(noNameMetadata, HOST, 0);
  64. address = InetAddress.getByName(HOST);
  65. }
  66. public void testGetServerName()
  67. {
  68. assertEquals(SERVER, testee.getServerName());
  69. }
  70. public void testGetServiceBindings()
  71. {
  72. assertEquals(Collections.singleton(binding), testee.getServiceBindings());
  73. }
  74. /**
  75. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getIntBinding(java.lang.String)}.
  76. * @throws Exception
  77. */
  78. public void testGetIntBindingString() throws Exception
  79. {
  80. mockStore.setBinding(noNameBinding);
  81. assertEquals(PORT, testee.getIntBinding(SVC_NAME));
  82. }
  83. /**
  84. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getIntBinding(java.lang.String, java.lang.String)}.
  85. */
  86. public void testGetIntBindingStringString() throws Exception
  87. {
  88. assertEquals(PORT, testee.getIntBinding(SVC_NAME, BINDING_NAME));
  89. }
  90. public void testGetIntBindingViaGeneric() throws Exception
  91. {
  92. Integer result = new Integer(5);
  93. MockServiceBindingValueSource source = new MockServiceBindingValueSource(result);
  94. bindingMetadata.setServiceBindingValueSource(source);
  95. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  96. mockStore.setBinding(binding);
  97. assertEquals(5, testee.getIntBinding(SVC_NAME, BINDING_NAME));
  98. Object[] params = source.getParams();
  99. assertNull(params);
  100. }
  101. public void testGetIntBindingNoBinding() throws Exception
  102. {
  103. mockStore.setBinding(null);
  104. assertEquals(binding.getPort() + mockStore.getDefaultPortOffset(SERVER), testee.getIntBinding(SVC_NAME, BINDING_NAME, binding.getHostName(), binding.getPort()));
  105. mockStore.setBinding(null);
  106. assertEquals(binding.getPort() + mockStore.getDefaultPortOffset(SERVER), testee.getIntBinding(SVC_NAME, BINDING_NAME, null, binding.getPort()));
  107. mockStore.setBinding(null);
  108. assertEquals(binding.getPort(), testee.getIntBinding(SVC_NAME, BINDING_NAME, binding.getHostName(), binding.getPort(), true, true));
  109. }
  110. /**
  111. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getInetAddressBinding(java.lang.String)}.
  112. */
  113. public void testGetInetAddressBindingString() throws Exception
  114. {
  115. mockStore.setBinding(noNameBinding);
  116. assertEquals(address, testee.getInetAddressBinding(SVC_NAME));
  117. }
  118. /**
  119. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getInetAddressBinding(java.lang.String, java.lang.String)}.
  120. */
  121. public void testGetInetAddressBindingStringString() throws Exception
  122. {
  123. assertEquals(address, testee.getInetAddressBinding(SVC_NAME, BINDING_NAME));
  124. }
  125. public void testGetInetAddressBindingViaGeneric() throws Exception
  126. {
  127. InetAddress result = InetAddress.getByName("localhost");
  128. MockServiceBindingValueSource source = new MockServiceBindingValueSource(result);
  129. bindingMetadata.setServiceBindingValueSource(source);
  130. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  131. mockStore.setBinding(binding);
  132. assertEquals(result, testee.getInetAddressBinding(SVC_NAME, BINDING_NAME));
  133. Object[] params = source.getParams();
  134. assertNull(params);
  135. }
  136. public void testGetInetAddressBindingNoBinding() throws Exception
  137. {
  138. mockStore.setBinding(null);
  139. assertEquals(binding.getBindAddress(), testee.getInetAddressBinding(SVC_NAME, BINDING_NAME, binding.getHostName(), binding.getPort()));
  140. mockStore.setBinding(null);
  141. assertEquals(InetAddress.getByName(mockStore.getDefaultHostName(SERVER)), testee.getInetAddressBinding(SVC_NAME, BINDING_NAME, null, binding.getPort()));
  142. mockStore.setBinding(null);
  143. assertEquals(InetAddress.getByName(mockStore.getDefaultHostName(SERVER)), testee.getInetAddressBinding(SVC_NAME, BINDING_NAME, binding.getHostName(), binding.getPort(), false, false));
  144. mockStore.setBinding(null);
  145. assertEquals(InetAddress.getByName(null), testee.getInetAddressBinding(SVC_NAME, BINDING_NAME, null, binding.getPort(), true, true));
  146. }
  147. /**
  148. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getStringBinding(java.lang.String, java.lang.String)}.
  149. */
  150. public void testGetStringBindingStringString() throws Exception
  151. {
  152. mockStore.setBinding(noNameBinding);
  153. assertEquals(HOST, testee.getStringBinding(SVC_NAME, INPUT));
  154. }
  155. /**
  156. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getStringBinding(java.lang.String, java.lang.String, java.lang.String)}.
  157. */
  158. public void testGetStringBindingStringStringString() throws Exception
  159. {
  160. assertEquals(HOST, testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT));
  161. }
  162. public void testGetStringBindingViaGeneric() throws Exception
  163. {
  164. MockServiceBindingValueSource source = new MockServiceBindingValueSource(SVC_NAME);
  165. bindingMetadata.setServiceBindingValueSource(source);
  166. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  167. mockStore.setBinding(binding);
  168. assertEquals(SVC_NAME, testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT));
  169. Object[] params = source.getParams();
  170. assertNotNull(params);
  171. assertEquals(1, params.length);
  172. assertEquals(INPUT, params[0]);
  173. }
  174. public void testGetStringBindingNoBinding() throws Exception
  175. {
  176. mockStore.setBinding(null);
  177. assertEquals(binding.getHostName(), testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT, binding.getHostName(), binding.getPort()));
  178. mockStore.setBinding(null);
  179. assertEquals(InetAddress.getByName(mockStore.getDefaultHostName(SERVER)).getHostName(), testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT, null, binding.getPort()));
  180. mockStore.setBinding(null);
  181. assertEquals(InetAddress.getByName(mockStore.getDefaultHostName(SERVER)).getHostName(), testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT, binding.getHostName(), binding.getPort(), false, false));
  182. mockStore.setBinding(null);
  183. assertEquals(InetAddress.getByName(null).getHostName(), testee.getStringBinding(SVC_NAME, BINDING_NAME, INPUT, null, binding.getPort(), true, true));
  184. }
  185. /**
  186. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getElementBinding(java.lang.String, org.w3c.dom.Element)}.
  187. */
  188. public void testGetElementBindingStringElement() throws Exception
  189. {
  190. mockStore.setBinding(noNameBinding);
  191. PropertyEditor editor = PropertyEditorManager.findEditor(Element.class);
  192. editor.setAsText(ELEMENT_INPUT);
  193. Element input = (Element) editor.getValue();
  194. Element output = testee.getElementBinding(SVC_NAME, input);
  195. validateOutputElement(output, false, false);
  196. }
  197. /**
  198. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getElementBinding(java.lang.String, java.lang.String, org.w3c.dom.Element)}.
  199. */
  200. public void testGetElementBindingStringStringElement() throws Exception
  201. {
  202. PropertyEditor editor = PropertyEditorManager.findEditor(Element.class);
  203. editor.setAsText(ELEMENT_INPUT);
  204. Element input = (Element) editor.getValue();
  205. Element output = testee.getElementBinding(SVC_NAME, BINDING_NAME, input);
  206. validateOutputElement(output);
  207. }
  208. public void testGetElementBindingViaGeneric() throws Exception
  209. {
  210. PropertyEditor editor = PropertyEditorManager.findEditor(Element.class);
  211. editor.setAsText(ELEMENT_INPUT);
  212. Element result = (Element) editor.getValue();
  213. MockServiceBindingValueSource source = new MockServiceBindingValueSource(result);
  214. bindingMetadata.setServiceBindingValueSource(source);
  215. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  216. mockStore.setBinding(binding);
  217. assertEquals(result, testee.getElementBinding(SVC_NAME, BINDING_NAME, result));
  218. Object[] params = source.getParams();
  219. assertNotNull(params);
  220. assertEquals(1, params.length);
  221. assertEquals(result, params[0]);
  222. }
  223. public void testGetElementBindingViaXSLT() throws Exception
  224. {
  225. bindingMetadata.setServiceBindingValueSourceConfig(getXSLTConfig());
  226. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  227. mockStore.setBinding(binding);
  228. Element input = getDocumentElementFromClasspath(XSL_INPUT);
  229. Element output = testee.getElementBinding(SVC_NAME, BINDING_NAME, input);
  230. validateXSLTOutputElement(output);
  231. }
  232. public void testGetElementBindingNoBinding() throws Exception
  233. {
  234. mockStore.setBinding(null);
  235. PropertyEditor editor = PropertyEditorManager.findEditor(Element.class);
  236. editor.setAsText(ELEMENT_INPUT);
  237. Element input = (Element) editor.getValue();
  238. Element output = testee.getElementBinding(SVC_NAME, BINDING_NAME, input, binding.getHostName(), binding.getPort());
  239. validateOutputElement(output, false, true);
  240. mockStore.setBinding(null);
  241. output = testee.getElementBinding(SVC_NAME, BINDING_NAME, input, null, binding.getPort());
  242. validateOutputElement(output, true, true);
  243. }
  244. /**
  245. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getURLBinding(java.lang.String, java.net.URL)}.
  246. */
  247. public void testGetURLBindingStringURL() throws Exception
  248. {
  249. mockStore.setBinding(noNameBinding);
  250. String resource = getFullyQualifiedResourceName("input.xml");
  251. URL input = Thread.currentThread().getContextClassLoader().getResource(resource);
  252. URL output = testee.getURLBinding(SVC_NAME, input);
  253. assertNotNull(output);
  254. Element element = getDocumentElement(output);
  255. validateOutputElement(element);
  256. }
  257. /**
  258. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getURLBinding(java.lang.String, java.lang.String, java.net.URL)}.
  259. */
  260. public void testGetURLBindingStringStringURL() throws Exception
  261. {
  262. String resource = getFullyQualifiedResourceName("input.xml");
  263. URL input = Thread.currentThread().getContextClassLoader().getResource(resource);
  264. URL output = testee.getURLBinding(SVC_NAME, BINDING_NAME, input);
  265. assertNotNull(output);
  266. Element element = getDocumentElement(output);
  267. validateOutputElement(element);
  268. }
  269. public void testGetURLBindingViaGeneric() throws Exception
  270. {
  271. URL result = new File(getFullyQualifiedResourceName("input.xml")).toURL();
  272. MockServiceBindingValueSource source = new MockServiceBindingValueSource(result);
  273. bindingMetadata.setServiceBindingValueSource(source);
  274. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  275. mockStore.setBinding(binding);
  276. assertEquals(result, testee.getURLBinding(SVC_NAME, BINDING_NAME, result));
  277. Object[] params = source.getParams();
  278. assertNotNull(params);
  279. assertEquals(1, params.length);
  280. assertEquals(result, params[0]);
  281. }
  282. public void testGetURLBindingViaXSLT() throws Exception
  283. {
  284. bindingMetadata.setServiceBindingValueSourceConfig(getXSLTConfig());
  285. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  286. mockStore.setBinding(binding);
  287. URL input = Thread.currentThread().getContextClassLoader().getResource(XSL_INPUT);
  288. URL output = testee.getURLBinding(SVC_NAME, BINDING_NAME, input);
  289. assertNotNull(output);
  290. Element element = getDocumentElement(output);
  291. validateXSLTOutputElement(element);
  292. }
  293. public void testGetURLBindingNoBinding() throws Exception
  294. {
  295. mockStore.setBinding(null);
  296. String resource = getFullyQualifiedResourceName("input.xml");
  297. URL input = Thread.currentThread().getContextClassLoader().getResource(resource);
  298. URL output = testee.getURLBinding(SVC_NAME, BINDING_NAME, input, binding.getHostName(), binding.getPort());
  299. assertNotNull(output);
  300. Element element = getDocumentElement(output);
  301. validateOutputElement(element, false, true);
  302. mockStore.setBinding(null);
  303. output = testee.getURLBinding(SVC_NAME, BINDING_NAME, input, null, binding.getPort());
  304. assertNotNull(output);
  305. element = getDocumentElement(output);
  306. validateOutputElement(element, true, true);
  307. mockStore.setBinding(null);
  308. output = testee.getURLBinding(SVC_NAME, BINDING_NAME, input, binding.getHostName(), binding.getPort(), false, false);
  309. assertNotNull(output);
  310. element = getDocumentElement(output);
  311. validateOutputElement(element, true, true);
  312. }
  313. /**
  314. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getResourceBinding(java.lang.String, java.lang.String)}.
  315. */
  316. public void testGetResourceBindingStringString() throws Exception
  317. {
  318. String input = getFullyQualifiedResourceName("input.xml");
  319. String output = testee.getResourceBinding(SVC_NAME, BINDING_NAME, input);
  320. assertNotNull(output);
  321. Element element = getDocumentElement(output);
  322. validateOutputElement(element);
  323. }
  324. /**
  325. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getResourceBinding(java.lang.String, java.lang.String, java.lang.String)}.
  326. */
  327. public void testGetResourceBindingStringStringString() throws Exception
  328. {
  329. mockStore.setBinding(noNameBinding);
  330. String input = getFullyQualifiedResourceName("input.xml");
  331. String output = testee.getResourceBinding(SVC_NAME, input);
  332. assertNotNull(output);
  333. Element element = getDocumentElement(output);
  334. validateOutputElement(element);
  335. }
  336. public void testGetResourceBindingViaGeneric() throws Exception
  337. {
  338. MockServiceBindingValueSource source = new MockServiceBindingValueSource(SVC_NAME);
  339. bindingMetadata.setServiceBindingValueSource(source);
  340. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  341. mockStore.setBinding(binding);
  342. assertEquals(SVC_NAME, testee.getResourceBinding(SVC_NAME, BINDING_NAME, INPUT));
  343. Object[] params = source.getParams();
  344. assertNotNull(params);
  345. assertEquals(1, params.length);
  346. assertEquals(INPUT, params[0]);
  347. }
  348. public void testGetResourceBindingViaXSLT() throws Exception
  349. {
  350. bindingMetadata.setServiceBindingValueSourceConfig(getXSLTConfig());
  351. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  352. mockStore.setBinding(binding);
  353. String output = testee.getResourceBinding(SVC_NAME, BINDING_NAME, XSL_INPUT);
  354. assertNotNull(output);
  355. Element element = getDocumentElement(output);
  356. validateXSLTOutputElement(element);
  357. }
  358. public void testGetResourceBindingNoBinding() throws Exception
  359. {
  360. mockStore.setBinding(null);
  361. String input = getFullyQualifiedResourceName("input.xml");
  362. String output = testee.getResourceBinding(SVC_NAME, BINDING_NAME, input, binding.getHostName(), binding.getPort());
  363. assertNotNull(output);
  364. Element element = getDocumentElement(output);
  365. validateOutputElement(element, false, true);
  366. mockStore.setBinding(null);
  367. output = testee.getResourceBinding(SVC_NAME, BINDING_NAME, input, null, binding.getPort());
  368. assertNotNull(output);
  369. element = getDocumentElement(output);
  370. validateOutputElement(element, true, true);
  371. mockStore.setBinding(null);
  372. output = testee.getResourceBinding(SVC_NAME, BINDING_NAME, input, binding.getHostName(), binding.getPort(), false, false);
  373. assertNotNull(output);
  374. element = getDocumentElement(output);
  375. validateOutputElement(element, true, true);
  376. }
  377. /**
  378. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getGenericBinding(java.lang.String, java.lang.Object[])}.
  379. */
  380. public void testGetGenericBindingStringObjectArray() throws Exception
  381. {
  382. MockServiceBindingValueSource source = new MockServiceBindingValueSource(SVC_NAME);
  383. noNameMetadata.setServiceBindingValueSource(source);
  384. noNameBinding = new ServiceBinding(noNameMetadata, HOST, 0);
  385. mockStore.setBinding(noNameBinding);
  386. assertEquals(SVC_NAME, testee.getGenericBinding(SVC_NAME, null, INPUT));
  387. Object[] params = source.getParams();
  388. assertNotNull(params);
  389. assertEquals(1, params.length);
  390. assertEquals(INPUT, params[0]);
  391. }
  392. /**
  393. * Test method for {@link org.jboss.services.binding.ServiceBindingManager#getGenericBinding(java.lang.String, java.lang.String, java.lang.Object[])}.
  394. */
  395. public void testGetGenericBindingStringStringObjectArray() throws Exception
  396. {
  397. MockServiceBindingValueSource source = new MockServiceBindingValueSource(SVC_NAME);
  398. bindingMetadata.setServiceBindingValueSource(source);
  399. binding = new ServiceBinding(bindingMetadata, HOST, 0);
  400. mockStore.setBinding(binding);
  401. assertEquals(SVC_NAME, testee.getGenericBinding(SVC_NAME, BINDING_NAME, INPUT));
  402. Object[] params = source.getParams();
  403. assertNotNull(params);
  404. assertEquals(1, params.length);
  405. assertEquals(INPUT, params[0]);
  406. }
  407. public void testGetGenericBindingNoValueSource() throws Exception
  408. {
  409. try
  410. {
  411. Object[] params = null;
  412. testee.getGenericBinding(SVC_NAME, BINDING_NAME, params);
  413. fail("should not succeed without value source");
  414. }
  415. catch (IllegalStateException good) {}
  416. }
  417. protected void validateOutputElement(Element output, boolean expectDefaultHost, boolean expectOffset)
  418. {
  419. assertNotNull(output);
  420. assertEquals(expectDefaultHost ? mockStore.getDefaultHostName(SERVER) : HOST, output.getAttribute("host"));
  421. int expectedPort = PORT + (expectOffset ? mockStore.getDefaultPortOffset(SERVER) : 0);
  422. assertEquals(String.valueOf(expectedPort), output.getFirstChild().getNodeValue());
  423. }
  424. }