宏包装Java蚂蚁任务中的可选属性



在Java中实现的蚂蚁任务与XML蚂蚁宏相反,具有这种特殊性,即对于缺少属性提供了略有不同的行为。

就我而言,我正在尝试用宏包装在Java实现的<testng> ANT任务。具体来说,我想通过一些小调整来揭示测试蚂蚁任务提供的大多数功能。

除其他类似属性外,timeOut似乎很难复制,因为其遗漏的行为与指定和空字符串的行为不同。

这是我的宏定义的简化版本:

<macrodef name="my-wrapper">
  <attribute name="timeOut" default=""/>
  <element name="nested-elements" optional="true" implicit="true"/>
  <sequential>
    <testng timeOut="@{timeOut}">
      <nested-elements/>
    </testng>
  </sequential>
</macrodef>

失败了,因为Ant试图将值转换为整数:

Can't assign value '' to attribute timeout, reason: class java.lang.NumberFormatException with message 'For input string: ""'

我被建议使用<augment>,这似乎是解决此问题的解决方案。但是,我不明白应该如何使用它:

<macrodef name="my-wrapper">
  <attribute name="timeOut" default=""/>
  <element name="nested-elements" optional="true" implicit="true"/>
  <sequential>
    <augment unless:blank="timeOut" id="invocation" timeOut="@{timeOut}"/>
    <testng id="invocation">
      <nested-elements/>
    </testng>
  </sequential>
</macrodef>

以上因参考而失败:

java.lang.IllegalStateException: Unknown reference "invocation"

倒数<testng><augment>的顺序实际上行不通,因为<testng>任务开始执行增强。。

我需要的是一种有条件地将XML属性添加到任务调用的方法。这是否仅使用ANT XML语法?

在这种情况下,最简单的解决方案就是将timeOut的默认值设置为有效值。它期望一个可以作为整数解决的字符串,因此,如果您不希望有最大超时的情况,请尝试使用-1

<macrodef name="my-wrapper">
  <attribute name="timeOut" default="-1"/>
  <element name="nested-elements" optional="true" implicit="true"/>
  <sequential>
    <testng timeOut="@{timeOut}">
      <nested-elements/>
    </testng>
  </sequential>
</macrodef>

相关内容

最新更新