1 package org.apache.maven.index.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41 final List<String> sortedCreatorIds = new ArrayList<String>();
42 for ( IndexCreator creator : sortedCreators )
43 {
44 sortedCreatorIds.add( creator.getId() );
45 }
46
47
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
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
66 creators.add( 0,
67 new SpoofIndexCreator( "depend-on-all", new ArrayList<String>(
68 getContainer().lookupMap( IndexCreator.class ).keySet() ) ) );
69
70
71
72 creators.add( 0, new SpoofIndexCreator( "last", Arrays.asList( "depend-on-all" ) ) );
73
74 final List<IndexCreator> sortedCreators = IndexCreatorSorter.sort( creators );
75
76
77 final List<String> sortedCreatorIds = new ArrayList<String>();
78 for ( IndexCreator creator : sortedCreators )
79 {
80 sortedCreatorIds.add( creator.getId() );
81 }
82
83
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
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
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
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 }