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.IOException;
22  import java.util.List;
23  import java.util.stream.StreamSupport;
24  
25  import org.junit.Test;
26  
27  import static org.apache.maven.index.reader.TestUtils.expandFunction;
28  import static org.hamcrest.MatcherAssert.assertThat;
29  import static org.hamcrest.core.IsEqual.equalTo;
30  
31  /**
32   * UT for {@link IndexWriter}
33   */
34  public class IndexWriterTest extends TestSupport {
35      @Test
36      public void roundtrip() throws IOException {
37          try (WritableResourceHandler writableResourceHandler = createWritableResourceHandler()) {
38              try (IndexReader indexReader = new IndexReader(null, testResourceHandler("simple"));
39                      IndexWriter indexWriter =
40                              new IndexWriter(writableResourceHandler, indexReader.getIndexId(), false)) {
41                  for (ChunkReader chunkReader : indexReader) {
42                      try (chunkReader) {
43                          indexWriter.writeChunk(chunkReader.iterator());
44                      }
45                  }
46              }
47  
48              // read what we wrote out
49              try (IndexReader indexReader = new IndexReader(null, writableResourceHandler)) {
50                  assertThat(indexReader.getIndexId(), equalTo("apache-snapshots-local"));
51                  // assertThat(indexReader.getPublishedTimestamp().getTime(), equalTo(published.getTime()));
52                  assertThat(indexReader.isIncremental(), equalTo(false));
53                  assertThat(indexReader.getChunkNames(), equalTo(List.of("nexus-maven-repository-index.gz")));
54                  int chunks = 0;
55                  int records = 0;
56                  for (ChunkReader chunkReader : indexReader) {
57                      chunks++;
58                      assertThat(chunkReader.getName(), equalTo("nexus-maven-repository-index.gz"));
59                      assertThat(chunkReader.getVersion(), equalTo(1));
60                      // assertThat(chunkReader.getTimestamp().getTime(), equalTo(1243533418015L));
61                      records = (int) StreamSupport.stream(chunkReader.spliterator(), false)
62                              .map(expandFunction)
63                              .count();
64                  }
65  
66                  assertThat(chunks, equalTo(1));
67                  assertThat(records, equalTo(5));
68              }
69          }
70      }
71  }