1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.it.util.ResourceExtractor;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.regex.Pattern;
29
30 import junit.framework.TestCase;
31
32
33
34
35
36
37
38
39
40
41 public class MavenITmng2690MojoLoadingErrorsTest
42 extends AbstractMavenIntegrationTestCase
43 {
44
45 public MavenITmng2690MojoLoadingErrorsTest()
46 {
47 super( "(2.1.0-M1,)" );
48 }
49
50 public void testNoClassDefFromMojoLoad()
51 throws IOException, VerificationException
52 {
53 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/noclassdef-mojo" );
54
55 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
56 verifier.setAutoclean( false );
57
58 try
59 {
60 verifier.executeGoal( "validate" );
61
62 fail( "should throw an error during execution." );
63 }
64 catch ( VerificationException e )
65 {
66
67 }
68 finally
69 {
70 verifier.resetStreams();
71 }
72
73 List lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
74
75 int msg = indexOf( lines, "(?i).*required class is missing.*" );
76 assertTrue( "User-friendly message was not found in output.", msg >= 0 );
77
78 int cls = lines.get( msg ).toString().replace( '/', '.' ).indexOf( TestCase.class.getName() );
79 assertTrue( "Missing class name was not found in output.", cls >= 0 );
80 }
81
82 public void testNoClassDefFromMojoConfiguration()
83 throws IOException, VerificationException
84 {
85 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/noclassdef-param" );
86
87 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
88 verifier.setAutoclean( false );
89
90 try
91 {
92 verifier.executeGoal( "validate" );
93
94 fail( "should throw an error during execution." );
95 }
96 catch ( VerificationException e )
97 {
98
99 }
100 finally
101 {
102 verifier.resetStreams();
103 }
104
105 List lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
106
107 int msg = indexOf( lines, "(?i).*required class (i|wa)s missing( during (mojo )?configuration)?.*" );
108 assertTrue( "User-friendly message was not found in output.", msg >= 0 );
109
110 int cls = lines.get( msg ).toString().replace( '/', '.' ).indexOf( TestCase.class.getName() );
111 assertTrue( "Missing class name was not found in output.", cls >= 0 );
112 }
113
114 public void testMojoComponentLookupException()
115 throws IOException, VerificationException
116 {
117 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/mojo-complookup" );
118
119 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
120 verifier.setAutoclean( false );
121
122 try
123 {
124 verifier.executeGoal( "validate" );
125
126 fail( "should throw an error during execution." );
127 }
128 catch ( VerificationException e )
129 {
130
131 }
132 finally
133 {
134 verifier.resetStreams();
135 }
136
137 List lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
138
139 String compLookupMsg =
140 "(?i).*unable to .* mojo 'mojo-component-lookup-exception' .* plugin "
141 + "'org\\.apache\\.maven\\.its\\.plugins:maven-it-plugin-error.*";
142
143 assertTrue( "User-friendly message was not found in output.", indexOf( lines, compLookupMsg ) > 0 );
144 }
145
146 public void testMojoRequirementComponentLookupException()
147 throws IOException, VerificationException
148 {
149 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/requirement-complookup" );
150
151 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
152 verifier.setAutoclean( false );
153
154 try
155 {
156 verifier.executeGoal( "validate" );
157
158 fail( "should throw an error during execution." );
159 }
160 catch ( VerificationException e )
161 {
162
163 }
164 finally
165 {
166 verifier.resetStreams();
167 }
168
169 List lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
170
171 String compLookupMsg =
172 "(?i).*unable to .* mojo 'requirement-component-lookup-exception' .* plugin "
173 + "'org\\.apache\\.maven\\.its\\.plugins:maven-it-plugin-error.*";
174
175 assertTrue( "User-friendly message was not found in output.", indexOf( lines, compLookupMsg ) > 0 );
176 }
177
178 private int indexOf( List logLines, String regex )
179 {
180 Pattern pattern = Pattern.compile( regex );
181
182 for ( int i = 0; i < logLines.size(); i++ )
183 {
184 String logLine = (String) logLines.get( i );
185
186 if ( pattern.matcher( logLine ).matches() )
187 {
188 return i;
189 }
190 }
191
192 return -1;
193 }
194
195 }