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.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.util.Date;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.index.reader.Record.Type;
30  import org.junit.Test;
31  
32  import static org.hamcrest.CoreMatchers.nullValue;
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.hamcrest.core.IsEqual.equalTo;
35  
36  /**
37   * UT for {@link ChunkReader}
38   */
39  public class ChunkReaderTest extends TestSupport {
40      @Test
41      public void simple() throws IOException {
42          try (WritableResourceHandler handler = testResourceHandler("simple");
43                  ChunkReader chunkReader = new ChunkReader(
44                          "full",
45                          handler.locate("nexus-maven-repository-index.gz").read())) {
46              final Map<Type, List<Record>> recordTypes = loadRecordsByType(chunkReader);
47              assertThat(recordTypes.get(Type.DESCRIPTOR).size(), equalTo(1));
48              assertThat(recordTypes.get(Type.ROOT_GROUPS).size(), equalTo(1));
49              assertThat(recordTypes.get(Type.ALL_GROUPS).size(), equalTo(1));
50              assertThat(recordTypes.get(Type.ARTIFACT_ADD).size(), equalTo(2));
51              assertThat(recordTypes.get(Type.ARTIFACT_REMOVE), nullValue());
52          }
53      }
54  
55      @Test
56      public void roundtrip() throws IOException {
57          final Date published;
58          File tempChunkFile = createTempFile("nexus-maven-repository-index.gz");
59          {
60              try (WritableResourceHandler resource = testResourceHandler("simple");
61                      ChunkReader chunkReader = new ChunkReader(
62                              "full",
63                              resource.locate("nexus-maven-repository-index.gz").read());
64                      ChunkWriter chunkWriter = new ChunkWriter(
65                              chunkReader.getName(), new FileOutputStream(tempChunkFile), 1, new Date())) {
66                  chunkWriter.writeChunk(chunkReader.iterator());
67                  published = chunkWriter.getTimestamp();
68              }
69          }
70  
71          try (ChunkReader chunkReader = new ChunkReader("full", new FileInputStream(tempChunkFile))) {
72              assertThat(chunkReader.getVersion(), equalTo(1));
73              assertThat(chunkReader.getTimestamp().getTime(), equalTo(published.getTime()));
74              final Map<Type, List<Record>> recordTypes = loadRecordsByType(chunkReader);
75              assertThat(recordTypes.get(Type.DESCRIPTOR).size(), equalTo(1));
76              assertThat(recordTypes.get(Type.ROOT_GROUPS).size(), equalTo(1));
77              assertThat(recordTypes.get(Type.ALL_GROUPS).size(), equalTo(1));
78              assertThat(recordTypes.get(Type.ARTIFACT_ADD).size(), equalTo(2));
79              assertThat(recordTypes.get(Type.ARTIFACT_REMOVE), nullValue());
80          }
81      }
82  }