KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > LocaliserTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import junit.framework.*;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import com.sun.enterprise.util.LocalStringManager;
30 import java.io.StringWriter JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import javax.xml.parsers.SAXParser JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36 import java.util.logging.Handler JavaDoc;
37 import java.io.Writer JavaDoc;
38 import java.io.IOException JavaDoc;
39 /**
40  *
41  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
42  * @version $Revision: 1.3 $
43  */

44
45 public class LocaliserTest extends TestCase {
46     public void testLocation() throws Exception JavaDoc {
47         final String JavaDoc input = "<top xmlns:m='messages'><m:location>location</m:location><m:messages><m:message id='mymessage'><m:param num='0'>Arg 1</m:param><m:param num='1'>Arg 2</m:param></m:message></m:messages></top>";
48         
49         final InputSource JavaDoc is = getInputSource(input);
50         final String JavaDoc prefix = DomainXmlVerifier.class.getName();
51         final LocalStringManager lsm = getLocalStringManager(prefix+".mymessage", "this is {0} and this is {1}");
52         final StringWriter JavaDoc output = new StringWriter JavaDoc();
53         final Localiser uut = new Localiser(lsm, output, prefix);
54         getParser().parse(is, uut);
55         assertEquals("location this is Arg 1 and this is Arg 2\n", output.toString());
56     }
57
58     
59     public void testBasicOperationWithPrefix() throws Exception JavaDoc {
60         final String JavaDoc input = "<m:messages xmlns:m='messages'><m:message id='mymessage'><m:param num='0'>Arg 1</m:param><m:param num='1'>Arg 2</m:param></m:message></m:messages>";
61         
62         final InputSource JavaDoc is = getInputSource(input);
63         final String JavaDoc prefix = DomainXmlVerifier.class.getName();
64         final LocalStringManager lsm = getLocalStringManager(prefix+".mymessage", "this is {0} and this is {1}");
65         final StringWriter JavaDoc output = new StringWriter JavaDoc();
66         final Localiser uut = new Localiser(lsm, output, prefix);
67         getParser().parse(is, uut);
68         assertEquals("this is Arg 1 and this is Arg 2\n", output.toString());
69     }
70
71     public void testLoggingOperation() throws Exception JavaDoc {
72         final String JavaDoc input = "<m:messages xmlns:m='messages'><m:message id='mymessage'><m:param num='0'>Arg 1</m:param><m:param num='1'>Arg 2</m:param></m:message></m:messages>";
73         
74         InputSource JavaDoc is = getInputSource(input);
75         LocalStringManager lsm = getLocalStringManager("unknown", "this is {0} and this is {1}");
76         StringWriter JavaDoc output = new StringWriter JavaDoc();
77         StringWriter JavaDoc log = new StringWriter JavaDoc();
78         Logger JavaDoc logger = getLogger(log);
79         Localiser uut = new Localiser(lsm, output, logger);
80         getParser().parse(is, uut);
81         assertEquals("SEVERE Internal Error, message id \"mymessage\" not present in localisation file", log.toString());
82         assertEquals("", output.toString());
83 }
84         
85     public void testErrorOperation() throws Exception JavaDoc {
86         final String JavaDoc input = "<m:messages xmlns:m='messages'><m:message id='mymessage'><m:param num='0'>Arg 1</m:param><m:param num='1'>Arg 2</m:param></m:message></m:messages>";
87         
88         InputSource JavaDoc is = getInputSource(input);
89         LocalStringManager lsm = getLocalStringManager("unknown", "this is {0} and this is {1}");
90         StringWriter JavaDoc output = new StringWriter JavaDoc();
91         Localiser uut = new Localiser(lsm, output);
92         getParser().parse(is, uut);
93         assertEquals("Internal Error, message id \"mymessage\" not present in localisation file\n", output.toString());
94 }
95     
96         
97     public void testBasicOperation() throws Exception JavaDoc {
98         final String JavaDoc input = "<m:messages xmlns:m='messages'><m:message id='mymessage'><m:param num='0'>Arg 1</m:param><m:param num='1'>Arg 2</m:param></m:message></m:messages>";
99         
100         InputSource JavaDoc is = getInputSource(input);
101         LocalStringManager lsm = getLocalStringManager("mymessage", "this is {0} and this is {1}");
102         StringWriter JavaDoc output = new StringWriter JavaDoc();
103         Localiser uut = new Localiser(lsm, output);
104         getParser().parse(is, uut);
105         assertEquals("this is Arg 1 and this is Arg 2\n", output.toString());
106     }
107
108     private Logger JavaDoc getLogger(final Writer JavaDoc log){
109         final Logger JavaDoc l = Logger.getAnonymousLogger();
110         l.setUseParentHandlers(false);
111         l.addHandler(getHandler(log));
112         return l;
113     }
114
115     private Handler JavaDoc getHandler(final Writer JavaDoc log){
116         return new Handler JavaDoc(){
117                 final Writer JavaDoc l = log;
118                 public void publish(LogRecord JavaDoc record){
119                     try {
120                         l.write(record.getLevel() +" "+ record.getMessage());
121                         l.flush();
122                     }
123                     catch (IOException JavaDoc e){
124                         e.printStackTrace();
125                     }
126                     
127                 }
128                 public void flush(){
129                 }
130                 public void close() throws SecurityException JavaDoc {
131                 }
132             };
133     }
134     
135                 
136     private SAXParser JavaDoc getParser() throws Exception JavaDoc {
137         final SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
138         spf.setNamespaceAware(true);
139         assertTrue("should be namespace aware", spf.isNamespaceAware());
140         return spf.newSAXParser();
141     }
142     
143     private InputSource JavaDoc getInputSource(final String JavaDoc input) throws Exception JavaDoc {
144         return new InputSource JavaDoc(new StringReader JavaDoc(input));
145     }
146
147     private LocalStringManager getLocalStringManager(final String JavaDoc key, final String JavaDoc message){
148         return new LocalStringManager(){
149                 final String JavaDoc k = key;
150                 final String JavaDoc m = message;
151                 public String JavaDoc getLocalString(final Class JavaDoc c, final String JavaDoc key, final String JavaDoc def){
152                     return (this.k.equals(key) ? this.m : def);
153                 }
154                 public String JavaDoc getLocalString(final Class JavaDoc c, final String JavaDoc key, final String JavaDoc def, final Object JavaDoc [] args){
155                     final String JavaDoc format = (this.k.equals(key) ? this.m : def);
156                     final String JavaDoc msg = MessageFormat.format(format, args);
157                     return msg;
158                 }
159             };
160     }
161
162
163     public LocaliserTest(String JavaDoc name){
164         super(name);
165     }
166
167     protected void setUp() {
168     }
169
170     protected void tearDown() {
171     }
172
173     private void nyi(){
174         fail("Not Yet Implemented");
175     }
176
177     public static void main(String JavaDoc args[]){
178         if (args.length == 0){
179             junit.textui.TestRunner.run(LocaliserTest.class);
180         } else {
181             junit.textui.TestRunner.run(makeSuite(args));
182         }
183     }
184     private static TestSuite makeSuite(String JavaDoc args[]){
185         final TestSuite ts = new TestSuite();
186         for (int i = 0; i < args.length; i++){
187             ts.addTest(new LocaliserTest(args[i]));
188         }
189         return ts;
190     }
191 }
192
Popular Tags