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 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import junit.framework.TestCase;
  56. import junit.framework.AssertionFailedError;
  57. import java.io.File;
  58. import java.util.*;
  59. /**
  60. * JUnit 3 testcases for org.apache.tools.ant.IntrospectionHelper.
  61. *
  62. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  63. */
  64. public class IntrospectionHelperTest extends TestCase {
  65. private Project p;
  66. public static boolean isUnixStyle = File.pathSeparatorChar == ':';
  67. public IntrospectionHelperTest(String name) {
  68. super(name);
  69. }
  70. public void setUp() {
  71. p = new Project();
  72. p.setBasedir("/tmp");
  73. }
  74. public void testAddText() throws BuildException {
  75. IntrospectionHelper ih = IntrospectionHelper.getHelper(java.lang.String.class);
  76. try {
  77. ih.addText(p, "", "test");
  78. fail("String doesn\'t support addText");
  79. } catch (BuildException be) {
  80. }
  81. ih = IntrospectionHelper.getHelper(getClass());
  82. ih.addText(p, this, "test");
  83. try {
  84. ih.addText(p, this, "test2");
  85. fail("test2 shouldn\'t be equal to test");
  86. } catch (BuildException be) {
  87. assert(be.getException() instanceof AssertionFailedError);
  88. }
  89. }
  90. public void testSupportsCharacters() {
  91. IntrospectionHelper ih = IntrospectionHelper.getHelper(java.lang.String.class);
  92. assert("String doesn\'t support addText", !ih.supportsCharacters());
  93. ih = IntrospectionHelper.getHelper(getClass());
  94. assert("IntrospectionHelperTest supports addText",
  95. ih.supportsCharacters());
  96. }
  97. public void addText(String text) {
  98. assertEquals("test", text);
  99. }
  100. public void testElementCreators() throws BuildException {
  101. IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
  102. try {
  103. ih.getElementType("one");
  104. fail("don't have element type one");
  105. } catch (BuildException be) {
  106. }
  107. try {
  108. ih.getElementType("two");
  109. fail("createTwo takes arguments");
  110. } catch (BuildException be) {
  111. }
  112. try {
  113. ih.getElementType("three");
  114. fail("createThree returns void");
  115. } catch (BuildException be) {
  116. }
  117. try {
  118. ih.getElementType("four");
  119. fail("createFour returns array");
  120. } catch (BuildException be) {
  121. }
  122. try {
  123. ih.getElementType("five");
  124. fail("createFive returns primitive type");
  125. } catch (BuildException be) {
  126. }
  127. assertEquals(java.lang.String.class, ih.getElementType("six"));
  128. assertEquals("test", ih.createElement(p, this, "six"));
  129. try {
  130. ih.getElementType("seven");
  131. fail("addSeven takes two arguments");
  132. } catch (BuildException be) {
  133. }
  134. try {
  135. ih.getElementType("eight");
  136. fail("addEight takes no arguments");
  137. } catch (BuildException be) {
  138. }
  139. try {
  140. ih.getElementType("nine");
  141. fail("nine return non void");
  142. } catch (BuildException be) {
  143. }
  144. try {
  145. ih.getElementType("ten");
  146. fail("addTen takes array argument");
  147. } catch (BuildException be) {
  148. }
  149. try {
  150. ih.getElementType("eleven");
  151. fail("addEleven takes primitive argument");
  152. } catch (BuildException be) {
  153. }
  154. try {
  155. ih.getElementType("twelve");
  156. fail("no primitive constructor for java.lang.Class");
  157. } catch (BuildException be) {
  158. }
  159. assertEquals(java.lang.StringBuffer.class, ih.getElementType("thirteen"));
  160. assertEquals("test", ih.createElement(p, this, "thirteen").toString());
  161. try {
  162. ih.createElement(p, this, "fourteen");
  163. fail("fourteen throws NullPointerException");
  164. } catch (BuildException be) {
  165. assert(be.getException() instanceof NullPointerException);
  166. }
  167. try {
  168. ih.createElement(p, this, "fourteen");
  169. fail("fifteen throws NullPointerException");
  170. } catch (BuildException be) {
  171. assert(be.getException() instanceof NullPointerException);
  172. }
  173. }
  174. public void testGetNestedElements() {
  175. Hashtable h = new Hashtable();
  176. h.put("six", java.lang.String.class);
  177. h.put("thirteen", java.lang.StringBuffer.class);
  178. h.put("fourteen", java.lang.StringBuffer.class);
  179. h.put("fifteen", java.lang.StringBuffer.class);
  180. IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
  181. Enumeration enum = ih.getNestedElements();
  182. while (enum.hasMoreElements()) {
  183. String name = (String) enum.nextElement();
  184. Class expect = (Class) h.get(name);
  185. assertNotNull("Support for "+name+" in IntrospectioNHelperTest?",
  186. expect);
  187. assertEquals("Return type of "+name, expect, ih.getElementType(name));
  188. h.remove(name);
  189. }
  190. assert("Found all", h.isEmpty());
  191. }
  192. public Object createTwo(String s) {
  193. return null;
  194. }
  195. public void createThree() {}
  196. public Object[] createFour() {
  197. return null;
  198. }
  199. public int createFive() {
  200. return 0;
  201. }
  202. public String createSix() {
  203. return "test";
  204. }
  205. public StringBuffer createFifteen() {
  206. throw new NullPointerException();
  207. }
  208. public void addSeven(String s, String s2) {}
  209. public void addEight() {}
  210. public String addNine(String s) {
  211. return null;
  212. }
  213. public void addTen(String[] s) {}
  214. public void addEleven(int i) {}
  215. public void addTwelve(Class c) {}
  216. public void addThirteen(StringBuffer sb) {
  217. sb.append("test");
  218. }
  219. public void addFourteen(StringBuffer s) {
  220. throw new NullPointerException();
  221. }
  222. public void testAttributeSetters() throws BuildException {
  223. IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
  224. try {
  225. ih.setAttribute(p, this, "one", "test");
  226. fail("setOne doesn't exist");
  227. } catch (BuildException be) {
  228. }
  229. try {
  230. ih.setAttribute(p, this, "two", "test");
  231. fail("setTwo returns non void");
  232. } catch (BuildException be) {
  233. }
  234. try {
  235. ih.setAttribute(p, this, "three", "test");
  236. fail("setThree takes no args");
  237. } catch (BuildException be) {
  238. }
  239. try {
  240. ih.setAttribute(p, this, "four", "test");
  241. fail("setFour takes two args");
  242. } catch (BuildException be) {
  243. }
  244. try {
  245. ih.setAttribute(p, this, "five", "test");
  246. fail("setFive takes array arg");
  247. } catch (BuildException be) {
  248. }
  249. try {
  250. ih.setAttribute(p, this, "six", "test");
  251. fail("Project doesn't have a String constructor");
  252. } catch (BuildException be) {
  253. }
  254. ih.setAttribute(p, this, "seven", "2");
  255. try {
  256. ih.setAttribute(p, this, "seven", "3");
  257. fail("2 shouldn't be equals to three");
  258. } catch (BuildException be) {
  259. assert(be.getException() instanceof AssertionFailedError);
  260. }
  261. ih.setAttribute(p, this, "eight", "2");
  262. try {
  263. ih.setAttribute(p, this, "eight", "3");
  264. fail("2 shouldn't be equals to three - as int");
  265. } catch (BuildException be) {
  266. assert(be.getException() instanceof AssertionFailedError);
  267. }
  268. ih.setAttribute(p, this, "nine", "2");
  269. try {
  270. ih.setAttribute(p, this, "nine", "3");
  271. fail("2 shouldn't be equals to three - as Integer");
  272. } catch (BuildException be) {
  273. assert(be.getException() instanceof AssertionFailedError);
  274. }
  275. ih.setAttribute(p, this, "ten", "2");
  276. try {
  277. ih.setAttribute(p, this, "ten", "3");
  278. fail("/tmp/2 shouldn't be equals to /tmp/3");
  279. } catch (BuildException be) {
  280. assert(be.getException() instanceof AssertionFailedError);
  281. }
  282. ih.setAttribute(p, this, "eleven", "2");
  283. try {
  284. ih.setAttribute(p, this, "eleven", "on");
  285. fail("on shouldn't be false");
  286. } catch (BuildException be) {
  287. assert(be.getException() instanceof AssertionFailedError);
  288. }
  289. ih.setAttribute(p, this, "twelve", "2");
  290. try {
  291. ih.setAttribute(p, this, "twelve", "on");
  292. fail("on shouldn't be false");
  293. } catch (BuildException be) {
  294. assert(be.getException() instanceof AssertionFailedError);
  295. }
  296. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
  297. try {
  298. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper");
  299. fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
  300. } catch (BuildException be) {
  301. assert(be.getException() instanceof AssertionFailedError);
  302. }
  303. try {
  304. ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2");
  305. fail("org.apache.tools.ant.Project2 doesn't exist");
  306. } catch (BuildException be) {
  307. assert(be.getException() instanceof ClassNotFoundException);
  308. }
  309. ih.setAttribute(p, this, "fourteen", "2");
  310. try {
  311. ih.setAttribute(p, this, "fourteen", "on");
  312. fail("2 shouldn't be equals to three - as StringBuffer");
  313. } catch (BuildException be) {
  314. assert(be.getException() instanceof AssertionFailedError);
  315. }
  316. ih.setAttribute(p, this, "fifteen", "abcd");
  317. try {
  318. ih.setAttribute(p, this, "fifteen", "on");
  319. fail("o shouldn't be equal to a");
  320. } catch (BuildException be) {
  321. assert(be.getException() instanceof AssertionFailedError);
  322. }
  323. ih.setAttribute(p, this, "sixteen", "abcd");
  324. try {
  325. ih.setAttribute(p, this, "sixteen", "on");
  326. fail("o shouldn't be equal to a");
  327. } catch (BuildException be) {
  328. assert(be.getException() instanceof AssertionFailedError);
  329. }
  330. }
  331. public void testGetAttributes() {
  332. Hashtable h = new Hashtable();
  333. h.put("seven", java.lang.String.class);
  334. h.put("eight", java.lang.Integer.TYPE);
  335. h.put("nine", java.lang.Integer.class);
  336. h.put("ten", java.io.File.class);
  337. h.put("eleven", java.lang.Boolean.TYPE);
  338. h.put("twelve", java.lang.Boolean.class);
  339. h.put("thirteen", java.lang.Class.class);
  340. h.put("fourteen", java.lang.StringBuffer.class);
  341. h.put("fifteen", java.lang.Character.TYPE);
  342. h.put("sixteen", java.lang.Character.class);
  343. /*
  344. * JUnit 3.7 adds a getName method to TestCase - so we now
  345. * have a name attribute in IntrospectionHelperTest if we run
  346. * under JUnit 3.7 but not in earlier versions.
  347. *
  348. * Simply add it here and remove it after the tests.
  349. */
  350. h.put("name", java.lang.String.class);
  351. IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
  352. Enumeration enum = ih.getAttributes();
  353. while (enum.hasMoreElements()) {
  354. String name = (String) enum.nextElement();
  355. Class expect = (Class) h.get(name);
  356. assertNotNull("Support for "+name+" in IntrospectionHelperTest?",
  357. expect);
  358. assertEquals("Type of "+name, expect, ih.getAttributeType(name));
  359. h.remove(name);
  360. }
  361. h.remove("name");
  362. assert("Found all", h.isEmpty());
  363. }
  364. public int setTwo(String s) {
  365. return 0;
  366. }
  367. public void setThree() {}
  368. public void setFour(String s1, String s2) {}
  369. public void setFive(String[] s) {}
  370. public void setSix(Project p) {}
  371. public void setSeven(String s) {
  372. assertEquals("2", s);
  373. }
  374. public void setEight(int i) {
  375. assertEquals(2, i);
  376. }
  377. public void setNine(Integer i) {
  378. assertEquals(2, i.intValue());
  379. }
  380. public void setTen(File f) {
  381. if (isUnixStyle) {
  382. assertEquals("/tmp/2", f.getAbsolutePath());
  383. } else if (System.getProperty("os.name").toLowerCase().equals("netware")) {
  384. assertEquals("\\tmp\\2", f.getAbsolutePath().toLowerCase());
  385. } else {
  386. assertEquals(":\\tmp\\2", f.getAbsolutePath().toLowerCase().substring(1));
  387. }
  388. }
  389. public void setEleven(boolean b) {
  390. assert(!b);
  391. }
  392. public void setTwelve(Boolean b) {
  393. assert(!b.booleanValue());
  394. }
  395. public void setThirteen(Class c) {
  396. assertEquals(Project.class, c);
  397. }
  398. public void setFourteen(StringBuffer sb) {
  399. assertEquals("2", sb.toString());
  400. }
  401. public void setFifteen(char c) {
  402. assertEquals(c, 'a');
  403. }
  404. public void setSixteen(Character c) {
  405. assertEquals(c.charValue(), 'a');
  406. }
  407. }// IntrospectionHelperTest