KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > error > TestErrorHandler


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
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 cintoo.messages.error;
17
18 import cintoo.messages.context.PackageContext;
19 import cintoo.messages.context.DefaultContextCache;
20 import cintoo.messages.context.IdContext;
21 import cintoo.messages.bundle.DefaultBundleManager;
22 import cintoo.messages.DefaultMessageHandler;
23 import api.cintoo.messages.context.Context;
24 import api.cintoo.messages.bundle.BundleManager;
25 import api.cintoo.messages.MessageHandler;
26 import org.testng.annotations.*;
27 import org.testng.Assert;
28
29 public class TestErrorHandler {
30   @Test
31   public void testErrorContext() {
32     MessageHandler message = new DefaultMessageHandler(new DefaultBundleManager(new DefaultContextCache()));
33     ErrorHandler handler = new ErrorHandler(message);
34     handler.setBundle("errors1", IdContext.id("error1"));
35     handler.setBundle("errors2", IdContext.id("error2"));
36     String JavaDoc value1 = handler.error(IdContext.id("error1"), ErrorCode.error(1));
37     String JavaDoc value2 = handler.error(IdContext.id("error2"), ErrorCode.error(2));
38     Assert.assertEquals(value1, "value1", "ErrorHandler returns correct message");
39     Assert.assertEquals(value2, "value2", "ErrorHandler returns correct message");
40   }
41
42   @Test
43   public void testErrorPattern() {
44     MessageHandler message = new DefaultMessageHandler(new DefaultBundleManager(new DefaultContextCache()));
45     ErrorHandler handler = new ErrorHandler(message);
46     handler.setPattern("{0} - {1}");
47     Assert.assertEquals(handler.error(ErrorCode.error(12345)), "testerror12345 - 12345", "ErrorHandler returns correct message");
48     handler.clearPattern();
49   }
50
51   @Test
52   public void testError() {
53     MessageHandler message = new DefaultMessageHandler(new DefaultBundleManager(new DefaultContextCache()));
54     ErrorHandler handler = new ErrorHandler(message);
55     Assert.assertEquals(handler.error(ErrorCode.error(12345)), "testerror12345", "ErrorHandler returns correct message");
56     Assert.assertEquals(handler.error(IdContext.id("errors"), ErrorCode.error(12345)), "testerror12345", "ErrorHandler returns correct message with context");
57   }
58
59   @Test
60   public void testInitialErrorContext() {
61     ErrorHandler handler = new ErrorHandler();
62     Assert.assertEquals(handler.getErrorContext(), IdContext.id("errors"), "ErrorHandler returns correct initial context");
63   }
64
65   @Test
66   public void testBean() {
67     ErrorHandler handler = new ErrorHandler();
68     Context context = IdContext.id("testerrors");
69     handler.setErrorContext(context);
70     Assert.assertEquals(handler.getErrorContext(), context, "ErrorHandler returns correct context after setting context");
71   }
72
73 }
Popular Tags