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.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
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 Path tempDir;
46  
47      private List<DirectoryResourceHandler> directoryResourceHandlers;
48  
49      
50  
51  
52      @Before
53      public void setup() throws IOException {
54          this.tempDir = Files.createTempDirectory(getClass().getSimpleName() + ".temp");
55          this.directoryResourceHandlers = new ArrayList<>();
56      }
57  
58      
59  
60  
61      @After
62      public void cleanup() throws IOException {
63          for (DirectoryResourceHandler directoryResourceHandler : directoryResourceHandlers) {
64              directoryResourceHandler.close();
65          }
66          
67      }
68  
69      
70  
71  
72      protected Path createTempFile(final String name) throws IOException {
73          Path file = tempDir.resolve(name);
74          file.toFile().deleteOnExit();
75          return file;
76      }
77  
78      
79  
80  
81      protected Path createTempDirectory() throws IOException {
82          return Files.createTempDirectory(tempDir, testName.getMethodName() + "-dir");
83      }
84  
85      
86  
87  
88      protected WritableResourceHandler createWritableResourceHandler() throws IOException {
89          DirectoryResourceHandler result = new DirectoryResourceHandler(createTempDirectory());
90          directoryResourceHandlers.add(result);
91          return result;
92      }
93  
94      
95  
96  
97  
98      protected DirectoryResourceHandler testResourceHandler(final String name) throws IOException {
99          DirectoryResourceHandler result = new DirectoryResourceHandler(Path.of("src/test/resources/" + name));
100         directoryResourceHandlers.add(result);
101         return result;
102     }
103 
104     
105 
106 
107     protected Map<Type, List<Record>> loadRecordsByType(final ChunkReader chunkReader) throws IOException {
108         HashMap<Type, List<Record>> stat = new HashMap<>();
109         try (chunkReader) {
110             assertThat(chunkReader.getVersion(), equalTo(1));
111             final RecordExpander recordExpander = new RecordExpander();
112             for (Map<String, String> rec : chunkReader) {
113                 final Record record = recordExpander.apply(rec);
114                 if (!stat.containsKey(record.getType())) {
115                     stat.put(record.getType(), new ArrayList<>());
116                 }
117                 stat.get(record.getType()).add(record);
118             }
119         }
120         return stat;
121     }
122 
123     
124 
125 
126     protected Map<Type, Integer> countRecordsByType(final ChunkReader chunkReader) throws IOException {
127         HashMap<Type, Integer> stat = new HashMap<>();
128         try (chunkReader) {
129             assertThat(chunkReader.getVersion(), equalTo(1));
130             final RecordExpander recordExpander = new RecordExpander();
131             for (Map<String, String> rec : chunkReader) {
132                 final Record record = recordExpander.apply(rec);
133                 if (!stat.containsKey(record.getType())) {
134                     stat.put(record.getType(), 0);
135                 }
136                 stat.put(record.getType(), stat.get(record.getType()) + 1);
137             }
138         }
139         return stat;
140     }
141 }