Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jformatter/text/test/PrintfTests.java


1   /* This file is part of Formatter API, which provides C-style printf 
2    * and scanf text formatting.
3    *
4    * Copyright (c) 2001 Tero Vuorela (tero@vuorela.net)
5    * All rights reserved.
6    *         
7    * Redistribution and use in source and binary forms, with or without 
8    * modification, are permitted provided that the following conditions are met:
9    * 
10   * 1. Redistributions of source code must retain the above copyright notice, 
11   *    this list of conditions and the following disclaimer.
12   *       
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   * 
17   * 3. The name of the author may not be used to endorse or promote products 
18   *    derived from this software without specific prior written permission. 
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
21   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
22   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
23   * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
24   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
26   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
27   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
28   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
29   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * $Date:$
32   * $Author:$
33   * 
34   */
35  
36  package jformatter.text.test;
37  
38  import java.util.Locale;
39  import java.util.Vector;
40  import junit.framework.Test;
41  import junit.framework.TestCase;
42  import junit.framework.TestSuite;
43  
44  import jformatter.text.Printf;
45  import jformatter.text.FormatException;
46  import jformatter.text.Parameters;
47  
48  import jformatter.text.format.FormatPosition;
49  
50  /**
51   */
52  
53  public class PrintfTests extends TestCase {
54  
55      private Printf m_printf;
56      private Vector m_vec;
57      private Parameters m_params;
58  
59      /**
60       */
61      public PrintfTests(String name) {
62    super(name);
63      }
64  
65      /**
66       */
67      protected void setUp() {
68    //Locale.setDefault(new Locale("en", "GB"));
69    m_printf = new Printf();
70    m_vec = new Vector();
71    m_params = new Parameters();
72      }
73  
74      /**
75       */
76      public static Test suite() {
77    return new TestSuite(PrintfTests.class);
78      }
79  
80      //==============================================================
81      // Actual tests start here
82      //==============================================================
83  
84      /**
85       * Idea: Format pattern doesn't contain any type information.
86       *       So we'll get the same string back.
87       */
88      public void test_plain_formatting() throws Exception {
89          String testStr = "this is a test";
90          m_printf.setPattern(testStr);
91          String result = m_printf.print(new Vector());
92          
93          assert("Got \""+ result +"\" instead of \""+ testStr +"\"", 
94           testStr.equals(result));
95      }
96  
97      public void test_plain_percent_character() throws Exception {
98          String testStr  = "test  %%   test %%";
99          String expected = "test  %   test %";
100         m_printf.setPattern(testStr);
101         String result = m_printf.print(new Vector());
102         
103         assert("Got \""+ result +"\" instead of \""+ testStr +"\"", 
104          expected.equals(result));
105     }
106     
107     public void test_string_formatting_plain() throws Exception {
108         String testStr = "this is a ";
109         String testStr2 = "monkey";
110         String testStr3 = ", or isn't ?";
111         String expected = testStr + testStr2 + testStr3;
112 
113         m_printf.setPattern(testStr + "%s" + testStr3);
114         m_vec.addElement(testStr2);
115         String result = m_printf.print(m_vec);
116         
117         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
118          expected.equals(result));
119     }
120 
121     public void test_string_formatting_min_width() throws Exception {
122         String testStr = "(4) spaces ==>%8s<==";
123         String testStr2 = "test";
124         String expected = "(4) spaces ==>    test<==";
125 
126         m_printf.setPattern(testStr);
127         m_vec.addElement(testStr2);
128         String result = m_printf.print(m_vec);
129         
130         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
131          expected.equals(result));
132     }
133 
134     public void test_string_formatting_precision() throws Exception {
135         String testStr = "(4) chars ==>%.4s<==";
136         String testStr2 = "testXXXXXXXXX";
137         String expected = "(4) chars ==>test<==";
138 
139         m_printf.setPattern(testStr);
140         m_vec.addElement(testStr2);
141         String result = m_printf.print(m_vec);
142         
143         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
144          expected.equals(result));
145     }
146 
147  
148     public void test_string_formatting_pad_right_with_zeros_precision() 
149   throws Exception {
150         String testStr = "(4) chars + 4 zeros ==>%-8.4s<==";
151         String testStr2 = "testXXXXX";
152         String expected = "(4) chars + 4 zeros ==>test    <==";
153 
154         m_printf.setPattern(testStr);
155         m_vec.addElement(testStr2);
156         String result = m_printf.print(m_vec);
157         
158         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
159          expected.equals(result));
160     }
161 
162     public void test_string_formatting_int_params() throws Exception {
163         String testStr = "%#-*.*s %-5.-3s";
164         m_params.add(4).add(2).add("word").add("word");
165 
166         m_printf.setPattern(testStr);
167         String result = m_printf.print(m_params);
168         
169         assert("Got '"+ result +"' instead of 'wo   word '",
170          result.equals("wo   word "));
171 
172     }
173 
174     public void test_string_formatting_wrong_flag() 
175   throws Exception {
176   
177   try {
178       m_printf.setPattern(" %?.4s ");
179       assert("Should have thrown exception", false);
180         } catch (FormatException e) {
181       // OK
182   }
183 
184     }
185 
186     
187     public void test_integer_formatting() 
188   throws Exception {
189   
190   String pattern  = "%#-4hd %li %+.4i %o %u %#x %X";
191   String expected = "12   -1234 +0012 11 1 0xc8 C8";
192   m_params.add((short)12).add((long)-1234).add(12).add(9).add(-1).add(200).add(200);
193   m_printf.setPattern(pattern);
194   String result = m_printf.print(m_params);
195 
196   assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
197          expected.equals(result));
198     }
199 
200  
201     public void test_counter() throws Exception {
202         String testStr = "(4) chars ==>%.4s%n<==";
203         String testStr2 = "testXXXXXXXXX";
204         String expected = "(4) chars ==>test<==";
205 
206         m_printf.setPattern(testStr);
207         m_vec.addElement(testStr2);
208   FormatPosition pos = new FormatPosition();
209   m_vec.addElement(pos);
210 
211         String result = m_printf.print(m_vec);
212         
213         assert("should got 17", pos.getIndex() == 17);
214     }
215 
216     public void test_class_print() throws Exception {
217         String testStr = "%p";
218         String expected = "java.lang.Short";
219 
220         m_printf.setPattern(testStr);
221         m_vec.addElement(new Short((short)1));
222 
223         String result = m_printf.print(m_vec);
224         
225         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
226      expected.equals(result));
227 
228     }
229 
230     public void test_character_format() throws Exception {
231         String testStr = "%2c %03c %0-3c";
232         String expected = " A 00A A  ";
233 
234         m_printf.setPattern(testStr);
235   m_params.add('A').add('A').add('A');
236  
237         String result = m_printf.print(m_params);
238         
239         assert("Got \""+ result +"\" instead of \""+ expected +"\"", 
240      expected.equals(result));
241 
242     }
243 
244     public void test_decimal_format() throws Exception {
245         String testStr = "%f %f %+f";
246         String expected = "0.123456 -10.123457 +0.123456";
247         m_printf.setPattern(testStr);
248   m_params.add(0.123456f).add(-10.12345678f).add(0.123456f);
249  
250         String result = m_printf.print(m_params);
251         
252         assert("Got '"+ result +"' instead of '"+ expected +"'", 
253      expected.equals(result));
254 
255     }
256 
257     public void test_decimal_format2() throws Exception {
258         String testStr = "%5.2f % f";
259         String expected = " 0.12  0.123457";
260         m_printf.setPattern(testStr);
261   m_params.add(0.123456f).add(0.12345678f);
262  
263         String result = m_printf.print(m_params);
264         
265         assert("Got '"+ result +"' instead of '"+ expected +"'", 
266      expected.equals(result));
267 
268     }
269 
270     public void test_decimal_format3() throws Exception {
271         String testStr = "%e %E %e";
272         String expected = "1.234560e-01 1.234568E+12 -1.234567e+00";
273         m_printf.setPattern(testStr);
274   m_params.add(0.123456f).add(1234567891234f).add(-1.234567f);
275  
276         String result = m_printf.print(m_params);
277         
278         assert("Got '"+ result +"' instead of '"+ expected +"'", 
279      expected.equals(result));
280 
281     }
282 
283     public void test_decimal_format_localisation() throws Exception {
284   Printf p = new Printf("%f", new Locale("fi", "FI"));
285   m_params.add(0.123456f);
286   String result = p.print(m_params);
287         
288         assert("Got '"+ result +"' instead of '0,123456'", 
289      result.equals("0,123456"));
290 
291     }
292 
293     public void test_parameters() throws Exception {
294   Printf p = new Printf("%s");
295   m_params.add("test");
296   String result = p.print(m_params);
297         
298     }
299     
300 }