add a new logcontent resource and a test

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@449047 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2006-09-22 18:43:25 +00:00
parent e5a28cd3c5
commit 24824acda6
2 changed files with 211 additions and 0 deletions

View File

@ -0,0 +1,124 @@
/*
* 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.
*
*/
package org.apache.ant.antunit;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.LogLevel;
import org.apache.tools.ant.types.Resource;
/**
* Exposes AntUnit log content as a (read-only) Resource.
*/
public class LogContent extends Resource {
private LogLevel level;
/**
* Create a new LogContent resource.
*/
public LogContent() {
setLevel(LogLevel.INFO);
}
/**
* Create a new LogContent resource, specifying Project and log level.
* This constructor is provided primarily for convenience during
* programmatic usage.
* @param level the LogLevel.
*/
public LogContent(Project p, LogLevel level) {
setProject(p);
setLevel(level);
}
/**
* Set the desired log level.
* @param level a LogLevel enumerated attribute.
*/
public void setLevel(LogLevel level) {
this.level = level;
setName(level.getValue());
}
//inherit doc
public InputStream getInputStream() throws IOException {
if (isReference()) {
return ((Resource) getCheckedRef()).getInputStream();
}
LogCapturer lc = getLogCapturer();
if (lc != null) {
return getLogStream(lc);
}
throw new IllegalStateException("antunit log unavailable");
}
//inherit doc
public boolean isExists() {
return getLogCapturer() != null;
}
//inherit doc
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof LogContent)) {
return false;
}
LogContent olc = (LogContent) o;
return olc.getProject() == getProject()
&& olc.level.getLevel() == level.getLevel();
}
private LogCapturer getLogCapturer() {
Object o = getProject().getReference(LogCapturer.REFERENCE_ID);
return o instanceof LogCapturer ? (LogCapturer) o : null;
}
private InputStream getLogStream(LogCapturer lc) {
String log = null;
switch (level.getLevel()) {
case Project.MSG_ERR:
log = lc.getErrLog();
break;
case Project.MSG_WARN:
log = lc.getWarnLog();
break;
case Project.MSG_INFO:
log = lc.getInfoLog();
break;
case Project.MSG_VERBOSE:
log = lc.getVerboseLog();
break;
case Project.MSG_DEBUG:
log = lc.getDebugLog();
break;
default:
throw new IllegalStateException("how possible?");
}
return new ByteArrayInputStream(log.getBytes());
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.
*
*/
package org.apache.ant.antunit;
import java.io.IOException;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.LogLevel;
import org.apache.tools.ant.types.resources.StringResource;
import org.apache.tools.ant.util.ResourceUtils;
import junit.framework.Assert;
import junit.framework.TestCase;
public class LogContentTest extends TestCase {
public LogContentTest(String name) {
super(name);
}
public void testNoLogCapturer() throws IOException {
LogContent content = new LogContent();
content.setProject(new Project());
try {
content.getInputStream();
fail("should fail due to no LogCapturer set");
} catch (IllegalStateException e) {
//pass
}
}
public void testMessagePriorities() throws IOException {
Project p = new Project();
LogCapturer c = new LogCapturer(p);
String[] msgs = new String[] {"err", "warn", "info", "verbose",
"debug"};
for (int i = 0; i < msgs.length; i++) {
BuildEvent be = new BuildEvent(p);
be.setMessage(msgs[i], i);
c.messageLogged(be);
}
assertMessages(new LogContent(p, LogLevel.ERR), msgs,
Project.MSG_ERR);
assertMessages(new LogContent(p, LogLevel.WARN), msgs,
Project.MSG_WARN);
assertMessages(new LogContent(p, LogLevel.INFO), msgs,
Project.MSG_INFO);
assertMessages(new LogContent(p, LogLevel.VERBOSE), msgs,
Project.MSG_VERBOSE);
assertMessages(new LogContent(p, LogLevel.DEBUG), msgs,
Project.MSG_DEBUG);
}
private static void assertMessages(LogContent content, String[] messages,
int upTo) throws IOException {
StringResource s = new StringResource();
ResourceUtils.copyResource(content, s);
String actual = s.getValue();
for (int i = 0; i <= upTo && i < messages.length; i++) {
Assert.assertTrue("checking for " + messages[i] + " in " + actual,
actual.indexOf(messages[i]) > -1);
}
for (int i = upTo + 1; i < messages.length; i++) {
Assert.assertTrue("checking for " + messages[i] + " in " + actual,
actual.indexOf(messages[i]) == -1);
}
}
}