01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.extensions.validation.validator;
18:
19: import java.util.regex.Matcher;
20:
21: import org.apache.wicket.extensions.validation.validator.RfcCompliantEmailAddressValidator;
22:
23: import junit.framework.TestCase;
24:
25: /**
26: * Test that it really validates RFC valid email addresses.
27: *
28: * @author Frank Bille
29: */
30: public class RfcCompliantEmailValidatorTest extends TestCase {
31: /**
32: * Test a couple of valid email addresses.
33: */
34: public void testValidEmailAddresses() {
35: RfcCompliantEmailAddressValidator validator = RfcCompliantEmailAddressValidator
36: .getInstance();
37:
38: String[] validEmails = new String[] { "bill.gates@gmail.com",
39: "firstname.middlename@lastname.dk", "buy@something.nu",
40: "user@post.inet.tele.dk", "read@my.info",
41: "my @email.com", "my@ email.com",
42: "\"John Doe\"@email.com", "no@domain",
43: "german@m�dchen.de", "another.german@�m��l.com" };
44: for (int i = 0; i < validEmails.length; i++) {
45: String emailAddress = validEmails[i];
46:
47: Matcher matcher = validator.getPattern().matcher(
48: emailAddress);
49:
50: assertTrue(emailAddress + " wasn't valid but should be",
51: matcher.matches());
52: }
53: }
54:
55: /**
56: * Test a couple of invalid email addresses.
57: */
58: public void testInValidEmailAddresses() {
59: RfcCompliantEmailAddressValidator validator = RfcCompliantEmailAddressValidator
60: .getInstance();
61:
62: String[] inValidEmails = new String[] { "whatever",
63: "dont.end.in.a.dot.@gmail.com",
64: ".dot.in.the.beginning.is.not.good@wicketframework.org" };
65:
66: for (int i = 0; i < inValidEmails.length; i++) {
67: String emailAddress = inValidEmails[i];
68:
69: Matcher matcher = validator.getPattern().matcher(
70: emailAddress);
71:
72: assertFalse(emailAddress + " was valid but shouldn't be",
73: matcher.matches());
74: }
75: }
76: }
|