Spring AntPathMatcher 사용법
2015. 3. 20. 15:13 개발노트/Spring
반응형
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.springframework.util.AntPathMatcher;
public class AntPathMatcherTest {
AntPathMatcher pathMatcher = new AntPathMatcher();
@Test
// 패턴 여부를 알 수 있다. *|? 포함한다면 패턴으로 인식!
public void isPattern() {
String a = "/syaku/module?/list/*";
String b = "/syaku/module/list";
assertThat( pathMatcher.isPattern(a), equalTo(true) );
assertThat( pathMatcher.isPattern(b), equalTo(false) );
}
@Test
// @PathVariable 과 비슷함.
public void extractUriTemplateVariables() {
String a = "/syaku/module/list";
String b = "/{id}/{name}/{action}";
Map<String, String> map = pathMatcher.extractUriTemplateVariables(b, a);
assertThat(map.get("id"), equalTo("syaku") ); // syaku
assertThat(map.get("name"), equalTo("module") ); // module
assertThat(map.get("action"), equalTo("list") ); // list
}
@Test
// 패턴과 일치하는 문자를 제거함. 하지만 ? * ** 에 해당하는 문자는 제거되지 않음!
public void extractPathWithinPattern() {
String str = "/syaku/module/list";
String a = "/syaku/module/list";
String b = "/syaku/*/list";
String c = "/syaku/?odule/*";
String d = "/syaku/**";
assertThat( pathMatcher.extractPathWithinPattern(a, str), equalTo("") ); // ""
assertThat( pathMatcher.extractPathWithinPattern(b, str), equalTo("module") ); // module/list
assertThat( pathMatcher.extractPathWithinPattern(c, str), equalTo("module/list") ); // module/list
assertThat( pathMatcher.extractPathWithinPattern(d, str), equalTo("module/list") ); // module/list
}
@Test
// 두 패턴을 합쳐준다.
public void combine() {
String a = "/syaku";
String aa = "/module";
assertThat( pathMatcher.combine(a, aa), equalTo("/syaku/module") );
String b = "/syaku/*";
String bb = "/module";
assertThat( pathMatcher.combine(b, bb), equalTo("/syaku/module") );
String c = "/{test}";
String cc = "/module";
assertThat( pathMatcher.combine(c, cc), equalTo("/{test}/module") );
}
@Test
public void match() {
String syaku = "/syaku"; // syaku
String _syaku = "/syaku/"; // syaku
String module = "/syaku/module"; // module 혹은 목록
String insert = "/syaku/module/insert"; // 등록
String _insert = "/syaku/module/insert/"; // 등록
String view = "/syaku/module/1102111"; // 뷰
String _view = "/syaku/module/1102111/"; // 뷰
String syaku_url = "/syaku/**"; // syaku 전체 찾기
assertThat( pathMatcher.match(syaku_url, syaku), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, _syaku), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, module), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, insert), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, _insert), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, view), equalTo(true) );
assertThat( pathMatcher.match(syaku_url, _view), equalTo(true) );
String module_url = "/syaku/module/**"; // module 전체 찾기
assertThat( pathMatcher.match(module_url, syaku), equalTo(false) );
assertThat( pathMatcher.match(module_url, _syaku), equalTo(false) );
assertThat( pathMatcher.match(module_url, module), equalTo(true) );
assertThat( pathMatcher.match(module_url, insert), equalTo(true) );
assertThat( pathMatcher.match(module_url, _insert), equalTo(true) );
assertThat( pathMatcher.match(module_url, view), equalTo(true) );
assertThat( pathMatcher.match(module_url, _view), equalTo(true) );
String list_url = "/syaku/module"; // list 전체 찾기
assertThat( pathMatcher.match(list_url, syaku), equalTo(false) );
assertThat( pathMatcher.match(list_url, _syaku), equalTo(false) );
assertThat( pathMatcher.match(list_url, module), equalTo(true) );
assertThat( pathMatcher.match(list_url, insert), equalTo(false) );
assertThat( pathMatcher.match(list_url, _insert), equalTo(false) );
assertThat( pathMatcher.match(list_url, view), equalTo(false) );
assertThat( pathMatcher.match(list_url, _view), equalTo(false) );
String view_url = "/syaku/module/insert/**"; // insert 전체 찾기
assertThat( pathMatcher.match(view_url, syaku), equalTo(false) );
assertThat( pathMatcher.match(view_url, _syaku), equalTo(false) );
assertThat( pathMatcher.match(view_url, module), equalTo(false) );
assertThat( pathMatcher.match(view_url, insert), equalTo(true) );
assertThat( pathMatcher.match(view_url, _insert), equalTo(true) );
assertThat( pathMatcher.match(view_url, view), equalTo(false) );
assertThat( pathMatcher.match(list_url, _view), equalTo(false) );
}
}
반응형
'개발노트 > Spring' 카테고리의 다른 글
Spring Bean Object 를 새로운 Object 에 복사하기 (0) | 2015.08.31 |
---|---|
Spring 엑셀 데이터 추출 및 차트 만들기 : jxls Excel View , Chart (0) | 2015.08.22 |
Spring Repository Generator : getter setter mybatis mapper (1) | 2015.01.28 |
Error configuring application listener of class org.springframework.web.context.ContextLoaderListener (0) | 2015.01.26 |