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

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.util.Enumeration;
  25. import java.util.HashMap;
  26. import java.util.Hashtable;
  27. import java.util.List;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import org.apache.tools.ant.taskdefs.condition.Os;
  31. import org.junit.Before;
  32. import org.junit.ComparisonFailure;
  33. import org.junit.Ignore;
  34. import org.junit.Test;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.junit.Assert.assertFalse;
  37. import static org.junit.Assert.assertNotNull;
  38. import static org.junit.Assert.assertTrue;
  39. import static org.junit.Assert.fail;
  40. /**
  41. * JUnit testcases for org.apache.tools.ant.IntrospectionHelper.
  42. *
  43. */
  44. public class IntrospectionHelperTest {
  45. private Project p;
  46. private IntrospectionHelper ih;
  47. private static final String projectBasedir = File.separator;
  48. @Before
  49. public void setUp() {
  50. p = new Project();
  51. p.setBasedir(projectBasedir);
  52. ih = IntrospectionHelper.getHelper(getClass());
  53. }
  54. @Test
  55. public void testIsDynamic() {
  56. assertFalse("Not dynamic", ih.isDynamic());
  57. }
  58. @Test
  59. public void testIsContainer() {
  60. assertFalse("Not a container", ih.isContainer());
  61. }
  62. @Test
  63. public void testAddText() throws BuildException {
  64. ih.addText(p, this, "test");
  65. try {
  66. ih.addText(p, this, "test2");
  67. fail("test2 shouldn\'t be equal to test");
  68. } catch (BuildException be) {
  69. assertTrue(be.getCause() instanceof ComparisonFailure);
  70. }
  71. ih = IntrospectionHelper.getHelper(String.class);
  72. try {
  73. ih.addText(p, "", "test");
  74. fail("String doesn\'t support addText");
  75. } catch (BuildException be) {
  76. //TODO the value should be asserted
  77. }
  78. }
  79. @Test
  80. @Ignore("This silently ignores a build exception")
  81. public void testGetAddTextMethod() {
  82. Method m = ih.getAddTextMethod();
  83. assertMethod(m, "addText", String.class, "test", "bing!");
  84. ih = IntrospectionHelper.getHelper(String.class);
  85. try {
  86. m = ih.getAddTextMethod();
  87. } catch (BuildException e) {
  88. }
  89. }
  90. @Test
  91. public void testSupportsCharacters() {
  92. assertTrue("IntrospectionHelperTest supports addText",
  93. ih.supportsCharacters());
  94. ih = IntrospectionHelper.getHelper(String.class);
  95. assertFalse("String doesn\'t support addText", ih.supportsCharacters());
  96. }
  97. public void addText(String text) {
  98. assertEquals("test", text);
  99. }
  100. @Test
  101. public void testElementCreators() throws BuildException {
  102. try {
  103. ih.getElementType("one");
  104. fail("don't have element type one");
  105. } catch (BuildException be) {
  106. //TODO we should be asserting a value in here
  107. }
  108. try {
  109. ih.getElementType("two");
  110. fail("createTwo takes arguments");
  111. } catch (BuildException be) {
  112. //TODO we should be asserting a value in here
  113. }
  114. try {
  115. ih.getElementType("three");
  116. fail("createThree returns void");
  117. } catch (BuildException be) {
  118. //TODO we should be asserting a value in here
  119. }
  120. try {
  121. ih.getElementType("four");
  122. fail("createFour returns array");
  123. } catch (BuildException be) {
  124. //TODO we should be asserting a value in here
  125. }
  126. try {
  127. ih.getElementType("five");
  128. fail("createFive returns primitive type");
  129. } catch (BuildException be) {
  130. //TODO we should be asserting a value in here
  131. }
  132. assertEquals(String.class, ih.getElementType("six"));
  133. assertEquals("test", ih.createElement(p, this, "six"));
  134. try {
  135. ih.getElementType("seven");
  136. fail("addSeven takes two arguments");
  137. } catch (BuildException be) {
  138. //TODO we should be asserting a value in here
  139. }
  140. try {
  141. ih.getElementType("eight");
  142. fail("addEight takes no arguments");
  143. } catch (BuildException be) {
  144. //TODO we should be asserting a value in here
  145. }
  146. try {
  147. ih.getElementType("nine");
  148. fail("nine return non void");
  149. } catch (BuildException be) {
  150. //TODO we should be asserting a value in here
  151. }
  152. try {
  153. ih.getElementType("ten");
  154. fail("addTen takes array argument");
  155. } catch (BuildException be) {
  156. //TODO we should be asserting a value in here
  157. }
  158. try {
  159. ih.getElementType("eleven");
  160. fail("addEleven takes primitive argument");
  161. } catch (BuildException be) {
  162. //TODO we should be asserting a value in here
  163. }
  164. try {
  165. ih.getElementType("twelve");
  166. fail("no primitive constructor for java.lang.Class");
  167. } catch (BuildException be) {
  168. //TODO we should be asserting a value in here
  169. }
  170. assertEquals(StringBuffer.class, ih.getElementType("thirteen"));
  171. assertEquals("test", ih.createElement(p, this, "thirteen").toString());
  172. try {
  173. ih.createElement(p, this, "fourteen");
  174. fail("fourteen throws NullPointerException");
  175. } catch (BuildException be) {
  176. assertTrue(be.getCause() instanceof NullPointerException);
  177. }
  178. try {
  179. ih.createElement(p, this, "fourteen");
  180. fail("fifteen throws NullPointerException");
  181. } catch (BuildException be) {
  182. assertTrue(be.getCause() instanceof NullPointerException);
  183. }
  184. }
  185. private Map<String, Class<?>> getExpectedNestedElements() {
  186. Map<String, Class<?>> elemMap = new Hashtable<>();
  187. elemMap.put("six", String.class);
  188. elemMap.put("thirteen", StringBuffer.class);
  189. elemMap.put("fourteen", StringBuffer.class);
  190. elemMap.put("fifteen", StringBuffer.class);
  191. return elemMap;
  192. }
  193. @Test
  194. public void testGetNestedElements() {
  195. Map<String, Class<?>> elemMap = getExpectedNestedElements();
  196. Enumeration e = ih.getNestedElements();
  197. while (e.hasMoreElements()) {
  198. String name = (String) e.nextElement();
  199. Class expect = elemMap.get(name);
  200. assertNotNull("Support for "+name+" in IntrospectioNHelperTest?",
  201. expect);
  202. assertEquals("Return type of " + name, expect, ih.getElementType(name));
  203. elemMap.remove(name);
  204. }
  205. assertTrue("Found all", elemMap.isEmpty());
  206. }
  207. @Test
  208. public void testGetNestedElementMap() {
  209. Map<String, Class<?>> elemMap = getExpectedNestedElements();
  210. Map<String, Class<?>> actualMap = ih.getNestedElementMap();
  211. for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
  212. String elemName = entry.getKey();
  213. Class<?> elemClass = 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<String, Class<?>> getExpectedAttributes() {
  421. Map<String, Class<?>> 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. attrMap.put("twenty", Path.class);
  436. /*
  437. * JUnit 3.7 adds a getName method to TestCase - so we now
  438. * have a name attribute in IntrospectionHelperTest if we run
  439. * under JUnit 3.7 but not in earlier versions.
  440. *
  441. * Simply add it here and remove it after the tests.
  442. */
  443. attrMap.put("name", String.class);
  444. return attrMap;
  445. }
  446. @Test
  447. public void testGetAttributes() {
  448. Map<String, Class<?>> attrMap = getExpectedAttributes();
  449. Enumeration<String> e = ih.getAttributes();
  450. while (e.hasMoreElements()) {
  451. String name = e.nextElement();
  452. Class<?> expect = attrMap.get(name);
  453. assertNotNull("Support for " + name + " in IntrospectionHelperTest?",
  454. expect);
  455. assertEquals("Type of " + name, expect, ih.getAttributeType(name));
  456. attrMap.remove(name);
  457. }
  458. attrMap.remove("name");
  459. assertTrue("Found all", attrMap.isEmpty());
  460. }
  461. @Test
  462. public void testGetAttributeMap() {
  463. Map<String, Class<?>> attrMap = getExpectedAttributes();
  464. Map<String, Class<?>> actualMap = ih.getAttributeMap();
  465. for (Map.Entry<String, Class<?>> entry : actualMap.entrySet()) {
  466. String attrName = entry.getKey();
  467. Class<?> attrClass = 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. 2, 3);
  488. assertAttrMethod("nine", "setNine", Integer.class,
  489. 2, 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. 'a', 'b');
  502. assertAttrMethod("sixteen", "setSixteen", Character.class,
  503. 'a', 'b');
  504. assertAttrMethod("seventeen", "setSeventeen", Byte.TYPE,
  505. (byte) 17, (byte) 10);
  506. assertAttrMethod("eightteen", "setEightteen", Short.TYPE,
  507. (short) 18, (short) 10);
  508. assertAttrMethod("nineteen", "setNineteen", Double.TYPE,
  509. 19d, (double) (short) 10);
  510. assertAttrMethod("twenty", "setTwenty", Path.class,
  511. new File(projectBasedir + 20).toPath(), Paths.get("toto"));
  512. try {
  513. assertAttrMethod("onehundred", null, null, null, null);
  514. fail("Should have raised a BuildException!");
  515. } catch (BuildException e) {
  516. //TODO we should be asserting a value in here
  517. }
  518. }
  519. private void assertAttrMethod(String attrName, String methodName,
  520. Class<?> methodArg, Object arg, Object badArg) {
  521. Method m = ih.getAttributeMethod(attrName);
  522. assertMethod(m, methodName, methodArg, arg, badArg);
  523. }
  524. public int setTwo(String s) {
  525. return 0;
  526. }
  527. public void setThree() {
  528. }
  529. public void setFour(String s1, String s2) {
  530. }
  531. public void setFive(String[] s) {
  532. }
  533. public void setSix(Project p) {
  534. }
  535. public void setSeven(String s) {
  536. assertEquals("2", s);
  537. }
  538. public void setEight(int i) {
  539. assertEquals(2, i);
  540. }
  541. public void setNine(Integer i) {
  542. assertEquals(2, i.intValue());
  543. }
  544. public void setTen(File f) {
  545. String path = f.getAbsolutePath();
  546. if (Os.isFamily("unix") || Os.isFamily("openvms")) {
  547. assertEquals(projectBasedir + "2", path);
  548. } else if (Os.isFamily("netware")) {
  549. assertEquals(projectBasedir + "2", path.toLowerCase(Locale.US));
  550. } else {
  551. assertEquals(":" + projectBasedir + "2",
  552. path.toLowerCase(Locale.US).substring(1));
  553. }
  554. }
  555. public void setEleven(boolean b) {
  556. assertFalse(b);
  557. }
  558. public void setTwelve(Boolean b) {
  559. assertFalse(b);
  560. }
  561. public void setThirteen(Class<?> c) {
  562. assertEquals(Project.class, c);
  563. }
  564. public void setFourteen(StringBuffer sb) {
  565. assertEquals("2", sb.toString());
  566. }
  567. public void setFifteen(char c) {
  568. assertEquals(c, 'a');
  569. }
  570. public void setSixteen(Character c) {
  571. assertEquals(c.charValue(), 'a');
  572. }
  573. public void setSeventeen(byte b) {
  574. assertEquals(17, b);
  575. }
  576. public void setEightteen(short s) {
  577. assertEquals(18, s);
  578. }
  579. public void setNineteen(double d) {
  580. double diff = d - 19;
  581. assertTrue("Expected 19, received " + d, diff > -1e-6 && diff < 1e-6);
  582. }
  583. public void setTwenty(Path p) {
  584. String path = p.toAbsolutePath().toString();
  585. if (Os.isFamily("unix") || Os.isFamily("openvms")) {
  586. assertEquals(projectBasedir + "20", path);
  587. } else if (Os.isFamily("netware")) {
  588. assertEquals(projectBasedir + "20", path.toLowerCase(Locale.US));
  589. } else {
  590. assertEquals(":" + projectBasedir + "20",
  591. path.toLowerCase(Locale.US).substring(1));
  592. }
  593. }
  594. @Test
  595. public void testGetExtensionPoints() {
  596. List<Method> extensions = ih.getExtensionPoints();
  597. final int adders = 2;
  598. assertEquals("extension count", adders, extensions.size());
  599. // this original test assumed something about the order of
  600. // add(Number) and addConfigured(Map) returned by reflection.
  601. // Unfortunately the assumption doesn't hold for all VMs
  602. // (failed on MacOS X using JDK 1.4.2_05) and the possible
  603. // combinatorics are too hard to check. We really only want
  604. // to ensure that the more derived Hashtable can be found
  605. // before Map.
  606. // assertMethod(extensions.get(0), "add", Number.class,
  607. // new Integer(2), new Integer(3));
  608. // addConfigured(Hashtable) should come before addConfigured(Map)
  609. assertMethod(extensions.get(adders - 2),
  610. "addConfigured", Hashtable.class,
  611. makeTable("key", "value"), makeTable("1", "2"));
  612. assertMethod(extensions.get(adders - 1), "addConfigured", Map.class,
  613. new HashMap<String, String>(), makeTable("1", "2"));
  614. }
  615. private void assertMethod(Method m, String methodName, Class<?> methodArg,
  616. Object arg, Object badArg) {
  617. assertEquals("Method name", methodName, m.getName());
  618. assertEquals("Return type", Void.TYPE, m.getReturnType());
  619. Class<?>[] args = m.getParameterTypes();
  620. assertEquals("Arg Count", 1, args.length);
  621. assertEquals("Arg Type", methodArg, args[0]);
  622. try {
  623. m.invoke(this, arg);
  624. } catch (IllegalAccessException | InvocationTargetException e) {
  625. throw new BuildException(e);
  626. }
  627. try {
  628. m.invoke(this, badArg);
  629. fail("Should have raised an assertion exception");
  630. } catch (IllegalAccessException e) {
  631. throw new BuildException(e);
  632. } catch (InvocationTargetException e) {
  633. Throwable t = e.getTargetException();
  634. assertTrue(t.toString(), t instanceof AssertionError);
  635. }
  636. }
  637. public List<Object> add(List l) {
  638. // INVALID extension point
  639. return null;
  640. }
  641. // see comments in testGetExtensionPoints
  642. // public void add(Number n) {
  643. // // Valid extension point
  644. // assertEquals(2, n.intValue());
  645. // }
  646. public void add(List<Object> l, int i) {
  647. // INVALID extension point
  648. }
  649. public void addConfigured(Map<Object, Object> m) {
  650. // Valid extension point
  651. assertTrue(m.isEmpty());
  652. }
  653. public void addConfigured(Hashtable<String, String> h) {
  654. // Valid extension point, more derived than Map above, but *after* it!
  655. assertEquals(makeTable("key", "value"), h);
  656. }
  657. private Hashtable<String, String> makeTable(String key, String value) {
  658. Hashtable<String, String> table = new Hashtable<>();
  659. table.put(key, value);
  660. return table;
  661. }
  662. } // IntrospectionHelperTest