001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation.fieldlevel; 017 018 import org.junit.Before; 019 import org.junit.Test; 020 import org.kuali.rice.kns.datadictionary.validation.fieldlevel.FixedPointValidationPattern; 021 import org.kuali.rice.krad.datadictionary.validation.ValidationPattern; 022 import org.kuali.rice.test.BaseRiceTestCase; 023 024 import static org.junit.Assert.assertFalse; 025 import static org.junit.Assert.assertTrue; 026 027 /** 028 * FixedPointValidationPatternTest tests {@link FixedPointValidationPattern} 029 * 030 * <p>Valid negative and positive fixed point numbers should match</p> 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 */ 034 public class FixedPointValidationPatternTest extends BaseRiceTestCase { 035 // Unlike its superclass, FixedPointValidationPattern does not use Spring. 036 FixedPointValidationPattern pattern; 037 038 @Before 039 public final void setUp() throws Exception { 040 041 pattern = new FixedPointValidationPattern(); 042 pattern.setPrecision(2); 043 pattern.setScale(1); 044 } 045 046 047 @Test public final void testDefaultAllows_empty() { 048 assertFalse(pattern.matches("")); 049 } 050 051 @Test public final void testDefaultAllows_positive1() { 052 assertTrue(pattern.matches(".1")); 053 } 054 055 @Test public final void testDefaultAllows_positive2() { 056 assertTrue(pattern.matches("0.1")); 057 } 058 059 @Test public final void testDefaultAllows_positive3() { 060 assertTrue(pattern.matches("1.1")); 061 } 062 063 @Test public final void testDefaultAllows_positive4() { 064 assertTrue(pattern.matches("1")); 065 } 066 067 @Test public final void testDefaultAllows_positive5() { 068 assertTrue(pattern.matches("1.0")); 069 } 070 071 072 @Test public final void testDefaultAllows_negative1() { 073 assertFalse(pattern.matches("-.1")); 074 } 075 076 @Test public final void testDefaultAllows_negative2() { 077 assertFalse(pattern.matches("-0.1")); 078 } 079 080 @Test public final void testDefaultAllows_negative3() { 081 assertFalse(pattern.matches("-1.1")); 082 } 083 084 @Test public final void testDefaultAllows_negative4() { 085 assertFalse(pattern.matches("-1")); 086 } 087 088 @Test public final void testDefaultAllows_negative5() { 089 assertFalse(pattern.matches("-1.0")); 090 } 091 092 093 @Test public final void testDefaultAllows_invalid1() { 094 assertFalse(pattern.matches("-.")); 095 } 096 097 @Test public final void testDefaultAllows_invalid2() { 098 assertFalse(pattern.matches("1.")); 099 } 100 101 @Test public final void testDefaultAllows_invalid3() { 102 assertFalse(pattern.matches("-1.")); 103 } 104 105 @Test public final void testDefaultAllows_invalid4() { 106 assertFalse(pattern.matches("12.")); 107 } 108 109 @Test public final void testDefaultAllows_invalid5() { 110 assertFalse(pattern.matches("1245678901234567890123.23")); 111 } 112 113 @Test public final void testDefaultAllows_invalid6() { 114 assertFalse(pattern.matches("123")); 115 } 116 117 @Test public final void testAllowNegative_positive1() { 118 assertTrue(pattern.matches(".1")); 119 } 120 121 @Test public final void testAllowNegative_positive2() { 122 pattern.setAllowNegative(true); 123 124 assertTrue(pattern.matches("0.1")); 125 } 126 127 @Test public final void testAllowNegative_positive3() { 128 pattern.setAllowNegative(true); 129 130 assertTrue(pattern.matches("1.1")); 131 } 132 133 @Test public final void testAllowNegative_positive4() { 134 pattern.setAllowNegative(true); 135 136 assertTrue(pattern.matches("1")); 137 } 138 139 @Test public final void testAllowNegative_positive5() { 140 pattern.setAllowNegative(true); 141 142 assertTrue(pattern.matches("1.0")); 143 } 144 145 146 @Test public final void testAllowNegative_negative1() { 147 pattern.setAllowNegative(true); 148 149 assertTrue(pattern.matches("-.1")); 150 } 151 152 @Test public final void testAllowNegative_negative2() { 153 pattern.setAllowNegative(true); 154 155 assertTrue(pattern.matches("-0.1")); 156 } 157 158 @Test public final void testAllowNegative_negative3() { 159 pattern.setAllowNegative(true); 160 161 assertTrue(pattern.matches("-1.1")); 162 } 163 164 @Test public final void testAllowNegative_negative4() { 165 pattern.setAllowNegative(true); 166 167 assertTrue(pattern.matches("-1")); 168 } 169 170 @Test public final void testAllowNegative_negative5() { 171 pattern.setAllowNegative(true); 172 173 assertTrue(pattern.matches("-1.0")); 174 } 175 176 177 @Test public final void testAllowNegative_invalid1() { 178 pattern.setAllowNegative(true); 179 180 assertFalse(pattern.matches("-.")); 181 } 182 183 @Test public final void testAllowNegative_invalid2() { 184 pattern.setAllowNegative(true); 185 186 assertFalse(pattern.matches("1.")); 187 } 188 189 @Test public final void testAllowNegative_invalid3() { 190 pattern.setAllowNegative(true); 191 192 assertFalse(pattern.matches("-1.")); 193 } 194 195 @Test public final void testAllowNegative_invalid4() { 196 pattern.setAllowNegative(true); 197 198 assertFalse(pattern.matches("-12.")); 199 } 200 201 @Test public final void testAllowNegative_invalid5() { 202 pattern.setAllowNegative(true); 203 204 assertFalse(pattern.matches("-1.23")); 205 } 206 207 @Test public final void testAllowNegative_invalid6() { 208 pattern.setAllowNegative(true); 209 210 assertFalse(pattern.matches("123.")); 211 } 212 213 @Test public final void testAllowNegative_invalid7() { 214 pattern.setAllowNegative(true); 215 216 assertFalse(pattern.matches(".123")); 217 } 218 219 @Test(expected=ValidationPattern.ValidationPatternException.class) 220 public final void testZero_Percision_And_Scale_invalid8() { 221 //what happens when precision & scale are not set in DD 222 pattern.setPrecision(0); 223 pattern.setScale(0); 224 pattern.completeValidation(); 225 } 226 227 @Test(expected=ValidationPattern.ValidationPatternException.class) 228 public final void testPrecision_Less_Than_Scale_invalid9() { 229 pattern.setPrecision(1); 230 pattern.setScale(3); 231 pattern.completeValidation(); 232 } 233 234 @Test(expected=ValidationPattern.ValidationPatternException.class) 235 public final void testPrecision_And_Scale_Negative_invalid10() { 236 pattern.setPrecision(-3); 237 pattern.setScale(-1); 238 pattern.completeValidation(); 239 } 240 241 }