Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Allow @Wirtable to be applied to String
Browse files Browse the repository at this point in the history
  • Loading branch information
britter committed Aug 1, 2015
1 parent b829699 commit a50bd2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package com.github.britter.beanvalidators.file;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.io.File;

public class WritableConstraintValidator implements ConstraintValidator<Writable, File> {
public class WritableConstraintValidator extends AbstractFileConstraintValidator<Writable> {

@Override
public void initialize(Writable constraintAnnotation) {
super.initialize(constraintAnnotation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.britter.beanvalidators.file;

import javax.validation.ValidationException;
import java.io.File;

import com.github.britter.beanvalidators.ValidationWrapper;
Expand Down Expand Up @@ -64,8 +65,45 @@ public void shouldNotValidateUnwritableFile() throws Exception {
validator.assertViolation("file");
}

@Test
public void shouldValidateBlankStrings() throws Exception {
fileBean.path = " ";

validator.assertNoViolations("path");
}

@Test
public void shouldValidateStringRepresentingWritableDirectory() throws Exception {
fileBean.path = tmpFolder.newFolder().getAbsolutePath();

validator.assertNoViolations("path");
}

@Test
public void shouldValidateStringRepresentingWritableFile() throws Exception {
fileBean.path = tmpFolder.newFile().getAbsolutePath();

validator.assertNoViolations("path");
}

@Test
public void shouldNotValidateStringRepresentingNonExistingFile() throws Exception {
fileBean.path = "/should/not/exist";

validator.assertViolation("path");
}

@Test(expected = ValidationException.class)
public void shouldThrowExceptionWhenAppliedToOtherTypes() throws Exception {
validator.validate("object");
}

private static final class FileBean {
@Writable
private File file;
@Writable
private String path;
@Writable
private Object object = new Object();
}
}

0 comments on commit a50bd2e

Please sign in to comment.