Skip to content

Commit

Permalink
Merge pull request #11 from sashirestela/10-add-constraint-for-file-e…
Browse files Browse the repository at this point in the history
…xtension

Add Extension constraint
  • Loading branch information
sashirestela authored Apr 5, 2024
2 parents bdf0b1e + 4a2d8d2 commit 95208f9
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>slimvalidator</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>slimvalidator</name>
Expand Down Expand Up @@ -52,16 +52,16 @@
<maven.compiler.release>11</maven.compiler.release>
<!-- Dependencies Versions -->
<slf4j.version>2.0.12</slf4j.version>
<lombok.version>1.18.30</lombok.version>
<lombok.version>1.18.32</lombok.version>
<junit.version>5.10.2</junit.version>
<!-- Plugins Versions -->
<compiler.version>3.12.1</compiler.version>
<compiler.version>3.13.0</compiler.version>
<enforcer.version>3.4.1</enforcer.version>
<surefire.version>3.2.5</surefire.version>
<exec.version>3.2.0</exec.version>
<jacoco.version>0.8.11</jacoco.version>
<jacoco.version>0.8.12</jacoco.version>
<versions.version>2.16.2</versions.version>
<source.version>3.3.0</source.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.6.3</javadoc.version>
<gpg.version>3.1.0</gpg.version>
<sonatype.version>1.6.13</sonatype.version>
Expand Down
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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static String toStringByAnnotMethodType(Object value) {
return value.toString();
}
}
if (value.getClass().isArray() && value.getClass().getComponentType().equals(String.class)) {
return Arrays.toString((String[]) value);
}
return value.toString();
}

Expand Down
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public boolean isValid(Object value) {
if (value == null) {
return false;
} else {
var groupSize = getGroupSize(value);
if (groupSize == -1) {
return true;
} else {
return (groupSize > 0);
}
return getGroupSize(value) != 0;
}
}

Expand Down
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;
}

};

}

}

}
1 change: 1 addition & 0 deletions src/test/resources/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a sample file for testing.

0 comments on commit 95208f9

Please sign in to comment.