View Javadoc
1   package org.apache.maven.index.incremental;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.lucene.search.IndexSearcher;
23  import org.apache.maven.index.AbstractIndexCreatorHelper;
24  import org.apache.maven.index.NexusIndexer;
25  import org.apache.maven.index.context.IndexingContext;
26  import org.apache.maven.index.packer.IndexPackingRequest;
27  import org.apache.maven.index.updater.IndexUpdateRequest;
28  import org.apache.maven.index.updater.ResourceFetcher;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.junit.Test;
31  
32  import java.io.File;
33  import java.io.FileNotFoundException;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.util.List;
37  import java.util.Properties;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertNull;
41  
42  public class DefaultIncrementalHandlerTest
43      extends AbstractIndexCreatorHelper
44  {
45      IncrementalHandler handler = null;
46  
47      NexusIndexer indexer = null;
48  
49      IndexingContext context = null;
50  
51      File indexDir = null;
52  
53      File repoDir = null;
54  
55      @Override
56      public void setUp()
57          throws Exception
58      {
59          super.setUp();
60  
61          indexer = lookup( NexusIndexer.class );
62          handler = lookup( IncrementalHandler.class );
63  
64          indexDir = new File( getBasedir(), "target/index/nexus-incremental-test" );
65          repoDir = new File( getBasedir(), "target/repos/nexus-incremental-test" );
66          FileUtils.deleteDirectory( indexDir );
67          FileUtils.deleteDirectory( repoDir );
68  
69          context = indexer.addIndexingContext( "test", "test", repoDir, indexDir, null, null, DEFAULT_CREATORS );
70      }
71  
72      @Override
73      public void tearDown()
74          throws Exception
75      {
76          super.tearDown();
77  
78          indexer.removeIndexingContext( context, true );
79      }
80  
81      @Test
82      public void testUpdateInvalidProperties()
83          throws Exception
84      {
85          final IndexSearcher indexSearcher = context.acquireIndexSearcher();
86          try
87          {
88              Properties properties = new Properties();
89  
90              IndexPackingRequest request = new IndexPackingRequest( context, indexSearcher.getIndexReader(), indexDir );
91  
92              // No properties definite fail
93              assertNull( handler.getIncrementalUpdates( request, properties ) );
94  
95              properties.setProperty( IndexingContext.INDEX_TIMESTAMP, "junk" );
96  
97              // property set, but invalid
98              assertNull( handler.getIncrementalUpdates( request, properties ) );
99  
100             properties.setProperty( IndexingContext.INDEX_TIMESTAMP, "19991112182432.432 -0600" );
101 
102             List<Integer> updates = handler.getIncrementalUpdates( request, properties );
103 
104             assertEquals( updates.size(), 0 );
105         }
106         finally
107         {
108             context.releaseIndexSearcher( indexSearcher );
109         }
110     }
111 
112     @Test
113     public void testUpdateValid()
114         throws Exception
115     {
116         Properties properties = new Properties();
117 
118         properties.setProperty( IndexingContext.INDEX_TIMESTAMP, "19991112182432.432 -0600" );
119 
120         FileUtils.copyDirectoryStructure( new File( getBasedir(), "src/test/repo/ch" ), new File( repoDir, "ch" ) );
121 
122         indexer.scan( context );
123 
124         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
125         try
126         {
127             IndexPackingRequest request = new IndexPackingRequest( context, indexSearcher.getIndexReader(), indexDir );
128             List<Integer> updates = handler.getIncrementalUpdates( request, properties );
129 
130             assertEquals( updates.size(), 1 );
131         }
132         finally
133         {
134             context.releaseIndexSearcher( indexSearcher );
135         }
136     }
137 
138     @Test
139     public void testRemoteUpdatesInvalidProperties()
140         throws Exception
141     {
142         // just a dummy fetcher, it's not used here anyway
143         IndexUpdateRequest request = new IndexUpdateRequest( context, new ResourceFetcher()
144         {
145             public InputStream retrieve( String name )
146                 throws IOException, FileNotFoundException
147             {
148                 // TODO Auto-generated method stub
149                 return null;
150             }
151 
152             public void retrieve( String name, File targetFile )
153                 throws IOException, FileNotFoundException
154             {
155                 // TODO Auto-generated method stub
156 
157             }
158 
159             public void disconnect()
160                 throws IOException
161             {
162                 // TODO Auto-generated method stub
163 
164             }
165 
166             public void connect( String id, String url )
167                 throws IOException
168             {
169                 // TODO Auto-generated method stub
170 
171             }
172         } );
173 
174         Properties localProperties = new Properties();
175         Properties remoteProperties = new Properties();
176 
177         List<String> filenames = handler.loadRemoteIncrementalUpdates( request, localProperties, remoteProperties );
178 
179         assertNull( filenames );
180     }
181 }