001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.utils.file; 015 016import java.io.File; 017import java.io.FileNotFoundException; 018import java.io.IOException; 019import java.util.Collections; 020import java.util.HashSet; 021import java.util.Set; 022 023import org.junit.jupiter.api.Test; 024 025import static org.junit.jupiter.api.Assertions.assertEquals; 026import static org.junit.jupiter.api.Assertions.assertThrows; 027 028public class ResourcesUtilTest { 029 030 private InputStreamUtils isu = new InputStreamUtils(); 031 032 @Test 033 public void testCopyResources() throws Exception { 034 File tmp = FileUtils.createTempDir(); 035 tmp.deleteOnExit(); 036 ResourcesUtil.copy( 037 tmp, "", true, "testNOT_EXISTING.txt", "test1/test.txt", "test1/test2/test.txt"); 038 039 // test 040 File t1 = new File(tmp, "test1/test.txt"); 041 File t2 = new File(tmp, "test1/test2/test.txt"); 042 org.apache.commons.io.FileUtils.contentEquals(t1, t2); 043 assertTestFile(t1); 044 assertTestFile(t2); 045 } 046 047 @Test 048 public void testCopyResourcesThrowing() throws Exception { 049 File tmp = FileUtils.createTempDir(); 050 tmp.deleteOnExit(); 051 assertThrows( 052 IOException.class, 053 () -> 054 ResourcesUtil.copy( 055 tmp, "", false, "testNOT_EXISTING.txt", "test1/test.txt", "test1/test2/test.txt")); 056 057 // test 058 File t1 = new File(tmp, "test1/test.txt"); 059 File t2 = new File(tmp, "test1/test2/test.txt"); 060 org.apache.commons.io.FileUtils.contentEquals(t1, t2); 061 assertThrows(IOException.class, () -> assertTestFile(t1)); 062 assertThrows(IOException.class, () -> assertTestFile(t2)); 063 } 064 065 @Test 066 public void testList() throws Exception { 067 assertEquals( 068 newHashSet("test.txt", "test2"), 069 newHashSet(ResourcesUtil.list(ResourcesUtil.class, "test1"))); 070 assertEquals( 071 newHashSet("test.txt"), newHashSet(ResourcesUtil.list(ResourcesUtil.class, "test1/test2"))); 072 assertEquals( 073 newHashSet("utf16be.xml", "utf16le.xml", "utf8.xml", "utf8bom.xml"), 074 newHashSet(ResourcesUtil.list(ResourcesUtil.class, "sax"))); 075 assertEquals(newHashSet(), newHashSet(ResourcesUtil.list(ResourcesUtil.class, "abba"))); 076 } 077 078 private Set<String> newHashSet(String... elements) { 079 HashSet<String> set = new HashSet<>(elements.length); 080 Collections.addAll(set, elements); 081 return set; 082 } 083 084 private void assertTestFile(File tf) throws FileNotFoundException { 085 assertEquals("hallo", isu.readEntireStream(FileUtils.getInputStream(tf))); 086 } 087}