View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.updater;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.net.ServerSocket;
25  import java.nio.file.Files;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Properties;
29  import java.util.TimeZone;
30  
31  import org.apache.maven.index.Java11HttpClient;
32  import org.apache.maven.index.context.IndexingContext;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.eclipse.jetty.server.Handler;
35  import org.eclipse.jetty.server.Server;
36  import org.eclipse.jetty.server.handler.DefaultHandler;
37  import org.eclipse.jetty.server.handler.HandlerList;
38  import org.eclipse.jetty.server.handler.ResourceHandler;
39  import org.junit.Test;
40  
41  import static org.junit.Assert.assertTrue;
42  
43  public class DownloadRemoteIndexerManagerTest extends AbstractIndexUpdaterTest {
44      private Server server;
45  
46      private File fakeCentral;
47  
48      private IndexingContext centralContext;
49  
50      @Override
51      public void setUp() throws Exception {
52          super.setUp();
53  
54          fakeCentral = new File(getBasedir(), "target/repos/fake-central");
55          fakeCentral.mkdirs();
56  
57          // create proxy server
58          ServerSocket s = new ServerSocket(0);
59          int port = s.getLocalPort();
60          s.close();
61  
62          server = new Server(port);
63  
64          ResourceHandler resource_handler = new ResourceHandler();
65          resource_handler.setResourceBase(fakeCentral.getAbsolutePath());
66          HandlerList handlers = new HandlerList();
67          handlers.setHandlers(new Handler[] {resource_handler, new DefaultHandler()});
68          server.setHandler(handlers);
69  
70          server.start();
71  
72          // make context "fake central"
73          centralContext = indexer.addIndexingContext(
74                  "central",
75                  "central",
76                  fakeCentral,
77                  getDirectory("central"),
78                  "http://localhost:" + port,
79                  null,
80                  MIN_CREATORS);
81      }
82  
83      @Override
84      public void tearDown() throws Exception {
85          server.stop();
86  
87          FileUtils.forceDelete(fakeCentral);
88  
89          super.tearDown();
90      }
91  
92      @Test
93      public void testRepoReindex() throws Exception {
94          IndexUpdateRequest iur;
95  
96          File index1 = new File(getBasedir(), "src/test/resources/repo-index/index");
97          File index2 = new File(getBasedir(), "src/test/resources/repo-index/index2");
98          File centralIndex = new File(fakeCentral, ".index");
99  
100         // copy index 02
101         overwriteIndex(index2, centralIndex);
102 
103         iur = new IndexUpdateRequest(centralContext, new Java11HttpClient());
104         iur.setForceFullUpdate(true);
105 
106         updater.fetchAndUpdateIndex(iur);
107 
108         searchFor("org.sonatype.nexus", 8, centralContext);
109 
110         // copy index 01
111         overwriteIndex(index1, centralIndex);
112 
113         iur = new IndexUpdateRequest(centralContext, new Java11HttpClient());
114         iur.setForceFullUpdate(true);
115         // just a dummy filter to invoke filtering! -- this is what I broke unnoticing it
116         iur.setDocumentFilter(doc -> true);
117 
118         updater.fetchAndUpdateIndex(iur);
119 
120         searchFor("org.sonatype.nexus", 1, centralContext);
121 
122         // copy index 02
123         overwriteIndex(index2, centralIndex);
124 
125         iur = new IndexUpdateRequest(centralContext, new Java11HttpClient());
126         iur.setForceFullUpdate(true);
127 
128         updater.fetchAndUpdateIndex(iur);
129 
130         searchFor("org.sonatype.nexus", 8, centralContext);
131     }
132 
133     private void overwriteIndex(File source, File destination) throws Exception {
134         File indexFile = new File(destination, "nexus-maven-repository-index.gz");
135         File indexProperties = new File(destination, "nexus-maven-repository-index.properties");
136 
137         long lastMod = -1;
138         if (destination.exists()) {
139             FileUtils.forceDelete(destination);
140             lastMod = indexFile.lastModified();
141         }
142         FileUtils.copyDirectory(source, destination);
143         long lastMod2 = indexFile.lastModified();
144         assertTrue(lastMod < lastMod2);
145 
146         Properties p = new Properties();
147         try (InputStream input = Files.newInputStream(indexProperties.toPath())) {
148             p.load(input);
149         }
150 
151         p.setProperty("nexus.index.time", format(new Date()));
152         p.setProperty("nexus.index.timestamp", format(new Date()));
153 
154         try (OutputStream output = Files.newOutputStream(indexProperties.toPath())) {
155             p.store(output, null);
156         }
157     }
158 
159     private String format(Date d) {
160         SimpleDateFormat df = new SimpleDateFormat(IndexingContext.INDEX_TIME_FORMAT);
161         df.setTimeZone(TimeZone.getTimeZone("GMT"));
162         return df.format(d);
163     }
164 }