1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.reader;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.maven.index.reader.Record.Type;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.rules.TestName;
34
35 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.hamcrest.Matchers.equalTo;
37
38
39
40
41 public class TestSupport {
42 @Rule
43 public TestName testName = new TestName();
44
45 private File tempDir;
46
47 private List<DirectoryResourceHandler> directoryResourceHandlers;
48
49
50
51
52 @Before
53 public void setup() throws IOException {
54 this.tempDir =
55 Files.createTempDirectory(getClass().getSimpleName() + ".temp").toFile();
56 this.directoryResourceHandlers = new ArrayList<>();
57 }
58
59
60
61
62 @After
63 public void cleanup() throws IOException {
64 for (DirectoryResourceHandler directoryResourceHandler : directoryResourceHandlers) {
65 directoryResourceHandler.close();
66 }
67
68 }
69
70
71
72
73 protected File createTempFile(final String name) throws IOException {
74 File file = new File(tempDir, name);
75 file.deleteOnExit();
76 return file;
77 }
78
79
80
81
82 protected File createTempDirectory() throws IOException {
83 return Files.createTempDirectory(tempDir.toPath(), testName.getMethodName() + "-dir")
84 .toFile();
85 }
86
87
88
89
90 protected WritableResourceHandler createWritableResourceHandler() throws IOException {
91 DirectoryResourceHandler result = new DirectoryResourceHandler(createTempDirectory());
92 directoryResourceHandlers.add(result);
93 return result;
94 }
95
96
97
98
99
100 protected DirectoryResourceHandler testResourceHandler(final String name) throws IOException {
101 DirectoryResourceHandler result = new DirectoryResourceHandler(new File("src/test/resources/" + name));
102 directoryResourceHandlers.add(result);
103 return result;
104 }
105
106
107
108
109 protected Map<Type, List<Record>> loadRecordsByType(final ChunkReader chunkReader) throws IOException {
110 HashMap<Type, List<Record>> stat = new HashMap<>();
111 try (chunkReader) {
112 assertThat(chunkReader.getVersion(), equalTo(1));
113 final RecordExpander recordExpander = new RecordExpander();
114 for (Map<String, String> rec : chunkReader) {
115 final Record record = recordExpander.apply(rec);
116 if (!stat.containsKey(record.getType())) {
117 stat.put(record.getType(), new ArrayList<>());
118 }
119 stat.get(record.getType()).add(record);
120 }
121 }
122 return stat;
123 }
124
125
126
127
128 protected Map<Type, Integer> countRecordsByType(final ChunkReader chunkReader) throws IOException {
129 HashMap<Type, Integer> stat = new HashMap<>();
130 try (chunkReader) {
131 assertThat(chunkReader.getVersion(), equalTo(1));
132 final RecordExpander recordExpander = new RecordExpander();
133 for (Map<String, String> rec : chunkReader) {
134 final Record record = recordExpander.apply(rec);
135 if (!stat.containsKey(record.getType())) {
136 stat.put(record.getType(), 0);
137 }
138 stat.put(record.getType(), stat.get(record.getType()) + 1);
139 }
140 }
141 return stat;
142 }
143 }