View Javadoc

1   package org.apache.maven.index.util;
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.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.apache.maven.index.context.IndexCreator;
27  import org.codehaus.plexus.PlexusTestCase;
28  import org.junit.Assert;
29  
30  public class IndexCreatorSorterTest
31      extends PlexusTestCase
32  {
33      public void testLookupList()
34          throws Exception
35      {
36          final List<IndexCreator> creators = getContainer().lookupList( IndexCreator.class );
37  
38          final List<IndexCreator> sortedCreators = IndexCreatorSorter.sort( creators );
39  
40          // we are interested in IDs only
41          final List<String> sortedCreatorIds = new ArrayList<String>();
42          for ( IndexCreator creator : sortedCreators )
43          {
44              sortedCreatorIds.add( creator.getId() );
45          }
46  
47          // ensure we fulfil some basic conditions
48          Assert.assertTrue( "min should be present", sortedCreatorIds.contains( "min" ) );
49          Assert.assertTrue( "maven-plugin should be present", sortedCreatorIds.contains( "maven-plugin" ) );
50          Assert.assertTrue( "maven-archetype should be present", sortedCreatorIds.contains( "maven-archetype" ) );
51  
52          // currently, both "maven-plugin" and "maven-archetype" creator depend on "min" creator
53          Assert.assertTrue( "maven-archetype depends on min",
54              sortedCreatorIds.indexOf( "min" ) < sortedCreatorIds.indexOf( "maven-archetype" ) );
55          Assert.assertTrue( "maven-plugin depends on min",
56              sortedCreatorIds.indexOf( "min" ) < sortedCreatorIds.indexOf( "maven-plugin" ) );
57      }
58  
59      public void testLookupListWithSpoofedCreator()
60          throws Exception
61      {
62          final List<IndexCreator> creators =
63              new ArrayList<IndexCreator>( getContainer().lookupList( IndexCreator.class ) );
64  
65          // now we add spoofs to it, this one depends on ALL creators. Note: we add it as 1st intentionally
66          creators.add( 0,
67              new SpoofIndexCreator( "depend-on-all", new ArrayList<String>(
68                  getContainer().lookupMap( IndexCreator.class ).keySet() ) ) );
69  
70          // now we add spoofs to it, this one depends on only one, the "depend-on-all" creator Note: we add it as 1st
71          // intentionally
72          creators.add( 0, new SpoofIndexCreator( "last", Arrays.asList( "depend-on-all" ) ) );
73  
74          final List<IndexCreator> sortedCreators = IndexCreatorSorter.sort( creators );
75  
76          // we are interested in IDs only
77          final List<String> sortedCreatorIds = new ArrayList<String>();
78          for ( IndexCreator creator : sortedCreators )
79          {
80              sortedCreatorIds.add( creator.getId() );
81          }
82  
83          // ensure we fulfil some basic conditions
84          Assert.assertTrue( "min should be present", sortedCreatorIds.contains( "min" ) );
85          Assert.assertTrue( "maven-plugin should be present", sortedCreatorIds.contains( "maven-plugin" ) );
86          Assert.assertTrue( "maven-archetype should be present", sortedCreatorIds.contains( "maven-archetype" ) );
87          Assert.assertTrue( "depend-on-all should be present", sortedCreatorIds.contains( "depend-on-all" ) );
88          Assert.assertTrue( "last should be present", sortedCreatorIds.contains( "last" ) );
89  
90          // "last" has to be last
91          Assert.assertTrue( "last creator should be last",
92              sortedCreatorIds.indexOf( "last" ) == sortedCreatorIds.size() - 1 );
93          Assert.assertTrue( "depend-on-all should be next to last",
94              sortedCreatorIds.indexOf( "depend-on-all" ) == sortedCreatorIds.size() - 2 );
95      }
96  
97      public void testLookupListWithNonExistentCreatorDependency()
98          throws Exception
99      {
100         final List<IndexCreator> creators =
101             new ArrayList<IndexCreator>( getContainer().lookupList( IndexCreator.class ) );
102 
103         // now we add spoofs to it, this one depends on non existent creator. Note: we add it as 1st intentionally
104         creators.add( 0,
105             new SpoofIndexCreator( "non-satisfyable", Arrays.asList( "this-creator-i-depend-on-does-not-exists" ) ) );
106 
107         try
108         {
109             final List<IndexCreator> sortedCreators = IndexCreatorSorter.sort( creators );
110 
111             Assert.fail( "IndexCreator list is not satisfyable!" );
112         }
113         catch ( IllegalArgumentException e )
114         {
115             // good, check message
116             final String message = e.getMessage();
117 
118             Assert.assertTrue( "Exception message should mention the problematic creator's ID",
119                 message.contains( "non-satisfyable" ) );
120             Assert.assertTrue( "Exception message should mention the missing creator's ID",
121                 message.contains( "this-creator-i-depend-on-does-not-exists" ) );
122         }
123     }
124 }