KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > Formatter_TestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.io.*;
35 import java.util.*;
36 import net.sf.retrotranslator.tests.BaseTestCase;
37
38 /**
39  * @author Taras Puchko
40  */

41 public class Formatter_TestCase extends BaseTestCase {
42
43     public void testFormatter_1() throws Exception JavaDoc {
44         Formatter formatter = new Formatter();
45         assertTrue(formatter.out() instanceof StringBuilder JavaDoc);
46         assertEquals(Locale.getDefault(), formatter.locale());
47     }
48
49     public void testFormatter_2() throws Exception JavaDoc {
50         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
51         Formatter formatter = new Formatter(buffer);
52         assertSame(buffer, formatter.out());
53         assertEquals(Locale.getDefault(), formatter.locale());
54     }
55
56     public void testFormatter_3() throws Exception JavaDoc {
57         Formatter formatter = new Formatter(Locale.CANADA_FRENCH);
58         assertTrue(formatter.out() instanceof StringBuilder JavaDoc);
59         assertSame(Locale.CANADA_FRENCH, formatter.locale());
60     }
61
62     public void testFormatter_4() throws Exception JavaDoc {
63         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
64         Formatter formatter = new Formatter(buffer, Locale.CANADA_FRENCH);
65         assertSame(buffer, formatter.out());
66         assertSame(Locale.CANADA_FRENCH, formatter.locale());
67     }
68
69     public void testFormatter_5() throws Exception JavaDoc {
70         File file = getFile();
71         Formatter formatter = new Formatter(file.getPath());
72         assertEquals(Locale.getDefault(), formatter.locale());
73         assertToFile(formatter, file);
74     }
75
76     public void testFormatter_6() throws Exception JavaDoc {
77         File file = getFile();
78         Formatter formatter = new Formatter(file.getPath(), "UTF-8");
79         assertEquals(Locale.getDefault(), formatter.locale());
80         assertToFile(formatter, file);
81     }
82
83     public void testFormatter_7() throws Exception JavaDoc {
84         File file = getFile();
85         Formatter formatter = new Formatter(file.getPath(), "UTF-8", Locale.CANADA_FRENCH);
86         assertSame(Locale.CANADA_FRENCH, formatter.locale());
87         assertToFile(formatter, file);
88     }
89
90     public void testFormatter_8() throws Exception JavaDoc {
91         File file = getFile();
92         Formatter formatter = new Formatter(file);
93         assertEquals(Locale.getDefault(), formatter.locale());
94         assertToFile(formatter, file);
95     }
96
97     public void testFormatter_9() throws Exception JavaDoc {
98         File file = getFile();
99         Formatter formatter = new Formatter(file, "UTF-8");
100         assertEquals(Locale.getDefault(), formatter.locale());
101         assertToFile(formatter, file);
102     }
103
104     public void testFormatter_10() throws Exception JavaDoc {
105         File file = getFile();
106         Formatter formatter = new Formatter(file, "UTF-8", Locale.CANADA_FRENCH);
107         assertSame(Locale.CANADA_FRENCH, formatter.locale());
108         assertToFile(formatter, file);
109     }
110
111     public void testFormatter_11() throws Exception JavaDoc {
112         File file = getFile();
113         Formatter formatter = new Formatter(new PrintStream(new FileOutputStream(file)));
114         assertEquals(Locale.getDefault(), formatter.locale());
115         assertToFile(formatter, file);
116     }
117
118     public void testFormatter_12() throws Exception JavaDoc {
119         File file = getFile();
120         Formatter formatter = new Formatter(new FileOutputStream(file));
121         assertEquals(Locale.getDefault(), formatter.locale());
122         assertToFile(formatter, file);
123     }
124
125     public void testFormatter_13() throws Exception JavaDoc {
126         File file = getFile();
127         Formatter formatter = new Formatter(new FileOutputStream(file), "UTF-8");
128         assertEquals(Locale.getDefault(), formatter.locale());
129         assertToFile(formatter, file);
130     }
131
132     public void testFormatter_14() throws Exception JavaDoc {
133         File file = getFile();
134         Formatter formatter = new Formatter(new FileOutputStream(file), "UTF-8", Locale.CANADA_FRENCH);
135         assertSame(Locale.CANADA_FRENCH, formatter.locale());
136         assertToFile(formatter, file);
137     }
138
139     private File getFile() throws IOException {
140         File file = File.createTempFile("fmt", "tmp");
141         file.deleteOnExit();
142         return file;
143     }
144
145     private void assertToFile(Formatter formatter, File file) throws Exception JavaDoc {
146         try {
147             formatter.format("%s", "Hello");
148         } finally {
149             formatter.close();
150         }
151         BufferedReader reader = new BufferedReader(new FileReader(file));
152         try {
153             assertEquals("Hello", reader.readLine());
154         } finally {
155             reader.close();
156         }
157     }
158
159     static class MyWriter extends StringWriter {
160
161         private boolean flushed;
162         private boolean closed;
163
164         public void flush() {
165             flushed = true;
166             super.flush();
167         }
168
169         public void close() throws IOException {
170             closed = true;
171             super.close();
172         }
173     }
174
175     public void testFlush() throws Exception JavaDoc {
176         new Formatter().flush();
177         MyWriter writer = new MyWriter();
178         Formatter formatter = new Formatter(writer);
179         formatter.format("%s", "x");
180         assertFalse(writer.flushed);
181         formatter.flush();
182         assertTrue(writer.flushed);
183         assertFalse(writer.closed);
184     }
185
186     public void testClose() throws Exception JavaDoc {
187         new Formatter().close();
188         MyWriter writer = new MyWriter();
189         Formatter formatter = new Formatter(writer);
190         formatter.format("%s", "x");
191         assertFalse(writer.closed);
192         formatter.close();
193         assertTrue(writer.closed);
194         assertFalse(writer.flushed);
195     }
196
197     public void testIoException() throws Exception JavaDoc {
198         Formatter formatter = new Formatter(new Appendable JavaDoc() {
199
200             public Appendable JavaDoc append(CharSequence JavaDoc csq) throws IOException {
201                 throw new IOException("test");
202             }
203
204             public Appendable JavaDoc append(CharSequence JavaDoc csq, int start, int end) throws IOException {
205                 throw new IOException("test");
206             }
207
208             public Appendable JavaDoc append(char c) throws IOException {
209                 throw new IOException("test");
210             }
211         });
212         formatter.format("%s", "Hello");
213         assertEquals("test", formatter.ioException().getMessage());
214     }
215
216     public void testClosed() throws Exception JavaDoc {
217         Formatter formatter = new Formatter();
218         formatter.close();
219         try {
220             formatter.locale();
221             fail();
222         } catch (FormatterClosedException e) {
223             //ok
224
}
225         try {
226             formatter.out();
227             fail();
228         } catch (FormatterClosedException e) {
229             //ok
230
}
231         try {
232             formatter.toString();
233             fail();
234         } catch (FormatterClosedException e) {
235             //ok
236
}
237         try {
238             formatter.flush();
239             fail();
240         } catch (FormatterClosedException e) {
241             //ok
242
}
243         try {
244             formatter.format("");
245             fail();
246         } catch (FormatterClosedException e) {
247             //ok
248
}
249     }
250
251     public void testFormat() throws Exception JavaDoc {
252         assertFormat("AxByC", "A%sB%sC", "x", "y");
253         assertFormat("AxBxC", "A%sB%<sC", "x");
254         assertFormat("AyByC", "A%2$sB%<sC", "x", "y");
255         assertFormat("AyBxCyD", "A%2$sB%sC%sD", "x", "y");
256         assertFormat("AyBxC", "A%2$sB%1$sC", "x", "y");
257         assertFormatException(MissingFormatWidthException.class, "%-%", "x");
258         assertFormatException(IllegalFormatPrecisionException.class, "%1$10.4%", "x");
259         assertFormatException(IllegalFormatPrecisionException.class, "%1$.3n", "x");
260         assertFormatException(IllegalFormatWidthException.class, "%1$10n", "x");
261         assertFormatException(UnknownFormatConversionException.class, "A%qB", "x");
262         assertFormatException(UnknownFormatConversionException.class, "A%", "x");
263         assertFormatException(MissingFormatArgumentException.class, "A%<sB", "x");
264         assertFormatException(MissingFormatArgumentException.class, "A%3$sB", "x");
265     }
266 }
Popular Tags