You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntrospectionHelperTest.java 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. package org.apache.tools.ant;
  19. import java.io.File;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.lang.reflect.Method;
  22. import java.util.Enumeration;
  23. import java.util.HashMap;
  24. import java.util.Hashtable;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Map;
  28. import org.apache.tools.ant.taskdefs.condition.Os;
  29. import org.junit.Before;
  30. import org.junit.ComparisonFailure;
  31. import org.junit.Ignore;
  32. import org.junit.Test;
  33. import static org.junit.Assert.assertEquals;
  34. import static org.junit.Assert.assertFalse;
  35. import static org.junit.Assert.assertNotNull;
  36. import static org.junit.Assert.assertTrue;
  37. import static org.junit.Assert.fail;
  38. /**
  39. * JUnit testcases for org.apache.tools.ant.IntrospectionHelper.
  40. *
  41. */
  42. public class IntrospectionHelperTest {
  43. private Project p;
  44. private IntrospectionHelper ih;
  45. private static final String projectBasedir = File.separator;
  46. @Before
  47. public void setUp() {
  48. p = new Project();
  49. p.setBasedir(projectBasedir);
  50. ih = IntrospectionHelper.getHelper(getClass());
  51. }
  52. @Test
  53. public void testIsDynamic() {
  54. assertFalse("Not dynamic", ih.isDynamic());
  55. }
  56. @Test
  57. public void testIsContainer() {
  58. assertFalse("Not a container", ih.isContainer());
  59. }
  60. @Test
  61. public void testAddText() throws BuildException {
  62. ih.addText(p, this, "test");
  63. try {
  64. ih.addText(p, this, "test2");
  65. fail("test2 shouldn\'t be equal to test");
  66. } catch (BuildException be) {
  67. assertTrue(be.getCause() instanceof ComparisonFailure);
  68. }
  69. ih = IntrospectionHelper.getHelper(String.class);
  70. try {
  71. ih.addText(p, "", "test");
  72. fail("String doesn\'t support addText");
  73. } catch (BuildException be) {
  74. //TODO the value should be asserted
  75. }
  76. }
  77. @Test
  78. @Ignore("This silently ignores a build exception")
  79. public void testGetAddTextMethod() {
  80. Method m = ih.getAddTextMethod();
  81. assertMethod(m, "addText", String.class, "test", "bing!");
  82. ih = IntrospectionHelper.getHelper(String.class);
  83. try {
  84. m = ih.getAddTextMethod();
  85. } catch (BuildException e) {
  86. }
  87. }
  88. @Test
  89. public void testSupportsCharacters() {
  90. assertTrue("IntrospectionHelperTest supports addText",
  91. ih.supportsCharacters());
  92. ih = IntrospectionHelper.getHelper(String.class);
  93. assertTrue("String doesn\'t support addText", !ih.supportsCharacters());
  94. }
  95. public void addText(String text) {
  96. assertEquals("test", text);
  97. }
  98. @Test
  99. public void testElementCreators() throws BuildException {
  100. try {
  101. ih.getElementType("one");
  102. fail("don't have element type one");
  103. } catch (BuildException be) {
  104. //TODO we should be asserting a value in here
  105. }
  106. try {
  107. ih.getElementType("two");
  108. fail("createTwo takes arguments");
  109. } catch (BuildException be) {
  110. //TODO we should be asserting a value in here
  111. }
  112. try {
  113. ih.getElementType("three");
  114. fail("createThree returns void");
  115. } catch (BuildException be) {
  116. //TODO we should be asserting a value in here
  117. }
  118. try {
  119. ih.getElementType("four");
  120. fail("createFour returns array");
  121. } catch (BuildException be) {
  122. //TODO we should be asserting a value in here
  123. }
  124. try {
  125. ih.getElementType("five");
  126. fail("createFive returns primitive type");
  127. } catch (BuildException be) {
  128. //TODO we should be asserting a value in here
  129. }
  130. assertEquals(String.class, ih.getElementType("six"));
  131. assertEquals("test", ih.createElement(p, this, "six"));
  132. try {
  133. ih.getElementType("seven");
  134. fail("addSeven takes two arguments");
  135. } catch (BuildException be) {
  136. //TODO we should be asserting a value in here
  137. }
  138. try {
  139. ih.getElementType("eight");
  140. fail("addEight takes no arguments");
  141. } catch (BuildException be) {
  142. //TODO we should be asserting a value in here
  143. }
  144. try {
  145. ih.getElementType("nine");
  146. fail("nine return non void");
  147. } catch (BuildException be) {
  148. //TODO we should be asserting a value in here
  149. }
  150. try {
  151. ih.getElementType("ten");
  152. fail("addTen takes array argument");
  153. } catch (BuildException be) {
  154. //TODO we should be asserting a value in here
  155. }
  156. try {
  157. ih.getElementType("eleven");
  158. fail("addEleven takes primitive argument");
  159. } catch (BuildException be) {
  160. //TODO we should be asserting a value in here
  161. }
  162. try {
  163. ih.getElementType("twelve");
  164. fail("no primitive constructor for java.lang.Class");
  165. } catch (BuildException be) {
  166. //TODO we should be asserting a value in here
  167. }
  168. assertEquals(StringBuffer.class, ih.getElementType("thirteen"));
  169. assertEquals("test", ih.createElement(p, this, "thirteen").toString());
  170. try {
  171. ih.createElement(p, this, "fourteen");
  172. fail("fourteen throws NullPointerException");
  173. } catch (BuildException be) {
  174. assertTrue(be.getCause() instanceof NullPointerException);
  175. }
  176. try {
  177. ih.createElement(p, this, "fourteen");
  178. fail("fifteen throws NullPointerException");
  179. } catch (BuildException be) {
  180. assertTrue(be.getCause() instanceof NullPointerException);
  181. }
  182. }
  183. private Map<String, Class<?>> getExpectedNestedElements() {
  184. Map<String, Class<?>> elemMap = new Hashtable<String, Class<?>>();
  185. elemMap.put("six", String.class);
  186. elemMap.put("thirteen", StringBuffer.class);
  187. elemMap.put("fourteen", StringBuffer.class);
  188. elemMap.put("fifteen", StringBuffer.class);
  189. return elemMap;
  190. }
  191. @Test
  192. public void testGetNestedElements() {
  193. Map<String, Class<?>> elemMap = getExpectedNestedElements();
  194. Enumeration e = ih.getNestedElements();
  195. while (e.hasMoreElements()) {
  196. String name = (String) e.nextElement();
  197. Class expect = (Class) elemMap.get(name);
  198. assertNotNull("Support for "+name+" in IntrospectioNHelperTest?",
  199. expect);
  200. assertEquals("Return type of " + name, expect, ih.getElementType(name));
  201. elemMap.remove(name);
  202. }
  203. assertTrue("Found all", elemMap.isEmpty());
  204. }
  205. @Test
  206. public void testGetNestedElementMap() {
  207. Map<String, Class<?>> elemMap = getExpectedNestedElements();
  208. Map<String, Class<?>> actualMap = ih.getNestedElementMap();
  209. for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
  210. String elemName = (String) entry.getKey();
  211. Class<?> elemClass = (Class) elemMap.get(elemName);
  212. assertNotNull("Support for " + elemName +
  213. " in IntrospectionHelperTest?", elemClass);
  214. assertEquals("Type of " + elemName, elemClass, entry.getValue());
  215. elemMap.remove(elemName);
  216. }
  217. assertTrue("Found all", elemMap.isEmpty());
  218. // Check it's a read-only map.
  219. try {
  220. actualMap.clear();
  221. //TODO we should be asserting a value somewhere in here
  222. } catch (UnsupportedOperationException e) {
  223. }
  224. }
  225. @Test
  226. public void testGetElementMethod() {
  227. assertElemMethod("six", "createSix", String.class, null);
  228. assertElemMethod("thirteen", "addThirteen", null, StringBuffer.class);
  229. assertElemMethod("fourteen", "addFourteen", null, StringBuffer.class);
  230. assertElemMethod("fifteen", "createFifteen", StringBuffer.class, null);
  231. }
  232. private void assertElemMethod(String elemName, String methodName,
  233. Class returnType, Class methodArg) {
  234. Method m = ih.getElementMethod(elemName);
  235. assertEquals("Method name", methodName, m.getName());
  236. Class expectedReturnType = (returnType == null)? Void.TYPE: returnType;
  237. assertEquals("Return type", expectedReturnType, m.getReturnType());
  238. Class[] args = m.getParameterTypes();
  239. if (methodArg != null) {
  240. assertEquals("Arg Count", 1, args.length);
  241. assertEquals("Arg Type", methodArg, args[0]);
  242. } else {
  243. assertEquals("Arg Count", 0, args.length);
  244. }
  245. }
  246. public Object createTwo(String s) {
  247. return null;
  248. }
  249. public void createThree() {
  250. }
  251. public Object[] createFour() {
  252. return null;
  253. }
  254. public int createFive() {
  255. return 0;
  256. }
  257. public String createSix() {
  258. return "test";
  259. }
  260. public StringBuffer createFifteen() {
  261. throw new NullPointerException();
  262. }
  263. public void addSeven(String s, String s2) {
  264. }
  265. public void addEight() {
  266. }
  267. public String addNine(String s) {
  268. return null;
  269. }
  270. public void addTen(String[] s) {
  271. }
  272. public void addEleven(int i) {
  273. }
  274. public void addTwelve(Class c) {
  275. }
  276. public void addThirteen(StringBuffer sb) {
  277. sb.append("test");
  278. }
  279. public void addFourteen(StringBuffer s) {
  280. throw new NullPointerException();
  281. }
  282. @Test
  283. public void testAttributeSetters() throws BuildException {
  284. try {
  285. ih.setAttribute(p, this, "one", "test");
  286. fail("setOne doesn't exist");
  287. } catch (BuildException be) {
  288. //TODO we should be asserting a value in here
  289. }
  290. try {
  291. ih.setAttribute(p, this, "two", "test");
  292. fail("setTwo returns non void");
  293. } catch (BuildException be) {
  294. //TODO we should be asserting a value in here
  295. }
  296. try {
  297. ih.setAttribute(p, this, "three", "test");
  298. fail("setThree takes no args");
  299. } catch (BuildException be) {
  300. //TODO we should be asserting a value in here
  301. }
  302. try {
  303. ih.setAttribute(p, this, "four", "test");
  304. fail("setFour takes two args");
  305. } catch (BuildException be) {
  306. //TODO we should be asserting a value in here
  307. }
  308. try {
  309. ih.setAttribute(p, this, "five", "test");
  310. fail("setFive takes array arg");
  311. } catch (BuildException be) {
  312. //TODO we should be asserting a value in here
  313. }
  314. try {
  315. ih.setAttribute(p, this, "six", "test");
  316. fail("Project doesn't have a String constructor");
  317. } catch (BuildException be) {
  318. //TODO we should be asserting a value in here
  319. }
  320. ih.setAttribute(p, this, "seven", "2");
  321. try {
  322. ih.setAttribute(p, this, "seven", "3");
  323. fail("2 shouldn't be equals to three");
  324. } catch (BuildException be) {
  325. assertTrue(be.getCause() instanceof ComparisonFailure);
  326. }
  327. ih.setAttribute(p, this, "eight", "2");
  328. try {
  329. ih.setAttribute(p, this, "eight", "3");
  330. fail("2 shouldn't be equals to three - as int");
  331. } catch (BuildException be) {
  332. assertTrue("Cause of error: " + be.toString(), be.getCause() instanceof AssertionError);
  333. }
  334. ih.setAttribute(p, this, "nine", "2");
  335. try {
  336. ih.setAttribute(p, this, "nine", "3");
  337. fail("2 shouldn't be equals to three - as Integer");
  338. } catch (BuildException be) {
  339. assertTrue(be.getCause() instanceof AssertionError);
  340. }
  341. ih.setAttribute(p, this, "ten", "2");
  342. try {
  343. ih.setAttribute(p, this, "ten", "3");
  344. fail(projectBasedir + "2 shouldn't be equals to " + projectBasedir + "3");
  345. } catch (BuildException be) {
  346. assertTrue(be.getCause() instanceof AssertionError);
  347. }
  348. ih.setAttribute(p, this, "eleven", "2");
  349. try {
  350. ih.setAttribute(p, this, "eleven", "on");
  351. fail("on shouldn't be false");
  352. } catch (BuildException be) {
  353. assertTrue(be.getCause() instanceof AssertionError);
  354. }
  355. ih.setAttribute(p, this, "twelve", "2");
  356. try {
  357. ih.setAttribute(p, this, "twelve", "on");
  358. fail("on shouldn't be false");
  359. } catch (BuildException be) {
  360. assertTrue(be.getCause() instanceof AssertionError);
  361. }
  362. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
  363. try {
  364. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper");
  365. fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
  366. } catch (BuildException be) {
  367. assertTrue(be.getCause() instanceof AssertionError);
  368. }
  369. try {
  370. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2");
  371. fail("org.apache.tools.ant.Project2 doesn't exist");
  372. } catch (BuildException be) {
  373. assertTrue(be.getCause() instanceof ClassNotFoundException);
  374. }
  375. ih.setAttribute(p, this, "fourteen", "2");
  376. try {
  377. ih.setAttribute(p, this, "fourteen", "on");
  378. fail("2 shouldn't be equals to three - as StringBuffer");
  379. } catch (BuildException be) {
  380. assertTrue(be.getCause() instanceof ComparisonFailure);
  381. }
  382. ih.setAttribute(p, this, "fifteen", "abcd");
  383. try {
  384. ih.setAttribute(p, this, "fifteen", "on");
  385. fail("o shouldn't be equal to a");
  386. } catch (BuildException be) {
  387. assertTrue(be.getCause() instanceof AssertionError);
  388. }
  389. ih.setAttribute(p, this, "sixteen", "abcd");
  390. try {
  391. ih.setAttribute(p, this, "sixteen", "on");
  392. fail("o shouldn't be equal to a");
  393. } catch (BuildException be) {
  394. assertTrue(be.getCause() instanceof AssertionError);
  395. }
  396. ih.setAttribute(p, this, "seventeen", "17");
  397. try {
  398. ih.setAttribute(p, this, "seventeen", "3");
  399. fail("17 shouldn't be equals to three");
  400. } catch (BuildException be) {
  401. assertTrue(be.getCause() instanceof AssertionError);
  402. }
  403. ih.setAttribute(p, this, "eightteen", "18");
  404. try {
  405. ih.setAttribute(p, this, "eightteen", "3");
  406. fail("18 shouldn't be equals to three");
  407. } catch (BuildException be) {
  408. assertTrue(be.getCause() instanceof AssertionError);
  409. }
  410. ih.setAttribute(p, this, "nineteen", "19");
  411. try {
  412. ih.setAttribute(p, this, "nineteen", "3");
  413. fail("19 shouldn't be equals to three");
  414. } catch (BuildException be) {
  415. assertTrue(be.getCause() instanceof AssertionError);
  416. }
  417. }
  418. private Map getExpectedAttributes() {
  419. Map attrMap = new Hashtable();
  420. attrMap.put("seven", String.class);
  421. attrMap.put("eight", Integer.TYPE);
  422. attrMap.put("nine", Integer.class);
  423. attrMap.put("ten", File.class);
  424. attrMap.put("eleven", Boolean.TYPE);
  425. attrMap.put("twelve", Boolean.class);
  426. attrMap.put("thirteen", Class.class);
  427. attrMap.put("fourteen", StringBuffer.class);
  428. attrMap.put("fifteen", Character.TYPE);
  429. attrMap.put("sixteen", Character.class);
  430. attrMap.put("seventeen", Byte.TYPE);
  431. attrMap.put("eightteen", Short.TYPE);
  432. attrMap.put("nineteen", Double.TYPE);
  433. /*
  434. * JUnit 3.7 adds a getName method to TestCase - so we now
  435. * have a name attribute in IntrospectionHelperTest if we run
  436. * under JUnit 3.7 but not in earlier versions.
  437. *
  438. * Simply add it here and remove it after the tests.
  439. */
  440. attrMap.put("name", String.class);
  441. return attrMap;
  442. }
  443. @Test
  444. public void testGetAttributes() {
  445. Map attrMap = getExpectedAttributes();
  446. Enumeration e = ih.getAttributes();
  447. while (e.hasMoreElements()) {
  448. String name = (String) e.nextElement();
  449. Class expect = (Class) attrMap.get(name);
  450. assertNotNull("Support for "+name+" in IntrospectionHelperTest?",
  451. expect);
  452. assertEquals("Type of "+name, expect, ih.getAttributeType(name));
  453. attrMap.remove(name);
  454. }
  455. attrMap.remove("name");
  456. assertTrue("Found all", attrMap.isEmpty());
  457. }
  458. @Test
  459. public void testGetAttributeMap() {
  460. Map<String, Class<?>> attrMap = getExpectedAttributes();
  461. Map<String, Class<?>> actualMap = ih.getAttributeMap();
  462. for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
  463. String attrName = (String) entry.getKey();
  464. Class attrClass = (Class) attrMap.get(attrName);
  465. assertNotNull("Support for " + attrName +
  466. " in IntrospectionHelperTest?", attrClass);
  467. assertEquals("Type of " + attrName, attrClass, entry.getValue());
  468. attrMap.remove(attrName);
  469. }
  470. attrMap.remove("name");
  471. assertTrue("Found all", attrMap.isEmpty());
  472. // Check it's a read-only map.
  473. try {
  474. actualMap.clear();
  475. //TODO we should be asserting a value somewhere in here
  476. } catch (UnsupportedOperationException e) {
  477. }
  478. }
  479. @Test
  480. public void testGetAttributeMethod() {
  481. assertAttrMethod("seven", "setSeven", String.class,
  482. "2", "3");
  483. assertAttrMethod("eight", "setEight", Integer.TYPE,
  484. Integer.valueOf(2), Integer.valueOf(3));
  485. assertAttrMethod("nine", "setNine", Integer.class,
  486. Integer.valueOf(2), Integer.valueOf(3));
  487. assertAttrMethod("ten", "setTen", File.class,
  488. new File(projectBasedir + 2), new File("toto"));
  489. assertAttrMethod("eleven", "setEleven", Boolean.TYPE,
  490. Boolean.FALSE, Boolean.TRUE);
  491. assertAttrMethod("twelve", "setTwelve", Boolean.class,
  492. Boolean.FALSE, Boolean.TRUE);
  493. assertAttrMethod("thirteen", "setThirteen", Class.class,
  494. Project.class, Map.class);
  495. assertAttrMethod("fourteen", "setFourteen", StringBuffer.class,
  496. new StringBuffer("2"), new StringBuffer("3"));
  497. assertAttrMethod("fifteen", "setFifteen", Character.TYPE,
  498. Character.valueOf('a'), Character.valueOf('b'));
  499. assertAttrMethod("sixteen", "setSixteen", Character.class,
  500. Character.valueOf('a'), Character.valueOf('b'));
  501. assertAttrMethod("seventeen", "setSeventeen", Byte.TYPE,
  502. Byte.valueOf((byte) 17), Byte.valueOf((byte) 10));
  503. assertAttrMethod("eightteen", "setEightteen", Short.TYPE,
  504. Short.valueOf((short) 18), Short.valueOf((short) 10));
  505. assertAttrMethod("nineteen", "setNineteen", Double.TYPE,
  506. Double.valueOf(19), Double.valueOf((short) 10));
  507. try {
  508. assertAttrMethod("onehundred", null, null, null, null);
  509. fail("Should have raised a BuildException!");
  510. } catch (BuildException e) {
  511. //TODO we should be asserting a value in here
  512. }
  513. }
  514. private void assertAttrMethod(String attrName, String methodName,
  515. Class methodArg, Object arg, Object badArg) {
  516. Method m = ih.getAttributeMethod(attrName);
  517. assertMethod(m, methodName, methodArg, arg, badArg);
  518. }
  519. public int setTwo(String s) {
  520. return 0;
  521. }
  522. public void setThree() {
  523. }
  524. public void setFour(String s1, String s2) {
  525. }
  526. public void setFive(String[] s) {
  527. }
  528. public void setSix(Project p) {
  529. }
  530. public void setSeven(String s) {
  531. assertEquals("2", s);
  532. }
  533. public void setEight(int i) {
  534. assertEquals(2, i);
  535. }
  536. public void setNine(Integer i) {
  537. assertEquals(2, i.intValue());
  538. }
  539. public void setTen(File f) {
  540. String path = f.getAbsolutePath();
  541. if (Os.isFamily("unix") || Os.isFamily("openvms")) {
  542. assertEquals(projectBasedir + "2", path);
  543. } else if (Os.isFamily("netware")) {
  544. assertEquals(projectBasedir + "2", path.toLowerCase(Locale.US));
  545. } else {
  546. assertEquals(":" + projectBasedir + "2",
  547. path.toLowerCase(Locale.US).substring(1));
  548. }
  549. }
  550. public void setEleven(boolean b) {
  551. assertTrue(!b);
  552. }
  553. public void setTwelve(Boolean b) {
  554. assertTrue(!b.booleanValue());
  555. }
  556. public void setThirteen(Class c) {
  557. assertEquals(Project.class, c);
  558. }
  559. public void setFourteen(StringBuffer sb) {
  560. assertEquals("2", sb.toString());
  561. }
  562. public void setFifteen(char c) {
  563. assertEquals(c, 'a');
  564. }
  565. public void setSixteen(Character c) {
  566. assertEquals(c.charValue(), 'a');
  567. }
  568. public void setSeventeen(byte b) {
  569. assertEquals(17, b);
  570. }
  571. public void setEightteen(short s) {
  572. assertEquals(18, s);
  573. }
  574. public void setNineteen(double d) {
  575. double diff = d - 19;
  576. assertTrue("Expected 19, received " + d, diff > -1e-6 && diff < 1e-6);
  577. }
  578. @Test
  579. public void testGetExtensionPoints() {
  580. List extensions = ih.getExtensionPoints();
  581. final int adders = 2;
  582. assertEquals("extension count", adders, extensions.size());
  583. // this original test assumed something about the order of
  584. // add(Number) and addConfigured(Map) returned by reflection.
  585. // Unfortunately the assumption doesn't hold for all VMs
  586. // (failed on MacOS X using JDK 1.4.2_05) and the possible
  587. // combinatorics are too hard to check. We really only want
  588. // to ensure that the more derived Hashtable can be found
  589. // before Map.
  590. // assertExtMethod(extensions.get(0), "add", Number.class,
  591. // new Integer(2), new Integer(3));
  592. // addConfigured(Hashtable) should come before addConfigured(Map)
  593. assertExtMethod(extensions.get(adders - 2),
  594. "addConfigured", Hashtable.class,
  595. makeTable("key", "value"), makeTable("1", "2"));
  596. assertExtMethod(extensions.get(adders - 1), "addConfigured", Map.class,
  597. new HashMap(), makeTable("1", "2"));
  598. }
  599. private void assertExtMethod(Object mo, String methodName, Class methodArg,
  600. Object arg, Object badArg) {
  601. assertMethod((Method) mo, methodName, methodArg, arg, badArg);
  602. }
  603. private void assertMethod(Method m, String methodName, Class methodArg,
  604. Object arg, Object badArg) {
  605. assertEquals("Method name", methodName, m.getName());
  606. assertEquals("Return type", Void.TYPE, m.getReturnType());
  607. Class[] args = m.getParameterTypes();
  608. assertEquals("Arg Count", 1, args.length);
  609. assertEquals("Arg Type", methodArg, args[0]);
  610. try {
  611. m.invoke(this, new Object[] { arg });
  612. } catch (IllegalAccessException e) {
  613. throw new BuildException(e);
  614. } catch (InvocationTargetException e) {
  615. throw new BuildException(e);
  616. }
  617. try {
  618. m.invoke(this, new Object[] { badArg });
  619. fail("Should have raised an assertion exception");
  620. } catch (IllegalAccessException e) {
  621. throw new BuildException(e);
  622. } catch (InvocationTargetException e) {
  623. Throwable t = e.getTargetException();
  624. assertTrue(t.toString(), t instanceof AssertionError);
  625. }
  626. }
  627. public List add(List l) {
  628. // INVALID extension point
  629. return null;
  630. }
  631. // see comments in testGetExtensionPoints
  632. // public void add(Number n) {
  633. // // Valid extension point
  634. // assertEquals(2, n.intValue());
  635. // }
  636. public void add(List l, int i) {
  637. // INVALID extension point
  638. }
  639. public void addConfigured(Map m) {
  640. // Valid extension point
  641. assertTrue(m.size() == 0);
  642. }
  643. public void addConfigured(Hashtable h) {
  644. // Valid extension point, more derived than Map above, but *after* it!
  645. assertEquals(makeTable("key", "value"), h);
  646. }
  647. private Hashtable makeTable(Object key, Object value) {
  648. Hashtable table = new Hashtable();
  649. table.put(key, value);
  650. return table;
  651. }
  652. } // IntrospectionHelperTest