01: /*
02: * (C) Janne Jalkanen 2005
03: *
04: */
05: package com.ecyrd.jspwiki.ui;
06:
07: import java.util.Properties;
08: import java.util.regex.Pattern;
09:
10: import junit.framework.Test;
11: import junit.framework.TestCase;
12: import junit.framework.TestSuite;
13:
14: import com.ecyrd.jspwiki.TestEngine;
15: import com.ecyrd.jspwiki.TestHttpServletRequest;
16: import com.ecyrd.jspwiki.WikiSession;
17:
18: public class InputValidatorTest extends TestCase {
19: TestEngine testEngine;
20:
21: InputValidator val;
22:
23: String TEST = "test";
24:
25: protected void setUp() throws Exception {
26: Properties props = new Properties();
27: props.load(TestEngine.findTestProperties());
28: testEngine = new TestEngine(props);
29: WikiSession session = WikiSession.getWikiSession(testEngine,
30: new TestHttpServletRequest());
31: val = new InputValidator(TEST, session);
32: }
33:
34: public void testUnsafePattern() {
35: Pattern unsafe = InputValidator.UNSAFE_PATTERN;
36: assertFalse(unsafe.matcher("a b c d e f g").find());
37: assertTrue(unsafe.matcher("<a> b c d e f g").find());
38: assertTrue(unsafe.matcher("foo$").find());
39: }
40:
41: public void testValidate() {
42: assertTrue(val.validate("Test string", "Name",
43: InputValidator.STANDARD));
44: assertFalse(val.validate("Test $tring", "Name",
45: InputValidator.STANDARD));
46: assertFalse(val.validate("Test <string>", "Name",
47: InputValidator.STANDARD));
48: assertFalse(val.validate("Test & string", "Name",
49: InputValidator.STANDARD));
50: assertFalse(val.validate("Test @ string", "Name",
51: InputValidator.STANDARD));
52:
53: // Null or blank fields should validate
54: assertTrue(val.validate("", "Name", InputValidator.STANDARD));
55: assertTrue(val.validate(null, "Name", InputValidator.STANDARD));
56: }
57:
58: public void testValidateNotNull() {
59: assertTrue(val.validateNotNull("Test string", "Name"));
60: assertFalse(val.validateNotNull("Test $tring", "Name"));
61: assertFalse(val.validateNotNull("", "Name"));
62: assertFalse(val.validateNotNull(null, "Name"));
63: }
64:
65: public void testValidateEmail() {
66: assertTrue(val.validateNotNull("foo@bar.com", "E-mail",
67: InputValidator.EMAIL));
68: assertTrue(val.validateNotNull("foo-bar@foo.com", "E-mail",
69: InputValidator.EMAIL));
70: assertTrue(val.validateNotNull("foo-bar@foo.co.uk", "E-mail",
71: InputValidator.EMAIL));
72: assertTrue(val.validateNotNull("foo+bar@foo.co.uk", "E-mail",
73: InputValidator.EMAIL));
74: assertTrue(val.validateNotNull("foo.bar@foo.co.uk", "E-mail",
75: InputValidator.EMAIL));
76: assertFalse(val.validateNotNull("foobar", "E-mail",
77: InputValidator.EMAIL));
78: assertFalse(val.validateNotNull("foobar@foo", "E-mail",
79: InputValidator.EMAIL));
80: }
81:
82: public static Test suite() {
83: return new TestSuite(InputValidatorTest.class);
84: }
85: }
|