1 package org.apache.maven.index.updater;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.ServerSocket;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.Properties;
32 import java.util.TimeZone;
33
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.apache.lucene.document.Document;
39 import org.apache.maven.index.context.DocumentFilter;
40 import org.apache.maven.index.context.IndexingContext;
41 import org.codehaus.plexus.util.FileUtils;
42 import org.mortbay.jetty.Handler;
43 import org.mortbay.jetty.Response;
44 import org.mortbay.jetty.Server;
45 import org.mortbay.jetty.handler.DefaultHandler;
46 import org.mortbay.jetty.handler.HandlerList;
47 import org.mortbay.jetty.handler.ResourceHandler;
48
49 public class DownloadRemoteIndexerManagerTest
50 extends AbstractIndexUpdaterTest
51 {
52 private Server server;
53
54 private File fakeCentral;
55
56 private IndexingContext centralContext;
57
58 @Override
59 protected void setUp()
60 throws Exception
61 {
62 super.setUp();
63
64 fakeCentral = new File( getBasedir(), "target/repos/fake-central" );
65 fakeCentral.mkdirs();
66
67
68 ServerSocket s = new ServerSocket( 0 );
69 int port = s.getLocalPort();
70 s.close();
71
72 server = new Server( port );
73
74 ResourceHandler resource_handler = new ResourceHandler()
75 {
76 @Override
77 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
78 throws IOException, ServletException
79 {
80
81 super.handle( target, request, response, dispatch );
82
83 }
84 };
85 resource_handler.setResourceBase( fakeCentral.getAbsolutePath() );
86 HandlerList handlers = new HandlerList();
87 handlers.setHandlers( new Handler[] { resource_handler, new DefaultHandler() } );
88 server.setHandler( handlers );
89
90
91 server.start();
92
93
94 centralContext =
95 indexer.addIndexingContext( "central", "central", fakeCentral, getDirectory( "central" ),
96 "http://localhost:" + port, null, MIN_CREATORS );
97 }
98
99 @Override
100 protected void tearDown()
101 throws Exception
102 {
103 server.stop();
104
105 FileUtils.forceDelete( fakeCentral );
106
107 super.tearDown();
108 }
109
110 public void testRepoReindex()
111 throws Exception
112 {
113 IndexUpdateRequest iur;
114
115 File index1 = new File( getBasedir(), "src/test/resources/repo-index/index" );
116 File index2 = new File( getBasedir(), "src/test/resources/repo-index/index2" );
117 File centralIndex = new File( fakeCentral, ".index" );
118
119
120 overwriteIndex( index2, centralIndex );
121
122 iur =
123 new IndexUpdateRequest( centralContext, new WagonHelper( getContainer() ).getWagonResourceFetcher( null ) );
124 iur.setForceFullUpdate( true );
125
126 updater.fetchAndUpdateIndex( iur );
127
128 searchFor( "org.sonatype.nexus", 8, centralContext );
129
130
131 overwriteIndex( index1, centralIndex );
132
133 iur =
134 new IndexUpdateRequest( centralContext, new WagonHelper( getContainer() ).getWagonResourceFetcher( null ) );
135 iur.setForceFullUpdate( true );
136
137 iur.setDocumentFilter( new DocumentFilter()
138 {
139 public boolean accept( Document doc )
140 {
141 return true;
142 }
143 });
144
145 updater.fetchAndUpdateIndex( iur );
146
147 searchFor( "org.sonatype.nexus", 1, centralContext );
148
149
150 overwriteIndex( index2, centralIndex );
151
152 iur =
153 new IndexUpdateRequest( centralContext, new WagonHelper( getContainer() ).getWagonResourceFetcher( null ) );
154 iur.setForceFullUpdate( true );
155
156 updater.fetchAndUpdateIndex( iur );
157
158 searchFor( "org.sonatype.nexus", 8, centralContext );
159 }
160
161 private void overwriteIndex( File source, File destination )
162 throws Exception
163 {
164 File indexFile = new File( destination, "nexus-maven-repository-index.gz" );
165 File indexProperties = new File( destination, "nexus-maven-repository-index.properties" );
166
167 long lastMod = -1;
168 if ( destination.exists() )
169 {
170 FileUtils.forceDelete( destination );
171 lastMod = indexFile.lastModified();
172 }
173 FileUtils.copyDirectory( source, destination );
174 long lastMod2 = indexFile.lastModified();
175 assertTrue( lastMod < lastMod2 );
176
177 Properties p = new Properties();
178 InputStream input = new FileInputStream( indexProperties );
179 p.load( input );
180 input.close();
181
182 p.setProperty( "nexus.index.time", format( new Date() ) );
183 p.setProperty( "nexus.index.timestamp", format( new Date() ) );
184
185 OutputStream output = new FileOutputStream( indexProperties );
186 p.store( output, null );
187 output.close();
188 }
189
190 private String format( Date d )
191 {
192 SimpleDateFormat df = new SimpleDateFormat( IndexingContext.INDEX_TIME_FORMAT );
193 df.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
194 return df.format( d );
195 }
196 }