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.incremental;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.lucene.search.IndexSearcher;
29  import org.apache.maven.index.AbstractIndexCreatorHelper;
30  import org.apache.maven.index.NexusIndexer;
31  import org.apache.maven.index.context.IndexingContext;
32  import org.apache.maven.index.packer.IndexPackingRequest;
33  import org.apache.maven.index.updater.IndexUpdateRequest;
34  import org.apache.maven.index.updater.ResourceFetcher;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.junit.Test;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertNull;
40  
41  public class DefaultIncrementalHandlerTest extends AbstractIndexCreatorHelper {
42      IncrementalHandler handler = null;
43  
44      NexusIndexer indexer = null;
45  
46      IndexingContext context = null;
47  
48      File indexDir = null;
49  
50      File repoDir = null;
51  
52      @Override
53      public void setUp() throws Exception {
54          super.setUp();
55  
56          indexer = lookup(NexusIndexer.class);
57          handler = lookup(IncrementalHandler.class);
58  
59          indexDir = new File(getBasedir(), "target/index/nexus-incremental-test");
60          repoDir = new File(getBasedir(), "target/repos/nexus-incremental-test");
61          FileUtils.deleteDirectory(indexDir);
62          FileUtils.deleteDirectory(repoDir);
63  
64          context = indexer.addIndexingContext("test", "test", repoDir, indexDir, null, null, DEFAULT_CREATORS);
65      }
66  
67      @Override
68      public void tearDown() throws Exception {
69          super.tearDown();
70  
71          indexer.removeIndexingContext(context, true);
72      }
73  
74      @Test
75      public void testUpdateInvalidProperties() throws Exception {
76          final IndexSearcher indexSearcher = context.acquireIndexSearcher();
77          try {
78              Properties properties = new Properties();
79  
80              IndexPackingRequest request = new IndexPackingRequest(context, indexSearcher.getIndexReader(), indexDir);
81  
82              // No properties definite fail
83              assertNull(handler.getIncrementalUpdates(request, properties));
84  
85              properties.setProperty(IndexingContext.INDEX_TIMESTAMP, "junk");
86  
87              // property set, but invalid
88              assertNull(handler.getIncrementalUpdates(request, properties));
89  
90              properties.setProperty(IndexingContext.INDEX_TIMESTAMP, "19991112182432.432 -0600");
91  
92              List<Integer> updates = handler.getIncrementalUpdates(request, properties);
93  
94              assertEquals(updates.size(), 0);
95          } finally {
96              context.releaseIndexSearcher(indexSearcher);
97          }
98      }
99  
100     @Test
101     public void testUpdateValid() throws Exception {
102         Properties properties = new Properties();
103 
104         properties.setProperty(IndexingContext.INDEX_TIMESTAMP, "19991112182432.432 -0600");
105 
106         FileUtils.copyDirectoryStructure(new File(getBasedir(), "src/test/repo/ch"), new File(repoDir, "ch"));
107 
108         indexer.scan(context);
109 
110         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
111         try {
112             IndexPackingRequest request = new IndexPackingRequest(context, indexSearcher.getIndexReader(), indexDir);
113             List<Integer> updates = handler.getIncrementalUpdates(request, properties);
114 
115             assertEquals(updates.size(), 1);
116         } finally {
117             context.releaseIndexSearcher(indexSearcher);
118         }
119     }
120 
121     @Test
122     public void testRemoteUpdatesInvalidProperties() throws Exception {
123         // just a dummy fetcher, it's not used here anyway
124         IndexUpdateRequest request = new IndexUpdateRequest(context, new ResourceFetcher() {
125             public InputStream retrieve(String name) throws IOException, FileNotFoundException {
126                 // TODO Auto-generated method stub
127                 return null;
128             }
129 
130             public void retrieve(String name, File targetFile) throws IOException, FileNotFoundException {
131                 // TODO Auto-generated method stub
132 
133             }
134 
135             public void disconnect() throws IOException {
136                 // TODO Auto-generated method stub
137 
138             }
139 
140             public void connect(String id, String url) throws IOException {
141                 // TODO Auto-generated method stub
142 
143             }
144         });
145 
146         Properties localProperties = new Properties();
147         Properties remoteProperties = new Properties();
148 
149         List<String> filenames = handler.loadRemoteIncrementalUpdates(request, localProperties, remoteProperties);
150 
151         assertNull(filenames);
152     }
153 }