Commit 9b822714 authored by piotr.gawron's avatar piotr.gawron
Browse files

issue #48 - login cannot contain whitespace anymore

parent fbd5a493
package lcsb.mapviewer.validator;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
* Validator of login field.
*
* @author Piotr Gawron
*
*/
@FacesValidator("loginValidator")
public class LoginValidator implements Validator {
/**
* Regex pattern used for login validation.
*/
private static final Pattern LOGIN_PATTERN = Pattern.compile("[a-zA-Z0-9_\\.\\-]+");
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value == null) {
return; // Let required="true" handle.
}
if (!LOGIN_PATTERN.matcher((String) value).matches()) {
String summary = "Incorrect login.";
String detail = "Only alphanumeric characters and \"-_.\" special characters are allowed";
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail));
}
}
}
\ No newline at end of file
/**
* Provides validator for JSF forms.
*/
package lcsb.mapviewer.validator;
......@@ -78,7 +78,7 @@
<h:outputText value="Login:" />
</td>
<td>
<p:inputText value="#{usersMB.selectedUser.login}" styleClass="bold" rendered="#{empty usersMB.selectedUser.login}"/>
<p:inputText value="#{usersMB.selectedUser.login}" styleClass="bold" rendered="#{empty usersMB.selectedUser.login}" validator="loginValidator"/>
<h:outputText autocomplete="off" value="#{usersMB.selectedUser.login}" class="bold" rendered="#{not empty usersMB.selectedUser.login}"/>
</td>
</tr>
......
package lcsb.mapviewer;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.bean.AllBeanTests;
import lcsb.mapviewer.validator.AllValidatorTests;
@RunWith(Suite.class)
@SuiteClasses({ AllBeanTests.class, //
AllValidatorTests.class,//
})
public class AllWebTests {
}
package lcsb.mapviewer.validator;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ LoginValidatorTest.class })
public class AllValidatorTests {
}
package lcsb.mapviewer.validator;
import static org.junit.Assert.fail;
import javax.faces.validator.ValidatorException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
public class LoginValidatorTest {
LoginValidator validator = new LoginValidator();
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testCorrect() {
validator.validate(null, null, "piotr.gawron");
}
@Test
public void testIncorrect() {
try {
validator.validate(null, null, "piotr gawron");
fail("Exception expected");
} catch (ValidatorException e) {
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment