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