KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > ExceptionSelectorTestCase


1 /*
2 * Copyright 1999-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 package org.apache.cocoon.selection;
17
18 import java.util.Map JavaDoc;
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.SitemapComponentTestCase;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26
27 public class ExceptionSelectorTestCase extends SitemapComponentTestCase {
28
29     private static final String JavaDoc EXCEPTION_SELECTOR = "exception";
30
31     /**
32      * Run this test suite from commandline
33      *
34      * @param args commandline arguments (ignored)
35      */

36     public static void main( String JavaDoc[] args ) {
37         TestRunner.run(suite());
38     }
39     
40     /** Create a test suite.
41      * This test suite contains all test cases of this class.
42      * @return the Test object containing all test cases.
43      */

44     public static Test suite() {
45         TestSuite suite = new TestSuite(ExceptionSelectorTestCase.class);
46         return suite;
47     }
48     
49     /**
50      * A simple exception select test
51      */

52     public void testExceptionSelect() throws Exception JavaDoc {
53         
54         // create an exception
55
final java.lang.NullPointerException JavaDoc npe = new java.lang.NullPointerException JavaDoc( "ExceptionSelectorTestCase");
56
57         // put the exception into the objectModel
58
Map JavaDoc objectModel = this.getObjectModel();
59         objectModel.put( ObjectModelHelper.THROWABLE_OBJECT, npe );
60         
61         Parameters parameters = new Parameters();
62         boolean result;
63         
64         // test selection success
65
result = this.select( EXCEPTION_SELECTOR, "npe", parameters );
66         System.out.println( result );
67         assertTrue( "Test if a npe is selected", result );
68         
69         // test selection failure
70
result = this.select( EXCEPTION_SELECTOR, "non-specified-exception", parameters );
71         System.out.println( result );
72         assertTrue( "Test if a non specified exception is not selected", !result );
73     }
74
75     /**
76      * A simple exception select test
77      */

78     public void testExceptionSelectUnknownException() throws Exception JavaDoc {
79         
80         // create an exception
81
final java.lang.IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc( "ExceptionSelectorTestCase");
82
83         // put the exception into the objectModel
84
Map JavaDoc objectModel = this.getObjectModel();
85         objectModel.put( ObjectModelHelper.THROWABLE_OBJECT, iae );
86         
87         Parameters parameters = new Parameters();
88         boolean result;
89         
90         // test selection failure
91
result = this.select( EXCEPTION_SELECTOR, "npe", parameters );
92         System.out.println( result );
93         assertTrue( "Test if a npe is not selected selected", !result );
94     }
95     
96     /**
97      * A simple exception select test
98      * The causing exception is listed, thus selecting the unrolling
99      * exception fails, selecting the causing exception succeeds.
100      */

101     public void testExceptionSelectProcessingException() throws Exception JavaDoc {
102         
103         // create an exception
104
final java.lang.NullPointerException JavaDoc npe = new NullPointerException JavaDoc( "NullPointerExceptionSelectorTestCase" );
105         final ProcessingException pe = new ProcessingException( "ProcessingExceptionSelectorTestCase", npe );
106         
107         // put the exception into the objectModel
108
Map JavaDoc objectModel = this.getObjectModel();
109         objectModel.put( ObjectModelHelper.THROWABLE_OBJECT, pe );
110         
111         Parameters parameters = new Parameters();
112         boolean result;
113         
114         // test selection success
115
result = this.select( EXCEPTION_SELECTOR, "npe", parameters );
116         System.out.println( result );
117         assertTrue( "Test if a npe is selected", result );
118         
119         // test selection failure
120
result = this.select( EXCEPTION_SELECTOR, "pe", parameters );
121         System.out.println( result );
122         assertTrue( "Test if a pe is not selected", !result );
123     }
124     
125     /**
126      * A simple exception select test.
127      * The causing exception is not listed, thus matching the unrolling
128      * exception succeeds
129      */

130     public void testExceptionSelectProcessingException2() throws Exception JavaDoc {
131         
132         // create an exception
133
final java.lang.IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc( "ExceptionSelectorTestCase");
134         final ProcessingException pe = new ProcessingException( "ProcessingExceptionSelectorTestCase", iae );
135         
136         // put the exception into the objectModel
137
Map JavaDoc objectModel = this.getObjectModel();
138         objectModel.put( ObjectModelHelper.THROWABLE_OBJECT, pe );
139         
140         Parameters parameters = new Parameters();
141         boolean result;
142         
143         // test selection success
144
result = this.select( EXCEPTION_SELECTOR, "pe", parameters );
145         System.out.println( result );
146         assertTrue( "Test if a pe is not selected", result );
147     }
148 }
149
Popular Tags