1 package org.apache.archiva.metadata.repository.stats;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.text.ParseException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.LinkedHashMap;
28 import java.util.List;
31 import junit.framework.TestCase;
32 import org.apache.archiva.metadata.repository.MetadataRepository;
33 import org.easymock.MockControl;
35 public class RepositoryStatisticsManagerTest
38 private DefaultRepositoryStatisticsManager repositoryStatisticsManager;
40 private static final String TEST_REPO_ID = "test-repo";
42 private MockControl metadataRepositoryControl;
44 private MetadataRepository metadataRepository;
46 private static final String FIRST_TEST_SCAN = "20091201.123456.789";
48 private static final String SECOND_TEST_SCAN = "20091202.012345.678";
50 private Map<String, RepositoryStatistics> statsCreated = new LinkedHashMap<String, RepositoryStatistics>();
53 protected void setUp()
58 repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
60 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
61 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
62 repositoryStatisticsManager.setMetadataRepository( metadataRepository );
65 public void testGetLatestStats()
68 Date endTime = new Date( RepositoryStatistics.SCAN_TIMESTAMP.parse( SECOND_TEST_SCAN ).getTime() + 60000 );
70 RepositoryStatistics stats = new RepositoryStatistics();
71 stats.setScanStartTime( RepositoryStatistics.SCAN_TIMESTAMP.parse( SECOND_TEST_SCAN ) );
72 stats.setScanEndTime( endTime );
73 stats.setTotalArtifactFileSize( 1314527915L );
74 stats.setNewFileCount( 123 );
75 stats.setTotalArtifactCount( 10386 );
76 stats.setTotalProjectCount( 2031 );
77 stats.setTotalGroupCount( 529 );
78 stats.setTotalFileCount( 56229 );
80 metadataRepositoryControl.expectAndReturn(
81 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
82 Arrays.asList( FIRST_TEST_SCAN, SECOND_TEST_SCAN ) );
83 metadataRepositoryControl.expectAndReturn(
84 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, SECOND_TEST_SCAN ),
86 metadataRepositoryControl.replay();
88 stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
89 assertNotNull( stats );
90 assertEquals( 1314527915L, stats.getTotalArtifactFileSize() );
91 assertEquals( 123, stats.getNewFileCount() );
92 assertEquals( 10386, stats.getTotalArtifactCount() );
93 assertEquals( 2031, stats.getTotalProjectCount() );
94 assertEquals( 529, stats.getTotalGroupCount() );
95 assertEquals( 56229, stats.getTotalFileCount() );
96 assertEquals( SECOND_TEST_SCAN, RepositoryStatistics.SCAN_TIMESTAMP.format( stats.getScanStartTime() ) );
97 assertEquals( SECOND_TEST_SCAN, stats.getName() );
98 assertEquals( endTime, stats.getScanEndTime() );
100 metadataRepositoryControl.verify();
103 public void testGetLatestStatsWhenEmpty()
105 metadataRepositoryControl.expectAndReturn(
106 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
107 Collections.emptyList() );
108 metadataRepositoryControl.replay();
110 RepositoryStatistics stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
113 metadataRepositoryControl.verify();
116 public void testAddNewStats()
118 Date current = new Date();
119 Date startTime = new Date( current.getTime() - 12345 );
121 RepositoryStatistics stats = createTestStats( startTime, current );
123 metadataRepository.addMetadataFacet( TEST_REPO_ID, stats );
124 metadataRepositoryControl.expectAndReturn(
125 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
126 Arrays.asList( stats.getName() ) );
127 metadataRepositoryControl.expectAndReturn(
128 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, stats.getName() ),
131 metadataRepositoryControl.replay();
133 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
135 stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
136 assertNotNull( stats );
137 assertEquals( 1400032000L, stats.getTotalArtifactFileSize() );
138 assertEquals( 45, stats.getNewFileCount() );
139 assertEquals( 10412, stats.getTotalArtifactCount() );
140 assertEquals( 2036, stats.getTotalProjectCount() );
141 assertEquals( 531, stats.getTotalGroupCount() );
142 assertEquals( 56345, stats.getTotalFileCount() );
143 assertEquals( current.getTime() - 12345, stats.getScanStartTime().getTime() );
144 assertEquals( current, stats.getScanEndTime() );
146 metadataRepositoryControl.verify();
149 public void testDeleteStats()
151 Date current = new Date();
153 Date startTime1 = new Date( current.getTime() - 12345 );
154 RepositoryStatistics stats1 = createTestStats( startTime1, new Date( current.getTime() - 6000 ) );
155 metadataRepository.addMetadataFacet( TEST_REPO_ID, stats1 );
157 Date startTime2 = new Date( current.getTime() - 3000 );
158 RepositoryStatistics stats2 = createTestStats( startTime2, current );
159 metadataRepository.addMetadataFacet( TEST_REPO_ID, stats2 );
161 metadataRepositoryControl.expectAndReturn(
162 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
163 Arrays.asList( stats1.getName(), stats2.getName() ) );
164 metadataRepositoryControl.expectAndReturn(
165 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, stats2.getName() ),
168 metadataRepository.removeMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID );
170 metadataRepositoryControl.expectAndReturn(
171 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
172 Collections.emptyList() );
174 metadataRepositoryControl.replay();
176 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats1 );
177 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats2 );
179 assertNotNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
181 repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
183 assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
185 metadataRepositoryControl.verify();
188 public void testDeleteStatsWhenEmpty()
190 metadataRepositoryControl.expectAndReturn(
191 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
192 Collections.emptyList(), 2 );
193 metadataRepository.removeMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID );
195 metadataRepositoryControl.replay();
197 assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
199 repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
201 assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
203 metadataRepositoryControl.verify();
206 public void testGetStatsRangeInside()
208 Date current = new Date();
210 addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
211 addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
212 addStats( new Date( current.getTime() - 1000 ), current );
214 ArrayList<String> keys = new ArrayList<String>( statsCreated.keySet() );
215 metadataRepositoryControl.expectAndReturn(
216 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ), keys );
218 // only match the middle one
219 String key = keys.get( 1 );
220 metadataRepositoryControl.expectAndReturn(
221 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
222 statsCreated.get( key ) );
224 metadataRepositoryControl.replay();
226 for ( RepositoryStatistics stats : statsCreated.values() )
228 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
231 List<RepositoryStatistics> list =
232 repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID, new Date( current.getTime() - 4000 ),
233 new Date( current.getTime() - 2000 ) );
235 assertEquals( 1, list.size() );
236 assertEquals( new Date( current.getTime() - 3000 ), list.get( 0 ).getScanStartTime() );
238 metadataRepositoryControl.verify();
241 public void testGetStatsRangeUpperOutside()
243 Date current = new Date();
245 addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
246 addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
247 addStats( new Date( current.getTime() - 1000 ), current );
249 ArrayList<String> keys = new ArrayList<String>( statsCreated.keySet() );
250 metadataRepositoryControl.expectAndReturn(
251 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ), keys );
253 String key = keys.get( 1 );
254 metadataRepositoryControl.expectAndReturn(
255 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
256 statsCreated.get( key ) );
258 metadataRepositoryControl.expectAndReturn(
259 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
260 statsCreated.get( key ) );
262 metadataRepositoryControl.replay();
264 for ( RepositoryStatistics stats : statsCreated.values() )
266 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
269 List<RepositoryStatistics> list =
270 repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID, new Date( current.getTime() - 4000 ),
273 assertEquals( 2, list.size() );
274 assertEquals( new Date( current.getTime() - 3000 ), list.get( 1 ).getScanStartTime() );
275 assertEquals( new Date( current.getTime() - 1000 ), list.get( 0 ).getScanStartTime() );
277 metadataRepositoryControl.verify();
280 public void testGetStatsRangeLowerOutside()
282 Date current = new Date();
284 addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
285 addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
286 addStats( new Date( current.getTime() - 1000 ), current );
288 ArrayList<String> keys = new ArrayList<String>( statsCreated.keySet() );
289 metadataRepositoryControl.expectAndReturn(
290 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ), keys );
292 String key = keys.get( 0 );
293 metadataRepositoryControl.expectAndReturn(
294 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
295 statsCreated.get( key ) );
297 metadataRepositoryControl.expectAndReturn(
298 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
299 statsCreated.get( key ) );
301 metadataRepositoryControl.replay();
303 for ( RepositoryStatistics stats : statsCreated.values() )
305 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
308 List<RepositoryStatistics> list =
309 repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID, new Date( current.getTime() - 20000 ),
310 new Date( current.getTime() - 2000 ) );
312 assertEquals( 2, list.size() );
313 assertEquals( new Date( current.getTime() - 12345 ), list.get( 1 ).getScanStartTime() );
314 assertEquals( new Date( current.getTime() - 3000 ), list.get( 0 ).getScanStartTime() );
316 metadataRepositoryControl.verify();
319 public void testGetStatsRangeLowerAndUpperOutside()
321 Date current = new Date();
323 addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
324 addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
325 addStats( new Date( current.getTime() - 1000 ), current );
327 ArrayList<String> keys = new ArrayList<String>( statsCreated.keySet() );
328 metadataRepositoryControl.expectAndReturn(
329 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ), keys );
331 String key = keys.get( 0 );
332 metadataRepositoryControl.expectAndReturn(
333 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
334 statsCreated.get( key ) );
336 metadataRepositoryControl.expectAndReturn(
337 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
338 statsCreated.get( key ) );
340 metadataRepositoryControl.expectAndReturn(
341 metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, key ),
342 statsCreated.get( key ) );
344 metadataRepositoryControl.replay();
346 for ( RepositoryStatistics stats : statsCreated.values() )
348 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
351 List<RepositoryStatistics> list =
352 repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID, new Date( current.getTime() - 20000 ),
355 assertEquals( 3, list.size() );
356 assertEquals( new Date( current.getTime() - 12345 ), list.get( 2 ).getScanStartTime() );
357 assertEquals( new Date( current.getTime() - 3000 ), list.get( 1 ).getScanStartTime() );
358 assertEquals( new Date( current.getTime() - 1000 ), list.get( 0 ).getScanStartTime() );
360 metadataRepositoryControl.verify();
363 public void testGetStatsRangeNotInside()
365 Date current = new Date();
367 addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
368 addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
369 addStats( new Date( current.getTime() - 1000 ), current );
371 ArrayList<String> keys = new ArrayList<String>( statsCreated.keySet() );
372 metadataRepositoryControl.expectAndReturn(
373 metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ), keys );
375 metadataRepositoryControl.replay();
377 for ( RepositoryStatistics stats : statsCreated.values() )
379 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
382 List<RepositoryStatistics> list =
383 repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID, new Date( current.getTime() - 20000 ),
384 new Date( current.getTime() - 16000 ) );
386 assertEquals( 0, list.size() );
388 metadataRepositoryControl.verify();
391 private void addStats( Date startTime, Date endTime )
393 RepositoryStatistics stats = createTestStats( startTime, endTime );
394 metadataRepository.addMetadataFacet( TEST_REPO_ID, stats );
395 statsCreated.put( stats.getName(), stats );
398 private RepositoryStatistics createTestStats( Date startTime, Date endTime )
400 RepositoryStatistics stats = new RepositoryStatistics();
401 stats.setScanStartTime( startTime );
402 stats.setScanEndTime( endTime );
403 stats.setTotalArtifactFileSize( 1400032000L );
404 stats.setNewFileCount( 45 );
405 stats.setTotalArtifactCount( 10412 );
406 stats.setTotalProjectCount( 2036 );
407 stats.setTotalGroupCount( 531 );
408 stats.setTotalFileCount( 56345 );