001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.geometry; 019 020 import java.text.NumberFormat; 021 import java.text.ParseException; 022 import java.text.ParsePosition; 023 import java.util.Locale; 024 025 import junit.framework.TestCase; 026 027 import org.apache.commons.math.util.CompositeFormat; 028 029 public abstract class Vector3DFormatAbstractTest extends TestCase { 030 031 Vector3DFormat vector3DFormat = null; 032 Vector3DFormat vector3DFormatSquare = null; 033 034 protected abstract Locale getLocale(); 035 036 protected abstract char getDecimalCharacter(); 037 038 @Override 039 protected void setUp() throws Exception { 040 vector3DFormat = Vector3DFormat.getInstance(getLocale()); 041 final NumberFormat nf = NumberFormat.getInstance(getLocale()); 042 nf.setMaximumFractionDigits(2); 043 vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf); 044 } 045 046 public void testSimpleNoDecimals() { 047 Vector3D c = new Vector3D(1, 1, 1); 048 String expected = "{1; 1; 1}"; 049 String actual = vector3DFormat.format(c); 050 assertEquals(expected, actual); 051 } 052 053 public void testSimpleWithDecimals() { 054 Vector3D c = new Vector3D(1.23, 1.43, 1.63); 055 String expected = 056 "{1" + getDecimalCharacter() + 057 "23; 1" + getDecimalCharacter() + 058 "43; 1" + getDecimalCharacter() + 059 "63}"; 060 String actual = vector3DFormat.format(c); 061 assertEquals(expected, actual); 062 } 063 064 public void testSimpleWithDecimalsTrunc() { 065 Vector3D c = new Vector3D(1.2323, 1.4343, 1.6333); 066 String expected = 067 "{1" + getDecimalCharacter() + 068 "23; 1" + getDecimalCharacter() + 069 "43; 1" + getDecimalCharacter() + 070 "63}"; 071 String actual = vector3DFormat.format(c); 072 assertEquals(expected, actual); 073 } 074 075 public void testNegativeX() { 076 Vector3D c = new Vector3D(-1.2323, 1.4343, 1.6333); 077 String expected = 078 "{-1" + getDecimalCharacter() + 079 "23; 1" + getDecimalCharacter() + 080 "43; 1" + getDecimalCharacter() + 081 "63}"; 082 String actual = vector3DFormat.format(c); 083 assertEquals(expected, actual); 084 } 085 086 public void testNegativeY() { 087 Vector3D c = new Vector3D(1.2323, -1.4343, 1.6333); 088 String expected = 089 "{1" + getDecimalCharacter() + 090 "23; -1" + getDecimalCharacter() + 091 "43; 1" + getDecimalCharacter() + 092 "63}"; 093 String actual = vector3DFormat.format(c); 094 assertEquals(expected, actual); 095 } 096 097 public void testNegativeZ() { 098 Vector3D c = new Vector3D(1.2323, 1.4343, -1.6333); 099 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 }