001: /*
002: * @(#)TestCorrelate.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.autodoc.v1.testserver;
030:
031: import net.sourceforge.groboutils.util.throwable.v1.ThrowableParser;
032: import net.sourceforge.groboutils.util.throwable.v1.StackTraceLineParser;
033:
034: import org.apache.log4j.Logger;
035:
036: /**
037: * Helper method to aid in the use of Monitors and TestData.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @since March 17, 2002
041: * @version $Date: 2003/02/10 22:52:13 $
042: */
043: public abstract class TestCorrelate {
044: private static final Logger LOG = Logger
045: .getLogger(TestCorrelate.class);
046:
047: private MonitorFinder finder;
048: private Class owner;
049:
050: public TestCorrelate(Class owner, MonitorFinder finder) {
051: if (finder == null) {
052: throw new IllegalArgumentException("no null arguments");
053: }
054: this .owner = owner;
055: this .finder = finder;
056: }
057:
058: /**
059: * Helper method to discover the last method from the owning class in
060: * the current stack. If the method is not in the stack, then <tt>null</tt>
061: * is returned. This can be very slow.
062: */
063: protected String findClassMethodFromStack() {
064: Throwable t = new Throwable();
065: t.fillInStackTrace();
066:
067: ThrowableParser tp = new ThrowableParser(t);
068: for (StackTraceLineParser stlp = tp.next(); stlp != null; stlp = tp
069: .next()) {
070: if (stlp.getClassName().equals(getOwnerName())) {
071: String methodName = stlp.getMethodName();
072: LOG.debug("Owning method for stack trace is '"
073: + methodName + "'", t);
074: return methodName;
075: }
076: }
077:
078: // could not be found
079: return null;
080: }
081:
082: /**
083: * Uses the owner class and the <tt>findClassMethodFromStack</tt>
084: * result to crate a TestInfo instance.
085: */
086: protected TestInfo createTestInfoFromStack() {
087: return new DefaultTestInfo(getOwnerName(),
088: findClassMethodFromStack());
089: }
090:
091: /**
092: * Use the given method name and the known owner class to create a new
093: * <tt>TestInfo</tt> instance.
094: */
095: protected TestInfo createTestInfo(String methodName) {
096: return new DefaultTestInfo(getOwnerName(), methodName);
097: }
098:
099: protected MonitorFinder getFinder() {
100: return this .finder;
101: }
102:
103: /**
104: * @param info the test info to find its corresponding TestData.
105: * @return the TestData associated with the info, or <tt>null</tt> if
106: * it was not found.
107: */
108: protected TestData getTestData(TestInfo info) {
109: // this could be hacky...
110: try {
111: return getFinder().getMonitor().getTestData(info);
112: } catch (IllegalArgumentException iae) {
113: LOG.warn("Test " + info + " caused error w/ monitor", iae);
114: } catch (IllegalStateException ise) {
115: LOG.warn("Test " + info + " caused error w/ monitor", ise);
116: }
117: return null;
118: }
119:
120: protected Class getOwner() {
121: return this .owner;
122: }
123:
124: protected String getOwnerName() {
125: Class c = getOwner();
126: String res;
127: if (c == null) {
128: res = null;
129: } else {
130: res = c.getName();
131: }
132: return res;
133: }
134: }
|