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