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  
35  public class AbstractIndexCreatorHelper
36      extends AbstractTestSupport
37  {
38      public List<IndexCreator> DEFAULT_CREATORS;
39  
40      public List<IndexCreator> FULL_CREATORS;
41  
42      public List<IndexCreator> MIN_CREATORS;
43  
44      Random rand = new Random();
45  
46      @Override
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51  
52          DEFAULT_CREATORS = new ArrayList<>();
53          FULL_CREATORS = new ArrayList<>();
54          MIN_CREATORS = new ArrayList<>();
55  
56          IndexCreator min = lookup( IndexCreator.class, MinimalArtifactInfoIndexCreator.ID );
57          IndexCreator mavenPlugin = lookup( IndexCreator.class, MavenPluginArtifactInfoIndexCreator.ID );
58          IndexCreator mavenArchetype = lookup( IndexCreator.class, MavenArchetypeArtifactInfoIndexCreator.ID );
59          IndexCreator jar = lookup( IndexCreator.class, JarFileContentsIndexCreator.ID );
60  
61          MIN_CREATORS.add( min );
62  
63          DEFAULT_CREATORS.add( min );
64          DEFAULT_CREATORS.add( mavenPlugin );
65          DEFAULT_CREATORS.add( mavenArchetype );
66  
67          FULL_CREATORS.add( min );
68          FULL_CREATORS.add( mavenPlugin );
69          FULL_CREATORS.add( mavenArchetype );
70          FULL_CREATORS.add( jar );
71      }
72  
73      protected void deleteDirectory( File dir )
74          throws IOException
75      {
76          FileUtils.deleteDirectory( dir );
77      }
78  
79      protected File getDirectory( String name )
80      {
81          // pick random output location
82  
83          File outputFolder = new File( getBasedir(), "target/tests/" + name + "-" + rand.nextLong() + "/" );
84          outputFolder.delete();
85          assertFalse( outputFolder.exists() );
86          return outputFolder;
87      }
88  
89      public void testDirectory()
90          throws IOException
91      {
92          File dir = this.getDirectory( "foo" );
93          assert ( dir.getAbsolutePath().contains( "foo" ) );
94          this.deleteDirectory( dir );
95          assertFalse( dir.exists() );
96  
97          File dir2 = this.getDirectory( "foo" );
98          assertFalse( "Directories aren't unique", dir.getCanonicalPath().equals( dir2.getCanonicalPath() ) );
99      }
100 }