1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index.updater;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.ServerSocket;
24 import java.nio.file.Files;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import com.google.inject.Inject;
29 import org.apache.maven.index.Java11HttpClient;
30 import org.apache.maven.index.context.DefaultIndexingContext;
31 import org.apache.maven.index.context.IndexCreator;
32 import org.apache.maven.index.context.IndexingContext;
33 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
34 import org.apache.maven.index.updater.fixtures.ServerTestFixture;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.eclipse.sisu.launch.InjectedTest;
37 import org.junit.Test;
38
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertTrue;
41 import static org.junit.Assert.fail;
42
43 public class DefaultIndexUpdaterEmbeddingIT extends InjectedTest {
44 private String baseUrl;
45
46 private ServerTestFixture server;
47
48 @Inject
49 private IndexUpdater updater;
50
51 @Override
52 public void setUp() throws Exception {
53
54 int port;
55 try (final ServerSocket ss = new ServerSocket(0)) {
56 ss.setReuseAddress(true);
57 port = ss.getLocalPort();
58 }
59
60 baseUrl = "http://127.0.0.1:" + port + "/";
61 server = new ServerTestFixture(port);
62
63 super.setUp();
64 }
65
66 @Override
67 public void tearDown() throws Exception {
68 server.stop();
69 }
70
71 @Test
72 public void testBasicIndexRetrieval() throws IOException, UnsupportedExistingLuceneIndexException {
73 File basedir = Files.createTempDirectory("nexus-indexer.").toFile();
74 basedir.delete();
75 basedir.mkdirs();
76
77 try {
78 IndexingContext ctx = newTestContext(basedir, baseUrl);
79
80 IndexUpdateRequest updateRequest = new IndexUpdateRequest(ctx, new Java11HttpClient());
81
82 updater.fetchAndUpdateIndex(updateRequest);
83
84 ctx.close(false);
85 } finally {
86 try {
87 FileUtils.forceDelete(basedir);
88 } catch (IOException e) {
89 }
90 }
91 }
92
93 @Test
94 public void testIndexTempDirB() throws IOException, UnsupportedExistingLuceneIndexException {
95 File basedir = Files.createTempDirectory("nexus-indexer.").toFile();
96 basedir.delete();
97 basedir.mkdirs();
98
99 File indexTempDir = Files.createTempDirectory("index-temp").toFile();
100 indexTempDir.delete();
101
102 assertFalse(indexTempDir.exists());
103
104 try {
105 IndexingContext ctx = newTestContext(basedir, baseUrl);
106
107 IndexUpdateRequest updateRequest = new IndexUpdateRequest(ctx, new Java11HttpClient());
108 updateRequest.setIndexTempDir(indexTempDir);
109
110 updater.fetchAndUpdateIndex(updateRequest);
111
112
113 assertTrue(indexTempDir.exists());
114 indexTempDir.delete();
115 ctx.close(false);
116 } finally {
117 try {
118 FileUtils.forceDelete(basedir);
119 } catch (IOException e) {
120 }
121 }
122 }
123
124 @Test
125 public void testBasicHighLatencyIndexRetrieval() throws IOException, UnsupportedExistingLuceneIndexException {
126 File basedir = Files.createTempDirectory("nexus-indexer.").toFile();
127
128 try {
129 IndexingContext ctx = newTestContext(basedir, baseUrl + "slow/");
130
131 IndexUpdateRequest updateRequest = new IndexUpdateRequest(ctx, new Java11HttpClient());
132
133 updater.fetchAndUpdateIndex(updateRequest);
134
135 ctx.close(false);
136 } finally {
137 try {
138 FileUtils.forceDelete(basedir);
139 } catch (IOException e) {
140 }
141 }
142 }
143
144 @Test
145 public void testIndexRetrieval_InfiniteRedirection() throws IOException, UnsupportedExistingLuceneIndexException {
146 File basedir = Files.createTempDirectory("nexus-indexer.").toFile();
147
148 try {
149 IndexingContext ctx = newTestContext(basedir, baseUrl + "redirect-trap/");
150
151 IndexUpdateRequest updateRequest = new IndexUpdateRequest(ctx, new Java11HttpClient());
152
153 try {
154 updater.fetchAndUpdateIndex(updateRequest);
155 fail("Should throw IOException from too many redirects.");
156 } catch (IOException e) {
157 System.out.println("Operation timed out due to too many redirects, as expected.");
158 }
159
160 ctx.close(false);
161 } finally {
162 try {
163 FileUtils.forceDelete(basedir);
164 } catch (IOException e) {
165 }
166 }
167 }
168
169 @Test
170 public void testIndexRetrieval_BadHostname() throws IOException, UnsupportedExistingLuceneIndexException {
171 File basedir = Files.createTempDirectory("nexus-indexer.").toFile();
172
173 try {
174 IndexingContext ctx = newTestContext(basedir, "http://dummy/");
175
176 IndexUpdateRequest updateRequest = new IndexUpdateRequest(ctx, new Java11HttpClient());
177
178 try {
179 updater.fetchAndUpdateIndex(updateRequest);
180 fail("Should timeout and throw IOException.");
181 } catch (Exception e) {
182 System.out.println("Connection failed as expected.");
183 }
184
185 ctx.close(false);
186 } finally {
187 try {
188 FileUtils.forceDelete(basedir);
189 } catch (IOException e) {
190 }
191 }
192 }
193
194 private IndexingContext newTestContext(final File basedir, final String baseUrl)
195 throws IOException, UnsupportedExistingLuceneIndexException {
196 IndexCreator min = lookup(IndexCreator.class, "min");
197 IndexCreator jar = lookup(IndexCreator.class, "jarContent");
198
199 List<IndexCreator> creators = new ArrayList<>();
200 creators.add(min);
201 creators.add(jar);
202
203 String repositoryId = "test";
204
205 return new DefaultIndexingContext(
206 repositoryId, repositoryId, basedir, basedir, baseUrl, baseUrl, creators, true);
207 }
208 }