View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: AlertCase.java,v 1.1.1.1 2004/05/19 12:33:40 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.alert;
21  //import java.lang.reflect.Method;
22  import java.lang.reflect.Constructor;
23  import java.util.Iterator;
24  import java.util.Properties;
25  import org.w3c.dom.Element;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * The alert test suite.
34   *
35   *<p>This is the central class in the alert framework. The ant driven junit 
36   * tets will be run agains this class, and it is up the the class to return
37   * a TestSuite. This test suite is dynalically created throu reading of an
38   * alert XML file, where all the agents are specifyed.</p>
39   *
40   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
41   * @version $Revision: 1.1.1.1 $
42   */
43  
44  public class AlertCase{
45     static final Log log = LogFactory.getLog( AlertCase.class);
46     public AlertCase (){
47        
48     }
49     public static junit.framework.Test suite() throws Exception{
50        TestSuite suite= new TestSuite();
51        try {
52           
53           
54           Iterator agents = PropertyUtil.getAgents().iterator();
55           // Iterate ower the agent, fetching its code, method and agentName.
56           // and all its properties and add it to the suite
57           while (agents.hasNext()) {
58              Element agent = (Element)agents.next();
59              String code = agent.getAttribute("code");
60              String method = agent.getAttribute("method");
61              String name =agent.getAttribute("name");
62              Properties prop = PropertyUtil.getProperties(agent);
63              suite.addTest(getTest(code,method,name,prop));
64           } // end of while ()
65           
66        } catch (Exception e) {
67           log.error("Could not setup Agents",e);
68           throw e;
69        } // end of try-catch
70        return suite;
71     }
72     /***
73      * Dynalically create an Agent.
74      *
75      * @param clazz the fully qualifyed name of the Agent class.
76      * @param test the name of the method to invoke on the test.
77      * @param name the unique agent name.
78      * @param prop the properties for the agent.
79      */
80     public static TestCase getTest(String clazz, String test, String name, Properties prop) throws Exception {
81        Class tc =  Class.forName(clazz);
82        Constructor c = tc.getConstructor(new Class[] {String.class});
83  
84        Object o = c.newInstance(new Object[] {test});
85        ((Agent)o).initialize(name,prop);
86        //Method m = tc.getDeclaredMethod("setProp", new Class[]{String.class, String.class});
87        // m.invoke(o,new Object[]{name,arg});
88        return (TestCase)o;
89     }
90     public static void main(String[] args){
91        
92  
93     }
94  } // AlertCase