1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.cli;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Random;
25
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 extends InjectedTest {
35
36 private static final long rand = new Random().nextLong();
37
38
39
40
41
42
43
44
45 private final String DEST_DIR =
46 new File(getBasedir(), "target/tests/clitest-" + rand + "/output").getAbsolutePath();
47
48 private final String INDEX_DIR =
49 new File(getBasedir(), "target/tests/clitest-" + rand + "/index").getAbsolutePath();
50
51 private final String UNPACK_DIR =
52 new File(getBasedir(), "target/tests/clitest-" + rand + "/unpack").getAbsolutePath();
53
54 private final String TEST_REPO = new File(getBasedir(), "src/test/repo").getAbsolutePath();
55
56 protected OutputStream out;
57
58 @Override
59 public void setUp() throws Exception {
60 super.setUp();
61
62 out = new OutputStream() {
63
64 private StringBuilder buf = new StringBuilder();
65
66 @Override
67 public void write(int b) {
68 byte[] bytes = new byte[1];
69 bytes[0] = (byte) b;
70 buf.append(new String(bytes));
71 }
72
73 @Override
74 public String toString() {
75 String string = buf.toString();
76 buf = new StringBuilder();
77 return string;
78 }
79 };
80
81 FileUtils.deleteDirectory(INDEX_DIR);
82 FileUtils.deleteDirectory(DEST_DIR);
83 FileUtils.deleteDirectory(UNPACK_DIR);
84 }
85
86 @Override
87 public void tearDown() throws Exception {
88 super.tearDown();
89 }
90
91 protected File getTestFile(String path) {
92 return new File(new File(getBasedir()), path);
93 }
94
95 @Test
96 public void testNoArgs() {
97 int code = execute();
98 String output = out.toString();
99 assertEquals(output, 1, code);
100 assertTrue("Should print usage", output.contains("usage: nexus-indexer [options]"));
101 }
102
103 @Test
104 public void testRequiredArgs() throws Exception {
105 int code = execute("--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR);
106 String output = out.toString();
107 assertEquals(output, 0, code);
108 }
109
110 @Test
111 public void testUnpack() throws Exception {
112
113 execute("--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR);
114
115 int code = execute("--unpack", "--index", DEST_DIR, "-d", UNPACK_DIR);
116 String output = out.toString();
117 assertEquals(output, 0, code);
118
119
120
121 }
122
123 @Test
124 public void testMissingArgs() throws IOException {
125 String usage = "usage: nexus-indexer";
126
127 int code = execute("--repository", "--index", INDEX_DIR, "-d", DEST_DIR);
128 String output = out.toString();
129 assertEquals(output, 1, code);
130 assertTrue("Should print bad usage", output.contains(usage));
131
132 code = execute("--repository", TEST_REPO, "--index", "-d", DEST_DIR);
133 output = out.toString();
134 assertEquals(output, 1, code);
135 assertTrue("Should print bad usage", output.contains(usage));
136
137 code = execute("--repository", TEST_REPO, "--index", INDEX_DIR, "-d");
138 output = out.toString();
139 assertEquals(output, 1, code);
140 assertTrue("Should print bad usage", output.contains(usage));
141
142 code = execute("--repository", "--index", "-d");
143 output = out.toString();
144 assertEquals(output, 1, code);
145 assertTrue("Should print bad usage but '" + output + "'", output.contains(usage));
146
147 assertFalse("Index file was generated", new File(INDEX_DIR).exists());
148 }
149
150 @Test
151 public void testAbrvsRequiredArgs() throws Exception {
152 int code = execute("-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR);
153 String output = out.toString();
154 assertEquals(output, 0, code);
155 }
156
157 @Test
158 public void testInvalidRepo() throws Exception {
159 int code = execute(
160 "-r",
161 new File("target/undexinting/repo/to/try/what/will/happen/here").getCanonicalPath(),
162 "-i",
163 INDEX_DIR,
164 "-d",
165 DEST_DIR);
166 String output = out.toString();
167 assertEquals(output, 1, code);
168 }
169
170 protected abstract int execute(String... args);
171 }