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.

UnknownElementTest.java 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 org.junit.Before;
  20. import org.junit.Ignore;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertTrue;
  27. import static org.junit.Assert.assertNotNull;
  28. public class UnknownElementTest {
  29. @Rule
  30. public BuildFileRule buildRule = new BuildFileRule();
  31. @Before
  32. public void setUp() {
  33. buildRule.configureProject("src/etc/testcases/core/unknownelement.xml");
  34. }
  35. @Test
  36. public void testMaybeConfigure() {
  37. // make sure we do not get a NPE
  38. buildRule.executeTarget("testMaybeConfigure");
  39. }
  40. /**
  41. * Not really a UnknownElement test but rather one of "what
  42. * information is available in taskFinished".
  43. * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=26197">
  44. * https://issues.apache.org/bugzilla/show_bug.cgi?id=26197</a>
  45. */
  46. @Test
  47. @Ignore("Previously disabled through naming convention")
  48. public void XtestTaskFinishedEvent() {
  49. buildRule.getProject().addBuildListener(new BuildListener() {
  50. public void buildStarted(BuildEvent event) {
  51. }
  52. public void buildFinished(BuildEvent event) {
  53. }
  54. public void targetStarted(BuildEvent event) {
  55. }
  56. public void targetFinished(BuildEvent event) {
  57. }
  58. public void taskStarted(BuildEvent event) {
  59. assertTaskProperties(event.getTask());
  60. }
  61. public void taskFinished(BuildEvent event) {
  62. assertTaskProperties(event.getTask());
  63. }
  64. public void messageLogged(BuildEvent event) {
  65. }
  66. private void assertTaskProperties(Task ue) {
  67. assertNotNull(ue);
  68. assertTrue(ue instanceof UnknownElement);
  69. Task t = ((UnknownElement) ue).getTask();
  70. assertNotNull(t);
  71. assertEquals("org.apache.tools.ant.taskdefs.Echo",
  72. t.getClass().getName());
  73. }
  74. });
  75. buildRule.executeTarget("echo");
  76. }
  77. public static class Child extends Task {
  78. Parent parent;
  79. public void injectParent(Parent parent) {
  80. this.parent = parent;
  81. }
  82. public void execute() {
  83. parent.fromChild();
  84. }
  85. }
  86. public static class Parent extends Task implements TaskContainer {
  87. List<Task> children = new ArrayList<>();
  88. public void addTask(Task t) {
  89. children.add(t);
  90. }
  91. public void fromChild() {
  92. log("fromchild");
  93. }
  94. public void execute() {
  95. for (Task task : children) {
  96. UnknownElement el = (UnknownElement) task;
  97. el.maybeConfigure();
  98. Child child = (Child) el.getRealThing();
  99. child.injectParent(this);
  100. child.perform();
  101. }
  102. }
  103. }
  104. }