Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Eng
committed
Jan 9, 2018
1 parent
05e00bb
commit d688311
Showing
10 changed files
with
191 additions
and
10 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
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
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
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
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
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
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
41 changes: 41 additions & 0 deletions
41
server/zanata-model/src/main/java/org/zanata/model/validator/ZanataEmail.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,41 @@ | ||
/* | ||
* Copyright 2018, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.model.validator; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Constraint(validatedBy = { ZanataEmailValidator.class }) | ||
@Target({ METHOD, FIELD }) | ||
@Retention(RUNTIME) | ||
public @interface ZanataEmail { | ||
Class<?>[] groups() default {}; | ||
|
||
String message() default "{org.hibernate.validator.constraints.Email.message}"; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
53 changes: 53 additions & 0 deletions
53
server/zanata-model/src/main/java/org/zanata/model/validator/ZanataEmailValidator.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,53 @@ | ||
/* | ||
* Copyright 2018, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.model.validator; | ||
|
||
import org.apache.commons.validator.routines.EmailValidator; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.io.Serializable; | ||
|
||
public class ZanataEmailValidator implements | ||
ConstraintValidator<ZanataEmail, String>, Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
// allowed by hibernate validator | ||
private static final boolean allowTLD = true; | ||
|
||
// allowed by hibernate validator | ||
private static final boolean allowLocale = true; | ||
|
||
private static final EmailValidator VALIDATOR = | ||
EmailValidator.getInstance(allowLocale, allowTLD); | ||
|
||
|
||
@Override | ||
public void initialize(ZanataEmail zanataEmail) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isValid(String string, | ||
ConstraintValidatorContext constraintValidatorContext) { | ||
return VALIDATOR.isValid(string); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
server/zanata-model/src/test/java/org/zanata/model/validator/ZanataEmailValidatorTest.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,61 @@ | ||
/* | ||
* Copyright 2018, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.zanata.model.validator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
**/ | ||
public class ZanataEmailValidatorTest { | ||
private ZanataEmailValidator validator; | ||
@Mock | ||
private ConstraintValidatorContext context; | ||
|
||
@Before | ||
public void setUp() { | ||
validator = new ZanataEmailValidator(); | ||
} | ||
|
||
@Test | ||
public void testEmail() { | ||
assertThat(validator.isValid(null, context)).isFalse(); | ||
assertThat(validator.isValid(" ", context)).isFalse(); | ||
assertThat(validator.isValid(".", context)).isFalse(); | ||
assertThat(validator.isValid("test@zanata+", context)).isFalse(); | ||
assertThat(validator.isValid("test@zanata.", context)).isFalse(); | ||
assertThat(validator.isValid("test@zanata-", context)).isFalse(); | ||
assertThat(validator.isValid("test", context)).isFalse(); | ||
assertThat(validator.isValid("test@", context)).isFalse(); | ||
assertThat(validator.isValid("test@0.0.0.0", context)).isFalse(); | ||
assertThat(validator.isValid("@zanata", context)).isFalse(); | ||
|
||
assertThat(validator.isValid("test@zanata", context)).isTrue(); | ||
assertThat(validator.isValid("test@zanata.org", context)).isTrue(); | ||
} | ||
} |