1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.any23;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import org.apache.any23.source.DocumentSource;
25 import org.apache.any23.source.FileDocumentSource;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.rules.TemporaryFolder;
31
32
33
34
35
36
37
38 public class AbstractAny23TestBase {
39
40 @Rule
41 public TemporaryFolder testFolder = new TemporaryFolder();
42 protected File tempDirectory;
43
44 public AbstractAny23TestBase() {
45 super();
46 }
47
48 @Before
49 public void setUp() throws Exception {
50 tempDirectory = testFolder.newFolder();
51 }
52
53 protected DocumentSource getDocumentSourceFromResource(String resourceLocation) throws IOException {
54 return new FileDocumentSource(copyResourceToTempFile(resourceLocation));
55 }
56
57 protected DocumentSource getDocumentSourceFromResource(String resourceLocation, String baseUri) throws IOException {
58 return new FileDocumentSource(copyResourceToTempFile(resourceLocation), baseUri);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 protected File copyResourceToTempFile(String resourceLocation) throws FileNotFoundException, IOException {
77 Assert.assertNotNull("Temporary directory was null. Did you forget to call super.setUp() to initialise it?",
78 tempDirectory);
79 String fileEnding = resourceLocation.substring(resourceLocation.lastIndexOf("/") + 1);
80
81 File tempFile = File.createTempFile("any23test-", "-" + fileEnding, tempDirectory);
82
83 FileOutputStream output = new FileOutputStream(tempFile);
84
85 InputStream input = this.getClass().getResourceAsStream(resourceLocation);
86
87 Assert.assertNotNull("Test resource was not found: " + resourceLocation, input);
88
89 IOUtils.copy(input, output);
90
91 return tempFile;
92 }
93
94 }