Full Stack Web Developer.
Syaku (샤쿠)

Java, JS (ES6+), Spring, Spring security, jQuery, Reactjs, Bootstrap.

        

07-12 08:23


Spring Boot 2 보기 Front-end 보기 DevOps 보기 Spring 3 보기 Spring Security 3 보기

Spring AntPathMatcher 사용법

written by Seok Kyun. Choi. 최석균


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) );


    }
}

















posted syaku blog

Syaku Blog by Seok Kyun. Choi. 최석균.

http://syaku.tistory.com


댓글 남기기
◀ PREV 1···727374757677787980···313 NEXT ▶