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