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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Random;
26  
27  import org.apache.maven.index.context.IndexCreator;
28  import org.apache.maven.index.creator.JarFileContentsIndexCreator;
29  import org.apache.maven.index.creator.MavenArchetypeArtifactInfoIndexCreator;
30  import org.apache.maven.index.creator.MavenPluginArtifactInfoIndexCreator;
31  import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertFalse;
36  import static org.junit.Assert.assertNotEquals;
37  
38  public class AbstractIndexCreatorHelper extends AbstractTestSupport {
39      public List<IndexCreator> DEFAULT_CREATORS;
40  
41      public List<IndexCreator> FULL_CREATORS;
42  
43      public List<IndexCreator> MIN_CREATORS;
44  
45      Random rand = new Random();
46  
47      @Override
48      public void setUp() throws Exception {
49          super.setUp();
50  
51          DEFAULT_CREATORS = new ArrayList<>();
52          FULL_CREATORS = new ArrayList<>();
53          MIN_CREATORS = new ArrayList<>();
54  
55          IndexCreator min = lookup(IndexCreator.class, MinimalArtifactInfoIndexCreator.ID);
56          IndexCreator mavenPlugin = lookup(IndexCreator.class, MavenPluginArtifactInfoIndexCreator.ID);
57          IndexCreator mavenArchetype = lookup(IndexCreator.class, MavenArchetypeArtifactInfoIndexCreator.ID);
58          IndexCreator jar = lookup(IndexCreator.class, JarFileContentsIndexCreator.ID);
59  
60          MIN_CREATORS.add(min);
61  
62          DEFAULT_CREATORS.add(min);
63          DEFAULT_CREATORS.add(mavenPlugin);
64          DEFAULT_CREATORS.add(mavenArchetype);
65  
66          FULL_CREATORS.add(min);
67          FULL_CREATORS.add(mavenPlugin);
68          FULL_CREATORS.add(mavenArchetype);
69          FULL_CREATORS.add(jar);
70      }
71  
72      protected void deleteDirectory(File dir) throws IOException {
73          FileUtils.deleteDirectory(dir);
74      }
75  
76      protected File getDirectory(String name) {
77          // pick random output location
78  
79          File outputFolder = new File(getBasedir(), "target/tests/" + name + "-" + rand.nextLong() + "/");
80          outputFolder.delete();
81          assertFalse(outputFolder.exists());
82          return outputFolder;
83      }
84  
85      @Test
86      public void testDirectory() throws IOException {
87          File dir = this.getDirectory("foo");
88          assert (dir.getAbsolutePath().contains("foo"));
89          this.deleteDirectory(dir);
90          assertFalse(dir.exists());
91  
92          File dir2 = this.getDirectory("foo");
93          assertNotEquals("Directories aren't unique", dir.getCanonicalPath(), dir2.getCanonicalPath());
94      }
95  }