KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > exception > InternalExceptionTest


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Patrick Pelletier <pp.pelletier@gmail.com>
4  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.sablecc.sablecc.exception;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23
24 import org.junit.Test;
25
26 public class InternalExceptionTest {
27
28     @Test
29     @SuppressWarnings JavaDoc("unused")
30     public void testInternalExceptionString() {
31
32         // Initializing a good internal exception.
33
InternalException goodInternalException = new InternalException(
34                 "Not null");
35
36         // Testing no thrown exception when the message is not null.
37
try {
38             goodInternalException = new InternalException("Valid message");
39         }
40         catch (InternalException e) {
41             fail("InternalException not expected here.");
42         }
43
44         // Testing thrown exception when the message is null.
45
try {
46             InternalException nullMessageInternalException = new InternalException(
47                     null);
48             fail("An InternalException should be thrown.");
49         }
50         catch (InternalException e) {
51             // Expected
52
}
53
54         // Verifying validity provided message is taken correctly.
55
assertEquals("Invalid exception message", "Valid message",
56                 goodInternalException.getMessage());
57     }
58
59     @Test
60     @SuppressWarnings JavaDoc("unused")
61     public void testInternalExceptionStringThrowable() {
62
63         Throwable JavaDoc cause = new Throwable JavaDoc();
64
65         // Initializing a good internal exception.
66
InternalException goodInternalException = new InternalException(
67                 "Not null", cause);
68
69         // Testing no thrown exception when message and cause not null.
70
try {
71             goodInternalException = new InternalException("Valid message",
72                     cause);
73         }
74         catch (InternalException e) {
75             fail("InternalException not expected here.");
76         }
77
78         // Testing thrown exception when message is null, cause is good.
79
try {
80             InternalException nullMessageInternalException = new InternalException(
81                     null, cause);
82             fail("An InternalException should be thrown.");
83         }
84         catch (InternalException e) {
85             // Expected
86
}
87
88         // Testing thrown exception when cause is null, message is good.
89
try {
90             InternalException nullCauseInternalException = new InternalException(
91                     "Message", null);
92             fail("An InternalException should be thrown.");
93         }
94         catch (InternalException e) {
95             // Expected
96
}
97
98         // Testing thrown exception when message and cause are null.
99
try {
100             InternalException nullMessageAndCauseInternalException = new InternalException(
101                     null, null);
102             fail("An InternalException should be thrown.");
103         }
104         catch (InternalException e) {
105             // Expected
106
}
107
108         // Verifying validity provided message is taken correctly.
109
assertEquals("Invalid exception message", "Valid message",
110                 goodInternalException.getMessage());
111
112         // Verifying validity provided message is taken correctly.
113
assertEquals("Invalid exception cause", cause, goodInternalException
114                 .getCause());
115
116     }
117
118 }
119
Popular Tags