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
This commit is contained in:
Stefan Bodewig 2014-05-06 20:01:13 +00:00
parent 6a398bb070
commit c3b4b5563d
4 changed files with 86 additions and 7 deletions

View File

@ -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>

View File

@ -37,6 +37,7 @@ public class LogContains extends ProjectComponent implements Condition {
private String text;
private int logLevel = Project.MSG_INFO;
private boolean mergeLines = true;
/**
* Test the log shall contain.
@ -52,6 +53,14 @@ public class LogContains extends ProjectComponent implements Condition {
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 @@ public class LogContains extends ProjectComponent implements Condition {
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:

View File

@ -350,11 +350,13 @@ under the License.
<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 @@ under the License.
<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>

View File

@ -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>