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 public class FixedPointValidationPatternTest extends BaseRiceTestCase { 028 // Unlike its superclass, FixedPointValidationPattern does not use Spring. 029 FixedPointValidationPattern pattern; 030 031 @Before 032 public final void setUp() throws Exception { 033 034 pattern = new FixedPointValidationPattern(); 035 pattern.setPrecision(2); 036 pattern.setScale(1); 037 } 038 039 040 @Test public final void testDefaultAllows_empty() { 041 assertFalse(pattern.matches("")); 042 } 043 044 @Test public final void testDefaultAllows_positive1() { 045 assertTrue(pattern.matches(".1")); 046 } 047 048 @Test public final void testDefaultAllows_positive2() { 049 assertTrue(pattern.matches("0.1")); 050 } 051 052 @Test public final void testDefaultAllows_positive3() { 053 assertTrue(pattern.matches("1.1")); 054 } 055 056 @Test public final void testDefaultAllows_positive4() { 057 assertTrue(pattern.matches("1")); 058 } 059 060 @Test public final void testDefaultAllows_positive5() { 061 assertTrue(pattern.matches("1.0")); 062 } 063 064 065 @Test public final void testDefaultAllows_negative1() { 066 assertFalse(pattern.matches("-.1")); 067 } 068 069 @Test public final void testDefaultAllows_negative2() { 070 assertFalse(pattern.matches("-0.1")); 071 } 072 073 @Test public final void testDefaultAllows_negative3() { 074 assertFalse(pattern.matches("-1.1")); 075 } 076 077 @Test public final void testDefaultAllows_negative4() { 078 assertFalse(pattern.matches("-1")); 079 } 080 081 @Test public final void testDefaultAllows_negative5() { 082 assertFalse(pattern.matches("-1.0")); 083 } 084 085 086 @Test public final void testDefaultAllows_invalid1() { 087 assertFalse(pattern.matches("-.")); 088 } 089 090 @Test public final void testDefaultAllows_invalid2() { 091 assertFalse(pattern.matches("1.")); 092 } 093 094 @Test public final void testDefaultAllows_invalid3() { 095 assertFalse(pattern.matches("-1.")); 096 } 097 098 @Test public final void testDefaultAllows_invalid4() { 099 assertFalse(pattern.matches("12.")); 100 } 101 102 @Test public final void testDefaultAllows_invalid5() { 103 assertFalse(pattern.matches("1245678901234567890123.23")); 104 } 105 106 @Test public final void testDefaultAllows_invalid6() { 107 assertFalse(pattern.matches("123")); 108 } 109 110 @Test public final void testAllowNegative_positive1() { 111 assertTrue(pattern.matches(".1")); 112 } 113 114 @Test public final void testAllowNegative_positive2() { 115 pattern.setAllowNegative(true); 116 117 assertTrue(pattern.matches("0.1")); 118 } 119 120 @Test public final void testAllowNegative_positive3() { 121 pattern.setAllowNegative(true); 122 123 assertTrue(pattern.matches("1.1")); 124 } 125 126 @Test public final void testAllowNegative_positive4() { 127 pattern.setAllowNegative(true); 128 129 assertTrue(pattern.matches("1")); 130 } 131 132 @Test public final void testAllowNegative_positive5() { 133 pattern.setAllowNegative(true); 134 135 assertTrue(pattern.matches("1.0")); 136 } 137 138 139 @Test public final void testAllowNegative_negative1() { 140 pattern.setAllowNegative(true); 141 142 assertTrue(pattern.matches("-.1")); 143 } 144 145 @Test public final void testAllowNegative_negative2() { 146 pattern.setAllowNegative(true); 147 148 assertTrue(pattern.matches("-0.1")); 149 } 150 151 @Test public final void testAllowNegative_negative3() { 152 pattern.setAllowNegative(true); 153 154 assertTrue(pattern.matches("-1.1")); 155 } 156 157 @Test public final void testAllowNegative_negative4() { 158 pattern.setAllowNegative(true); 159 160 assertTrue(pattern.matches("-1")); 161 } 162 163 @Test public final void testAllowNegative_negative5() { 164 pattern.setAllowNegative(true); 165 166 assertTrue(pattern.matches("-1.0")); 167 } 168 169 170 @Test public final void testAllowNegative_invalid1() { 171 pattern.setAllowNegative(true); 172 173 assertFalse(pattern.matches("-.")); 174 } 175 176 @Test public final void testAllowNegative_invalid2() { 177 pattern.setAllowNegative(true); 178 179 assertFalse(pattern.matches("1.")); 180 } 181 182 @Test public final void testAllowNegative_invalid3() { 183 pattern.setAllowNegative(true); 184 185 assertFalse(pattern.matches("-1.")); 186 } 187 188 @Test public final void testAllowNegative_invalid4() { 189 pattern.setAllowNegative(true); 190 191 assertFalse(pattern.matches("-12.")); 192 } 193 194 @Test public final void testAllowNegative_invalid5() { 195 pattern.setAllowNegative(true); 196 197 assertFalse(pattern.matches("-1.23")); 198 } 199 200 @Test public final void testAllowNegative_invalid6() { 201 pattern.setAllowNegative(true); 202 203 assertFalse(pattern.matches("123.")); 204 } 205 206 @Test public final void testAllowNegative_invalid7() { 207 pattern.setAllowNegative(true); 208 209 assertFalse(pattern.matches(".123")); 210 } 211 212 @Test(expected=ValidationPattern.ValidationPatternException.class) 213 public final void testZero_Percision_And_Scale_invalid8() { 214 //what happens when precision & scale are not set in DD 215 pattern.setPrecision(0); 216 pattern.setScale(0); 217 pattern.completeValidation(); 218 } 219 220 @Test(expected=ValidationPattern.ValidationPatternException.class) 221 public final void testPrecision_Less_Than_Scale_invalid9() { 222 pattern.setPrecision(1); 223 pattern.setScale(3); 224 pattern.completeValidation(); 225 } 226 227 @Test(expected=ValidationPattern.ValidationPatternException.class) 228 public final void testPrecision_And_Scale_Negative_invalid10() { 229 pattern.setPrecision(-3); 230 pattern.setScale(-1); 231 pattern.completeValidation(); 232 } 233 234 }