KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > TestExample


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestExample.java,v 1.14 2004/02/01 05:16:32 christianc Exp $
19  */

20 package org.enhydra.barracuda;
21
22 import java.util.*;
23
24 import junit.framework.*;
25
26 import org.apache.log4j.*;
27 import org.enhydra.barracuda.testbed.*;
28
29
30 /**
31  * Just a little sample test to illustrate how it all works, Runtime parameters
32  * are defined in TestUtil.java. We are using basic Log4J logging.
33  *
34  * You can run individual test classes directly, or you can run the entire test
35  * suite using Ant. You control what gets logged by modifying the values in
36  * /WEB-INF/log4j.xml
37  *
38  * If you wish to create custom test classes, you should use this class as a
39  * model.
40  */

41 public class TestExample extends DefaultTestCase {
42     //common vars (customize for every test class)
43
private static final String JavaDoc testClass = TestExample.class.getName();
44     private static final Logger logger = Logger.getLogger("test."+testClass);
45
46     //variables
47
private static boolean demonstrateFailure = false; //change this to true to see how a failure is handled
48
private static boolean demonstrateException = false; //change this to true to see how an exception is handled
49

50     //-------------------- Basics --------------------------------
51
/**
52      * Public Constructor
53      */

54     public TestExample(String JavaDoc name) {
55         super(name);
56     }
57     
58     /**
59      * Every test class should have a main method so it can be run
60      * directly (when debugging tests you often will not want to run
61      * the whole suite)
62      *
63      * @param args defined in test.util.TestUtil
64      */

65     public static void main(String JavaDoc args[]) {
66         //check for standard runtime parameters
67
TestUtil.parseParams(args);
68
69         //launch the test
70
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String JavaDoc[] {testClass});
71         else junit.swingui.TestRunner.main(new String JavaDoc[] {testClass});
72     }
73
74     
75     //-------------------- TestCase ------------------------------
76
/**
77      * Set up the tests
78      */

79     protected void setUp() throws Exception JavaDoc {
80         if (logger.isDebugEnabled()) logger.debug("Setting up test case");
81         super.setUp();
82         
83         //this is where we can do any setup prior to
84
//actually running the tests if we so desire
85
//(--n/a--)
86
}
87
88     /**
89      * Tear down after the tests
90      */

91     protected void tearDown() throws Exception JavaDoc {
92         if (logger.isDebugEnabled()) logger.debug("Tearing down test case");
93         super.tearDown();
94         
95         //this is where we can do any cleanup after
96
//actually running the tests if we so desire
97
//(--n/a--)
98
}
99     
100     
101     //-------------------- Actual Tests --------------------------
102
//Note: all the methods herein should follow the testXXXX naming convention
103
//Also keep in mind that local vars set in one test method are NOT retained
104
//when the next method is invoked because JUnit makes a separate instance of
105
//the test class for each testXXXX method!!!
106

107     /**
108      * Simple test to demonstrate how we can test various scenarios
109      * and then log any errors which occur.
110      */

111     public void testSample() {
112         if (logger.isInfoEnabled()) logger.info("running testSample()");
113
114         //sample assertions (if an assertion fails then the error
115
//message will be thrown)
116
try {
117             //assert true!=false
118
if (logger.isInfoEnabled()) logger.info("testing true!=false");
119             assertTrue("true not equal false", true!=false);
120
121             //assert 1==0
122
if (demonstrateFailure) {
123                 if (logger.isInfoEnabled()) logger.info("testing intentional failure");
124                 assertTrue("intentionally failed assertion", 1==0);
125             }
126             
127             //sample exception
128
if (demonstrateException) {
129                 if (logger.isInfoEnabled()) logger.info("generating sample exception");
130                 throw new Exception JavaDoc("Sample Exception");
131             }
132
133             //finished successfully!
134
if (logger.isInfoEnabled()) logger.info("tests complete!");
135         } catch (Exception JavaDoc e) {
136             //here's where we should catch unexpected exceptions and
137
//manually cause the test to fail
138
logger.error("Unexpected Test Exception", e);
139             fail("Unexpected Test Exception:"+e);
140         }
141     }
142 }
143
Popular Tags