1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
83 assertNull(handler.getIncrementalUpdates(request, properties));
84
85 properties.setProperty(IndexingContext.INDEX_TIMESTAMP, "junk");
86
87
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
124 IndexUpdateRequest request = new IndexUpdateRequest(context, new ResourceFetcher() {
125 public InputStream retrieve(String name) throws IOException, FileNotFoundException {
126
127 return null;
128 }
129
130 public void retrieve(String name, File targetFile) throws IOException, FileNotFoundException {
131
132
133 }
134
135 public void disconnect() throws IOException {
136
137
138 }
139
140 public void connect(String id, String url) throws IOException {
141
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 }