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

Commit

Permalink
Add executable constaint
Browse files Browse the repository at this point in the history
  • Loading branch information
britter committed Jul 28, 2015
1 parent 24d5b29 commit 3a973f9
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -282,6 +282,20 @@ Makes sure a string contains a valid Java number.
private File dir;
```

### Executable

```java
/**
* valid:
* file.canExecute() == true
*
* invalid:
* dir.canExecute() == false
*/
@Executable
private File file;
```

### Readable

```java
Expand Down
@@ -0,0 +1,41 @@
/*
* Copyright 2015 Benedikt Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.britter.beanvalidators.file;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = ExecutableConstraintValidator.class)
@Documented
public @interface Executable {

String message() default "{com.github.britter.beanvalidators.io.Executable.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
@@ -0,0 +1,33 @@
/*
* Copyright 2015 Benedikt Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.britter.beanvalidators.file;

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

public class ExecutableConstraintValidator implements ConstraintValidator<Executable, File> {

@Override
public void initialize(Executable constraintAnnotation) {
}

@Override
public boolean isValid(File value, ConstraintValidatorContext context) {
return value == null || value.canExecute();
}

}
1 change: 1 addition & 0 deletions src/main/resources/ValidationMessages.properties
Expand Up @@ -4,6 +4,7 @@ com.github.britter.beanvalidators.io.Directory.message=must be a directory
com.github.britter.beanvalidators.io.NotDirectory.message=must not be a directory
com.github.britter.beanvalidators.io.Readable.message=must be readable
com.github.britter.beanvalidators.io.Writable.message=must be writable
com.github.britter.beanvalidators.io.Executable.message=must be executable

com.github.britter.beanvalidators.net.Domain.message=must be a domain
com.github.britter.beanvalidators.net.IP.message=must be an IP
Expand Down
@@ -0,0 +1,92 @@
/*
* Copyright 2015 Benedikt Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.britter.beanvalidators.file;

import static com.google.common.collect.Iterables.getLast;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import javax.validation.ConstraintViolation;
import java.io.File;
import java.util.Set;

import com.github.britter.beanvalidators.ValidationWrapper;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class ExecutableTest {

private FileBean fileBean;
private ValidationWrapper<FileBean> validator;

@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();

@Before
public void setUp() {
fileBean = new FileBean();
validator = new ValidationWrapper<>(fileBean);
}

@Test
public void shouldValidateNull() throws Exception {
fileBean.file = null;

Set<ConstraintViolation<FileBean>> violations = validator.validate("file");

assertThat(violations, is(empty()));
}

@Test
public void shouldValidateExecutableDirectory() throws Exception {
fileBean.file = tmpFolder.newFolder();

Set<ConstraintViolation<FileBean>> violations = validator.validate("file");

assertThat(violations, is(empty()));
}

@Test
public void shouldValidateExecutableFile() throws Exception {
fileBean.file = tmpFolder.newFile();
fileBean.file.setExecutable(true);

Set<ConstraintViolation<FileBean>> violations = validator.validate("file");

assertThat(violations, is(empty()));
}

@Test
public void shouldNotValidateUnexecutableFile() throws Exception {
fileBean.file = new File("/should/not/exist");

Set<ConstraintViolation<FileBean>> violations = validator.validate("file");

assertThat(violations, hasSize(1));
ConstraintViolation<FileBean> violation = getLast(violations);
assertThat(violation.getMessage(), is(equalTo("must be executable")));
}

private static final class FileBean {
@Executable
private File file;
}
}

0 comments on commit 3a973f9

Please sign in to comment.