@Test public void testStandardUrlSegmentPattern() { MetaPattern pattern = new MetaPattern(AbstractURLSegment.SEGMENT_PARAMETER); Matcher matcher = pattern.matcher(""); assertFalse(matcher.matches()); matcher = pattern.matcher("seg&ment"); assertFalse(matcher.matches()); matcher = pattern.matcher("segment:"); assertFalse(matcher.matches()); matcher = pattern.matcher("{*}"); assertFalse(matcher.matches()); matcher = pattern.matcher("{segment}"); assertTrue(matcher.matches()); matcher = pattern.matcher("{segment0} a segment {segment1} another segment {segment2}"); assertTrue(matcher.find()); matcher.reset(); assertFalse(matcher.matches()); matcher = pattern.matcher("{117}"); assertFalse(matcher.matches()); pattern = new MetaPattern(AbstractURLSegment.REGEXP_BODY); matcher = pattern.matcher("[0-9]*:abba"); assertTrue(matcher.matches()); matcher = pattern.matcher("^\\(?\\d{3}\\)?[ -]?\\d{3}[ -]?\\d{4}$anotherseg"); assertTrue(matcher.matches()); }
@Test public void testParamSegment() throws Exception { String segmentWithRegEx = "{id:[0-9]*:abba}"; AbstractURLSegment segment = AbstractURLSegment.newSegment(segmentWithRegEx); assertTrue(segment instanceof ParamSegment); ParamSegment paramSegment = (ParamSegment) segment; assertEquals(paramSegment.getParamName(), "id"); assertEquals(paramSegment.getMetaPattern().toString(), "[0-9]*:abba"); MetaPattern metaPattern = paramSegment.getMetaPattern(); assertTrue(metaPattern.matcher("1:abba").matches()); assertTrue(metaPattern.matcher("1234521:abba").matches()); assertTrue(metaPattern.matcher(":abba").matches()); String segmentMultiParam = "{segment0}asegment{segment1:^\\(?\\d{3}\\)?[ -]?\\d{3}[ -]?\\d{4}$}anotherseg"; segment = AbstractURLSegment.newSegment(segmentMultiParam); assertTrue(segment instanceof MultiParamSegment); MultiParamSegment multiParamSegment = (MultiParamSegment) segment; List<AbstractURLSegment> subSegments = multiParamSegment.getSubSegments(); assertEquals(4, subSegments.size()); metaPattern = subSegments.get(2).getMetaPattern(); assertEquals(metaPattern.toString(), "^\\(?\\d{3}\\)?[ -]?\\d{3}[ -]?\\d{4}$"); segmentMultiParam = "filename-{symbolicName:[a-z]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}"; segment = AbstractURLSegment.newSegment(segmentMultiParam); multiParamSegment = (MultiParamSegment) segment; metaPattern = multiParamSegment.getMetaPatternWithGroups(); String fileName = "filename-gsaon-1.2.3.zip"; Matcher matcher = metaPattern.matcher(fileName); assertTrue(matcher.matches()); // testing segment parsing with regular expressions assertEquals("gsaon", matcher.group(1)); assertEquals("1.2.3", matcher.group(2)); assertEquals(".zip", matcher.group(3)); matcher = metaPattern.matcher("gsaon-1.2.3.zip"); assertFalse(matcher.matches()); }