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.Random;
26 import org.codehaus.plexus.util.FileUtils;
27 import org.eclipse.sisu.launch.InjectedTest;
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertTrue;
33
34 public abstract class AbstractNexusIndexerCliTest
35 extends InjectedTest
36 {
37
38 private static final long rand = new Random().nextLong();
39
40
41
42
43
44
45
46
47 private final String DEST_DIR =
48 new File( getBasedir(), "target/tests/clitest-" + rand + "/output" ).getAbsolutePath();
49
50 private final String INDEX_DIR =
51 new File( getBasedir(), "target/tests/clitest-" + rand + "/index" ).getAbsolutePath();
52
53 private final String UNPACK_DIR =
54 new File( getBasedir(), "target/tests/clitest-" + rand + "/unpack" ).getAbsolutePath();
55
56 private final String TEST_REPO = new File( getBasedir(), "src/test/repo" ).getAbsolutePath();
57
58 protected OutputStream out;
59
60 @Override
61 public void setUp()
62 throws Exception
63 {
64 super.setUp();
65
66 out = new OutputStream()
67 {
68
69 private StringBuilder buf = new StringBuilder();
70
71 @Override
72 public void write( int b )
73 {
74 byte[] bytes = new byte[1];
75 bytes[0] = (byte) b;
76 buf.append( new String( bytes ) );
77 }
78
79 @Override
80 public String toString()
81 {
82 String string = buf.toString();
83 buf = new StringBuilder();
84 return string;
85 }
86 };
87
88 FileUtils.deleteDirectory( INDEX_DIR );
89 FileUtils.deleteDirectory( DEST_DIR );
90 FileUtils.deleteDirectory( UNPACK_DIR );
91
92 }
93
94 @Override
95 public void tearDown()
96 throws Exception
97 {
98 super.tearDown();
99
100 }
101
102 protected File getTestFile( String path )
103 {
104 return new File( new File( getBasedir() ), path );
105 }
106
107 @Test
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 @Test
117 public void testRequiredArgs()
118 throws Exception
119 {
120 int code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
121 String output = out.toString();
122 assertEquals( output, 0, code );
123 }
124
125 @Test
126 public void testUnpack()
127 throws Exception
128 {
129
130 execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
131
132 int code = execute( "--unpack", "--index", DEST_DIR, "-d", UNPACK_DIR );
133 String output = out.toString();
134 assertEquals( output, 0, code );
135
136
137
138 }
139
140 @Test
141 public void testMissingArgs()
142 throws IOException
143 {
144 String usage = "usage: nexus-indexer";
145
146 int code = execute( "--repository", "--index", INDEX_DIR, "-d", DEST_DIR );
147 String output = out.toString();
148 assertEquals( output, 1, code );
149 assertTrue( "Should print bad usage", output.contains( usage ) );
150
151 code = execute( "--repository", TEST_REPO, "--index", "-d", DEST_DIR );
152 output = out.toString();
153 assertEquals( output, 1, code );
154 assertTrue( "Should print bad usage", output.contains( usage ) );
155
156 code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d" );
157 output = out.toString();
158 assertEquals( output, 1, code );
159 assertTrue( "Should print bad usage", output.contains( usage ) );
160
161 code = execute( "--repository", "--index", "-d" );
162 output = out.toString();
163 assertEquals( output, 1, code );
164 assertTrue( "Should print bad usage but '" + output + "'", output.contains( usage ) );
165
166 assertFalse( "Index file was generated", new File( INDEX_DIR ).exists() );
167 }
168
169 @Test
170 public void testAbrvsRequiredArgs()
171 throws Exception
172 {
173 int code = execute( "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
174 String output = out.toString();
175 assertEquals( output, 0, code );
176 }
177
178 @Test
179 public void testInvalidRepo()
180 throws Exception
181 {
182 int code =
183 execute( "-r", new File( "target/undexinting/repo/to/try/what/will/happen/here" ).getCanonicalPath(), "-i",
184 INDEX_DIR, "-d", DEST_DIR );
185 String output = out.toString();
186 assertEquals( output, 1, code );
187 }
188
189 protected abstract int execute( String... args );
190
191 }