|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.github.jsonldjava.utils; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Iterator; |
| 14 | + |
| 15 | +import org.apache.commons.io.IOUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Peter Ansell p_ansell@yahoo.com |
| 19 | + * |
| 20 | + */ |
| 21 | +public class TestUtils { |
| 22 | + |
| 23 | + public static InputStream copyResourceToFileStream(File testDir, String resource) |
| 24 | + throws Exception { |
| 25 | + return new FileInputStream(copyResourceToFile(testDir, resource)); |
| 26 | + } |
| 27 | + |
| 28 | + public static String copyResourceToFile(File testDir, String resource) throws Exception { |
| 29 | + String filename = resource.substring(resource.lastIndexOf('/')); |
| 30 | + String directory = resource.substring(0, resource.lastIndexOf('/')); |
| 31 | + File nextDirectory = new File(testDir, directory); |
| 32 | + nextDirectory.mkdirs(); |
| 33 | + File nextFile = new File(nextDirectory, filename); |
| 34 | + nextFile.createNewFile(); |
| 35 | + |
| 36 | + InputStream inputStream = TestUtils.class.getResourceAsStream(resource); |
| 37 | + assertNotNull("Missing test resource: " + resource, inputStream); |
| 38 | + |
| 39 | + IOUtils.copy(inputStream, new FileOutputStream(nextFile)); |
| 40 | + return nextFile.getAbsolutePath(); |
| 41 | + } |
| 42 | + |
| 43 | + public static String join(Collection<String> list, String delim) { |
| 44 | + final StringBuilder builder = new StringBuilder(); |
| 45 | + final Iterator<String> iter = list.iterator(); |
| 46 | + while (iter.hasNext()) { |
| 47 | + builder.append(iter.next()); |
| 48 | + if (!iter.hasNext()) { |
| 49 | + break; |
| 50 | + } |
| 51 | + builder.append(delim); |
| 52 | + } |
| 53 | + return builder.toString(); |
| 54 | + } |
| 55 | + |
| 56 | + private TestUtils() { |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments