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.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   * Test support.
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       * Creates the temp directory and list for resource handlers.
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       * Closes all the registered resources handlers and deletes the temp directory.
61       */
62      @After
63      public void cleanup() throws IOException {
64          for (DirectoryResourceHandler directoryResourceHandler : directoryResourceHandlers) {
65              directoryResourceHandler.close();
66          }
67          // delete(tempDir);
68      }
69  
70      /**
71       * Creates a temp file within {@link #tempDir} with given name.
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       * Creates a temp directory within {@link #tempDir}.
81       */
82      protected File createTempDirectory() throws IOException {
83          return Files.createTempDirectory(tempDir.toPath(), testName.getMethodName() + "-dir")
84                  .toFile();
85      }
86  
87      /**
88       * Creates an empty {@link DirectoryResourceHandler}.
89       */
90      protected WritableResourceHandler createWritableResourceHandler() throws IOException {
91          DirectoryResourceHandler result = new DirectoryResourceHandler(createTempDirectory());
92          directoryResourceHandlers.add(result);
93          return result;
94      }
95  
96      /**
97       * Creates a "test" {@link ResourceHandler} that contains predefined files, is mapped to test resources under given
98       * name.
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      * Consumes {@link ChunkReader} and creates a map "by type" with records.
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      * Consumes {@link ChunkReader} and creates a map "by type" with record type counts.
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 }