KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > autodoc > v1 > testserver > TestCorrelate


1 /*
2  * @(#)TestCorrelate.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.autodoc.v1.testserver;
30
31
32 import net.sourceforge.groboutils.util.throwable.v1.ThrowableParser;
33 import net.sourceforge.groboutils.util.throwable.v1.StackTraceLineParser;
34
35 import org.apache.log4j.Logger;
36
37
38 /**
39  * Helper method to aid in the use of Monitors and TestData.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @since March 17, 2002
43  * @version $Date: 2003/02/10 22:52:13 $
44  */

45 public abstract class TestCorrelate
46 {
47     private static final Logger LOG = Logger.getLogger( TestCorrelate.class );
48     
49     private MonitorFinder finder;
50     private Class JavaDoc owner;
51     
52     
53     public TestCorrelate( Class JavaDoc owner, MonitorFinder finder )
54     {
55         if (finder == null)
56         {
57             throw new IllegalArgumentException JavaDoc("no null arguments");
58         }
59         this.owner = owner;
60         this.finder = finder;
61     }
62     
63     
64     
65     /**
66      * Helper method to discover the last method from the owning class in
67      * the current stack. If the method is not in the stack, then <tt>null</tt>
68      * is returned. This can be very slow.
69      */

70     protected String JavaDoc findClassMethodFromStack()
71     {
72         Throwable JavaDoc t = new Throwable JavaDoc();
73         t.fillInStackTrace();
74         
75         ThrowableParser tp = new ThrowableParser( t );
76         for (StackTraceLineParser stlp = tp.next(); stlp != null;
77                 stlp = tp.next() )
78         {
79             if (stlp.getClassName().equals( getOwnerName() ))
80             {
81                 String JavaDoc methodName = stlp.getMethodName();
82                 LOG.debug( "Owning method for stack trace is '"+methodName+
83                     "'", t );
84                 return methodName;
85             }
86         }
87         
88         // could not be found
89
return null;
90     }
91     
92     
93     /**
94      * Uses the owner class and the <tt>findClassMethodFromStack</tt>
95      * result to crate a TestInfo instance.
96      */

97     protected TestInfo createTestInfoFromStack()
98     {
99         return new DefaultTestInfo( getOwnerName(),
100             findClassMethodFromStack() );
101     }
102     
103     
104     /**
105      * Use the given method name and the known owner class to create a new
106      * <tt>TestInfo</tt> instance.
107      */

108     protected TestInfo createTestInfo( String JavaDoc methodName )
109     {
110         return new DefaultTestInfo( getOwnerName(), methodName );
111     }
112     
113     
114     protected MonitorFinder getFinder()
115     {
116         return this.finder;
117     }
118     
119     
120     /**
121      * @param info the test info to find its corresponding TestData.
122      * @return the TestData associated with the info, or <tt>null</tt> if
123      * it was not found.
124      */

125     protected TestData getTestData( TestInfo info )
126     {
127         // this could be hacky...
128
try
129         {
130             return getFinder().getMonitor().getTestData( info );
131         }
132         catch (IllegalArgumentException JavaDoc iae)
133         {
134             LOG.warn( "Test "+info+" caused error w/ monitor", iae );
135         }
136         catch (IllegalStateException JavaDoc ise)
137         {
138             LOG.warn( "Test "+info+" caused error w/ monitor", ise );
139         }
140         return null;
141     }
142     
143     
144     protected Class JavaDoc getOwner()
145     {
146         return this.owner;
147     }
148     
149     
150     protected String JavaDoc getOwnerName()
151     {
152         Class JavaDoc c = getOwner();
153         String JavaDoc res;
154         if (c == null)
155         {
156             res = null;
157         }
158         else
159         {
160             res = c.getName();
161         }
162         return res;
163     }
164 }
165
166
Popular Tags