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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  import org.apache.lucene.search.IndexSearcher;
26  import org.apache.maven.index.context.IndexingContext;
27  import org.apache.maven.index.packer.IndexPacker;
28  import org.apache.maven.index.packer.IndexPackingRequest;
29  
30  /**
31   * The point in this test is: we use Merged context, and we modify some of the "members" in the merged context, while we
32   * try to publish those simultaneously.
33   *
34   * @author cstamas
35   */
36  public class ConcurrentUseWithMergedContextPublishingTest extends ConcurrentUseWithMergedContextTest {
37      protected IndexPacker packer;
38  
39      protected File repoPublish = new File(getBasedir(), "target/repo-publish");
40  
41      protected final AtomicInteger counter = new AtomicInteger();
42  
43      @Override
44      public void setUp() throws Exception {
45          super.setUp();
46  
47          packer = lookup(IndexPacker.class);
48      }
49  
50      @Override
51      public void tearDown() throws Exception {
52          File props = new File(IndexingContext.INDEX_PACKER_PROPERTIES_FILE);
53          if (props.exists()) {
54              props.delete();
55          }
56          super.tearDown();
57      }
58  
59      @Override
60      protected int readIndex(final NexusIndexer nexusIndexer, final IndexingContext indexingContext) throws IOException {
61          // note: concurrent Index publishing into SAME directory is not supported and should be avoided.
62          // This test had multiple threads doing it, and since it was not checking actual results of publish (that was
63          // not the goal of the test, but simultaneous publishing of merged context that has member changes happening),
64          // it was probably publish rubbish anyway.
65          final File publish = new File(repoPublish, "publish-" + counter.getAndIncrement());
66  
67          final IndexSearcher indexSearcher = context.acquireIndexSearcher();
68          try {
69              final IndexPackingRequest request =
70                      new IndexPackingRequest(context, indexSearcher.getIndexReader(), publish);
71              request.setCreateIncrementalChunks(false);
72              packer.packIndex(request);
73          } finally {
74              context.releaseIndexSearcher(indexSearcher);
75          }
76  
77          return 1;
78      }
79  }