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.reader;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.net.URI;
25  import java.util.Date;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.stream.StreamSupport;
29  
30  import org.apache.maven.index.reader.Record.Type;
31  import org.junit.Ignore;
32  import org.junit.Test;
33  
34  import static org.apache.maven.index.reader.TestUtils.expandFunction;
35  import static org.hamcrest.MatcherAssert.assertThat;
36  import static org.hamcrest.core.IsEqual.equalTo;
37  import static org.hamcrest.core.IsNot.not;
38  import static org.hamcrest.core.IsNull.nullValue;
39  
40  /**
41   * UT for {@link IndexReader}
42   */
43  public class IndexReaderTest extends TestSupport {
44      @Test
45      public void simple() throws IOException {
46          try (IndexReader indexReader = new IndexReader(null, testResourceHandler("simple"))) {
47              assertThat(indexReader.getIndexId(), equalTo("apache-snapshots-local"));
48              assertThat(indexReader.getPublishedTimestamp().getTime(), equalTo(1243533418015L));
49              assertThat(indexReader.isIncremental(), equalTo(false));
50              assertThat(indexReader.getChunkNames(), equalTo(List.of("nexus-maven-repository-index.gz")));
51              int chunks = 0;
52              int records = 0;
53              for (ChunkReader chunkReader : indexReader) {
54                  try (chunkReader) {
55                      chunks++;
56                      assertThat(chunkReader.getName(), equalTo("nexus-maven-repository-index.gz"));
57                      assertThat(chunkReader.getVersion(), equalTo(1));
58                      assertThat(chunkReader.getTimestamp().getTime(), equalTo(1243533418015L));
59                      records = (int) StreamSupport.stream(chunkReader.spliterator(), false)
60                              .map(expandFunction)
61                              .count();
62                  }
63              }
64  
65              assertThat(chunks, equalTo(1));
66              assertThat(records, equalTo(5));
67          }
68      }
69  
70      @Test
71      public void simpleWithLocal() throws IOException {
72          try (WritableResourceHandler writableResourceHandler = createWritableResourceHandler()) {
73              try (IndexReader indexReader = new IndexReader(writableResourceHandler, testResourceHandler("simple"))) {
74                  assertThat(indexReader.getIndexId(), equalTo("apache-snapshots-local"));
75                  assertThat(indexReader.getPublishedTimestamp().getTime(), equalTo(1243533418015L));
76                  assertThat(indexReader.isIncremental(), equalTo(false));
77                  assertThat(indexReader.getChunkNames(), equalTo(List.of("nexus-maven-repository-index.gz")));
78                  int chunks = 0;
79                  int records = 0;
80                  for (ChunkReader chunkReader : indexReader) {
81                      try (chunkReader) {
82                          chunks++;
83                          assertThat(chunkReader.getName(), equalTo("nexus-maven-repository-index.gz"));
84                          assertThat(chunkReader.getVersion(), equalTo(1));
85                          assertThat(chunkReader.getTimestamp().getTime(), equalTo(1243533418015L));
86                          records = (int) StreamSupport.stream(chunkReader.spliterator(), false)
87                                  .map(expandFunction)
88                                  .count();
89                      }
90                  }
91  
92                  assertThat(chunks, equalTo(1));
93                  assertThat(records, equalTo(5));
94              }
95  
96              assertThat(
97                      writableResourceHandler
98                              .locate("nexus-maven-repository-index.properties")
99                              .read(),
100                     not(nullValue()));
101         }
102     }
103 
104     @Test
105     public void roundtrip() throws IOException {
106         try (WritableResourceHandler writableResourceHandler = createWritableResourceHandler()) {
107             Date published;
108             IndexWriter iw; // TODO: fix this, IW close will set timestamp but ref to it is lost from try-with-res
109             {
110                 try (IndexReader indexReader = new IndexReader(null, testResourceHandler("simple"));
111                         IndexWriter indexWriter =
112                                 iw = new IndexWriter(writableResourceHandler, indexReader.getIndexId(), false)) {
113                     for (ChunkReader chunkReader : indexReader) {
114                         try (chunkReader) {
115                             indexWriter.writeChunk(chunkReader.iterator());
116                         }
117                     }
118                 }
119                 published = iw.getPublishedTimestamp();
120             }
121 
122             try (IndexReader indexReader = new IndexReader(null, writableResourceHandler)) {
123                 assertThat(indexReader.getIndexId(), equalTo("apache-snapshots-local"));
124                 assertThat(indexReader.getPublishedTimestamp().getTime(), equalTo(published.getTime()));
125                 assertThat(indexReader.isIncremental(), equalTo(false));
126                 assertThat(indexReader.getChunkNames(), equalTo(List.of("nexus-maven-repository-index.gz")));
127                 int chunks = 0;
128                 int records = 0;
129                 for (ChunkReader chunkReader : indexReader) {
130                     try (chunkReader) {
131                         chunks++;
132                         assertThat(chunkReader.getName(), equalTo("nexus-maven-repository-index.gz"));
133                         assertThat(chunkReader.getVersion(), equalTo(1));
134                         // assertThat(chunkReader.getTimestamp().getTime(), equalTo(1243533418015L));
135                         records = (int) StreamSupport.stream(chunkReader.spliterator(), false)
136                                 .map(expandFunction)
137                                 .count();
138                     }
139                 }
140 
141                 assertThat(chunks, equalTo(1));
142                 assertThat(records, equalTo(5));
143             }
144         }
145     }
146 
147     /**
148      * This UT is here for demonstration purposes only. Bashing Central is not something you want to do, and risk your
149      * IP address being banned. You were warned!
150      */
151     @Test
152     @Ignore("For eyes only")
153     public void central() throws Exception {
154         // local index location, against which we perform incremental updates
155         final File indexDir = createTempDirectory();
156         // cache of remote, to not rely on HTTP transport possible failures, or, to detect them early
157         final File cacheDir = createTempDirectory();
158 
159         final PrintWriter writer = new PrintWriter(System.out, true);
160 
161         try (WritableResourceHandler local = new DirectoryResourceHandler(indexDir);
162                 CachingResourceHandler remote = new CachingResourceHandler(
163                         new DirectoryResourceHandler(cacheDir),
164                         new HttpResourceHandler(URI.create("https://repo1.maven.org/maven2/.index/")));
165                 IndexReader indexReader = new IndexReader(local, remote)) {
166             writer.println("indexRepoId=" + indexReader.getIndexId());
167             writer.println("indexLastPublished=" + indexReader.getPublishedTimestamp());
168             writer.println("isIncremental=" + indexReader.isIncremental());
169             writer.println("indexRequiredChunkNames=" + indexReader.getChunkNames());
170             for (ChunkReader chunkReader : indexReader) {
171                 writer.println("chunkName=" + chunkReader.getName());
172                 writer.println("chunkVersion=" + chunkReader.getVersion());
173                 writer.println("chunkPublished=" + chunkReader.getTimestamp());
174                 writer.println("Chunk stats:");
175                 Map<Type, Integer> stats = countRecordsByType(chunkReader);
176                 for (Map.Entry<Type, Integer> entry : stats.entrySet()) {
177                     writer.println(entry.getKey() + " = " + entry.getValue());
178                 }
179                 writer.println("= = = = = =");
180             }
181         }
182     }
183 }