1   /*
2    * 
3    * $Revision: 1.3 $
4    * $Date: 2004/06/28 19:49:43 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004 - strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 2004-06-05  Manfred Wolff
23   */
24  package org.strutsit.chain;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.commons.chain.Command;
31  import org.apache.commons.chain.Context;
32  import org.apache.commons.chain.impl.ContextBase;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.strutsit.chain.configuration.ChainXMLConfiguration;
36  import org.strutsit.chain.interfaces.ChainConfiguration;
37  import org.strutsit.chain.interfaces.ChainFacade;
38  
39  /***
40   * This Unit-Test test the several configuration classes.
41   * 
42   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff </a>
43   * @since JDK 1.4
44   * @version $Revision: 1.3 $
45   */
46  public class TestFacade extends TestCase {
47  
48  	/***
49  	 * The logger for this class (commons-logging).
50  	 */
51  	private static Log log = LogFactory.getLog(TestFacade.class);
52  
53  	/***
54  	 * Defines the testcase name for JUnit.
55  	 * 
56  	 * @param theName
57  	 *            the testcase's name.
58  	 */
59  	public TestFacade(String theName) {
60  		super(theName);
61  		if (log.isTraceEnabled()) {
62  			log.trace("TestConfiguration -> START");
63  		}
64  		if (log.isTraceEnabled()) {
65  			log.trace("TestConfiguration -> END");
66  		}
67  	}
68  
69  	/***
70  	 * Start the tests.
71  	 * 
72  	 * @param theArgs
73  	 *            the arguments. Not used
74  	 */
75  	public static void main(String[] theArgs) {
76  		if (log.isTraceEnabled()) {
77  			log.trace("main -> START");
78  		}
79  		junit.awtui.TestRunner
80  				.main(new String[] { TestFacade.class.getName() });
81  		if (log.isTraceEnabled()) {
82  			log.trace("main -> END");
83  		}
84  	}
85  
86  	/***
87  	 * @return a test suite (<code>TestSuite</code>) that includes all
88  	 *         methods starting with "test"
89  	 */
90  	public static Test suite() {
91  		// All methods starting with "test" will be executed in the test suite.
92  		if (log.isTraceEnabled()) {
93  			log.trace("suite -> START");
94  		}
95  		if (log.isTraceEnabled()) {
96  			log.trace("suite -> END");
97  		}
98  		return new TestSuite(TestFacade.class);
99  	}
100 
101 	/***
102 	 * Nothing to be done.
103 	 */
104 	public void setUp() {
105 		if (log.isTraceEnabled()) {
106 			log.trace("setUp -> START");
107 		}
108 		if (log.isTraceEnabled()) {
109 			log.trace("setUp -> END");
110 		}
111 	}
112 
113 	/***
114 	 * Nothing to be done.
115 	 */
116 	public void tearDown() {
117 		if (log.isTraceEnabled()) {
118 			log.trace("tearDown -> START");
119 		}
120 		if (log.isTraceEnabled()) {
121 			log.trace("tearDown -> END");
122 		}
123 	}
124 
125 	private Context createContext() {
126 		Context context = new ContextBase();
127 		StringBuffer buffer = new StringBuffer();
128 		context.put("chainerg", buffer);
129 		return context;
130 	}
131 
132 	/***
133 	 * Tests the configuration.
134 	 */
135 	public void testLoadXMLConfiguration() {
136 
137 		if (log.isTraceEnabled()) {
138 			log.trace("testLoadXMLConfiguration -> START");
139 		}
140 		ChainConfiguration config = new ChainXMLConfiguration();
141 		try {
142 			config.init();
143 		} catch (Exception e) {
144 			log.error("Exception while init the configuration.");
145 			if (log.isDebugEnabled()) {
146 				log.debug("testLoadXMLConfiguration -> " + e);
147 			}
148 		}
149 
150 		Command testCommand = config.getCatalog().getCommand("Test-Command");
151 		assertNotNull("Error getting command out of XML Configuration",
152 				testCommand);
153 	}
154 
155 	/***
156 	 * 
157 	 *  
158 	 */
159 	public void testFacade() {
160 
161 		ChainFacade facade = ChainFacadeImpl.getInstance();
162 		try {
163 			facade.initFacade();
164 		} catch (ChainException e) {
165 			fail("Cannot init the facade");
166 		}
167 		Context context = createContext();
168 		try {
169 			facade.execute("Test-Command", context);
170 		} catch (ChainException e1) {
171 			fail("Fail executing an command.");
172 			e1.printStackTrace();
173 		}
174 	}
175 
176 	public void testExecutingSimpleCommand() {
177 
178 		// creating context
179 		Context context = createContext();
180 
181 		ChainFacade facade = ChainFacadeImpl.getInstance();
182 		try {
183 			facade.initFacade();
184 		} catch (ChainException e) {
185 			fail("Cannot init the facade");
186 		}
187 
188 		try {
189 			// gets the right command for the test
190 			facade.execute("double", context);
191 		} catch (ChainException e1) {
192 			fail("Executing command double");
193 			e1.printStackTrace();
194 		}
195 
196 		boolean fail = false;
197 		// executing and testing the response from the command
198 		StringBuffer buffer = (StringBuffer) context.get("chainerg");
199 		if (log.isInfoEnabled()) {
200 			log.info("erg  = " + buffer.toString());
201 		}
202 	}
203 }