DefaultJobValidator implements JobValidator { @Nullable @Override @CallSuper public List<String> validate(@NonNull JobParameters job) { List<String> errors; errors = mergeErrorLists(null, validate(job.getTrigger())); errors = mergeErrorLists(errors, validate(job.getRetryStrategy())); if (job.isRecurring() && job.getTrigger() == Trigger.NOW) { errors = addError(errors, "ImmediateTriggers can't be used with recurring jobs"); } errors = mergeErrorLists(errors, validateForTransport(job.getExtras())); if (job.getLifetime() > Lifetime.UNTIL_NEXT_BOOT) { errors = mergeErrorLists(errors, validateForPersistence(job.getExtras())); } errors = mergeErrorLists(errors, validateTag(job.getTag())); errors = mergeErrorLists(errors, validateService(job.getService())); return errors; } DefaultJobValidator(@NonNull Context context); @Nullable @Override @CallSuper List<String> validate(@NonNull JobParameters job); @Nullable @Override @CallSuper List<String> validate(@NonNull JobTrigger trigger); @Nullable @Override @CallSuper List<String> validate(@NonNull RetryStrategy retryStrategy); static final int MAX_TAG_LENGTH; static final int MAX_EXTRAS_SIZE_BYTES; }
@SuppressWarnings("WrongConstant") @Test public void testValidate_retryStrategy() throws Exception { Map<RetryStrategy, List<String>> testCases = new HashMap<>(); testCases.put( new RetryStrategy(0 , 30, 3600), singletonList("Unknown retry policy provided")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_LINEAR, 15, 3600), singletonList("Initial backoff must be at least 30s")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_EXPONENTIAL, 15, 3600), singletonList("Initial backoff must be at least 30s")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_LINEAR, 30, 60), singletonList("Maximum backoff must be greater than 300s (5 minutes)")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_EXPONENTIAL, 30, 60), singletonList("Maximum backoff must be greater than 300s (5 minutes)")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_LINEAR, 301, 300), singletonList("Maximum backoff must be greater than or equal to initial backoff")); testCases.put( new RetryStrategy(RetryStrategy.RETRY_POLICY_EXPONENTIAL, 301, 300), singletonList("Maximum backoff must be greater than or equal to initial backoff")); for (Entry<RetryStrategy, List<String>> testCase : testCases.entrySet()) { List<String> validationErrors = validator.validate(testCase.getKey()); assertNotNull("Expected validation errors, but got null", validationErrors); for (String expected : testCase.getValue()) { assertTrue( "Expected validation errors to contain \"" + expected + "\"", validationErrors.contains(expected)); } } }
@Test public void testValidate_trigger() throws Exception { Map<JobTrigger, String> testCases = new HashMap<>(); testCases.put(Trigger.NOW, null); testCases.put(Trigger.executionWindow(0, 100), null); ContentUriTrigger contentUriTrigger = Trigger.contentUriTrigger( Arrays.asList( new ObservedUri( ContactsContract.AUTHORITY_URI, Flags.FLAG_NOTIFY_FOR_DESCENDANTS))); testCases.put(contentUriTrigger, null); for (Entry<JobTrigger, String> testCase : testCases.entrySet()) { List<String> validationErrors = validator.validate(testCase.getKey()); if (testCase.getValue() == null) { assertNull("Expected no validation errors for trigger", validationErrors); } else { assertTrue( "Expected validation errors to contain \"" + testCase.getValue() + "\"", validationErrors.contains(testCase.getValue())); } } }