Skip to content

Commit

Permalink
Use ValidationAssertions in TestPowerOfTwoValidator
Browse files Browse the repository at this point in the history
Cherry-pick of trinodb/trino#25

Co-authored-by: Wenlei Xie <wenlei.xie@gmail.com>
  • Loading branch information
2 people authored and Sreeni Viswanadha committed Jan 15, 2020
1 parent a0ab663 commit d2359b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 78 deletions.
6 changes: 0 additions & 6 deletions presto-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,6 @@
<artifactId>jjwt</artifactId>
</dependency>

<!-- used by tests but also needed transitively -->
<dependency>
<groupId>org.apache.bval</groupId>
<artifactId>bval-jsr</artifactId>
</dependency>

<!-- for testing -->
<dependency>
<groupId>org.testng</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,43 @@
*/
package com.facebook.presto.util;

import org.apache.bval.jsr.ApacheValidationProvider;
import org.testng.annotations.Test;

import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import java.util.Set;

import static io.airlift.testing.Assertions.assertInstanceOf;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static io.airlift.testing.ValidationAssertions.assertFailsValidation;
import static io.airlift.testing.ValidationAssertions.assertValidates;

public class TestPowerOfTwoValidator
{
private static final Validator VALIDATOR = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory().getValidator();

@Test
public void testValidator()
{
PowerOfTwoValidator validator = new PowerOfTwoValidator();
validator.initialize(new MockPowerOfTwo());

assertTrue(validator.isValid(1, new MockContext()));
assertTrue(validator.isValid(2, new MockContext()));
assertTrue(validator.isValid(64, new MockContext()));
assertFalse(validator.isValid(0, new MockContext()));
assertFalse(validator.isValid(3, new MockContext()));
assertFalse(validator.isValid(99, new MockContext()));
assertFalse(validator.isValid(-1, new MockContext()));
assertFalse(validator.isValid(-2, new MockContext()));
assertFalse(validator.isValid(-4, new MockContext()));
assertValid(1);
assertValid(2);
assertValid(64);
assertInvalid(0);
assertInvalid(3);
assertInvalid(99);
assertInvalid(-1);
assertInvalid(-2);
assertInvalid(-4);
}

@Test
public void testAllowsNullPowerOfTwoAnnotation()
{
VALIDATOR.validate(new NullPowerOfTwoAnnotation());
assertValidates(new NullPowerOfTwoAnnotation());
}

@Test
public void testPassesValidation()
private static void assertValid(int value)
{
ConstrainedPowerOfTwo object = new ConstrainedPowerOfTwo(128);
Set<ConstraintViolation<ConstrainedPowerOfTwo>> violations = VALIDATOR.validate(object);
assertTrue(violations.isEmpty());
}

@Test
public void testFailsValidation()
{
ConstrainedPowerOfTwo object = new ConstrainedPowerOfTwo(11);
Set<ConstraintViolation<ConstrainedPowerOfTwo>> violations = VALIDATOR.validate(object);
assertEquals(violations.size(), 2);

for (ConstraintViolation<ConstrainedPowerOfTwo> violation : violations) {
assertInstanceOf(violation.getConstraintDescriptor().getAnnotation(), PowerOfTwo.class);
}
assertValidates(new ConstrainedPowerOfTwo(value));
}

private static class MockContext
implements ConstraintValidatorContext
private static void assertInvalid(int value)
{
@Override
public void disableDefaultConstraintViolation()
{
throw new UnsupportedOperationException();
}

@Override
public String getDefaultConstraintMessageTemplate()
{
throw new UnsupportedOperationException();
}

@Override
public ConstraintViolationBuilder buildConstraintViolationWithTemplate(String s)
{
throw new UnsupportedOperationException();
}

@Override
public <T> T unwrap(Class<T> type)
{
throw new UnsupportedOperationException();
}
Object object = new ConstrainedPowerOfTwo(value);
assertFailsValidation(object, "unboxed", "is not a power of two", PowerOfTwo.class);
assertFailsValidation(object, "boxed", "is not a power of two", PowerOfTwo.class);
}

@SuppressWarnings("UnusedDeclaration")
Expand All @@ -114,13 +63,13 @@ public ConstrainedPowerOfTwo(int value)
}

@PowerOfTwo
public int getConstrainedByPowerOfTwoUnboxed()
public int getUnboxed()
{
return value;
}

@PowerOfTwo
public Integer getConstrainedByPowerOfTwoBoxed()
public Integer getBoxed()
{
return value;
}
Expand All @@ -130,7 +79,7 @@ public Integer getConstrainedByPowerOfTwoBoxed()
public static class NullPowerOfTwoAnnotation
{
@PowerOfTwo
public static Integer getConstrainedByPowerOfTwo()
public Integer getNull()
{
return null;
}
Expand Down

0 comments on commit d2359b1

Please sign in to comment.