-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sashirestela/10-add-constraint-for-file-e…
…xtension Add Extension constraint
- Loading branch information
Showing
7 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/io/github/sashirestela/slimvalidator/constraints/Extension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.github.sashirestela.slimvalidator.constraints; | ||
|
||
import io.github.sashirestela.slimvalidator.Constraint; | ||
import io.github.sashirestela.slimvalidator.validators.ExtensionValidator; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = ExtensionValidator.class) | ||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Extension { | ||
|
||
String message() default "extension must be one of {value}."; | ||
|
||
String[] value(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/io/github/sashirestela/slimvalidator/validators/ExtensionValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.sashirestela.slimvalidator.validators; | ||
|
||
import io.github.sashirestela.slimvalidator.ConstraintValidator; | ||
import io.github.sashirestela.slimvalidator.constraints.Extension; | ||
import io.github.sashirestela.slimvalidator.exception.ValidationException; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Checks that the file extension is one of an expected list. Applies to {@link java.nio.file.Path | ||
* Path} and {@link java.io.File File} objects. | ||
*/ | ||
public class ExtensionValidator implements ConstraintValidator<Extension, Object> { | ||
|
||
private String[] extensions; | ||
|
||
@Override | ||
public void initialize(Extension annotation) { | ||
extensions = annotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object value) { | ||
if (value == null) { | ||
return true; | ||
} | ||
return Arrays.stream(extensions).anyMatch(ext -> ext.equals(getExtension(value))); | ||
} | ||
|
||
private String getExtension(Object value) { | ||
if (value instanceof Path) { | ||
return ((Path) value).toString().split("\\.")[1]; | ||
} else if (value instanceof File) { | ||
return ((File) value).getName().split("\\.")[1]; | ||
} else { | ||
throw new ValidationException("Cannot get a extension from {0}.", value.getClass().getName(), null); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/io/github/sashirestela/slimvalidator/validators/ExtensionValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.github.sashirestela.slimvalidator.validators; | ||
|
||
import io.github.sashirestela.slimvalidator.constraints.Extension; | ||
import io.github.sashirestela.slimvalidator.exception.ValidationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.lang.annotation.Annotation; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class ExtensionValidatorTest { | ||
|
||
String fileName = "src/test/resources/sample.txt"; | ||
|
||
@Test | ||
void shouldReturnTrueWhenValidatingObjectAgainstAnnnotation() { | ||
Object[][] data = { | ||
{ null, Sample.extension(null) }, | ||
{ new File(fileName), Sample.extension(new String[] { "doc", "csv", "txt" }) }, | ||
{ Paths.get(fileName), Sample.extension(new String[] { "doc", "csv", "txt" }) } | ||
}; | ||
for (Object[] value : data) { | ||
var validator = new ExtensionValidator(); | ||
var annotation = (Extension) value[1]; | ||
validator.initialize(annotation); | ||
var actualResult = validator.isValid(value[0]); | ||
var expectedResult = true; | ||
assertEquals(expectedResult, actualResult); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenValidatingObjectAgainstAnnnotation() { | ||
Object[][] data = { | ||
{ new File(fileName), Sample.extension(new String[] { "jpg", "gif", "bmp" }) }, | ||
{ Paths.get(fileName), Sample.extension(new String[] { "jpg", "gif", "bmp" }) } | ||
}; | ||
for (Object[] value : data) { | ||
var validator = new ExtensionValidator(); | ||
var annotation = (Extension) value[1]; | ||
validator.initialize(annotation); | ||
var actualResult = validator.isValid(value[0]); | ||
var expectedResult = false; | ||
assertEquals(expectedResult, actualResult); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldThrownExceptionWhenNonExpectedObjectIsPassed() { | ||
var validator = new ExtensionValidator(); | ||
var annotation = Sample.extension(new String[] { "mp3", "wav" }); | ||
validator.initialize(annotation); | ||
var exception = assertThrows(ValidationException.class, () -> validator.isValid("filename.mp3")); | ||
var actualMessage = exception.getMessage(); | ||
var expectedMessage = "Cannot get a extension from java.lang.String."; | ||
assertEquals(expectedMessage, actualMessage); | ||
} | ||
|
||
static class Sample { | ||
|
||
static Extension extension(String[] value) { | ||
return new Extension() { | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return Extension.class; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String[] value() { | ||
return value; | ||
} | ||
|
||
}; | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a sample file for testing. |