KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DefaultMonitor.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 java.util.Hashtable JavaDoc;
33
34 import org.apache.log4j.Logger;
35
36
37 /**
38  * This implemenation of <tt>Monitor</tt> uses a Hashtable to store the
39  * test data.
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/05/27 13:30:16 $
44  */

45 public class DefaultMonitor implements Monitor
46 {
47     private static final Logger LOG = Logger.getLogger( DefaultMonitor.class );
48     
49     private Hashtable JavaDoc testData = new Hashtable JavaDoc();
50     private TestDataFactory dataFactory;
51     private Server server;
52     
53     
54     /**
55      * Constructor for a Monitor requiring a reference to the Server which will
56      * receive the completed <tt>TestData</tt> structures, and a reference to
57      * a factory for creating new <tt>TestData</tt> instances for the particular
58      * framework this Monitor belongs to.
59      *
60      * @param s the server to receive completed <tt>TestData</tt> entities.
61      * This cannot be <tt>null</tt>.
62      * @param f the factory in charge of creating new <tt>TestData</tt>
63      * entities. This cannot be <tt>null</tt>.
64      * @exception IllegalArgumentException if <tt>s</tt> or <tt>f</tt> is
65      * <tt>null</tt>.
66      */

67     public DefaultMonitor( Server s, TestDataFactory f )
68     {
69         if (s == null || f == null)
70         {
71             throw new IllegalArgumentException JavaDoc("no null arguments");
72         }
73         this.server = s;
74         this.dataFactory = f;
75     }
76     
77     
78     /**
79      * Adds a new <tt>TestData</tt> instance related to the given
80      * <tt>TestInfo</tt>. If there already is a <tt>TestData</tt> for the
81      * given <tt>TestInfo</tt>, then an exception is thrown.
82      *
83      * @param info the unique test identifier to create a new <tt>TestData</tt>
84      * instance for.
85      * @exception IllegalStateException if <tt>info</tt> is already
86      * been added without having been sent.
87      * @exception IllegalArgumentException if <tt>info</tt> is <tt>null</tt>.
88      */

89     public void addTestData( TestInfo info )
90     {
91         assertNotNull( info );
92         synchronized (this.testData)
93         {
94             if (this.testData.containsKey( info ))
95             {
96                 throw new IllegalStateException JavaDoc( "Test "+info+
97                     " has already been registered." );
98             }
99             LOG.debug("**** Monitor "+this+" adding test "+info);
100             TestData td = createTestData( info );
101             this.testData.put( info, td );
102         }
103     }
104     
105     
106     /**
107      * Retrieves the data associated with the given <tt>TestInfo</tt>, as was
108      * created through {@link #addTestData( TestInfo )}. If the <tt>info</tt>
109      * was never passed to the add method, then an exception is thrown.
110      *
111      * @param info the unique test identifier
112      * @exception IllegalStateException if <tt>info</tt> has not been added,
113      * or has been removed through the send call.
114      * @exception IllegalArgumentException if <tt>info</tt> is <tt>null</tt>.
115      */

116     public TestData getTestData( TestInfo info )
117     {
118         assertNotNull( info );
119         synchronized (this.testData)
120         {
121             TestData td = (TestData)this.testData.get( info );
122             assertNotNull( td );
123             return td;
124         }
125     }
126     
127     
128     /**
129      * Sends the <tt>TestData</tt> associated with <tt>info</tt> to the
130      * inner server, and removes the data from the inner cache. If the
131      * <tt>info</tt> has not been added, then an exception is thrown.
132      *
133      * @param info the unique test identifier
134      * @exception IllegalStateException if <tt>info</tt> has not been added,
135      * or has been removed through the send call.
136      * @exception IllegalArgumentException if <tt>info</tt> is <tt>null</tt>.
137      */

138     public void sendTestData( TestInfo info )
139     {
140         assertNotNull( info );
141         TestData td;
142         synchronized (this.testData)
143         {
144             td = (TestData)this.testData.remove( info );
145         }
146         LOG.debug("**** Monitor "+this+" removing test "+info);
147         sendTestDataToServer( td );
148     }
149     
150     
151     /**
152      * Creates a new TestData instance for the info object through the
153      * factory. This is guaranteed to never return <tt>null</tt>.
154      *
155      * @param info the unique test identifier
156      * @exception IllegalStateException if the factory returns <tt>null</tt>.
157      */

158     protected TestData createTestData( TestInfo info )
159     {
160         TestData td = this.dataFactory.createTestData( info );
161         if (td == null)
162         {
163             throw new IllegalStateException JavaDoc("factory returned null");
164         }
165         return td;
166     }
167     
168     
169     /**
170      * Retrieves the registered TestData instance for the info. This
171      * may return <tt>null</tt>.
172      *
173      * @param info the unique test identifier
174      */

175     protected TestData retrieveTestData( TestInfo info )
176     {
177         TestData td = (TestData)this.testData.get( info );
178         return td;
179     }
180     
181     
182     /**
183      * Sends off the test data to the server.
184      */

185     protected void sendTestDataToServer( TestData td )
186     {
187         assertNotNull( td );
188         this.server.addTestData( td );
189     }
190     
191     
192     /**
193      * Ensures that <tt>info</tt> is not <tt>null</tt>.
194      */

195     protected void assertNotNull( TestInfo info )
196     {
197         if (info == null)
198         {
199             throw new IllegalArgumentException JavaDoc( "TestInfo was null" );
200         }
201     }
202     
203     
204     /**
205      * Ensures that <tt>td</tt> is not <tt>null</tt>.
206      */

207     protected void assertNotNull( TestData td )
208     {
209         if (td == null)
210         {
211             throw new IllegalStateException JavaDoc( "Test is not registered." );
212         }
213     }
214 }
215
216
Popular Tags