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

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