View Javadoc
1   package org.apache.maven.index;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Random;
27  
28  import org.apache.maven.index.context.IndexCreator;
29  import org.apache.maven.index.creator.JarFileContentsIndexCreator;
30  import org.apache.maven.index.creator.MavenArchetypeArtifactInfoIndexCreator;
31  import org.apache.maven.index.creator.MavenPluginArtifactInfoIndexCreator;
32  import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.junit.Test;
35  
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertNotEquals;
38  
39  public class AbstractIndexCreatorHelper
40      extends AbstractTestSupport
41  {
42      public List<IndexCreator> DEFAULT_CREATORS;
43  
44      public List<IndexCreator> FULL_CREATORS;
45  
46      public List<IndexCreator> MIN_CREATORS;
47  
48      Random rand = new Random();
49  
50      @Override
51      public void setUp()
52          throws Exception
53      {
54          super.setUp();
55  
56          DEFAULT_CREATORS = new ArrayList<>();
57          FULL_CREATORS = new ArrayList<>();
58          MIN_CREATORS = new ArrayList<>();
59  
60          IndexCreator min = lookup( IndexCreator.class, MinimalArtifactInfoIndexCreator.ID );
61          IndexCreator mavenPlugin = lookup( IndexCreator.class, MavenPluginArtifactInfoIndexCreator.ID );
62          IndexCreator mavenArchetype = lookup( IndexCreator.class, MavenArchetypeArtifactInfoIndexCreator.ID );
63          IndexCreator jar = lookup( IndexCreator.class, JarFileContentsIndexCreator.ID );
64  
65          MIN_CREATORS.add( min );
66  
67          DEFAULT_CREATORS.add( min );
68          DEFAULT_CREATORS.add( mavenPlugin );
69          DEFAULT_CREATORS.add( mavenArchetype );
70  
71          FULL_CREATORS.add( min );
72          FULL_CREATORS.add( mavenPlugin );
73          FULL_CREATORS.add( mavenArchetype );
74          FULL_CREATORS.add( jar );
75      }
76  
77      protected void deleteDirectory( File dir )
78          throws IOException
79      {
80          FileUtils.deleteDirectory( dir );
81      }
82  
83      protected File getDirectory( String name )
84      {
85          // pick random output location
86  
87          File outputFolder = new File( getBasedir(), "target/tests/" + name + "-" + rand.nextLong() + "/" );
88          outputFolder.delete();
89          assertFalse( outputFolder.exists() );
90          return outputFolder;
91      }
92  
93      @Test
94      public void testDirectory()
95          throws IOException
96      {
97          File dir = this.getDirectory( "foo" );
98          assert ( dir.getAbsolutePath().contains( "foo" ) );
99          this.deleteDirectory( dir );
100         assertFalse( dir.exists() );
101 
102         File dir2 = this.getDirectory( "foo" );
103         assertNotEquals( "Directories aren't unique", dir.getCanonicalPath(), dir2.getCanonicalPath() );
104     }
105 }