1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package test.com;
import static org.junit.Assert.*;
import java.util.regex.Matcher; import java.util.regex.Pattern;
import org.junit.Test; /** * Unit Test for domain regular expression * @author Joe */ public class DomainRegexTester {
private final static Pattern pattern = Pattern.compile("(\w{3,30}\.)?(m\.)?((www|en|pt|es|ru)\.)?okchem\.com"); public static boolean match(String regex, String text) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); return matcher.matches(); } @Test public void testMatchGroup_NonVIP_PC() { Matcher matcher1 = pattern.matcher("www.okchem.com"); if(matcher1.find()) { assertTrue("www.".equals(matcher1.group(1))); assertTrue(matcher1.group(2)==null); assertTrue(matcher1.group(3)==null); assertTrue(matcher1.group(4)==null); } Matcher matcher2 = pattern.matcher("en.okchem.com"); if(matcher2.find()) { assertTrue("en".equals(matcher2.group(4))); assertTrue(matcher2.group(2)==null); assertTrue("en.".equals(matcher2.group(3))); } } @Test public void testMatchGroup() { Matcher matcher = pattern.matcher("www.okchem.com"); if(matcher.find()) { assertTrue("www.".equals(matcher.group(1))); assertTrue(matcher.group(2)==null); assertTrue(matcher.group(3)==null); } } @Test public void testParseLanguage_NonLanguage() throws Exception { assertTrue(parseLanguageFromText("www.okchem.com")==null); assertTrue(parseLanguageFromText("m.okchem.com")==null); } @Test public void testParseLanguage_HasLanguage() throws Exception { assertTrue("pt".equals(parseLanguageFromText("pt.okchem.com"))); assertTrue("pt".equals(parseLanguageFromText("m.pt.okchem.com"))); assertTrue("ru".equals(parseLanguageFromText("vipcode.ru.okchem.com"))); assertTrue("pt".equals(parseLanguageFromText("vipcode.m.pt.okchem.com"))); } @Test public void testDomainMatch() { assertTrue(match("www.okchem.com")); assertTrue(match("pt.okchem.com")); assertTrue(match("es.okchem.com")); assertTrue(match("pt.okchem.com")); assertTrue(match("ru.okchem.com")); assertTrue(match("m.okchem.com")); assertTrue(match("m.pt.okchem.com")); assertTrue(match("m.es.okchem.com")); assertTrue(match("m.ru.okchem.com")); /*** VIP store domain ***/ assertTrue(match("vipcode.ru.okchem.com")); assertTrue(match("vipcode.en.okchem.com")); assertTrue(match("vipcode.es.okchem.com")); assertTrue(match("vipcode.pt.okchem.com")); assertTrue(match("vipcode.m.pt.okchem.com")); assertTrue(match("vipcode.m.ru.okchem.com")); assertTrue(match("vipcode.m.en.okchem.com")); assertTrue(match("vipcode.m.es.okchem.com")); assertFalse(match("vi.pt.okchem.com"));// vipcode is too short assertTrue(match("abcdefghij12345678901234567980.en.okchem.com"));// vipcode is just 30 assertFalse(match("abcdefghij123456789012345679801.pt.okchem.com"));// vipcode is too short assertFalse(match("vi.m.pt.okchem.com"));// vipcode is too short assertTrue(match("abcdefghij12345678901234567980.m.pt.okchem.com"));// vipcode is just 30 assertFalse(match("abcdefghij123456789012345679801.m.pt.okchem.com"));// vipcode is too short assertTrue(match("m.www.okchem.com")); assertFalse(match("m.www.jiu-shu.com")); } public static boolean match(String text) { Matcher matcher = pattern.matcher(text); return matcher.matches(); } public static String parseLanguageFromText(String text) throws Exception { Matcher matcherPc = pattern.matcher(text); if(matcherPc.find()) { return matcherPc.group(4); } throw new Exception("Not Match"); } }
|
近期评论