1 package org.apache.maven.index.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.util.List;
26 import java.util.Random;
27 import org.apache.lucene.search.Query;
28 import org.apache.maven.index.FlatSearchRequest;
29 import org.apache.maven.index.FlatSearchResponse;
30 import org.apache.maven.index.MAVEN;
31 import org.apache.maven.index.NexusIndexer;
32 import org.apache.maven.index.SearchType;
33 import org.apache.maven.index.context.IndexCreator;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.codehaus.plexus.ContainerConfiguration;
36 import org.codehaus.plexus.PlexusConstants;
37 import org.codehaus.plexus.PlexusTestCase;
38 import org.codehaus.plexus.util.FileUtils;
39 import org.junit.Ignore;
40
41 public abstract class AbstractNexusIndexerCliTest
42 extends PlexusTestCase
43 {
44
45 private static final long rand = new Random().nextLong();
46
47
48
49
50
51
52
53
54 private static final String DEST_DIR =
55 new File( getBasedir(), "target/tests/clitest-" + rand + "/output" ).getAbsolutePath();
56
57 private static final String INDEX_DIR =
58 new File( getBasedir(), "target/tests/clitest-" + rand + "/index" ).getAbsolutePath();
59
60 private static final String UNPACK_DIR =
61 new File( getBasedir(), "target/tests/clitest-" + rand + "/unpack" ).getAbsolutePath();
62
63 private static final String TEST_REPO = new File( getBasedir(), "src/test/repo" ).getAbsolutePath();
64
65 protected OutputStream out;
66
67 @Override
68 protected void customizeContainerConfiguration( final ContainerConfiguration containerConfiguration )
69 {
70 super.customizeContainerConfiguration( containerConfiguration );
71 containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
72 }
73
74 @Override
75 protected void setUp()
76 throws Exception
77 {
78 super.setUp();
79
80 out = new OutputStream()
81 {
82
83 private StringBuffer buf = new StringBuffer();
84
85 @Override
86 public void write( int b )
87 throws IOException
88 {
89 byte[] bytes = new byte[1];
90 bytes[0] = (byte) b;
91 buf.append( new String( bytes ) );
92 }
93
94 @Override
95 public String toString()
96 {
97 String string = buf.toString();
98 buf = new StringBuffer();
99 return string;
100 }
101 };
102
103 FileUtils.deleteDirectory( INDEX_DIR );
104 FileUtils.deleteDirectory( DEST_DIR );
105 FileUtils.deleteDirectory( UNPACK_DIR );
106
107 }
108
109 @Override
110 protected void tearDown()
111 throws Exception
112 {
113 super.tearDown();
114
115 }
116
117 public void testNoArgs()
118 {
119 int code = execute();
120 String output = out.toString();
121 assertEquals( output, 1, code );
122 assertTrue( "Should print usage", output.contains( "usage: nexus-indexer [options]" ) );
123 }
124
125 public void testRequiredArgs()
126 throws Exception
127 {
128 int code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
129 String output = out.toString();
130 assertEquals( output, 0, code );
131 assertIndexFiles();
132 }
133
134 public void testUnpack()
135 throws Exception
136 {
137
138 execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
139
140 int code = execute( "--unpack", "--index", DEST_DIR, "-d", UNPACK_DIR );
141 String output = out.toString();
142 assertEquals( output, 0, code );
143
144
145
146 }
147
148 public void testMissingArgs()
149 throws IOException
150 {
151 String usage = "usage: nexus-indexer";
152
153 int code = execute( "--repository", "--index", INDEX_DIR, "-d", DEST_DIR );
154 String output = out.toString();
155 assertEquals( output, 1, code );
156 assertTrue( "Should print bad usage", output.contains( usage ) );
157
158 code = execute( "--repository", TEST_REPO, "--index", "-d", DEST_DIR );
159 output = out.toString();
160 assertEquals( output, 1, code );
161 assertTrue( "Should print bad usage", output.contains( usage ) );
162
163 code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d" );
164 output = out.toString();
165 assertEquals( output, 1, code );
166 assertTrue( "Should print bad usage", output.contains( usage ) );
167
168 code = execute( "--repository", "--index", "-d" );
169 output = out.toString();
170 assertEquals( output, 1, code );
171 assertTrue( "Should print bad usage but '" + output + "'", output.contains( usage ) );
172
173 assertFalse( "Index file was generated", new File( INDEX_DIR ).exists() );
174 }
175
176 public void testAbrvsRequiredArgs()
177 throws Exception
178 {
179 int code = execute( "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
180 String output = out.toString();
181 assertEquals( output, 0, code );
182 assertIndexFiles();
183 }
184
185 public void testLoggingLevel()
186 throws Exception
187 {
188 int code = execute( "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
189 String normal = out.toString();
190 assertEquals( normal, 0, code );
191 assertIndexFiles();
192
193 setUp();
194
195 code = execute( "-q", "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
196 String quiet = out.toString();
197 assertEquals( quiet, 0, code );
198 assertFalse( "Expected an different output on quiet mode:\n" + normal, normal.equals( quiet ) );
199 assertIndexFiles();
200
201 setUp();
202
203 code = execute( "-X", "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
204 String debug = out.toString();
205 assertEquals( debug, 0, code );
206 assertFalse( "Expected an different output on debug mode:\n" + normal, normal.equals( debug ) );
207 assertIndexFiles();
208
209 setUp();
210
211 code = execute( "-e", "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
212 String error = out.toString();
213 assertEquals( error, 0, code );
214 assertFalse( "Expected an different output on error mode:\n" + normal, normal.equals( error ) );
215 assertIndexFiles();
216 }
217
218 public void testInvalidRepo()
219 throws Exception
220 {
221 int code =
222 execute( "-r", new File( "target/undexinting/repo/to/try/what/will/happen/here" ).getCanonicalPath(), "-i",
223 INDEX_DIR, "-d", DEST_DIR );
224 String output = out.toString();
225 assertEquals( output, 1, code );
226 }
227
228 private void assertIndexFiles()
229 throws Exception
230 {
231
232
233 }
234
235 @Ignore("Old lucene format not supported")
236 private void ignoreAssertIndexFiles( final String indexDir )
237 throws Exception
238 {
239 IndexingContext context = null;
240 NexusIndexer indexer = lookup( NexusIndexer.class );
241 try
242 {
243 List<IndexCreator> indexCreators = getContainer().lookupList( IndexCreator.class );
244
245 context =
246 indexer.addIndexingContext( "index", "index", new File( TEST_REPO ), new File( indexDir ), null, null,
247 indexCreators );
248
249 assertFalse( "No index file was generated", new File( indexDir ).list().length == 0 );
250
251 Query query = indexer.constructQuery( MAVEN.GROUP_ID, "ch.marcus-schulte.maven", SearchType.SCORED );
252
253 FlatSearchRequest request = new FlatSearchRequest( query );
254 FlatSearchResponse response = indexer.searchFlat( request );
255 assertEquals( response.getResults().toString(), 1, response.getTotalHits() );
256 }
257 finally
258 {
259 if ( context != null )
260 {
261 indexer.removeIndexingContext( context, true );
262 }
263 }
264 }
265
266 protected abstract int execute( String... args );
267
268 }