001/* 002 * Licensed to the author 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 */ 017package de.cuioss.test.generator.internal.net.java.quickcheck.generator; 018 019import java.util.Date; 020import java.util.concurrent.TimeUnit; 021 022import de.cuioss.test.generator.internal.net.java.quickcheck.ExtendibleGenerator; 023import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 024import de.cuioss.test.generator.internal.net.java.quickcheck.ObjectGenerator; 025import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution; 026 027/** 028 * {@link PrimitiveGenerators} contains factory methods for primitive value 029 * generators. These can be used to build custom test case generators. 030 * <p> 031 * The default distribution for generators is {@link Distribution#UNIFORM}. 032 * </p> 033 */ 034@SuppressWarnings({ "WeakerAccess", "unused" }) 035public class PrimitiveGenerators { 036 037 public static final int DEFAULT_STRING_MAX_LENGTH = Generators.DEFAULT_STRING_MAX_LENGTH; 038 039 /** 040 * Create a new string generator.<br> 041 * 042 * The characters are from the Basic Latin and Latin-1 Supplement unicode blocks. 043 */ 044 public static ExtendibleGenerator<Character, String> strings() { 045 return Generators.strings(); 046 } 047 048 /** 049 * Create a new string generator which generates strings of characters 050 * ranging from lo to hi. 051 * 052 * @param lo 053 * lower boundary character 054 * @param hi 055 * upper boundary character 056 */ 057 public static ExtendibleGenerator<Character, String> strings(char lo, 058 char hi) { 059 return Generators.strings(lo, hi); 060 } 061 062 /** 063 * Create a new string generator which generates strings of characters from 064 * the given string. 065 */ 066 public static ExtendibleGenerator<Character, String> strings( 067 String allowedCharacters) { 068 return Generators.strings(allowedCharacters); 069 } 070 071 /** 072 * Create a new string generator which generates strings of characters from 073 * the given string with a length between min and max. 074 */ 075 public static ExtendibleGenerator<Character, String> strings(String allowedCharacters, int min, int max) { 076 return Generators.strings(allowedCharacters, min, max); 077 } 078 079 /** 080 * Creates a new String genearator which generates strings whose length 081 * ranges from zero to given length. 082 */ 083 public static ExtendibleGenerator<Character, String> strings(int max) { 084 return Generators.strings(max); 085 } 086 087 /** 088 * Create a new string generator which generates strings of sizes ranging 089 * from loLength to hiLength. 090 * 091 * @param min 092 * lower size boundary 093 * @param max 094 * upper size boundary 095 */ 096 public static ExtendibleGenerator<Character, String> strings(int min, int max) { 097 return Generators.strings(min, max); 098 } 099 100 /** 101 * Create a new string generator which creates strings of characters 102 * generated by the given character generator with a length generated by the 103 * length generator. 104 * 105 */ 106 public static ExtendibleGenerator<Character, String> strings( 107 Generator<Integer> length, Generator<Character> characters) { 108 return Generators.strings(length, characters); 109 } 110 111 /** 112 * Create a new string generator which creates strings of characters 113 * generated by the given character generator. 114 * 115 */ 116 public static ExtendibleGenerator<Character, String> strings( 117 Generator<Character> characterGenerator) { 118 return Generators.strings(characterGenerator); 119 } 120 121 /** 122 * Create a new string generator which creates strings of characters from 123 * a-z and A-Z. 124 */ 125 public static ExtendibleGenerator<Character, String> letterStrings() { 126 return Generators.letterStrings(); 127 } 128 129 /** 130 * Create a new string generator which creates strings with sizes ranging 131 * from loLengh to hiLength of characters from a-z and A-Z. 132 */ 133 public static ExtendibleGenerator<Character, String> letterStrings(int min, int max) { 134 return Generators.letterStrings(min, max); 135 } 136 137 /** 138 * Create a new string generator which creates strings of characters 139 * generated by {@link PrimitiveGenerators#basicLatinCharacters()} and 140 * {@link PrimitiveGenerators#latin1SupplementCharacters()}. 141 */ 142 public static ExtendibleGenerator<Character, String> printableStrings() { 143 return Generators.printableStrings(); 144 } 145 146 /** 147 * Create a new string generator for strings that are not empty. 148 */ 149 public static ExtendibleGenerator<Character, String> nonEmptyStrings() { 150 return Generators.nonEmptyStrings(); 151 } 152 153 /** 154 * Create a new string generator for substrings of a base string. 155 * 156 * <p> 157 * base.contains(generated string) will always be true. 158 * </p> 159 */ 160 public static Generator<String> substrings(String base) { 161 return Generators.substrings(base); 162 } 163 164 /** 165 * Create a new string generator for substrings of a base string. 166 * 167 * <p> 168 * base.contains(generated string) will always be true. 169 * </p> 170 * 171 * @param size of the generated string 172 */ 173 public static Generator<String> substrings(String base, int size) { 174 return Generators.substrings(base, size); 175 } 176 177 /** 178 * Create a new string generator for substrings of a base string. 179 * 180 * <p> 181 * base.contains(generated string) will always be true. 182 * </p> 183 * 184 * @param minSize is the minimum size of the generated string 185 * @param maxSize is the maximum size of the generated string 186 */ 187 public static Generator<String> substrings(String base, int minSize, int maxSize) { 188 return Generators.substrings(base, minSize, maxSize); 189 } 190 191 /** 192 * Create a new character generator which generates characters ranging from 193 * lo to hi. 194 */ 195 public static Generator<Character> characters(char lo, char hi) { 196 return Generators.characters(lo, hi); 197 } 198 199 /** 200 * Create a new character generator.<br> 201 * 202 * The characters are from the Basic Latin and Latin-1 Supplement unicode blocks. 203 */ 204 public static Generator<Character> characters() { 205 return Generators.characters(); 206 } 207 208 /** 209 * Create a new character generator which generates characters from the 210 * given character array. 211 */ 212 public static Generator<Character> characters(Character... chars) { 213 return Generators.characters(chars); 214 } 215 216 /** 217 * Create a new character generator which generates characters from the 218 * given string. 219 */ 220 public static Generator<Character> characters(String string) { 221 return Generators.characters(string); 222 } 223 224 /** 225 * Create a new character generator which generates characters from the 226 * given characters. 227 */ 228 public static Generator<Character> characters(Iterable<Character> chars) { 229 return Generators.characters(chars); 230 } 231 232 /** 233 * Create a new character generator which generates latin-1 supplement 234 * characters. 235 */ 236 public static Generator<Character> latin1SupplementCharacters() { 237 return Generators.latin1SupplementCharacters(); 238 } 239 240 /** 241 * Create a new character generator which generates latin characters. 242 */ 243 public static Generator<Character> basicLatinCharacters() { 244 return Generators.basicLatinCharacters(); 245 } 246 247 /** 248 * Create a new integer generator which creates integers ranging from 249 * {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}. 250 * 251 */ 252 public static Generator<Integer> integers() { 253 return Generators.integers(); 254 } 255 256 /** 257 * Create a new integer generator which creates integers that are at equal 258 * or greater than low. 259 */ 260 public static Generator<Integer> integers(int low) { 261 return Generators.integers(low); 262 } 263 264 /** 265 * Create a new integer generator which creates integers ranging from lo to 266 * hi. 267 */ 268 public static Generator<Integer> integers(int lo, int hi) { 269 return Generators.integers(lo, hi); 270 } 271 272 /** 273 * Create a new integer generator which creates integers ranging from {@code lo} to 274 * {@code hi} based on the given {@link Distribution}. 275 */ 276 public static Generator<Integer> integers(int lo, int hi, Distribution distribution) { 277 return Generators.integers(lo, hi, distribution); 278 } 279 280 /** 281 * Create a new integer generator which creates integers ranging from {@code 1} to 282 * {@link Integer#MAX_VALUE}. 283 */ 284 public static Generator<Integer> positiveIntegers() { 285 return Generators.positiveIntegers(); 286 } 287 288 /** 289 * Create a new integer generator which creates integers ranging from {@code 1} to 290 * {@code max} (which must be at least 1). 291 */ 292 public static Generator<Integer> positiveIntegers(int hi) { 293 return Generators.positiveIntegers(hi); 294 } 295 296 /** 297 * Create a new byte generator which creates byte values ranging from 298 * {@link Byte#MIN_VALUE} to {@link Byte#MAX_VALUE}. 299 */ 300 public static Generator<Byte> bytes() { 301 return Generators.bytes(); 302 } 303 304 /** 305 * Create a new byte generator which creates byte values ranging from lo to 306 * hi. 307 */ 308 public static Generator<Byte> bytes(byte lo, byte hi) { 309 return Generators.bytes(lo, hi); 310 } 311 312 /** 313 * Create a new integer generator which creates integers ranging from lo to 314 * hi based on the given {@link Distribution}. 315 */ 316 public static Generator<Byte> bytes(byte lo, byte hi, 317 Distribution distribution) { 318 return Generators.bytes(lo, hi, distribution); 319 } 320 321 /** 322 * Create a new long generator which creates longs ranging from 323 * {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE}. 324 */ 325 public static Generator<Long> longs() { 326 return Generators.longs(); 327 } 328 329 /** 330 * Create a new long generator which creates longs ranging from lo to hi. 331 */ 332 public static Generator<Long> longs(long lo, long hi) { 333 return Generators.longs(lo, hi); 334 } 335 336 /** 337 * Create a new long generator which creates longs ranging from lo to hi 338 * based on the given {@link Distribution}. 339 */ 340 public static Generator<Long> longs(long lo, long hi, 341 Distribution distribution) { 342 return Generators.longs(lo, hi, distribution); 343 } 344 345 /** 346 * Create a new long generator which creates long values ranging from 1 to 347 * {@link Long#MAX_VALUE}. 348 */ 349 public static Generator<Long> positiveLongs() { 350 return Generators.positiveLongs(); 351 } 352 353 /** 354 * Create a new long generator which creates long values ranging from 1 to 355 * hi. 356 */ 357 public static Generator<Long> positiveLongs(long hi) { 358 return Generators.positiveLongs(hi); 359 } 360 361 /** 362 * Create a new double generator which creates doubles ranging from 363 * {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE}. 364 */ 365 public static Generator<Double> doubles() { 366 return Generators.doubles(); 367 } 368 369 /** 370 * Create a new double generator which creates doubles ranging from lo to 371 * hi. 372 */ 373 public static Generator<Double> doubles(double lo, double hi) { 374 return Generators.doubles(lo, hi); 375 } 376 377 /** 378 * Create a new double generator which creates doubles ranging from lo to hi 379 * based on the given {@link Distribution}. 380 */ 381 public static Generator<Double> doubles(double lo, double hi, 382 Distribution distribution) { 383 return Generators.doubles(lo, hi, distribution); 384 } 385 386 /** 387 * Create a generator for boolean values. 388 */ 389 public static Generator<Boolean> booleans() { 390 return Generators.booleans(); 391 } 392 393 /** 394 * Create a generator for null values. 395 */ 396 public static <T> Generator<T> nulls() { 397 return Generators.nulls(); 398 } 399 400 /** 401 * Create a generator for date values. 402 */ 403 public static Generator<Date> dates() { 404 return Generators.dates(); 405 } 406 407 /** 408 * Create a generator for date values with the given precision. 409 */ 410 public static Generator<Date> dates(TimeUnit precision) { 411 return Generators.dates(precision); 412 } 413 414 /** 415 * Create a generator for date values from low to high. 416 */ 417 public static Generator<Date> dates(Date low, Date high) { 418 return Generators.dates(low, high); 419 } 420 421 /** 422 * Create a generator for date values from low to high. 423 */ 424 public static Generator<Date> dates(long low, long high) { 425 return Generators.dates(low, high); 426 } 427 428 /** 429 * Create a generator for date values from low to high with the given 430 * precision. 431 */ 432 public static Generator<Date> dates(Long low, Long high, TimeUnit precision) { 433 return Generators.dates(low, high, precision); 434 } 435 436 /** 437 * Create a generator for fixed value generator. 438 */ 439 public static <T> Generator<T> fixedValues(T value) { 440 return Generators.fixedValues(value); 441 } 442 443 /** 444 * Create a fixed value generator return 445 * values array. 446 */ 447 @SafeVarargs 448 public static <T> Generator<T> fixedValues(T... values) { 449 return Generators.fixedValues(values); 450 } 451 452 /** 453 * Create a fixed value generator return 454 * values. 455 */ 456 public static <T> Generator<T> fixedValues(Iterable<T> values) { 457 return Generators.fixedValues(values); 458 } 459 460 /** 461 * A cloning generator which uses object serialization to create clones of 462 * the prototype object. For each call a new copy of the prototype will be 463 * generated. 464 */ 465 public static <T> Generator<T> clonedValues(T prototype) { 466 return Generators.clonedValues(prototype); 467 } 468 469 /** 470 * Create a generator of enumeration values. 471 * 472 * @param <T> 473 * Type of enumerations 474 * @param enumClass 475 * class of enumeration 476 * @return generator of enum values 477 */ 478 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass) { 479 return Generators.enumValues(enumClass); 480 } 481 482 /** 483 * Create a generator of enumeration values. 484 * 485 * @param <T> 486 * Type of enumerations 487 * @param enumClass 488 * class of enumeration 489 * @param excluded 490 * excluded values of enumeration 491 * @return generator of enum values 492 */ 493 @SafeVarargs 494 public static <T extends Enum<T>> Generator<T> enumValues( 495 Class<T> enumClass, T... excluded) { 496 return Generators.enumValues(enumClass, excluded); 497 } 498 499 /** 500 * Create a generator of enumeration values. 501 * 502 * @param <T> 503 * Type of enumerations 504 * @param enumClass 505 * class of enumeration 506 * @param excludedValues 507 * excluded values of enumeration 508 * @return generator of enum values 509 */ 510 public static <T extends Enum<T>> Generator<T> enumValues( 511 Class<T> enumClass, Iterable<T> excludedValues) { 512 return Generators.enumValues(enumClass, excludedValues); 513 } 514 515 /** 516 * Create a generator for {@link Object java.lang.Object} instances. 517 * <p> 518 * Note: every invocation of {@link Generator#next()} creates a new instance. 519 * </p> 520 */ 521 public static Generator<Object> objects() { 522 return Generators.objects(); 523 } 524 525 /** 526 * Create a generator from a {@link ObjectGenerator declarative object generator definition}. 527 */ 528 public static <T> ObjectGenerator<T> objects(Class<T> objectType) { 529 return Generators.objects(objectType); 530 } 531 532 /** 533 * Create a generator from a {@link ObjectGenerator declarative object generator definition}. 534 * <p> 535 * Default values will be used for all {@link ObjectGenerator#on(Object) undefined methods}. 536 * </p> 537 */ 538 public static <T> ObjectGenerator<T> defaultObjects(Class<T> objectType) { 539 return Generators.defaultObjects(objectType); 540 } 541}