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.packer;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.apache.lucene.document.Document;
30  import org.apache.lucene.search.IndexSearcher;
31  import org.apache.maven.index.AbstractNexusIndexerTest;
32  import org.apache.maven.index.ArtifactInfo;
33  import org.apache.maven.index.NexusIndexer;
34  import org.apache.maven.index.context.IndexingContext;
35  import org.apache.maven.index.context.MergedIndexingContext;
36  import org.apache.maven.index.packer.IndexPackingRequest.IndexFormat;
37  import org.apache.maven.index.updater.IndexDataReader;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.junit.Test;
40  
41  import static org.junit.Assert.assertEquals;
42  import static org.junit.Assert.assertNotNull;
43  
44  public class NEXUS4149TransferFormatTest extends AbstractNexusIndexerTest {
45      protected File reposBase = new File(getBasedir(), "src/test/nexus-4149");
46  
47      protected File idxsBase = new File(getBasedir(), "target/index/nexus-4149");
48  
49      @Override
50      public void setUp() throws Exception {
51          super.setUp();
52      }
53  
54      @Override
55      protected void prepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
56          IndexingContext ctx1 = nexusIndexer.addIndexingContext(
57                  "repo1", "repo1", new File(reposBase, "repo1"), new File(idxsBase, "repo1"), null, null, MIN_CREATORS);
58          nexusIndexer.scan(ctx1);
59  
60          IndexingContext ctx2 = nexusIndexer.addIndexingContext(
61                  "repo2", "repo2", new File(reposBase, "repo2"), new File(idxsBase, "repo2"), null, null, MIN_CREATORS);
62          nexusIndexer.scan(ctx2);
63  
64          IndexingContext ctx3 = nexusIndexer.addIndexingContext(
65                  "repo3", "repo3", new File(reposBase, "repo3"), new File(idxsBase, "repo3"), null, null, MIN_CREATORS);
66          nexusIndexer.scan(ctx3);
67  
68          IndexingContext ctx4 = nexusIndexer.addIndexingContext(
69                  "repo4", "repo4", new File(reposBase, "repo4"), new File(idxsBase, "repo4"), null, null, MIN_CREATORS);
70          nexusIndexer.scan(ctx4);
71  
72          context = nexusIndexer.addMergedIndexingContext(
73                  "ctx",
74                  "ctx",
75                  new File(reposBase, "merged"),
76                  new File(idxsBase, "merged"),
77                  false,
78                  Arrays.asList(ctx1, ctx2, ctx3, ctx4));
79  
80          context.getIndexDirectoryFile().mkdirs();
81      }
82  
83      @Override
84      protected void unprepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
85          // remove the merged
86          nexusIndexer.removeIndexingContext(context, true);
87  
88          // remove members
89          MergedIndexingContext mctx = (MergedIndexingContext) context;
90  
91          for (IndexingContext member : mctx.getMembers()) {
92              nexusIndexer.removeIndexingContext(member, true);
93          }
94      }
95  
96      @Override
97      public void testDirectory() throws IOException {
98          // we use no directory
99      }
100 
101     @Test
102     public void testMembersAndMergedRootGroups() throws Exception {
103         MergedIndexingContext mctx = (MergedIndexingContext) context;
104 
105         for (IndexingContext member : mctx.getMembers()) {
106             if (!"repo4".equals(member.getId())) // repo4 is empty
107             {
108                 assertEquals(
109                         "Members should have one root group!",
110                         1,
111                         member.getRootGroups().size());
112             }
113         }
114 
115         assertEquals(
116                 "Merged should have one root multiply members count (sans repo4)!",
117                 3,
118                 mctx.getRootGroups().size());
119     }
120 
121     @Test
122     public void testTransportFile() throws Exception {
123         File packTargetDir = new File(getBasedir(), "target/nexus-4149/packed");
124 
125         IndexPacker packer = lookup(IndexPacker.class);
126 
127         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
128         try {
129             IndexPackingRequest request =
130                     new IndexPackingRequest(context, indexSearcher.getIndexReader(), packTargetDir);
131             request.setCreateIncrementalChunks(false);
132             request.setFormats(Arrays.asList(IndexFormat.FORMAT_V1));
133 
134             packer.packIndex(request);
135         } finally {
136             context.releaseIndexSearcher(indexSearcher);
137         }
138 
139         // read it up and verify, but stay "low level", directly consume the GZ file and count
140         InputStream fis = new BufferedInputStream(
141                 new FileInputStream(new File(packTargetDir, "nexus-maven-repository-index.gz")));
142         IndexDataReader reader = new IndexDataReader(fis);
143         try {
144             // read header and neglect it
145             reader.readHeader();
146 
147             // read docs
148             int totalDocs = 0;
149             int specialDocs = 0;
150             int artifactDocs = 0;
151             String allGroups = null;
152             String rootGroups = null;
153             Document doc;
154             while ((doc = reader.readDocument()) != null) {
155                 totalDocs++;
156                 if (doc.getField("DESCRIPTOR") != null
157                         || doc.getField(ArtifactInfo.ALL_GROUPS) != null
158                         || doc.getField(ArtifactInfo.ROOT_GROUPS) != null) {
159                     specialDocs++;
160 
161                     if (doc.get(ArtifactInfo.ALL_GROUPS) != null) {
162                         allGroups = doc.get(ArtifactInfo.ALL_GROUPS_LIST);
163                     }
164                     if (doc.get(ArtifactInfo.ROOT_GROUPS) != null) {
165                         rootGroups = doc.get(ArtifactInfo.ROOT_GROUPS_LIST);
166                     }
167                 } else {
168                     artifactDocs++;
169                 }
170             }
171 
172             assertNotNull("Group transport file should contain allGroups!", allGroups);
173             assertNotNull("Group transport file should contain rootGroups!", rootGroups);
174             checkListOfStringDoesNotContainEmptyString(ArtifactInfo.str2lst(allGroups));
175             checkListOfStringDoesNotContainEmptyString(ArtifactInfo.str2lst(rootGroups));
176 
177             assertEquals(15, totalDocs);
178             // 1 descriptor + 1 allGroups + 1 rootGroups
179             assertEquals(3, specialDocs);
180             // repo1 has 1 artifact, repo2 has 1 artifact and repo3 has 10 artifact
181             assertEquals(12, artifactDocs);
182 
183         } finally {
184             fis.close();
185         }
186     }
187 
188     protected void checkListOfStringDoesNotContainEmptyString(List<String> lst) {
189         if (lst != null) {
190             for (String str : lst) {
191                 if (StringUtils.isBlank(str)) {
192                     throw new IllegalArgumentException("List " + lst + " contains empty string!");
193                 }
194             }
195         }
196     }
197 }