diff --git a/Foundation/org.egovframe.rte.fdl.filehandling/src/test/java/org/egovframe/rte/fdl/filehandling/FilehandlingServiceTest.java b/Foundation/org.egovframe.rte.fdl.filehandling/src/test/java/org/egovframe/rte/fdl/filehandling/FilehandlingServiceTest.java index 0f9303f8..7f6f7c9a 100644 --- a/Foundation/org.egovframe.rte.fdl.filehandling/src/test/java/org/egovframe/rte/fdl/filehandling/FilehandlingServiceTest.java +++ b/Foundation/org.egovframe.rte.fdl.filehandling/src/test/java/org/egovframe/rte/fdl/filehandling/FilehandlingServiceTest.java @@ -15,49 +15,36 @@ */ package org.egovframe.rte.fdl.filehandling; -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.URL; -import java.util.Iterator; -import java.util.List; - import org.apache.commons.io.FileSystemUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.LineIterator; -import org.apache.commons.vfs2.CacheStrategy; -import org.apache.commons.vfs2.FileContent; -import org.apache.commons.vfs2.FileName; -import org.apache.commons.vfs2.FileObject; -import org.apache.commons.vfs2.FileSystemManager; -import org.apache.commons.vfs2.FileSystemOptions; -import org.apache.commons.vfs2.FilesCache; -import org.apache.commons.vfs2.Selectors; -import org.apache.commons.vfs2.VFS; +import org.apache.commons.vfs2.*; import org.apache.commons.vfs2.auth.StaticUserAuthenticator; import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder; import org.apache.commons.vfs2.impl.DefaultFileSystemManager; import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider; +import org.egovframe.rte.fdl.logging.util.EgovResourceReleaser; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.*; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Iterator; +import java.util.List; + import static org.junit.Assert.*; /** * FileServiceTest is TestCase of File Handling Service * @author Seongjong Yoon */ -//@RunWith(SpringJUnit4ClassRunner.class) -//@ContextConfiguration(locations = {"classpath*:spring/context-*.xml" }) public class FilehandlingServiceTest { private static final Logger LOGGER = LoggerFactory.getLogger(FilehandlingServiceTest.class); - private String filename = ""; private String text = ""; private String tmppath = ""; @@ -68,23 +55,19 @@ public void onSetUp() throws Exception { filename = "test.txt"; text = "test입니다."; tmppath = "tmp"; - + LOGGER.debug("System's temporary directory : {}", EgovFileUtil.getTmpDirectory()); - - absoluteFilePath = EgovFileUtil.getTmpDirectory() + "/testing.txt"; - + + absoluteFilePath = EgovFileUtil.getTmpDirectory() + "/testing.txt"; EgovFileUtil.cd(System.getProperty("user.dir")); } /** * 특정 위치에 파일을 생성하고 필요에 따라 생성한 파일을 캐싱한다. - * - * @throws Exception */ @Test public void testCeateFile() throws Exception { FileSystemManager manager = VFS.getManager(); - FileObject baseDir = manager.resolveFile(System.getProperty("user.dir")); final FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt"); @@ -100,14 +83,10 @@ public void testCeateFile() throws Exception { /** * 특정 위치에 존재하는 파일에 접근하여 파일 내용을 수정한다. * 파일 위치는 절대 경로 또는 상대 경로 등 다양한 형식을 지원한다. - * - * @throws Exception */ @Test - public void testAccessFile() throws Exception { - + public void testAccessFile() throws IOException { FileSystemManager manager = VFS.getManager(); - FileObject baseDir = manager.resolveFile(System.getProperty("user.dir")); FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt"); @@ -125,84 +104,60 @@ public void testAccessFile() throws Exception { // 파일 쓰기 String string = "test입니다."; OutputStream os = fileContent.getOutputStream(); - try { os.write(string.getBytes()); os.flush(); } finally { - if (os != null) { - try { - os.close(); - } catch (Exception ignore) { - // no-op - } - } + EgovResourceReleaser.close(os); } assertNotSame(0, fileContent.getSize()); // 파일 읽기 - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); FileObject writtenFile = manager.resolveFile(baseDir, "testfolder/file1.txt"); FileContent writtenContents = writtenFile.getContent(); InputStream is = writtenContents.getInputStream(); - try { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - - String line = ""; + String line; while ((line = reader.readLine()) != null) { sb.append(line); } } finally { - if (is != null) { - try { - is.close(); - } catch (Exception ignore) { - // no-op - } - } + EgovResourceReleaser.close(is); } - - // 파일내용 검증 assertEquals(sb.toString(), string); } /** * 캐싱 기능을 사용하여, 생성하거나 수정할 파일을 메모리 상에 로딩함으로써 * 파일 접근 시에 소요되는 시간을 단축한다. - * - * @throws Exception */ @Test - public void testCaching() throws Exception { + public void testCaching() throws IOException { String path = FilehandlingServiceTest.class.getResource("").getPath(); String testFolder = path + "/testfolder"; FileSystemManager manager = VFS.getManager(); - FileObject scratchFolder = manager.resolveFile(testFolder); // testfolder 내의 모든 파일 삭제 scratchFolder.delete(Selectors.EXCLUDE_SELF); - + FileObject file = manager.resolveFile(path + "/testfolder/dummy.txt"); file.createFile(); // 캐싱 Manager 생성 DefaultFileSystemManager fs = new DefaultFileSystemManager(); fs.setFilesCache(manager.getFilesCache()); - - // zip, jar, tgz, tar, tbz2, file if (!fs.hasProvider("file")) { fs.addProvider("file", new DefaultLocalFileProvider()); } - fs.setCacheStrategy(CacheStrategy.ON_RESOLVE); fs.init(); // 캐싱 객체 생성 FileObject foBase2 = fs.resolveFile(testFolder); LOGGER.debug("## scratchFolder.getName().getPath() : {}", scratchFolder.getName().getPath()); - FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath()); // 파일이 존재하지 않음 @@ -212,28 +167,23 @@ public void testCaching() throws Exception { // 파일생성 scratchFolder.resolveFile("file1.txt").createFile(); - // 파일 존재함 - // BUT cachedFolder 에는 파일이 존재하지 않음 + // 파일 존재, cachedFolder 에는 파일이 존재하지 않음 fos = cachedFolder.getChildren(); assertFalse(contains(fos, "file1.txt")); // 새로고침 cachedFolder.refresh(); + // 파일이 존재함 fos = cachedFolder.getChildren(); assertTrue(contains(fos, "file1.txt")); - } /** * 파일 생성 테스트. - * - * @throws Exception */ @Test - public void testWriteFile() throws Exception { - - // delete file + public void testWriteFile() throws IOException { if (EgovFileUtil.isExistsFile(filename)) { EgovFileUtil.delete(new File(filename)); } @@ -244,13 +194,9 @@ public void testWriteFile() throws Exception { /** * 파일 생성 테스트. - * - * @throws Exception */ @Test - public void testWriteFileWithAbsolutePath() throws Exception { - - // delete file + public void testWriteFileWithAbsolutePath() throws IOException { if (EgovFileUtil.isExistsFile(absoluteFilePath)) { EgovFileUtil.delete(new File(absoluteFilePath)); } @@ -261,60 +207,39 @@ public void testWriteFileWithAbsolutePath() throws Exception { /** * 파일 읽기 테스트. - * - * @throws Exception */ @Test - public void testReadFile() throws Exception { - + public void testReadFile() throws IOException { if (!EgovFileUtil.isExistsFile(filename)) { EgovFileUtil.writeFile(filename, text, "UTF-8"); } - assertEquals(EgovFileUtil.readFile(new File(filename), "UTF-8"), text); - //LOGGER.debug(EgovFileUtil.readTextFile(filename, false)); - List lines = FileUtils.readLines(new File(filename), "UTF-8"); - LOGGER.debug(lines.toString()); - String string = lines.get(0); - assertEquals(text, string); } /** * 파일 읽기 테스트. - * - * @throws Exception */ @Test - public void testReadFileWithAbsolutePath() throws Exception { - + public void testReadFileWithAbsolutePath() throws IOException { if (!EgovFileUtil.isExistsFile(absoluteFilePath)) { EgovFileUtil.writeFile(absoluteFilePath, text, "UTF-8"); } - assertEquals(EgovFileUtil.readFile(new File(absoluteFilePath), "UTF-8"), text); - //LOGGER.debug(EgovFileUtil.readTextFile(filename, false)); - List lines = FileUtils.readLines(new File(absoluteFilePath), "UTF-8"); - LOGGER.debug(lines.toString()); - String string = lines.get(0); - assertEquals(text, string); } /** * 파일 복사 테스트. - * - * @throws Exception */ @Test - public void testCp() throws Exception { - + public void testCp() throws IOException { if (!EgovFileUtil.isExistsFile(filename)) { EgovFileUtil.writeFile(filename, text); } @@ -326,15 +251,12 @@ public void testCp() throws Exception { EgovFileUtil.readFile(new File(tmppath + "/" + filename), "UTF-8") ); } - + /** * 파일 복사 테스트. - * - * @throws Exception */ @Test - public void testCpWithAbsolutePath() throws Exception { - + public void testCpWithAbsolutePath() throws IOException { if (!EgovFileUtil.isExistsFile(absoluteFilePath)) { EgovFileUtil.writeFile(absoluteFilePath, text); } @@ -347,15 +269,11 @@ public void testCpWithAbsolutePath() throws Exception { ); } - /** * 파일 이동 테스트. - * - * @throws Exception */ @Test - public void testMv() throws Exception { - + public void testMv() throws IOException { if (!EgovFileUtil.isExistsFile(tmppath + "/" + filename)) { EgovFileUtil.writeFile(tmppath + "/" + filename, text); } @@ -370,69 +288,50 @@ public void testMv() throws Exception { /** * 파일 터치 테스트. - * - * @throws Exception */ @Test - public void testTouch() throws Exception { - - String path = tmppath + "/movedfile.txt"; - FileObject file = EgovFileUtil.getFileObject(path); + public void testTouch() throws IOException { + String path = tmppath + "/movedfile.txt"; + FileObject file = EgovFileUtil.getFileObject(path); + long lastModifyTime = file.getContent().getLastModifiedTime(); - long setTime = EgovFileUtil.touch(path); - - assertNotEquals(file.getContent().getLastModifiedTime(), setTime); + long setTime = EgovFileUtil.touch(path); + assertNotEquals(lastModifyTime, setTime); } /** * 파일 확장자 처리 테스트. - * - * @throws Exception */ @Test - public void testGetFileExtension() throws Exception { - + public void testGetFileExtension() { assertTrue(EgovFileUtil.isExistsFile(filename)); assertEquals(EgovFileUtil.getFileExtension(filename), "txt"); - } /** * 파일 존재 유무 테스트. - * - * @throws Exception */ @Test - public void testIsExistsFile() throws Exception { - + public void testIsExistsFile() { assertTrue(EgovFileUtil.isExistsFile(filename)); - } /** * 파일명 확인 테스트. - * - * @throws Exception */ @Test - public void testStripFilename() throws Exception { - + public void testStripFilename() { assertTrue(EgovFileUtil.isExistsFile(filename)); assertEquals("test", EgovFileUtil.stripFilename(filename)); - } /** * 파일 삭제 테스트. - * - * @throws Exception */ @Test - public void testRm() throws Exception { - + public void testRm() throws IOException { String tmptarget = tmppath; - if (!EgovFileUtil.isExistsFile(tmptarget)) { EgovFileUtil.writeFile(tmptarget, text); } @@ -445,25 +344,17 @@ public void testRm() throws Exception { /** * 디렉토리 변경 테스트. - * - * @throws Exception */ @Test - public void testCd() throws Exception { - + public void testCd() throws IOException { String path = "/Users/EGOV/"; FileName foldername = EgovFileUtil.getFileObject(path).getName(); - EgovFileUtil.cd(""); String uri = EgovFileUtil.pwd().getURI(); - LOGGER.debug("EgovFileUtil.pwd().getURI() : {}", uri); - LOGGER.debug("foldername.getURI() : {}", foldername.getURI()); - assertFalse(foldername.getURI().equals(uri)); EgovFileUtil.cd(path); - uri = EgovFileUtil.pwd().getURI(); LOGGER.debug("EgovFileUtil.pwd().getURI() : {}", uri); LOGGER.debug("foldername.getURI() : {}", foldername.getURI()); @@ -472,16 +363,12 @@ public void testCd() throws Exception { /** * Stream 테스트. - * - * @throws Exception */ @Test - public void testIOUtils() throws Exception { - InputStream in = new URL("http://jakarta.apache.org").openStream(); + public void testIOUtils() throws IOException { + InputStream in = new URL("https://commons.apache.org/").openStream(); try { - - assertFalse(IOUtils.toString(in).equals("")); - + assertNotEquals("", IOUtils.toString(in, StandardCharsets.UTF_8)); } finally { IOUtils.closeQuietly(in); } @@ -489,119 +376,78 @@ public void testIOUtils() throws Exception { /** * FileSystemUtils 테스트. - * - * @throws Exception */ @Test - public void testFileSystemUtils() throws Exception { - - try { - long freeSpace = FileSystemUtils.freeSpaceKb("C:/"); - - assertTrue(freeSpace > 0); - - } catch (Exception e) { - LOGGER.error("error: {}", e.getCause()); - } + public void testFileSystemUtils() throws IOException { + long freeSpace = FileSystemUtils.freeSpaceKb("C:/"); + assertTrue(freeSpace > 0); } /** * GREP 테스트. - * - * @throws Exception */ @Test - public void testGrep() throws Exception { - - try { - String[] search = {"abcdefg", "efghijklmn", "12", "3"}; - - List lists = EgovFileUtil.grep(search, "\\d{1,2}"); - - for (Iterator it = lists.iterator(); it.hasNext();) { - LOGGER.info(it.next()); - } - - - lists = EgovFileUtil.grep(new File("pom.xml"), "org.egovframe.rte"); - - for (Iterator it = lists.iterator(); it.hasNext();) { - LOGGER.info(it.next()); - } - + public void testGrep() throws IOException { + String[] search = {"abcdefg", "efghijklmn", "12", "3"}; + List lists = EgovFileUtil.grep(search, "\\d{1,2}"); + for (Iterator it = lists.iterator(); it.hasNext();) { + LOGGER.info(it.next()); + } - } catch (Exception e) { - LOGGER.error("error: {}", e.getCause()); - } + lists = EgovFileUtil.grep(new File("pom.xml"), "org.egovframe.rte"); + for (Iterator it = lists.iterator(); it.hasNext();) { + LOGGER.info(it.next()); + } } /** * Line iterator 테스트. - * - * @throws Exception */ @Test - public void testLineIterator() throws Exception { - + public void testLineIterator() throws IOException { String[] string = { - "", - " 4.0.0", - " org.egovframe.rte", - " org.egovframe.rte.fdl.filehandling", - " jar", - " 4.0.0", - " org.egovframe.rte.fdl.filehandling" + " 4.0.0", + " org.egovframe.rte", + " org.egovframe.rte.fdl.filehandling", + " 4.2.0", + " jar", + " org.egovframe.rte.fdl.filehandling" }; - try { - File file = new File("pom.xml"); - - LineIterator it = FileUtils.lineIterator(file, "UTF-8"); + File file = new File("pom.xml"); + LineIterator it = FileUtils.lineIterator(file, "UTF-8"); - try { - LOGGER.debug("############################# LineIterator ###############################"); - - for (int i = 0; it.hasNext(); i++) { - String line = it.nextLine(); - LOGGER.info(line); - - assertEquals(string[i], line); - } - } finally { - LineIterator.closeQuietly(it); - } - - } catch (Exception e) { - LOGGER.error("error: {}", e.getCause()); - } + try { + for (int i = 0; i < string.length; i++) { + String line = it.nextLine(); + assertEquals(string[i], line); + } + } finally { + LineIterator.closeQuietly(it); + } } @Test - public void testUserAuthentication() throws Exception { + public void testUserAuthentication() throws IOException { StaticUserAuthenticator auth = new StaticUserAuthenticator(null, "username", "password"); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); FileSystemManager manager = VFS.getManager(); - FileObject baseDir = manager.resolveFile(System.getProperty("user.dir")); FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt"); FileObject fo = manager.resolveFile(file.getName().getPath(), opts); - fo.createFile(); - } @Test - public void testCaching1() throws Exception { + public void testCaching1() throws IOException { String testFolder = FilehandlingServiceTest.class.getResource(".").getPath(); - LOGGER.debug("testFolder = {}", testFolder); - FileSystemManager manager = VFS.getManager(); - EgovFileUtil.writeFile(testFolder + "/file1.txt", text, "UTF-8"); /* @@ -613,54 +459,30 @@ public void testCaching1() throws Exception { */ DefaultFileSystemManager fs = new DefaultFileSystemManager(); fs.setFilesCache(manager.getFilesCache()); - - // zip, jar, tgz, tar, tbz2, file if (!fs.hasProvider("file")) { fs.addProvider("file", new DefaultLocalFileProvider()); } - // StandardFileSystemManager fs = new StandardFileSystemManager(); - fs.setCacheStrategy(CacheStrategy.ON_RESOLVE); fs.init(); - // 캐싱 객체 생성 - //FileObject foBase2 = fs.resolveFile(testFolder); LOGGER.debug("####1"); FileObject cachedFile = fs.toFileObject(new File(testFolder + "/file1.txt")); LOGGER.debug("####2"); - FilesCache filesCache = fs.getFilesCache(); LOGGER.debug("####3"); filesCache.putFile(cachedFile); FileObject obj = filesCache.getFile(cachedFile.getFileSystem(), cachedFile.getName()); - // FileObject baseFile = fs.getBaseFile(); - // LOGGER.debug("### cachedFile.getContent().getSize() is {}", cachedFile.getContent().getSize()); - - - // long fileSize = cachedFile.getContent().getSize(); - // LOGGER.debug("#########size is {}", fileSize); - // FileObject cachedFile1 = cachedFile.resolveFile("file2.txt"); - - // FileObject scratchFolder = manager.resolveFile(testFolder); - // scratchFolder.delete(Selectors.EXCLUDE_SELF); - EgovFileUtil.delete(new File(testFolder + "/file1.txt")); - - // obj.createFile(); - - // LOGGER.debug("#########obj is {}", obj.toString()); - // LOGGER.debug("#########size is {}", obj.getContent().getSize()); LOGGER.debug("#########file is {}", obj.exists()); fs.close(); } @Test - public void testCaching3() throws Exception { + public void testCaching3() throws IOException { FileSystemManager manager = VFS.getManager(); - String testFolder = FilehandlingServiceTest.class.getResource(".").getPath(); FileObject scratchFolder = manager.resolveFile(testFolder + "/testfolder"); @@ -673,21 +495,16 @@ public void testCaching3() throws Exception { // check if the cache still holds the right instance FileObject dir22 = scratchFolder.resolveFile("file2.txt"); - assertTrue(dir2 == dir22); - - // check if the cache still holds the right instance - /* FileObject dir1_2 = scratchFolder.resolveFile("file1.txt"); - assertFalse(dir1 == dir1_2);*/ + assertSame(dir2, dir22); } private boolean contains(FileObject[] fos, String string) { - for (int i = 0; i < fos.length; i++) { - if (string.equals(fos[i].getName().getBaseName())) { + for (FileObject fo : fos) { + if (string.equals(fo.getName().getBaseName())) { LOGGER.debug("# {}", string); return true; } } - LOGGER.debug("# {} should be seen", string); return false; }