| 10 | import static java.util.GregorianCalendar.*; |
| 11 | |
| 12 | public class CalendarGenerator extends Generator<GregorianCalendar> { |
| 13 | |
| 14 | public CalendarGenerator() { |
| 15 | super(GregorianCalendar.class); // Register the type of objects that we can create |
| 16 | } |
| 17 | |
| 18 | // This method is invoked to generate a single test case |
| 19 | @Override |
| 20 | public GregorianCalendar generate(SourceOfRandomness random, GenerationStatus __ignore__) { |
| 21 | // Initialize a calendar object |
| 22 | GregorianCalendar cal = new GregorianCalendar(); |
| 23 | cal.setLenient(true); // This allows invalid dates to silently wrap (e.g. Apr 31 ==> May 1). |
| 24 | |
| 25 | // Randomly pick a day, month, and year |
| 26 | cal.set(DAY_OF_MONTH, random.nextInt(31) + 1); // a number between 1 and 31 inclusive |
| 27 | cal.set(MONTH, random.nextInt(12) + 1); // a number between 1 and 12 inclusive |
| 28 | cal.set(YEAR, random.nextInt(cal.getMinimum(YEAR), cal.getMaximum(YEAR))); |
| 29 | |
| 30 | // Optionally also pick a time |
| 31 | if (random.nextBoolean()) { |
| 32 | cal.set(HOUR, random.nextInt(24)); |
| 33 | cal.set(MINUTE, random.nextInt(60)); |
| 34 | cal.set(SECOND, random.nextInt(60)); |
| 35 | } |
| 36 | |
| 37 | // Let's set a timezone |
| 38 | // First, get supported timezone IDs (e.g. "America/Los_Angeles") |
| 39 | String[] allTzIds = TimeZone.getAvailableIDs(); |
| 40 | |
| 41 | // Next, choose one randomly from the array |
| 42 | String tzId = random.choose(allTzIds); |
| 43 | TimeZone tz = TimeZone.getTimeZone(tzId); |
| 44 | |
| 45 | // Assign it to the calendar |
| 46 | cal.setTimeZone(tz); |
| 47 | |
| 48 | // Return the randomly generated calendar object |
| 49 | return cal; |
| 50 | } |
| 51 | } |
nothing calls this directly
no outgoing calls
no test coverage detected