1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import junit.framework.*; 27 import javax.xml.parsers.SAXParserFactory ; 28 import org.xml.sax.InputSource ; 29 import com.sun.enterprise.util.LocalStringManager; 30 import java.io.StringWriter ; 31 import java.text.MessageFormat ; 32 import java.io.StringReader ; 33 import javax.xml.parsers.SAXParser ; 34 import java.util.logging.Logger ; 35 import java.util.logging.LogRecord ; 36 import java.util.logging.Handler ; 37 import java.io.Writer ; 38 import java.io.IOException ; 39 44 45 public class LocaliserTest extends TestCase { 46 public void testLocation() throws Exception { 47 final String 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 is = getInputSource(input); 50 final String prefix = DomainXmlVerifier.class.getName(); 51 final LocalStringManager lsm = getLocalStringManager(prefix+".mymessage", "this is {0} and this is {1}"); 52 final StringWriter output = new StringWriter (); 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 { 60 final String 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 is = getInputSource(input); 63 final String prefix = DomainXmlVerifier.class.getName(); 64 final LocalStringManager lsm = getLocalStringManager(prefix+".mymessage", "this is {0} and this is {1}"); 65 final StringWriter output = new StringWriter (); 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 { 72 final String 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 is = getInputSource(input); 75 LocalStringManager lsm = getLocalStringManager("unknown", "this is {0} and this is {1}"); 76 StringWriter output = new StringWriter (); 77 StringWriter log = new StringWriter (); 78 Logger 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 { 86 final String 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 is = getInputSource(input); 89 LocalStringManager lsm = getLocalStringManager("unknown", "this is {0} and this is {1}"); 90 StringWriter output = new StringWriter (); 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 { 98 final String 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 is = getInputSource(input); 101 LocalStringManager lsm = getLocalStringManager("mymessage", "this is {0} and this is {1}"); 102 StringWriter output = new StringWriter (); 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 getLogger(final Writer log){ 109 final Logger l = Logger.getAnonymousLogger(); 110 l.setUseParentHandlers(false); 111 l.addHandler(getHandler(log)); 112 return l; 113 } 114 115 private Handler getHandler(final Writer log){ 116 return new Handler (){ 117 final Writer l = log; 118 public void publish(LogRecord record){ 119 try { 120 l.write(record.getLevel() +" "+ record.getMessage()); 121 l.flush(); 122 } 123 catch (IOException e){ 124 e.printStackTrace(); 125 } 126 127 } 128 public void flush(){ 129 } 130 public void close() throws SecurityException { 131 } 132 }; 133 } 134 135 136 private SAXParser getParser() throws Exception { 137 final SAXParserFactory spf = SAXParserFactory.newInstance(); 138 spf.setNamespaceAware(true); 139 assertTrue("should be namespace aware", spf.isNamespaceAware()); 140 return spf.newSAXParser(); 141 } 142 143 private InputSource getInputSource(final String input) throws Exception { 144 return new InputSource (new StringReader (input)); 145 } 146 147 private LocalStringManager getLocalStringManager(final String key, final String message){ 148 return new LocalStringManager(){ 149 final String k = key; 150 final String m = message; 151 public String getLocalString(final Class c, final String key, final String def){ 152 return (this.k.equals(key) ? this.m : def); 153 } 154 public String getLocalString(final Class c, final String key, final String def, final Object [] args){ 155 final String format = (this.k.equals(key) ? this.m : def); 156 final String msg = MessageFormat.format(format, args); 157 return msg; 158 } 159 }; 160 } 161 162 163 public LocaliserTest(String 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 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 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 |