|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ConstraintFactory.java | - | 100% | 100% | 100% |
|
||||||||||||||
| 1 | package com.agical.rmock.core.match.constraint; | |
| 2 | ||
| 3 | import com.agical.rmock.core.match.Expression; | |
| 4 | import com.agical.rmock.core.match.constraint.clazz.ClassConstraintFactory; | |
| 5 | import com.agical.rmock.core.match.constraint.method.MethodConstraintFactory; | |
| 6 | import com.agical.rmock.core.match.operator.NotOperator; | |
| 7 | import com.agical.rmock.core.util.PrimitiveToObject; | |
| 8 | ||
| 9 | /** | |
| 10 | * <em>(c) Agical AB 2005</em> | |
| 11 | * @author joakim.ohlrogge | |
| 12 | * | |
| 13 | */ | |
| 14 | public class ConstraintFactory { | |
| 15 | /** | |
| 16 | * Special expression to indicate that a recorded argument on this position should | |
| 17 | * not be changed. This is useful if for instance you want to modify the second | |
| 18 | * argument of a recorded expectation but leave the first argument as it was recorded. | |
| 19 | */ | |
| 20 | public final Expression AS_RECORDED = null; | |
| 21 | ||
| 22 | /** | |
| 23 | * Special expression to indicate that a recorded argument on this position should | |
| 24 | * not be changed. This is useful if for instance you want to modify the second | |
| 25 | * argument of a recorded expectation but leave the first argument as it was recorded. | |
| 26 | * @deprecated Use AS_RECORDED instead | |
| 27 | */ | |
| 28 | public final Expression NoChange = null; | |
| 29 | ||
| 30 | /** | |
| 31 | * Passes if the checked actual value is null | |
| 32 | */ | |
| 33 | public final Expression NULL = new IsNullConstraint(); | |
| 34 | ||
| 35 | /** | |
| 36 | * Checks if the checked actual value is a boolen and <em>true</em> | |
| 37 | */ | |
| 38 | public final Expression TRUE = new IsTrueConstraint(); | |
| 39 | ||
| 40 | /** | |
| 41 | * Checks if the checked actual value is a boolen and <em>false</em> | |
| 42 | */ | |
| 43 | public final Expression FALSE = new IsFalseConstraint(); | |
| 44 | ||
| 45 | /** | |
| 46 | * Checks if the checked actual value is a boolen and not null. This is | |
| 47 | * convenient way to specify the equivalent of not(NULL). | |
| 48 | */ | |
| 49 | public final Expression NOT_NULL = new NotOperator( new IsNullConstraint() ); | |
| 50 | ||
| 51 | /** | |
| 52 | * Always passes | |
| 53 | */ | |
| 54 | public final Expression ANYTHING = new AnythingConstraint(); | |
| 55 | ||
| 56 | /** | |
| 57 | * A factory with expressions for checking attributes of java.lang.Class objects. | |
| 58 | * Especially useful in dynamic suites | |
| 59 | */ | |
| 60 | public final ClassConstraintFactory clazz = new ClassConstraintFactory(); | |
| 61 | ||
| 62 | ||
| 63 | /** | |
| 64 | * A factory with expressions for checking methods. | |
| 65 | */ | |
| 66 | public final MethodConstraintFactory method = new MethodConstraintFactory(); | |
| 67 | ||
| 68 | ||
| 69 | /** | |
| 70 | * Passes if the actual object is the same reference as the ref object. | |
| 71 | * <em>actual == ref</em> | |
| 72 | * @param ref the reference to check against | |
| 73 | * @return an expression that checks if the references of ref and actual are the same. | |
| 74 | */ | |
| 75 | 58 | public Expression same(Object ref) { |
| 76 | 58 | return new SameConstraint(ref); |
| 77 | } | |
| 78 | ||
| 79 | ||
| 80 | /** | |
| 81 | * Passes if the actual object is an instance of the class passed as the parameter clazz. | |
| 82 | * @param clazz The class to check if the actual object is an instance of. | |
| 83 | * @return An expression that checks if clazz.assignableFrom(actual) | |
| 84 | */ | |
| 85 | 105 | public Expression instanceOf(Class clazz) { |
| 86 | 105 | return new InstanceOfConstraint(clazz); |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 91 | * @param ref the reference to check against | |
| 92 | * @return an expression that checks for equality | |
| 93 | */ | |
| 94 | 260 | public Expression eq(Object ref) { |
| 95 | 260 | return new EqualsConstraint(ref); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 100 | * @param ref the reference to check against | |
| 101 | * @return an expression that checks for equality | |
| 102 | */ | |
| 103 | 10 | public Expression eq(boolean ref) { |
| 104 | 10 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 109 | * @param ref the reference to check against | |
| 110 | * @return an expression that checks for equality | |
| 111 | */ | |
| 112 | 5 | public Expression eq(long ref) { |
| 113 | 5 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 118 | * @param ref the reference to check against | |
| 119 | * @return an expression that checks for equality | |
| 120 | */ | |
| 121 | 50 | public Expression eq(int ref) { |
| 122 | 50 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 127 | * @param ref the reference to check against | |
| 128 | * @return an expression that checks for equality | |
| 129 | */ | |
| 130 | 6 | public Expression eq(short ref) { |
| 131 | 6 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 136 | * @param ref the reference to check against | |
| 137 | * @return an expression that checks for equality | |
| 138 | */ | |
| 139 | 4 | public Expression eq(byte ref) { |
| 140 | 4 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 145 | * @param ref the reference to check against | |
| 146 | * @return an expression that checks for equality | |
| 147 | */ | |
| 148 | 5 | public Expression eq(double ref) { |
| 149 | 5 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 154 | * @param ref the reference to check against | |
| 155 | * @return an expression that checks for equality | |
| 156 | */ | |
| 157 | 5 | public Expression eq(float ref) { |
| 158 | 5 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Passes if the reference is equal to the actual object. <em>ref.equals(actual)</em> | |
| 163 | * @param ref the reference to check against | |
| 164 | * @return an expression that checks for equality | |
| 165 | */ | |
| 166 | 5 | public Expression eq(char ref) { |
| 167 | 5 | return new EqualsConstraint(PrimitiveToObject.convert(ref)); |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 172 | * @param ref the reference to check against | |
| 173 | * @return an expression that checks for greater than | |
| 174 | */ | |
| 175 | 11 | public Expression gt(Object ref) { |
| 176 | 11 | return new GreaterThanConstraint(ref); |
| 177 | } | |
| 178 | ||
| 179 | /** | |
| 180 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 181 | * @param ref the reference to check against | |
| 182 | * @return an expression that checks for greater than | |
| 183 | */ | |
| 184 | 1 | public Expression gt(long ref) { |
| 185 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 186 | } | |
| 187 | ||
| 188 | /** | |
| 189 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 190 | * @param ref the reference to check against | |
| 191 | * @return an expression that checks for greater than | |
| 192 | */ | |
| 193 | 5 | public Expression gt(int ref) { |
| 194 | 5 | return gt(PrimitiveToObject.convert(ref)); |
| 195 | } | |
| 196 | ||
| 197 | /** | |
| 198 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 199 | * @param ref the reference to check against | |
| 200 | * @return an expression that checks for greater than | |
| 201 | */ | |
| 202 | 1 | public Expression gt(short ref) { |
| 203 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 208 | * @param ref the reference to check against | |
| 209 | * @return an expression that checks for greater than | |
| 210 | */ | |
| 211 | 1 | public Expression gt(byte ref) { |
| 212 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 213 | } | |
| 214 | ||
| 215 | /** | |
| 216 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 217 | * @param ref the reference to check against | |
| 218 | * @return an expression that checks for greater than | |
| 219 | */ | |
| 220 | 1 | public Expression gt(double ref) { |
| 221 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 222 | } | |
| 223 | ||
| 224 | /** | |
| 225 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 226 | * @param ref the reference to check against | |
| 227 | * @return an expression that checks for greater than | |
| 228 | */ | |
| 229 | 1 | public Expression gt(float ref) { |
| 230 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 231 | } | |
| 232 | ||
| 233 | /** | |
| 234 | * Passes if the actual is greater than the reference. <em>actual > ref</em> | |
| 235 | * @param ref the reference to check against | |
| 236 | * @return an expression that checks for greater than | |
| 237 | */ | |
| 238 | 1 | public Expression gt(char ref) { |
| 239 | 1 | return gt(PrimitiveToObject.convert(ref)); |
| 240 | } | |
| 241 | ||
| 242 | ||
| 243 | /** | |
| 244 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 245 | * @param ref the reference to check against | |
| 246 | * @return an expression that checks for greater than or equals | |
| 247 | */ | |
| 248 | 7 | public Expression ge(Object ref) { |
| 249 | 7 | return new GreaterThanOrEqualConstraint(ref); |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 254 | * @param ref the reference to check against | |
| 255 | * @return an expression that checks for greater than or equals | |
| 256 | */ | |
| 257 | 1 | public Expression ge(long ref) { |
| 258 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 259 | } | |
| 260 | ||
| 261 | /** | |
| 262 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 263 | * @param ref the reference to check against | |
| 264 | * @return an expression that checks for greater than or equals | |
| 265 | */ | |
| 266 | 1 | public Expression ge(int ref) { |
| 267 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 268 | } | |
| 269 | ||
| 270 | /** | |
| 271 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 272 | * @param ref the reference to check against | |
| 273 | * @return an expression that checks for greater than or equals | |
| 274 | */ | |
| 275 | 1 | public Expression ge(short ref) { |
| 276 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 277 | } | |
| 278 | ||
| 279 | /** | |
| 280 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 281 | * @param ref the reference to check against | |
| 282 | * @return an expression that checks for greater than or equals | |
| 283 | */ | |
| 284 | 1 | public Expression ge(byte ref) { |
| 285 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 286 | } | |
| 287 | ||
| 288 | /** | |
| 289 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 290 | * @param ref the reference to check against | |
| 291 | * @return an expression that checks for greater than or equals | |
| 292 | */ | |
| 293 | 1 | public Expression ge(double ref) { |
| 294 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 295 | } | |
| 296 | ||
| 297 | /** | |
| 298 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 299 | * @param ref the reference to check against | |
| 300 | * @return an expression that checks for greater than or equals | |
| 301 | */ | |
| 302 | 1 | public Expression ge(float ref) { |
| 303 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 304 | } | |
| 305 | ||
| 306 | /** | |
| 307 | * Passes if the actual is greater than or equal to the reference. <em>actual >= ref</em> | |
| 308 | * @param ref the reference to check against | |
| 309 | * @return an expression that checks for greater than or equals | |
| 310 | */ | |
| 311 | 1 | public Expression ge(char ref) { |
| 312 | 1 | return ge(PrimitiveToObject.convert(ref)); |
| 313 | } | |
| 314 | ||
| 315 | ||
| 316 | ||
| 317 | ||
| 318 | /** | |
| 319 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 320 | * @param i the reference to check against | |
| 321 | * @return an exprsssion that checks for less than | |
| 322 | */ | |
| 323 | 10 | public Expression lt(Object ref) { |
| 324 | 10 | return new LessThanConstraint(ref); |
| 325 | } | |
| 326 | ||
| 327 | /** | |
| 328 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 329 | * @param ref the reference to check against | |
| 330 | * @return an exprsssion that checks for less than | |
| 331 | */ | |
| 332 | 1 | public Expression lt(long ref) { |
| 333 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 334 | } | |
| 335 | ||
| 336 | /** | |
| 337 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 338 | * @param ref the reference to check against | |
| 339 | * @return an exprsssion that checks for less than | |
| 340 | */ | |
| 341 | 4 | public Expression lt(int ref) { |
| 342 | 4 | return lt(PrimitiveToObject.convert(ref)); |
| 343 | } | |
| 344 | ||
| 345 | /** | |
| 346 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 347 | * @param ref the reference to check against | |
| 348 | * @return an exprsssion that checks for less than | |
| 349 | */ | |
| 350 | 1 | public Expression lt(short ref) { |
| 351 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 352 | } | |
| 353 | ||
| 354 | /** | |
| 355 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 356 | * @param ref the reference to check against | |
| 357 | * @return an exprsssion that checks for less than | |
| 358 | */ | |
| 359 | 1 | public Expression lt(byte ref) { |
| 360 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 361 | } | |
| 362 | ||
| 363 | /** | |
| 364 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 365 | * @param ref the reference to check against | |
| 366 | * @return an exprsssion that checks for less than | |
| 367 | */ | |
| 368 | 1 | public Expression lt(double ref) { |
| 369 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 370 | } | |
| 371 | ||
| 372 | /** | |
| 373 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 374 | * @param ref the reference to check against | |
| 375 | * @return an exprsssion that checks for less than | |
| 376 | */ | |
| 377 | 1 | public Expression lt(float ref) { |
| 378 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 379 | } | |
| 380 | ||
| 381 | /** | |
| 382 | * Passes if the actual is less than the reference. <em>actual < ref</em> | |
| 383 | * @param ref the reference to check against | |
| 384 | * @return an exprsssion that checks for less than | |
| 385 | */ | |
| 386 | 1 | public Expression lt(char ref) { |
| 387 | 1 | return lt(PrimitiveToObject.convert(ref)); |
| 388 | } | |
| 389 | ||
| 390 | ||
| 391 | /** | |
| 392 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 393 | * @param ref the reference to check against | |
| 394 | * @return an exprsssion that checks for less than or equal to | |
| 395 | */ | |
| 396 | 7 | public Expression le(Object ref) { |
| 397 | 7 | return new LessThanOrEqualConstraint(ref); |
| 398 | } | |
| 399 | ||
| 400 | /** | |
| 401 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 402 | * @param ref the reference to check against | |
| 403 | * @return an exprsssion that checks for less than or equal to | |
| 404 | */ | |
| 405 | 1 | public Expression le(long ref) { |
| 406 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 407 | } | |
| 408 | ||
| 409 | /** | |
| 410 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 411 | * @param ref the reference to check against | |
| 412 | * @return an exprsssion that checks for less than or equal to | |
| 413 | */ | |
| 414 | 1 | public Expression le(int ref) { |
| 415 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 416 | } | |
| 417 | ||
| 418 | /** | |
| 419 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 420 | * @param ref the reference to check against | |
| 421 | * @return an exprsssion that checks for less than or equal to | |
| 422 | */ | |
| 423 | 1 | public Expression le(short ref) { |
| 424 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 425 | } | |
| 426 | ||
| 427 | /** | |
| 428 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 429 | * @param ref the reference to check against | |
| 430 | * @return an exprsssion that checks for less than or equal to | |
| 431 | */ | |
| 432 | 1 | public Expression le(byte ref) { |
| 433 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 434 | } | |
| 435 | ||
| 436 | /** | |
| 437 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 438 | * @param ref the reference to check against | |
| 439 | * @return an exprsssion that checks for less than or equal to | |
| 440 | */ | |
| 441 | 1 | public Expression le(double ref) { |
| 442 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 443 | } | |
| 444 | ||
| 445 | /** | |
| 446 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 447 | * @param ref the reference to check againsts | |
| 448 | * @return an exprsssion that checks for less than or equal to | |
| 449 | */ | |
| 450 | 1 | public Expression le(float ref) { |
| 451 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 452 | } | |
| 453 | ||
| 454 | /** | |
| 455 | * Passes if the actual is less than or equal to the reference. <em>actual <= ref</em> | |
| 456 | * @param ref the reference to check against | |
| 457 | * @return an exprsssion that checks for less than or equal to | |
| 458 | */ | |
| 459 | 1 | public Expression le(char ref) { |
| 460 | 1 | return le(PrimitiveToObject.convert(ref)); |
| 461 | } | |
| 462 | ||
| 463 | ||
| 464 | ||
| 465 | /** | |
| 466 | * Inverts an expression | |
| 467 | * @param expression the expression to invert | |
| 468 | * @return an exprsssion that inveerts another expression | |
| 469 | */ | |
| 470 | 13 | public Expression not(Expression expression) { |
| 471 | 13 | return new NotOperator(expression); |
| 472 | } | |
| 473 | ||
| 474 | /** | |
| 475 | * Passes if the actual string is containing the value of string | |
| 476 | * @param string the String that the actual object must contain for this expression to pass | |
| 477 | * @return An expression that checks if the actual object contains a String. | |
| 478 | */ | |
| 479 | 14 | public Expression containing(String string) { |
| 480 | 14 | return new ContainsConstraint(string); |
| 481 | } | |
| 482 | ||
| 483 | ||
| 484 | /** | |
| 485 | * Passes if the actual string begins with the value of string | |
| 486 | * @param string the String that the actual object must begin with for this expression to pass | |
| 487 | * @return An expression that checks if the actual object begins with the specified string. | |
| 488 | */ | |
| 489 | 2 | public Expression startingWith(String string) { |
| 490 | 2 | return new StartsWithConstraint( string ); |
| 491 | } | |
| 492 | ||
| 493 | ||
| 494 | /** | |
| 495 | * Passes if the actual string ends with the value of string | |
| 496 | * @param string the String that the actual object must end with for this expression to pass | |
| 497 | * @return An expression that checks if the actual object ends with the specified string. | |
| 498 | */ | |
| 499 | 1 | public Expression endingWith(String string) { |
| 500 | 1 | return new EndsWithConstraint( string ); |
| 501 | } | |
| 502 | ||
| 503 | } |
|
||||||||||