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.geometry;
19  
20  import java.text.NumberFormat;
21  import java.text.ParseException;
22  import java.text.ParsePosition;
23  import java.util.Locale;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.math.util.CompositeFormat;
28  
29  public abstract class Vector3DFormatAbstractTest extends TestCase {
30   
31      Vector3DFormat vector3DFormat = null;
32      Vector3DFormat vector3DFormatSquare = null;
33  
34      protected abstract Locale getLocale();
35  
36      protected abstract char getDecimalCharacter();
37  
38      @Override
39      protected void setUp() throws Exception {
40          vector3DFormat = Vector3DFormat.getInstance(getLocale());
41          final NumberFormat nf = NumberFormat.getInstance(getLocale());
42          nf.setMaximumFractionDigits(2);
43          vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf);
44      }
45     
46      public void testSimpleNoDecimals() {
47          Vector3D c = new Vector3D(1, 1, 1);
48          String expected = "{1; 1; 1}";
49          String actual = vector3DFormat.format(c); 
50          assertEquals(expected, actual);
51      }
52  
53      public void testSimpleWithDecimals() {
54          Vector3D c = new Vector3D(1.23, 1.43, 1.63);
55          String expected =
56              "{1"    + getDecimalCharacter() +
57              "23; 1" + getDecimalCharacter() +
58              "43; 1" + getDecimalCharacter() +
59              "63}";
60          String actual = vector3DFormat.format(c); 
61          assertEquals(expected, actual);
62      }
63  
64      public void testSimpleWithDecimalsTrunc() {
65          Vector3D c = new Vector3D(1.2323, 1.4343, 1.6333);
66          String expected =
67              "{1"    + getDecimalCharacter() +
68              "23; 1" + getDecimalCharacter() +
69              "43; 1" + getDecimalCharacter() +
70              "63}";
71          String actual = vector3DFormat.format(c); 
72          assertEquals(expected, actual);
73      }
74  
75      public void testNegativeX() {
76          Vector3D c = new Vector3D(-1.2323, 1.4343, 1.6333);
77          String expected =
78              "{-1"    + getDecimalCharacter() +
79              "23; 1" + getDecimalCharacter() +
80              "43; 1" + getDecimalCharacter() +
81              "63}";
82          String actual = vector3DFormat.format(c); 
83          assertEquals(expected, actual);
84      }
85  
86      public void testNegativeY() {
87          Vector3D c = new Vector3D(1.2323, -1.4343, 1.6333);
88          String expected =
89              "{1"    + getDecimalCharacter() +
90              "23; -1" + getDecimalCharacter() +
91              "43; 1" + getDecimalCharacter() +
92              "63}";
93          String actual = vector3DFormat.format(c); 
94          assertEquals(expected, actual);
95      }
96  
97      public void testNegativeZ() {
98          Vector3D c = new Vector3D(1.2323, 1.4343, -1.6333);
99          String expected =
100             "{1"    + getDecimalCharacter() +
101             "23; 1" + getDecimalCharacter() +
102             "43; -1" + getDecimalCharacter() +
103             "63}";
104         String actual = vector3DFormat.format(c); 
105         assertEquals(expected, actual);
106     }
107 
108     public void testNonDefaultSetting() {
109         Vector3D c = new Vector3D(1, 1, 1);
110         String expected = "[1 : 1 : 1]";
111         String actual = vector3DFormatSquare.format(c); 
112         assertEquals(expected, actual);
113     }
114     
115     public void testStaticFormatVector3D() {
116         Locale defaultLocal = Locale.getDefault();
117         Locale.setDefault(getLocale());
118         
119         Vector3D c = new Vector3D(232.222, -342.33, 432.444);
120         String expected =
121             "{232"    + getDecimalCharacter() +
122             "22; -342" + getDecimalCharacter() +
123             "33; 432" + getDecimalCharacter() +
124             "44}";
125         String actual = Vector3DFormat.formatVector3D(c); 
126         assertEquals(expected, actual);
127         
128         Locale.setDefault(defaultLocal);
129     }
130 
131     public void testNan() {
132         Vector3D c = Vector3D.NaN;
133         String expected = "{(NaN); (NaN); (NaN)}";
134         String actual = vector3DFormat.format(c); 
135         assertEquals(expected, actual);
136     }
137 
138     public void testPositiveInfinity() {
139         Vector3D c = Vector3D.POSITIVE_INFINITY;
140         String expected = "{(Infinity); (Infinity); (Infinity)}";
141         String actual = vector3DFormat.format(c); 
142         assertEquals(expected, actual);
143     }
144 
145     public void tesNegativeInfinity() {
146         Vector3D c = Vector3D.NEGATIVE_INFINITY;
147         String expected = "{(-Infinity); (-Infinity); (-Infinity)}";
148         String actual = vector3DFormat.format(c); 
149         assertEquals(expected, actual);
150     }
151 
152     public void testParseSimpleNoDecimals() {
153         String source = "{1; 1; 1}";
154         Vector3D expected = new Vector3D(1, 1, 1);
155         try {
156             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
157             assertEquals(expected, actual);
158         } catch (ParseException ex) {
159             fail(ex.getMessage());
160         }
161     }
162 
163     public void testParseIgnoredWhitespace() {
164         Vector3D expected = new Vector3D(1, 1, 1);
165         ParsePosition pos1 = new ParsePosition(0);
166         String source1 = "{1;1;1}";
167         assertEquals(expected, vector3DFormat.parseObject(source1, pos1));
168         assertEquals(source1.length(), pos1.getIndex());
169         ParsePosition pos2 = new ParsePosition(0);
170         String source2 = " { 1 ; 1 ; 1 } ";
171         assertEquals(expected, vector3DFormat.parseObject(source2, pos2));
172         assertEquals(source2.length() - 1, pos2.getIndex());
173     }
174 
175     public void testParseSimpleWithDecimals() {
176         String source =
177             "{1" + getDecimalCharacter() +
178             "23; 1" + getDecimalCharacter() +
179             "43; 1" + getDecimalCharacter() +
180             "63}";
181         Vector3D expected = new Vector3D(1.23, 1.43, 1.63);
182         try {
183             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
184             assertEquals(expected, actual);
185         } catch (ParseException ex) {
186             fail(ex.getMessage());
187         }
188     }
189 
190     public void testParseSimpleWithDecimalsTrunc() {
191         String source =
192             "{1" + getDecimalCharacter() +
193             "2323; 1" + getDecimalCharacter() +
194             "4343; 1" + getDecimalCharacter() +
195             "6333}";
196         Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
197         try {
198             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
199             assertEquals(expected, actual);
200         } catch (ParseException ex) {
201             fail(ex.getMessage());
202         }
203     }
204 
205     public void testParseNegativeX() {
206         String source =
207             "{-1" + getDecimalCharacter() +
208             "2323; 1" + getDecimalCharacter() +
209             "4343; 1" + getDecimalCharacter() +
210             "6333}";
211         Vector3D expected = new Vector3D(-1.2323, 1.4343, 1.6333);
212         try {
213             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
214             assertEquals(expected, actual);
215         } catch (ParseException ex) {
216             fail(ex.getMessage());
217         }
218     }
219 
220     public void testParseNegativeY() {
221         String source =
222             "{1" + getDecimalCharacter() +
223             "2323; -1" + getDecimalCharacter() +
224             "4343; 1" + getDecimalCharacter() +
225             "6333}";
226         Vector3D expected = new Vector3D(1.2323, -1.4343, 1.6333);
227         try {
228             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
229             assertEquals(expected, actual);
230         } catch (ParseException ex) {
231             fail(ex.getMessage());
232         }
233     }
234 
235     public void testParseNegativeZ() {
236         String source =
237             "{1" + getDecimalCharacter() +
238             "2323; 1" + getDecimalCharacter() +
239             "4343; -1" + getDecimalCharacter() +
240             "6333}";
241         Vector3D expected = new Vector3D(1.2323, 1.4343, -1.6333);
242         try {
243             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
244             assertEquals(expected, actual);
245         } catch (ParseException ex) {
246             fail(ex.getMessage());
247         }
248     }
249 
250     public void testParseNegativeAll() {
251         String source =
252             "{-1" + getDecimalCharacter() +
253             "2323; -1" + getDecimalCharacter() +
254             "4343; -1" + getDecimalCharacter() +
255             "6333}";
256         Vector3D expected = new Vector3D(-1.2323, -1.4343, -1.6333);
257         try {
258             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
259             assertEquals(expected, actual);
260         } catch (ParseException ex) {
261             fail(ex.getMessage());
262         }
263     }
264 
265     public void testParseZeroX() {
266         String source =
267             "{0" + getDecimalCharacter() +
268             "0; -1" + getDecimalCharacter() +
269             "4343; 1" + getDecimalCharacter() +
270             "6333}";
271         Vector3D expected = new Vector3D(0.0, -1.4343, 1.6333);
272         try {
273             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
274             assertEquals(expected, actual);
275         } catch (ParseException ex) {
276             fail(ex.getMessage());
277         }
278     }
279 
280     public void testParseNonDefaultSetting() {
281         String source =
282             "[1" + getDecimalCharacter() +
283             "2323 : 1" + getDecimalCharacter() +
284             "4343 : 1" + getDecimalCharacter() +
285             "6333]";
286         Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
287         try {
288             Vector3D actual = (Vector3D) vector3DFormatSquare.parseObject(source); 
289             assertEquals(expected, actual);
290         } catch (ParseException ex) {
291             fail(ex.getMessage());
292         }
293     }
294     
295     public void testParseNan() {
296         String source = "{(NaN); (NaN); (NaN)}";
297         try {
298             Vector3D actual = (Vector3D) vector3DFormat.parseObject(source); 
299             assertEquals(Vector3D.NaN, actual);
300         } catch (ParseException ex) {
301             fail(ex.getMessage());
302         }
303     }
304 
305     public void testParsePositiveInfinity() {
306         String source = "{(Infinity); (Infinity); (Infinity)}";
307         try {
308             Vector3D actual = (Vector3D)vector3DFormat.parseObject(source); 
309             assertEquals(Vector3D.POSITIVE_INFINITY, actual);
310         } catch (ParseException ex) {
311             fail(ex.getMessage());
312         }
313     }
314 
315     public void testParseNegativeInfinity() {
316         String source = "{(-Infinity); (-Infinity); (-Infinity)}";
317         try {
318             Vector3D actual = (Vector3D)vector3DFormat.parseObject(source); 
319             assertEquals(Vector3D.NEGATIVE_INFINITY, actual);
320         } catch (ParseException ex) {
321             fail(ex.getMessage());
322         }
323     }
324 
325     public void testConstructorSingleFormat() {
326         NumberFormat nf = NumberFormat.getInstance();
327         Vector3DFormat cf = new Vector3DFormat(nf);
328         assertNotNull(cf);
329         assertEquals(nf, cf.getFormat());
330     }
331     
332     public void testFormatObject() {
333         try {
334             CompositeFormat cf = new Vector3DFormat();
335             Object object = new Object();
336             cf.format(object);
337             fail();
338         } catch (IllegalArgumentException ex) {
339             // success
340         }
341     }
342 
343     public void testForgottenPrefix() {
344         ParsePosition pos = new ParsePosition(0);
345         assertNull(new Vector3DFormat().parse("1; 1; 1}", pos));
346         assertEquals(0, pos.getErrorIndex());
347     }
348 
349     public void testForgottenSeparator() {
350         ParsePosition pos = new ParsePosition(0);
351         assertNull(new Vector3DFormat().parse("{1; 1 1}", pos));
352         assertEquals(6, pos.getErrorIndex());
353     }
354 
355     public void testForgottenSuffix() {
356         ParsePosition pos = new ParsePosition(0);
357         assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos));
358         assertEquals(8, pos.getErrorIndex());
359     }
360 
361 }