Created
January 14, 2016 05:18
-
-
Save sbcoba/41004f4298a72ae50e82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.springframework.beans.BeanWrapper; | |
import org.springframework.beans.BeanWrapperImpl; | |
import org.springframework.core.io.Resource; | |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | |
import org.springframework.retry.support.RetryTemplate; | |
import org.springframework.util.FileCopyUtils; | |
import org.springframework.util.FileSystemUtils; | |
import org.springframework.web.client.RestTemplate; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.stream.Stream; | |
public class SpringUsefulUtilClassTests { | |
/** | |
* FileCopyUtils.class | |
* FileSystemUtils.class | |
* file과 stream 을 쉽게 컨트롤 할 수 있는 유틸 | |
* @throws Exception | |
*/ | |
@Test | |
public void fileUtilsTest() throws Exception { | |
String sampleDir = System.getProperty("user.home") + "/sample"; | |
String sampleTestDir = sampleDir + "/test"; | |
Path samplePath = Files.createDirectories(Paths.get(sampleTestDir)); | |
Path sampleFile = Files.createFile(Paths.get(samplePath.toRealPath() + "/test.log")); | |
// 파일에 문자열 적기 | |
FileCopyUtils.copy("test 파일 내용입니다 ^^".getBytes("utf-8"), sampleFile.toFile()); | |
// 파일 내용 가져오기 | |
System.out.println(FileCopyUtils.copyToString(new FileReader(sampleFile.toFile()))); | |
// 폴더 복사하기 | |
FileSystemUtils.copyRecursively(new File(sampleTestDir), new File(sampleTestDir + "_back")); | |
// 폴더 제거하기 | |
FileSystemUtils.deleteRecursively(new File(sampleDir)); | |
} | |
/** | |
* Http 요청을 쉽게 할 수 있는 형태를 제공 | |
* @throws Exception | |
*/ | |
@Test | |
public void restTemplateTest() throws Exception { | |
RestTemplate rt = new RestTemplate(); | |
String contents = rt.getForObject("http://daum.net", String.class); | |
FileCopyUtils.copy(contents.getBytes("utf-8"), new File(System.getProperty("user.home") + "/daum.html")); | |
System.out.println(contents); | |
} | |
/** | |
* RetryTemplate.class | |
* 현재 로직을 쉽게 재시도할 수 있도록 하는 형태를 제공 | |
*/ | |
@Test | |
public void retryTemplateTest() { | |
RetryTemplate r = new RetryTemplate(); | |
r.execute(retryContext -> { | |
System.out.println("Hello world! (retry count:" + (retryContext.getRetryCount() + 1) + ")"); | |
if (true) { | |
throw new RuntimeException("test"); | |
} | |
return Void.class; | |
}, retryContext -> { | |
System.out.printf("All(%d) fail!!!\n", retryContext.getRetryCount()); | |
return Void.class; | |
}); | |
} | |
/** | |
* BeanWrapper.class | |
* Java Bean 을 다이나믹하게 접근할 수 있는 형태를 제공 | |
*/ | |
@Test | |
public void beanWrapperTest() { | |
TestBean bean = new TestBean(); | |
bean.setName("aaa"); | |
bean.setId("1"); | |
BeanWrapper bw = new BeanWrapperImpl(bean); | |
bw.setPropertyValue("id", "aaa"); | |
bw.setPropertyValue("name", "bbb"); | |
Stream.of(bw.getPropertyDescriptors()).forEach(System.out::println); | |
Object wrappedInstance = bw.getWrappedInstance(); | |
System.out.println(wrappedInstance); | |
} | |
/** | |
* PathMatchingResourcePatternResolver.class | |
* Ant 패턴을 이용하여 리소스에 쉽게 접근 가능 | |
* @throws IOException | |
*/ | |
@Test | |
public void pathMatchingResourcePatternResolverTest() throws IOException { | |
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); | |
Resource[] resources = resourcePatternResolver.getResources("file:" + System.getProperty("user.home") + "/git/spring-boot/**/*.java"); | |
Stream.of(resources).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment