PageRenderTime 170ms CodeModel.GetById 82ms RepoModel.GetById 2ms app.codeStats 0ms

/src/test/java/net/ggtools/grand/ant/AntProjectTest.java

https://github.com/Ant-Grand/Grand
Java | 353 lines | 220 code | 37 blank | 96 comment | 3 complexity | 73230d2bd3c014d68d5504a226939b2b MD5 | raw file
Possible License(s): BSD-2-Clause
  1. // $Id$
  2. /*
  3. * ====================================================================
  4. * Copyright (c) 2002-2003, Christophe Labouisse All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package net.ggtools.grand.ant;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertFalse;
  31. import static org.junit.Assert.assertNotNull;
  32. import static org.junit.Assert.assertNull;
  33. import static org.junit.Assert.assertTrue;
  34. import static org.junit.Assert.fail;
  35. import java.io.File;
  36. import java.util.Collection;
  37. import java.util.Iterator;
  38. import java.util.NoSuchElementException;
  39. import org.junit.Before;
  40. import org.junit.Test;
  41. import net.ggtools.grand.exceptions.GrandException;
  42. import net.ggtools.grand.graph.Graph;
  43. import net.ggtools.grand.graph.Link;
  44. import net.ggtools.grand.graph.Node;
  45. import net.ggtools.grand.utils.AbstractAntTester;
  46. /**
  47. * @author Christophe Labouisse
  48. */
  49. public class AntProjectTest extends AbstractAntTester {
  50. /**
  51. * Field antProject.
  52. */
  53. protected AntProject antProject;
  54. /**
  55. * Field graph.
  56. */
  57. protected Graph graph;
  58. /**
  59. * Method testAnt.
  60. */
  61. @Test
  62. public final void testAnt() {
  63. // Test without antfile nor dir
  64. AntTargetNode node = (AntTargetNode) graph.getNode("ant-test");
  65. AntLink link = (AntLink) node.getLinks().iterator().next();
  66. assertNotNull("should have found a link", link);
  67. AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  68. assertEquals("Target", "[gruik]", endNode.getName());
  69. final String buildFile = new File(project.getBaseDir(), "build.xml").getAbsolutePath();
  70. assertEquals("Build file should be build.xml in the current dir", buildFile,
  71. endNode.getBuildFile());
  72. // Test with antfile & dir set
  73. node = (AntTargetNode) graph.getNode("ant-with-file-test");
  74. link = (AntLink) node.getLinks().iterator().next();
  75. assertNotNull("should have found a link", link);
  76. endNode = (AntTargetNode) link.getEndNode();
  77. assertEquals("Target", "[gabuzo]", endNode.getName());
  78. // TODO is this a misfeature of Windows or Ant?
  79. File referenceFile = System.getProperty("os.version").startsWith("Windows")
  80. || System.getProperty("os.name").startsWith("Windows")
  81. ? new File(project.getBaseDir(), "/gruik/gruik.xml") : new File("/gruik/gruik.xml");
  82. assertEquals("Build file", referenceFile.getAbsolutePath(),
  83. endNode.getBuildFile());
  84. }
  85. /**
  86. * Method testAntCall.
  87. */
  88. @Test
  89. public final void testAntCall() {
  90. final AntTargetNode node = (AntTargetNode) graph.getNode("antcall-test");
  91. AntLink link = null;
  92. try {
  93. link = (AntLink) node.getLinks().iterator().next();
  94. } catch (final NoSuchElementException e) {
  95. fail("Should have found a link");
  96. }
  97. assertNotNull("should have found a link", link);
  98. assertEquals("Target", "gruik", link.getEndNode().getName());
  99. final AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  100. assertEquals("Target", "gruik", endNode.getName());
  101. assertNull("Build file", endNode.getBuildFile());
  102. }
  103. /**
  104. * Method testAntCallWithTargetElements.
  105. */
  106. @Test
  107. public final void testAntCallWithTargetElements() {
  108. final AntTargetNode node = (AntTargetNode) graph.getNode("antcall-with-target-elements-test");
  109. final Collection<Link> links = node.getLinks();
  110. assertNotNull("Links should not be null", links);
  111. final Iterator<Link> iterator = links.iterator();
  112. AntLink link = (AntLink) iterator.next();
  113. assertNotNull("should have found a link", link);
  114. AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  115. assertEquals("Target", "gruik", endNode.getName());
  116. assertNull("Build file", endNode.getBuildFile());
  117. link = (AntLink) iterator.next();
  118. assertNotNull("should have found two links", link);
  119. endNode = (AntTargetNode) link.getEndNode();
  120. assertEquals("Target", "gabuzo", endNode.getName());
  121. assertNull("Build file", endNode.getBuildFile());
  122. assertFalse("There should be target after the second one", iterator.hasNext());
  123. }
  124. /**
  125. * Run a graph on a file including an antcall whose target is based on a
  126. * property.
  127. */
  128. @Test
  129. public final void testAntCallWithUndefinedProperty() {
  130. expectLogContaining("ant-call-with-property", "Outputting to ");
  131. assertLogContaining("Target antcall-props-1 has dependency to non existent target ${antcall.target}, creating a dummy node");
  132. assertLogContaining("Target antcall-props-2 has dependency to non existent target do-${antcall.target}, creating a dummy node");
  133. }
  134. /**
  135. * Run a graph on a file including an undefined task.
  136. */
  137. @Test
  138. public final void testAntCallWithUndefinedTask() {
  139. // TODO check if this test is useful.
  140. expectLogContaining("undefined-task", "Outputting to ");
  141. }
  142. /**
  143. * Method testAntNoTargetDifferentFile.
  144. */
  145. @Test
  146. public final void testAntNoTargetDifferentFile() {
  147. // Test without target different file
  148. final AntTargetNode node = (AntTargetNode) graph.getNode("ant-without-target-with-file-test");
  149. final AntLink link = (AntLink) node.getLinks().iterator().next();
  150. assertNotNull("should have found a link", link);
  151. final AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  152. assertEquals("Target", "['default']", endNode.getName());
  153. // TODO is this a misfeature of Windows or Ant?
  154. File referenceFile = System.getProperty("os.version").startsWith("Windows")
  155. || System.getProperty("os.name").startsWith("Windows")
  156. ? new File(project.getBaseDir(), "/gruik/gruik.xml") : new File("/gruik/gruik.xml");
  157. assertEquals("Build file", referenceFile.getAbsolutePath(),
  158. endNode.getBuildFile());
  159. }
  160. /**
  161. * Method testAntNoTargetSameFile.
  162. */
  163. @Test
  164. public final void testAntNoTargetSameFile() {
  165. // Test without target same file
  166. final AntTargetNode node = (AntTargetNode) graph.getNode("ant-without-target-test");
  167. final AntLink link = (AntLink) node.getLinks().iterator().next();
  168. assertNotNull("should have found a link", link);
  169. final AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  170. assertEquals("Should be the default target", "init", endNode.getName());
  171. assertNull("Build file should be the currentFile", endNode.getBuildFile());
  172. }
  173. /**
  174. * Method testAntWithTargetElements.
  175. */
  176. @Test
  177. public final void testAntWithTargetElements() {
  178. final AntTargetNode node = (AntTargetNode) graph.getNode("ant-with-target-elements-test");
  179. final Collection<Link> links = node.getLinks();
  180. assertNotNull("Links should not be null", links);
  181. final Iterator<Link> iterator = links.iterator();
  182. AntLink link = (AntLink) iterator.next();
  183. assertNotNull("should have found a link", link);
  184. AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  185. assertEquals("Target", "[gruik]", endNode.getName());
  186. String buildFile = new File(project.getBaseDir(), "build.xml").getAbsolutePath();
  187. assertEquals("Build file should be build.xml in the current dir", buildFile, endNode
  188. .getBuildFile());
  189. link = (AntLink) iterator.next();
  190. assertNotNull("should have found two links", link);
  191. endNode = (AntTargetNode) link.getEndNode();
  192. assertEquals("Target", "[gabuzo (2)]", endNode.getName());
  193. buildFile = new File(project.getBaseDir(), "build.xml").getAbsolutePath();
  194. assertEquals("Build file should be build.xml in the current dir", buildFile, endNode
  195. .getBuildFile());
  196. assertFalse("There should be target after the second one", iterator.hasNext());
  197. }
  198. /**
  199. * Method testIfCondition.
  200. */
  201. @Test
  202. public final void testIfCondition() {
  203. final AntTargetNode node = (AntTargetNode) graph.getNode("if-cond-test");
  204. assertNotNull("if-cond-test Node", node);
  205. assertEquals("If condition for target if-cond-test", "test-if-condition", node
  206. .getIfCondition());
  207. }
  208. /**
  209. * Test if the nested tasks are found (ticket #29).
  210. */
  211. @Test
  212. public final void testNestedAnt() {
  213. final AntTargetNode node = (AntTargetNode) graph.getNode("[nested-missing-node]");
  214. assertNotNull("nested-missing-node not found", node);
  215. assertTrue("nested-missing-node has MISSING_NODE_ATTR set", node
  216. .hasAttributes(Node.ATTR_MISSING_NODE));
  217. }
  218. /**
  219. * Method testRunTarget.
  220. */
  221. @Test
  222. public final void testRunTarget() {
  223. final AntTargetNode node = (AntTargetNode) graph.getNode("runtarget-test");
  224. AntLink link = null;
  225. try {
  226. link = (AntLink) node.getLinks().iterator().next();
  227. } catch (final NoSuchElementException e) {
  228. fail("Should have found a link");
  229. }
  230. assertNotNull("should have found a link", link);
  231. assertEquals("Target", "gruik", link.getEndNode().getName());
  232. final AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  233. assertEquals("Target", "gruik", endNode.getName());
  234. assertNull("Build file", endNode.getBuildFile());
  235. }
  236. /**
  237. * Test the subant task.
  238. */
  239. @Test
  240. public final void testSubant() {
  241. // Test for genericantfile.
  242. AntTargetNode node = (AntTargetNode) graph.getNode("subant-generic");
  243. Collection<Link> links = node.getLinks();
  244. assertEquals("Should have found 1 link", 1, links.size());
  245. AntLink link = (AntLink) links.iterator().next();
  246. assertNotNull("should have found a link", link);
  247. AntTargetNode endNode = (AntTargetNode) link.getEndNode();
  248. assertEquals("Target", "subant-generic-target", endNode.getName());
  249. assertNull("Build file should be the currentFile", endNode.getBuildFile());
  250. // Test with antfile & dir set
  251. node = (AntTargetNode) graph.getNode("subant-antfile");
  252. links = node.getLinks();
  253. assertEquals("Should have found 2 links", 2, links.size());
  254. final Iterator<Link> iterator = links.iterator();
  255. link = (AntLink) iterator.next();
  256. assertNotNull("should have found a link", link);
  257. endNode = (AntTargetNode) link.getEndNode();
  258. assertEquals("Target", "[init]", endNode.getName());
  259. String buildFile = new File(project.getBaseDir(), "subant-1/build.xml").getAbsolutePath();
  260. assertEquals("Build file should be build.xml in the subant-1 dir", buildFile, endNode
  261. .getBuildFile());
  262. link = (AntLink) iterator.next();
  263. assertNotNull("should have found a link", link);
  264. endNode = (AntTargetNode) link.getEndNode();
  265. assertEquals("Target", "[target]", endNode.getName());
  266. buildFile = new File(project.getBaseDir(), "subant-2/build.xml").getAbsolutePath();
  267. assertEquals("Build file should be build.xml in the subant-2 dir", buildFile, endNode
  268. .getBuildFile());
  269. // Test with property nested elements.
  270. node = (AntTargetNode) graph.getNode("subant-withproperties");
  271. links = node.getLinks();
  272. assertEquals("Should have found 2 links", 2, links.size());
  273. for (Link link2 : links) {
  274. final AntTaskLink subLink = (AntTaskLink) link2;
  275. assertEquals("A parameter call ga=meu should be defined",
  276. "bu", subLink.getParameter("ga"));
  277. }
  278. }
  279. /**
  280. * Method testUnlessCondition.
  281. */
  282. @Test
  283. public final void testUnlessCondition() {
  284. final AntTargetNode node = (AntTargetNode) graph.getNode("unless-cond-test");
  285. assertNotNull("unless-cond-test Node", node);
  286. assertEquals("Unless condition for target unless-cond-test",
  287. "test-unless-condition", node.getUnlessCondition());
  288. }
  289. /**
  290. * Method getTestBuildFileName.
  291. * @return String
  292. */
  293. protected final String getTestBuildFileName() {
  294. return TESTCASES_DIR + "ant-project-test.xml";
  295. }
  296. /**
  297. * Method setUp.
  298. */
  299. @Before
  300. public final void setUp() {
  301. configureProject(getTestBuildFileName());
  302. project.setBasedir(TESTCASES_DIR);
  303. createGraph();
  304. }
  305. /**
  306. * Method createGraph.
  307. */
  308. protected void createGraph() {
  309. antProject = new AntProject(project);
  310. try {
  311. graph = antProject.getGraph();
  312. } catch (final GrandException e) {
  313. fail("Got exception while creating graph: " + e.getMessage());
  314. }
  315. }
  316. }