1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar.cpptasks;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import junit.framework.TestCase;
29
30
31
32
33
34
35
36 public abstract class TestXMLConsumer extends TestCase {
37
38
39
40
41
42
43
44
45 public static void copyResourceToTmpDir(final String resourceName, final String tmpFile) throws IOException {
46 String tmpDir = System.getProperty("java.io.tmpdir");
47
48 final File tempdir = File.createTempFile(tmpFile, Long.toString(System.nanoTime()), new File(tmpDir));
49 tempdir.delete();
50 tempdir.mkdir();
51 tempdir.deleteOnExit();
52 tmpDir = tempdir.getAbsolutePath();
53
54
55
56 InputStream src = null;
57 if (TestTargetHistoryTable.class.getClassLoader().getResource(resourceName) != null) {
58 src = TestTargetHistoryTable.class.getClassLoader().getResourceAsStream(resourceName);
59 }
60
61
62
63 if (src == null) {
64 src = new FileInputStream(resourceName);
65 }
66 assertNotNull("Could not locate resource " + resourceName, src);
67 try {
68 final File destFile = new File(tmpDir, tmpFile);
69 destFile.deleteOnExit();
70 try (FileOutputStream dest = new FileOutputStream(destFile)) {
71 int bytesRead = 0;
72 final byte[] buffer = new byte[4096];
73 do {
74 bytesRead = src.read(buffer);
75 if (bytesRead > 0) {
76 dest.write(buffer, 0, bytesRead);
77 }
78 } while (bytesRead == buffer.length);
79 }
80 } finally {
81 src.close();
82 }
83 }
84
85
86
87
88
89
90
91 public static void deleteTmpFile(final String tmpName) throws IOException {
92 final String tmpDir = System.getProperty("java.io.tmpdir");
93 final File tmpFile = new File(tmpDir, tmpName);
94 if (tmpFile.exists()) {
95 tmpFile.delete();
96 }
97 }
98
99
100
101
102 protected TestXMLConsumer(final String testName) {
103 super(testName);
104 }
105 }