View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Test support.
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       * Creates the temp directory and list for resource handlers.
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       * Closes all the registered resources handlers and deletes the temp directory.
60       */
61      @After
62      public void cleanup() throws IOException {
63          for (DirectoryResourceHandler directoryResourceHandler : directoryResourceHandlers) {
64              directoryResourceHandler.close();
65          }
66          // delete(tempDir);
67      }
68  
69      /**
70       * Creates a temp file within {@link #tempDir} with given name.
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       * Creates a temp directory within {@link #tempDir}.
80       */
81      protected Path createTempDirectory() throws IOException {
82          return Files.createTempDirectory(tempDir, testName.getMethodName() + "-dir");
83      }
84  
85      /**
86       * Creates an empty {@link DirectoryResourceHandler}.
87       */
88      protected WritableResourceHandler createWritableResourceHandler() throws IOException {
89          DirectoryResourceHandler result = new DirectoryResourceHandler(createTempDirectory());
90          directoryResourceHandlers.add(result);
91          return result;
92      }
93  
94      /**
95       * Creates a "test" {@link ResourceHandler} that contains predefined files, is mapped to test resources under given
96       * name.
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      * Consumes {@link ChunkReader} and creates a map "by type" with records.
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      * Consumes {@link ChunkReader} and creates a map "by type" with record type counts.
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 }