1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.fraction;
19  
20  import java.text.NumberFormat;
21  import java.text.ParseException;
22  import java.util.Locale;
23  
24  import junit.framework.TestCase;
25  
26  public class FractionFormatTest extends TestCase {
27   
28      FractionFormat properFormat = null;
29      FractionFormat improperFormat = null;
30  
31      protected Locale getLocale() {
32          return Locale.getDefault();
33      }
34  
35      protected void setUp() throws Exception {
36          properFormat = FractionFormat.getProperInstance(getLocale());
37          improperFormat = FractionFormat.getImproperInstance(getLocale());
38      }
39     
40      public void testFormat() {
41          Fraction c = new Fraction(1, 2);
42          String expected = "1 / 2";
43          
44          String actual = properFormat.format(c); 
45          assertEquals(expected, actual);
46  
47          actual = improperFormat.format(c);
48          assertEquals(expected, actual);
49      }
50  
51      public void testFormatNegative() {
52          Fraction c = new Fraction(-1, 2);
53          String expected = "-1 / 2";
54  
55          String actual = properFormat.format(c); 
56          assertEquals(expected, actual);
57  
58          actual = improperFormat.format(c); 
59          assertEquals(expected, actual);
60      }
61  
62      public void testFormatZero() {
63          Fraction c = new Fraction(0, 1);
64          String expected = "0 / 1";
65  
66          String actual = properFormat.format(c); 
67          assertEquals(expected, actual);
68  
69          actual = improperFormat.format(c); 
70          assertEquals(expected, actual);
71      }
72      
73      public void testFormatImproper() {
74          Fraction c = new Fraction(5, 3);
75  
76          String actual = properFormat.format(c); 
77          assertEquals("1 2 / 3", actual);
78  
79          actual = improperFormat.format(c); 
80          assertEquals("5 / 3", actual);
81      }
82      
83      public void testFormatImproperNegative() {
84          Fraction c = new Fraction(-5, 3);
85  
86          String actual = properFormat.format(c); 
87          assertEquals("-1 2 / 3", actual);
88  
89          actual = improperFormat.format(c); 
90          assertEquals("-5 / 3", actual);
91      }
92      
93      public void testParse() {
94          String source = "1 / 2";
95  
96          try {
97              Fraction c = properFormat.parse(source);
98              assertNotNull(c);
99              assertEquals(1, c.getNumerator());
100             assertEquals(2, c.getDenominator());
101             
102             c = improperFormat.parse(source);
103             assertNotNull(c);
104             assertEquals(1, c.getNumerator());
105             assertEquals(2, c.getDenominator());
106         } catch (ParseException ex) {
107             fail(ex.getMessage());
108         }
109     }
110     
111     public void testParseInteger() {
112         String source = "10";
113         try {
114             Fraction c = properFormat.parse(source);
115             assertNotNull(c);
116             assertEquals(10, c.getNumerator());
117             assertEquals(1, c.getDenominator());
118         } catch (ParseException ex) {
119             fail(ex.getMessage());
120         }
121         try {
122             Fraction c = improperFormat.parse(source);
123             assertNotNull(c);
124             assertEquals(10, c.getNumerator());
125             assertEquals(1, c.getDenominator());
126         } catch (ParseException ex) {
127             fail(ex.getMessage());
128         }
129     }
130     
131     public void testParseInvalid() {
132         String source = "a";
133         String msg = "should not be able to parse '10 / a'.";
134         try {
135             properFormat.parse(source);
136             fail(msg);
137         } catch (ParseException ex) {
138             // success
139         }
140         try {
141             improperFormat.parse(source);
142             fail(msg);
143         } catch (ParseException ex) {
144             // success
145         }
146     }
147     
148     public void testParseInvalidDenominator() {
149         String source = "10 / a";
150         String msg = "should not be able to parse '10 / a'.";
151         try {
152             properFormat.parse(source);
153             fail(msg);
154         } catch (ParseException ex) {
155             // success
156         }
157         try {
158             improperFormat.parse(source);
159             fail(msg);
160         } catch (ParseException ex) {
161             // success
162         }
163     }
164     
165     public void testParseNegative() {
166 
167         try {
168             String source = "-1 / 2";
169             Fraction c = properFormat.parse(source);
170             assertNotNull(c);
171             assertEquals(-1, c.getNumerator());
172             assertEquals(2, c.getDenominator());
173             
174             c = improperFormat.parse(source);
175             assertNotNull(c);
176             assertEquals(-1, c.getNumerator());
177             assertEquals(2, c.getDenominator());
178 
179             source = "1 / -2";
180             c = properFormat.parse(source);
181             assertNotNull(c);
182             assertEquals(-1, c.getNumerator());
183             assertEquals(2, c.getDenominator());
184             
185             c = improperFormat.parse(source);
186             assertNotNull(c);
187             assertEquals(-1, c.getNumerator());
188             assertEquals(2, c.getDenominator());
189         } catch (ParseException ex) {
190             fail(ex.getMessage());
191         }
192     }
193     
194     public void testParseProper() {
195         String source = "1 2 / 3";
196 
197         try {
198             Fraction c = properFormat.parse(source);
199             assertNotNull(c);
200             assertEquals(5, c.getNumerator());
201             assertEquals(3, c.getDenominator());
202         } catch (ParseException ex) {
203             fail(ex.getMessage());
204         }
205         
206         try {
207             improperFormat.parse(source);
208             fail("invalid improper fraction.");
209         } catch (ParseException ex) {
210             // success
211         }
212     }
213     
214     public void testParseProperNegative() {
215         String source = "-1 2 / 3";
216         try {
217             Fraction c = properFormat.parse(source);
218             assertNotNull(c);
219             assertEquals(-5, c.getNumerator());
220             assertEquals(3, c.getDenominator());
221         } catch (ParseException ex) {
222             fail(ex.getMessage());
223         }
224         
225         try {
226             improperFormat.parse(source);
227             fail("invalid improper fraction.");
228         } catch (ParseException ex) {
229             // success
230         }
231     }
232     
233     public void testParseProperInvalidMinus() {
234         String source = "2 -2 / 3";
235         try {
236             properFormat.parse(source);
237             fail("invalid minus in improper fraction.");
238         } catch (ParseException ex) {
239             // expected
240         }
241         source = "2 2 / -3";
242         try {
243             properFormat.parse(source);
244             fail("invalid minus in improper fraction.");
245         } catch (ParseException ex) {
246             // expected
247         }
248     }
249     
250     public void testNumeratorFormat() {
251         NumberFormat old = properFormat.getNumeratorFormat();
252         NumberFormat nf = NumberFormat.getInstance();
253         nf.setParseIntegerOnly(true);
254         properFormat.setNumeratorFormat(nf);
255         assertEquals(nf, properFormat.getNumeratorFormat());
256         properFormat.setNumeratorFormat(old);
257 
258         old = improperFormat.getNumeratorFormat();
259         nf = NumberFormat.getInstance();
260         nf.setParseIntegerOnly(true);
261         improperFormat.setNumeratorFormat(nf);
262         assertEquals(nf, improperFormat.getNumeratorFormat());
263         improperFormat.setNumeratorFormat(old);
264     }
265     
266     public void testDenominatorFormat() {
267         NumberFormat old = properFormat.getDenominatorFormat();
268         NumberFormat nf = NumberFormat.getInstance();
269         nf.setParseIntegerOnly(true);
270         properFormat.setDenominatorFormat(nf);
271         assertEquals(nf, properFormat.getDenominatorFormat());
272         properFormat.setDenominatorFormat(old);
273 
274         old = improperFormat.getDenominatorFormat();
275         nf = NumberFormat.getInstance();
276         nf.setParseIntegerOnly(true);
277         improperFormat.setDenominatorFormat(nf);
278         assertEquals(nf, improperFormat.getDenominatorFormat());
279         improperFormat.setDenominatorFormat(old);
280     }
281     
282     public void testWholeFormat() {
283         ProperFractionFormat format = (ProperFractionFormat)properFormat;
284         
285         NumberFormat old = format.getWholeFormat();
286         NumberFormat nf = NumberFormat.getInstance();
287         nf.setParseIntegerOnly(true);
288         format.setWholeFormat(nf);
289         assertEquals(nf, format.getWholeFormat());
290         format.setWholeFormat(old);
291     }
292 }