001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.context;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.lang.reflect.Method;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Timer;
026:
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.io.IOUtils;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.kuali.core.util.cache.MethodCacheInterceptor;
034: import org.kuali.core.util.properties.PropertyTree;
035: import org.kuali.kfs.service.ParameterService;
036:
037: /**
038: * This class provides utility methods for use during manual testing.
039: */
040:
041: public class TestUtils {
042: private static final Log LOG = LogFactory
043: .getLog(KualiTestBase.class);
044:
045: /**
046: * Disables all scheduled tasks, to make debugging easier.
047: */
048: public static void disableScheduledTasks() {
049: Timer timer = SpringContext.getBean(Timer.class);
050: timer.cancel();
051: }
052:
053: /**
054: * Iterates through the given Collection, printing toString of each item in the collection to stderr
055: *
056: * @param collection
057: */
058: public static void dumpCollection(Collection collection) {
059: dumpCollection(collection, new ItemStringFormatter());
060: }
061:
062: /**
063: * Iterates through the given Collection, printing f.format() of each item in the collection to stderr
064: *
065: * @param collection
066: * @param formatter ItemFormatter used to format each item for printing
067: */
068: public static void dumpCollection(Collection collection,
069: ItemFormatter formatter) {
070: LOG.error(formatCollection(collection, formatter));
071: }
072:
073: /**
074: * Suitable for attaching as a detailFormatter in Eclipse
075: *
076: * @param collection
077: * @param formatter
078: * @return String composed of contents of the given Collection, one per line, formatted by the given ItemFormatter
079: */
080: public static String formatCollection(Collection collection,
081: ItemFormatter formatter) {
082: StringBuffer formatted = new StringBuffer("size= ");
083: formatted.append(collection.size());
084:
085: for (Iterator i = collection.iterator(); i.hasNext();) {
086: formatted.append(formatter.format(i.next()));
087: formatted.append("\n");
088: }
089:
090: return formatted.toString();
091: }
092:
093: /**
094: * Iterates through the entries of the given Map, printing toString of each (key,value) to stderr
095: *
096: * @param map
097: */
098: public static void dumpMap(Map map) {
099: dumpMap(map, new EntryStringFormatter());
100: }
101:
102: /**
103: * Iterates through the entries of the given Map, printing formatter.format() of each Map.Entry to stderr
104: *
105: * @param map
106: * @param formatter
107: */
108: public static void dumpMap(Map map, EntryFormatter formatter) {
109: LOG.error(formatMap(map, formatter));
110: }
111:
112: /**
113: * Suitable for attaching as a detailFormatter in Eclipse
114: *
115: * @param m
116: * @return String composed of contents of the given Map, one entry per line, formatted by the given EntryFormatter
117: */
118: public static String formatMap(Map map, EntryFormatter formatter) {
119: StringBuffer formatted = new StringBuffer("size= ");
120: formatted.append(map.size());
121:
122: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
123: formatted.append(formatter.format((Map.Entry) i.next()));
124: formatted.append("\n");
125: }
126:
127: return formatted.toString();
128: }
129:
130: /**
131: * Recursively prints the contents of the given PropertyTree to stderr
132: *
133: * @param tree
134: */
135: public static void dumpTree(PropertyTree tree) {
136: LOG.error(formatTree(tree));
137: }
138:
139: /**
140: * Suitable for attaching as a detailFormatter in Eclipse
141: *
142: * @param tree
143: * @return String composed of the contents of the given PropertyTree, one entry per line
144: */
145: public static String formatTree(PropertyTree tree) {
146: StringBuffer formatted = new StringBuffer("total size= "
147: + tree.size());
148:
149: formatted.append(formatLevel(tree, 0));
150:
151: return formatted.toString();
152: }
153:
154: private static String formatLevel(PropertyTree tree, int level) {
155: StringBuffer formatted = new StringBuffer();
156:
157: String prefix = buildIndent(level) + ": ";
158:
159: Map children = tree.getDirectChildren();
160: for (Iterator i = children.entrySet().iterator(); i.hasNext();) {
161: Map.Entry e = (Map.Entry) i.next();
162: formatted.append(prefix);
163:
164: String key = (String) e.getKey();
165: PropertyTree subtree = (PropertyTree) e.getValue();
166: String directValue = subtree.toString();
167: if (directValue == null) {
168: formatted.append(key);
169: } else {
170: formatted.append("(");
171: formatted.append(key);
172: formatted.append("=");
173: formatted.append(directValue);
174: formatted.append(")");
175: }
176: formatted.append("\n");
177:
178: formatted.append(formatLevel(subtree, level + 1));
179: }
180:
181: return formatted.toString();
182: }
183:
184: private static String buildIndent(int level) {
185: int indentSize = level * 4;
186: char[] indent = new char[indentSize];
187: for (int i = 0; i < indentSize; ++i) {
188: indent[i] = ' ';
189: }
190:
191: return new String(indent);
192: }
193:
194: public interface ItemFormatter {
195: public String format(Object o);
196: }
197:
198: public interface EntryFormatter {
199: public String format(Map.Entry e);
200: }
201:
202: private static class ItemStringFormatter implements ItemFormatter {
203: public String format(Object o) {
204: String result = "<null>";
205:
206: if (o != null) {
207: result = o.toString();
208: }
209:
210: return result;
211: }
212: }
213:
214: private static class EntryStringFormatter implements EntryFormatter {
215: public String format(Map.Entry e) {
216: String key = "<null>";
217: String value = "<null>";
218:
219: if (e != null) {
220: if (e.getKey() != null) {
221: key = e.getKey().toString();
222: }
223: if (e.getValue() != null) {
224: value = e.getValue().toString();
225: }
226: }
227:
228: return "(" + key + "," + value + ")";
229: }
230: }
231:
232: /**
233: * Given a list of classnames of TestCase subclasses, assembles a TestSuite containing all tests within those classes, then runs
234: * those tests and logs the results.
235: * <p>
236: * Created this method so that I could use OptimizeIt, which was asking for a main() method to run.
237: *
238: * @param args
239: */
240: public static void main(String args[]) {
241: TestSuite tests = new TestSuite();
242: for (int i = 0; i < args.length; ++i) {
243: String className = args[i];
244:
245: Class testClass = null;
246: try {
247: testClass = Class.forName(className);
248:
249: } catch (ClassNotFoundException e) {
250: LOG.error("unable to load class '" + className + "'");
251: }
252:
253: if (testClass != null) {
254: tests.addTestSuite(testClass);
255: }
256: }
257:
258: if (tests.countTestCases() == 0) {
259: LOG.error("no tests to run, exiting");
260: } else {
261: TestRunner.run(tests);
262: }
263: }
264:
265: /**
266: * This sets a given system parameter and clears the method cache for retrieving the parameter.
267: */
268: public static void setSystemParameter(Class componentClass,
269: String parameterName, String parameterText)
270: throws Exception {
271: SpringContext.getBean(ParameterService.class)
272: .setParameterForTesting(componentClass, parameterName,
273: parameterText);
274: }
275:
276: /**
277: * Converts an InputStream to a String using UTF-8 encoding.
278: *
279: * @param inputStream - InputStream to convert.
280: * @return String - converted from InputStream
281: * @throws IOException
282: */
283: public static String convertInputStreamToString(
284: InputStream inputStream) throws IOException {
285: return IOUtils.toString(inputStream, "UTF-8");
286: }
287:
288: /**
289: * Converts a String to an InputStream using UTF-8 encoding.
290: *
291: * @param string - string to convert
292: * @return InputStream - converted from the given string
293: * @throws IOException
294: */
295: public static InputStream convertStringToInputStream(String string)
296: throws IOException {
297: return IOUtils.toInputStream(string, "UTF-8");
298: }
299:
300: /**
301: * Returns the size of an InputStream by first converting it to an ByteArrayOutputStream and getting the size of it.
302: */
303: public static int getInputStreamSize(InputStream inputStream)
304: throws IOException {
305: ByteArrayOutputStream copiedOutputStream = null;
306: IOUtils.copy(inputStream, copiedOutputStream);
307:
308: return copiedOutputStream.size();
309: }
310: }
|