KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > io > _PrintStreamTestCase


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.io;
33
34 import java.io.*;
35 import java.util.Locale JavaDoc;
36 import net.sf.retrotranslator.tests.BaseTestCase;
37
38 /**
39  * @author Taras Puchko
40  */

41 public class _PrintStreamTestCase extends BaseTestCase {
42
43     static class MyPrintStream extends PrintStream {
44         public MyPrintStream(File file) throws FileNotFoundException {
45             super(file);
46         }
47
48         public MyPrintStream(String JavaDoc fileName, String JavaDoc csn)
49                 throws FileNotFoundException, UnsupportedEncodingException {
50             super(fileName, csn);
51         }
52     }
53
54     public void testConstructors() throws Exception JavaDoc {
55         File file = File.createTempFile("_PrintStreamTestCase", ".tmp");
56         try {
57             writeAndClose(new PrintStream(file), "test1");
58             assertEquals("test1", readLine(file, null));
59
60             writeAndClose(new PrintStream(file, "UTF-16"), "test2");
61             assertEquals("test2", readLine(file, "UTF-16"));
62
63             writeAndClose(new PrintStream(file.getPath()), "test3");
64             assertEquals("test3", readLine(file, null));
65
66             writeAndClose(new PrintStream(file.getPath(), "UTF-16"), "test4");
67             assertEquals("test4", readLine(file, "UTF-16"));
68
69             writeAndClose(new MyPrintStream(file), "test5");
70             assertEquals("test5", readLine(file, null));
71
72             writeAndClose(new MyPrintStream(file.getPath(), "UTF-16"), "test6");
73             assertEquals("test6", readLine(file, "UTF-16"));
74
75         } finally {
76             file.delete();
77         }
78     }
79
80     private static void writeAndClose(PrintStream writer, String JavaDoc s) throws Exception JavaDoc {
81         try {
82             writer.print(s);
83         } finally {
84             writer.close();
85         }
86     }
87
88     public void testAppend() throws Exception JavaDoc {
89         ByteArrayOutputStream stream = new ByteArrayOutputStream();
90         PrintStream printStream = new PrintStream(stream);
91         printStream.append("Hi,");
92         printStream.append("Hello", 1, 3);
93         printStream.append('!');
94         assertEquals("Hi,el!", stream.toString());
95     }
96
97     public void testFormat() throws Exception JavaDoc {
98         ByteArrayOutputStream stream = new ByteArrayOutputStream();
99         PrintStream printStream = new PrintStream(stream);
100         printStream.format("Hello, %s!", "World");
101         printStream.format(Locale.FRANCE, " - %,d", 1000000);
102         assertEquals("Hello, World! - 1 000 000", stream.toString());
103     }
104
105     public void testPrintf() throws Exception JavaDoc {
106         ByteArrayOutputStream stream = new ByteArrayOutputStream();
107         PrintStream printStream = new PrintStream(stream);
108         printStream.printf("Hello, %s!", "World");
109         printStream.printf(Locale.FRANCE, " - %f", 1.2);
110         assertEquals("Hello, World! - 1,200000", stream.toString());
111     }
112
113 }
Popular Tags