woniper

Spring *Utils Classes 배끼기 본문

Spring

Spring *Utils Classes 배끼기

Woniper 2017.04.01 15:56

Spring Tips: The Spring Framework *Utils Classes 를 보고 정리하면 좋겠다고 생각했다.

전체 예제

DemoClass

@Data
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class DemoClass {

    private List<Map<String, Object>> list = new ArrayList<>();

    @PostConstruct
    public void begin() {
        log.info("begin");
    }

}

org.springframework.util.Assert

@Test
public void assertIsNull() throws Exception {
    demoClass.setList(null);
    Assert.isNull(demoClass.getList(), "the list can't be null");
}

@Test
public void assertIsTrue() throws Exception {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map = new HashMap<>();
    map.put("test", new Object());
    list.add(map);
    demoClass.setList(list);

    Assert.isTrue(list.size() == 1, "list size is 1");
}

Assert 클래스는 간단한 validation 체크를 한다. validation 체크에 실패하면 IllegaArgumentException을 throw 한다.

org.springframework.beans.BeanUtils

@Test
public void testBeansUtils() throws Exception {
    PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(DemoClass.class);

    // class
    PropertyDescriptor classProperty = propertyDescriptors[0];
    assertThat(classProperty.getName()).isEqualTo("class");
    assertThat(classProperty.getReadMethod().getName()).isEqualTo("getClass");

    // list
    PropertyDescriptor listProperty = propertyDescriptors[1];
    assertThat(listProperty.getName()).isEqualTo("list");
    assertThat(listProperty.getReadMethod().getName()).isEqualTo("getList");
}

bean으로 등록된 DemoClass의 property 정보를 알 수 있다. 


DemoClass에서 접근 가능한 getter method.

  • bean instance
  • bean 속성 검사
  • bean 복사

org.springframework.util.ClassUtil

@Test
public void testClassUtils() throws Exception {
    Constructor<DemoClass> demoClassConstructor = ClassUtils.getConstructorIfAvailable(DemoClass.class);

    // DemoClass Constructor Info
    assertThat(demoClassConstructor.toString()).isEqualTo("public net.woniper.spring.utils.DemoClass()");

    // DemoClass newInstance Constructor Info
    DemoClass demoClass = demoClassConstructor.newInstance();
    assertThat(demoClass.toString()).isEqualTo("DemoClass(list=[])");
}

org.springframework.util.SystemPropertyUtils

@Test
public void testSystemPropertyUtils() throws Exception {
    // home directory
    String userHome = SystemPropertyUtils.resolvePlaceholders("${user.home}");

    // resolved home directory
    String userHomeText = SystemPropertyUtils.resolvePlaceholders("my home directory is ${user.home}");

    assertThat(userHomeText).isEqualTo("my home directory is " + userHome);
}

현재 사용하는 system(local) 정보를 알 수 있다.

org.springframework.web.bind.ServletRequestUtils, org.springframework.web.util.WebUtils

@Test
public void testServletRequestUtils() throws Exception {
    HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);

    // ServletRequestUtils
    int age = ServletRequestUtils.getIntParameter(mockServletRequest, "age", -1);
    assertThat(age).isEqualTo(-1);

    // WebUtils
    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE))
            .thenReturn(new File("/mock/directory"));
    when(mockServletRequest.getServletContext())
            .thenReturn(mockServletContext);

    File tempDir = WebUtils.getTempDir(mockServletRequest.getServletContext());
    assertThat(tempDir.getAbsolutePath()).isEqualTo("/mock/directory");
}

javax.servlet.http.HttpServletRequest를 이용해 Web 환경에서 사용할 수 있는 Utils.

org.springframework.aop.support.AopUtils

@Test
public void testAopUtils() throws Exception {
    assertThat(AopUtils.getTargetClass(demoClass).toString()).isEqualTo("class net.woniper.spring.utils.DemoClass");
    assertThat(AopUtils.isAopProxy(demoClass)).isTrue();
    assertThat(AopUtils.isCglibProxy(demoClass)).isTrue();
    assertThat(AopUtils.isJdkDynamicProxy(demoClass)).isFalse();
}

Aop 정보를 알 수 있다.

org.springframework.util.ReflectionUtils

@Test
public void testReflectionUtils() throws Exception {
    ReflectionUtils.doWithFields(DemoClass.class, System.out::print);
    ReflectionUtils.doWithMethods(DemoClass.class, System.out::print);

    Field list = ReflectionUtils.findField(DemoClass.class, "list");
    assertThat(list.toString())
            .isEqualTo("private java.util.List net.woniper.spring.utils.DemoClass.list");

    assertThat(ResolvableType.forField(list).toString())
            .isEqualTo("java.util.List<java.util.Map<java.lang.String, java.lang.Object>>");
}

ReflectionUtils를 이용해 java 기능 중 하나인 Reflection을 쉽게 사용할 수 있다.

0 Comments
댓글쓰기 폼