PR 56470 - merge messages in assertLogContains only when asked to
git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@1592859 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/docs/assertions.html b/docs/assertions.html
index e56d04f..19db1b3 100644
--- a/docs/assertions.html
+++ b/docs/assertions.html
@@ -918,6 +918,13 @@
levels. One of "error", "warning", "info", "verbose", "debug".</td>
<td valign="top" align="center">No</td>
</tr>
+ <tr>
+ <td valign="top">mergeLines</td>
+ <td valign="top">Whether to merge messages into a single
+ line or split them into multiple lines.</td>
+ <td valign="top" align="center">No, defaults
+ to <code>true</code></td>
+ </tr>
</table>
<h2><a name="assertLogDoesntContain">assertLogDoesntContain</a></h2>
@@ -944,6 +951,13 @@
levels. One of "error", "warning", "info", "verbose", "debug".</td>
<td valign="top" align="center">No</td>
</tr>
+ <tr>
+ <td valign="top">mergeLines</td>
+ <td valign="top">Whether to merge messages into a single
+ line or split them into multiple lines.</td>
+ <td valign="top" align="center">No, defaults
+ to <code>true</code></td>
+ </tr>
</table>
<h2><a name="assertResourceContains">assertResourceContains</a></h2>
diff --git a/src/main/org/apache/ant/antunit/LogContains.java b/src/main/org/apache/ant/antunit/LogContains.java
index 36e0d76..b5531bd 100644
--- a/src/main/org/apache/ant/antunit/LogContains.java
+++ b/src/main/org/apache/ant/antunit/LogContains.java
@@ -37,6 +37,7 @@
private String text;
private int logLevel = Project.MSG_INFO;
+ private boolean mergeLines = true;
/**
* Test the log shall contain.
@@ -52,6 +53,14 @@
logLevel = echoLevel.getLevel();
}
+ /**
+ * Whether to merge messages into a single line or split them into
+ * multiple lines.
+ */
+ public void setMergeLines(boolean b) {
+ mergeLines = b;
+ }
+
public boolean eval() {
if (text == null) {
throw new BuildException("the text attribute is required");
@@ -62,19 +71,19 @@
String log;
switch (logLevel) {
case Project.MSG_ERR:
- log = c.getErrLog();
+ log = c.getErrLog(mergeLines);
break;
case Project.MSG_WARN:
- log = c.getWarnLog();
+ log = c.getWarnLog(mergeLines);
break;
case Project.MSG_INFO:
- log = c.getInfoLog();
+ log = c.getInfoLog(mergeLines);
break;
case Project.MSG_VERBOSE:
- log = c.getVerboseLog();
+ log = c.getVerboseLog(mergeLines);
break;
case Project.MSG_DEBUG:
- log = c.getDebugLog();
+ log = c.getDebugLog(mergeLines);
break;
default:
diff --git a/src/main/org/apache/ant/antunit/antlib.xml b/src/main/org/apache/ant/antunit/antlib.xml
index 53f27a9..4ed528b 100644
--- a/src/main/org/apache/ant/antunit/antlib.xml
+++ b/src/main/org/apache/ant/antunit/antlib.xml
@@ -350,11 +350,13 @@
<macrodef name="assertLogContains" backtrace="false">
<attribute name="text"/>
<attribute name="level" default="info"/>
+ <attribute name="mergeLines" default="true"/>
<attribute name="message"
default="Expected log to contain '@{text}' at level @{level}"/>
<sequential>
<au:fail message="@{message}">
- <au:logcontains text="@{text}" level="@{level}"/>
+ <au:logcontains text="@{text}" level="@{level}"
+ mergeLines="@{mergeLines}"/>
</au:fail>
</sequential>
</macrodef>
@@ -362,11 +364,13 @@
<macrodef name="assertLogDoesntContain" backtrace="false">
<attribute name="text"/>
<attribute name="level" default="info"/>
+ <attribute name="mergeLines" default="true"/>
<attribute name="message"
default="Unexpected log '@{text}' at level @{level}"/>
<sequential>
<au:assertFalse message="@{message}">
- <au:logcontains text="@{text}" level="@{level}"/>
+ <au:logcontains text="@{text}" level="@{level}"
+ mergeLines="@{mergeLines}"/>
</au:assertFalse>
</sequential>
</macrodef>
diff --git a/src/tests/antunit/assertLogContains-test.xml b/src/tests/antunit/assertLogContains-test.xml
new file mode 100644
index 0000000..6d84e4e
--- /dev/null
+++ b/src/tests/antunit/assertLogContains-test.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">
+
+ <import file="antunit-base.xml"/>
+
+ <target name="testWithoutMerge">
+ <echo>0</echo>
+ <echo>1</echo>
+ <au:assertLogContains
+ mergeLines="false"
+ text="0${line.separator}1${line.separator}"/>
+ <au:assertLogDoesntContain
+ mergeLines="false"
+ text="01"/>
+ </target>
+
+ <target name="testWithExplicitMerge">
+ <echo>0</echo>
+ <echo>1</echo>
+ <au:assertLogDoesntContain
+ mergeLines="true"
+ text="0${line.separator}1${line.separator}"/>
+ <au:assertLogContains
+ mergeLines="true"
+ text="01"/>
+ </target>
+
+ <target name="testWithImplicitMerge">
+ <echo>0</echo>
+ <echo>1</echo>
+ <au:assertLogDoesntContain
+ text="0${line.separator}1${line.separator}"/>
+ <au:assertLogContains
+ text="01"/>
+ </target>
+</project>