001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.util;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import junit.framework.AssertionFailedError;
025:
026: public class MockMessageLogger extends AbstractMessageLogger {
027:
028: private List _endProgress = new ArrayList();
029:
030: private List _logs = new ArrayList();
031:
032: private List _rawLogs = new ArrayList();
033:
034: private int _progressCalls;
035:
036: public void doEndProgress(String msg) {
037: _endProgress.add(msg);
038: }
039:
040: public void log(String msg, int level) {
041: _logs.add(level + " " + msg);
042: }
043:
044: public void doProgress() {
045: _progressCalls++;
046: }
047:
048: public void rawlog(String msg, int level) {
049: _rawLogs.add(level + " " + msg);
050: }
051:
052: public List getEndProgress() {
053: return _endProgress;
054: }
055:
056: public List getLogs() {
057: return _logs;
058: }
059:
060: public int getProgressCalls() {
061: return _progressCalls;
062: }
063:
064: public List getRawLogs() {
065: return _rawLogs;
066: }
067:
068: public void clear() {
069: _logs.clear();
070: _rawLogs.clear();
071: _endProgress.clear();
072: _progressCalls = 0;
073: }
074:
075: public void assertLogContains(String message) {
076: for (Iterator iter = _logs.iterator(); iter.hasNext();) {
077: String log = (String) iter.next();
078: if (log.indexOf(message) != -1) {
079: return;
080: }
081: }
082: throw new AssertionFailedError(
083: "logs do not contain expected message: expected='"
084: + message + "' logs='\n" + join(_logs) + "'");
085: }
086:
087: public void assertLogDoesntContain(String message) {
088: for (Iterator iter = _logs.iterator(); iter.hasNext();) {
089: String log = (String) iter.next();
090: if (log.indexOf(message) != -1) {
091: throw new AssertionFailedError(
092: "logs contain unexpected message: '" + message
093: + "' logs='\n" + join(_logs) + "'");
094: }
095: }
096: }
097:
098: public void assertLogVerboseContains(String message) {
099: assertLogContains(Message.MSG_VERBOSE + " " + message);
100: }
101:
102: public void assertLogInfoContains(String message) {
103: assertLogContains(Message.MSG_INFO + " " + message);
104: }
105:
106: public void assertLogWarningContains(String message) {
107: Message.sumupProblems();
108: assertLogContains(Message.MSG_WARN + " \t" + message);
109: }
110:
111: public void assertLogErrorContains(String message) {
112: Message.sumupProblems();
113: assertLogContains(Message.MSG_ERR + " " + message);
114: }
115:
116: private String join(List logs) {
117: StringBuffer sb = new StringBuffer();
118: for (Iterator iter = logs.iterator(); iter.hasNext();) {
119: String log = (String) iter.next();
120: sb.append(log).append("\n");
121: }
122: return sb.toString();
123: }
124:
125: }
|