View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2010 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.test.constraints;
14  
15  import java.math.BigDecimal;
16  
17  import net.sf.oval.constraint.DigitsCheck;
18  
19  /**
20   * @author Sebastian Thomschke
21   */
22  public class DigitsTest extends AbstractContraintsTest
23  {
24  	public void testDigits()
25  	{
26  		final DigitsCheck check = new DigitsCheck();
27  		super.testCheck(check);
28  		check.setMaxFraction(2);
29  		check.setMaxInteger(2);
30  
31  		assertTrue(check.isSatisfied(null, null, null, null));
32  
33  		assertTrue(check.isSatisfied(null, 0, null, null));
34  		assertTrue(check.isSatisfied(null, 0.0, null, null));
35  		assertTrue(check.isSatisfied(null, 0.00, null, null));
36  		assertTrue(check.isSatisfied(null, 0.000, null, null)); // returns true since it equals 0.0d
37  		assertFalse(check.isSatisfied(null, 0.001, null, null));
38  		assertTrue(check.isSatisfied(null, 000.0, null, null)); // returns true since it equals 0.0d
39  		assertTrue(check.isSatisfied(null, 12, null, null));
40  		assertFalse(check.isSatisfied(null, 123, null, null));
41  		assertTrue(check.isSatisfied(null, 12.12, null, null));
42  		assertFalse(check.isSatisfied(null, 12.123, null, null));
43  		assertFalse(check.isSatisfied(null, 123.12, null, null));
44  
45  		assertTrue(check.isSatisfied(null, "0", null, null));
46  		assertTrue(check.isSatisfied(null, "0.0", null, null));
47  		assertTrue(check.isSatisfied(null, "0.00", null, null));
48  		assertFalse(check.isSatisfied(null, "0.000", null, null)); // returns false since BigDecimal keeps the fraction
49  		assertFalse(check.isSatisfied(null, "0.001", null, null));
50  		assertTrue(check.isSatisfied(null, "000.0", null, null)); // TODO returns true since it is automatically converted into a BigDecimal("0.0") => should return false in case of pure strings?
51  		assertTrue(check.isSatisfied(null, "12", null, null));
52  		assertFalse(check.isSatisfied(null, "123", null, null));
53  		assertTrue(check.isSatisfied(null, "12.12", null, null));
54  		assertFalse(check.isSatisfied(null, "12.123", null, null));
55  		assertFalse(check.isSatisfied(null, "123.12", null, null));
56  
57  		assertTrue(check.isSatisfied(null, new BigDecimal("0"), null, null));
58  		assertTrue(check.isSatisfied(null, new BigDecimal("0.0"), null, null));
59  		assertTrue(check.isSatisfied(null, new BigDecimal("0.00"), null, null));
60  		assertFalse(check.isSatisfied(null, new BigDecimal("0.000"), null, null)); // TODO returns false since BigDecimal keeps the fraction => should this return true for BigDecimals ?
61  		assertFalse(check.isSatisfied(null, new BigDecimal("0.001"), null, null));
62  		assertTrue(check.isSatisfied(null, new BigDecimal("000.0"), null, null)); // returns true since it is automatically converted into a BigDecimal("0.0")
63  		assertTrue(check.isSatisfied(null, new BigDecimal("12"), null, null));
64  		assertFalse(check.isSatisfied(null, new BigDecimal("123"), null, null));
65  		assertTrue(check.isSatisfied(null, new BigDecimal("12.12"), null, null));
66  		assertFalse(check.isSatisfied(null, new BigDecimal("12.123"), null, null));
67  		assertFalse(check.isSatisfied(null, new BigDecimal("123.12"), null, null));
68  
69  		check.setMaxInteger(13);
70  		check.setMaxFraction(13);
71  		assertTrue(check.isSatisfied(null, 1234567890123L, null, null));
72  		assertFalse(check.isSatisfied(null, 12345678901234L, null, null));
73  		assertTrue(check.isSatisfied(null, "1234567890123", null, null));
74  		assertFalse(check.isSatisfied(null, "12345678901234", null, null));
75  		assertTrue(check.isSatisfied(null, new BigDecimal("1234567890123"), null, null));
76  		assertFalse(check.isSatisfied(null, new BigDecimal("12345678901234"), null, null));
77  		assertTrue(check.isSatisfied(null, new BigDecimal("1234567890123.1234567890123"), null, null));
78  		assertFalse(check.isSatisfied(null, new BigDecimal("12345678901234.12345678901234"), null, null));
79  	}
80  }