]> source.dussan.org Git - archiva.git/blob
c10103a16001fd1b9a440672b394c768a69a7cae
[archiva.git] /
1 package org.apache.archiva.metadata.repository.stats;
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 junit.framework.TestCase;
23 import org.apache.archiva.metadata.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
25 import org.apache.archiva.metadata.repository.DefaultMetadataResolver;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
28 import org.apache.archiva.metadata.repository.MetadataService;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
31 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
32 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySessionFactory;
33 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySession;
34 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.jackrabbit.commons.JcrUtils;
37 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.test.context.ContextConfiguration;
46
47 import javax.jcr.ImportUUIDBehavior;
48 import javax.jcr.Node;
49 import javax.jcr.Repository;
50 import javax.jcr.RepositoryException;
51 import javax.jcr.Session;
52 import javax.jcr.nodetype.NodeTypeManager;
53 import javax.jcr.nodetype.NodeTypeTemplate;
54 import java.io.IOException;
55 import java.io.InputStream;
56 import java.nio.file.Files;
57 import java.nio.file.Path;
58 import java.nio.file.Paths;
59 import java.util.Calendar;
60 import java.util.Date;
61 import java.util.List;
62 import java.util.Map;
63 import java.util.zip.GZIPInputStream;
64
65 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
66 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
67 public class JcrRepositoryStatisticsGatheringTest
68     extends TestCase
69 {
70     private static final Logger log = LoggerFactory.getLogger( JcrRepositoryStatisticsGatheringTest.class );
71     private static final int TOTAL_FILE_COUNT = 1000;
72
73     private static final int NEW_FILE_COUNT = 500;
74
75     private static final String TEST_REPO = "test-repo";
76
77     static JcrMetadataRepository repository;
78     static JcrRepositorySessionFactory sessionFactory;
79
80     Session jcrSession;
81
82     private static Repository jcrRepository;
83
84     Logger logger = LoggerFactory.getLogger( getClass() );
85     private int assertRetrySleepMs = 500;
86     private int assertMaxTries = 5;
87
88     @BeforeClass
89     public static void setupSpec()
90         throws IOException, InvalidFileStoreVersionException
91     {
92         Path directory = Paths.get( "target/test-repositories" );
93         if ( Files.exists(directory) )
94         {
95             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
96         }
97         directory = Paths.get( "target/jcr" );
98         if (Files.exists( directory )) {
99             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
100         }
101
102         List<MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
103
104         MetadataService metadataService = new MetadataService( );
105         metadataService.setMetadataFacetFactories( factories );
106
107         JcrRepositorySessionFactory jcrSessionFactory = new JcrRepositorySessionFactory();
108         jcrSessionFactory.setMetadataResolver(new DefaultMetadataResolver());
109         jcrSessionFactory.setMetadataService(metadataService);
110
111         jcrSessionFactory.open();
112         sessionFactory = jcrSessionFactory;
113         repository = jcrSessionFactory.getMetadataRepository();
114     }
115
116
117     @AfterClass
118     public static void stopSpec() {
119         try
120         {
121             repository.close();
122         }
123         catch ( MetadataRepositoryException e )
124         {
125             e.printStackTrace( );
126         }
127         sessionFactory.close();
128     }
129
130     /*
131      * Used by tryAssert to allow to throw exceptions in the lambda expression.
132      */
133     @FunctionalInterface
134     protected interface AssertFunction
135     {
136         void accept( ) throws Exception;
137     }
138
139     protected void tryAssert( AssertFunction func ) throws Exception
140     {
141         tryAssert( func, assertMaxTries, assertRetrySleepMs );
142     }
143
144
145     /*
146      * Runs the assert method until the assert is successful or the number of retries
147      * is reached. This is needed because the JCR Oak index update is asynchronous, so updates
148      * may not be visible immediately after the modification.
149      */
150     private void tryAssert( AssertFunction func, int retries, int sleepMillis ) throws Exception
151     {
152         Throwable t = null;
153         int retry = retries;
154         while ( retry-- > 0 )
155         {
156             try
157             {
158                 func.accept( );
159                 return;
160             }
161             catch ( Exception | AssertionError e )
162             {
163                 t = e;
164                 Thread.currentThread( ).sleep( sleepMillis );
165                 log.warn( "Retrying assert {}: {}", retry, e.getMessage( ) );
166             }
167         }
168         log.warn( "Retries: {}, Exception: {}", retry, t.getMessage( ) );
169         if ( retry <= 0 && t != null )
170         {
171             if ( t instanceof RuntimeException )
172             {
173                 throw (RuntimeException) t;
174             }
175             else if ( t instanceof Exception )
176             {
177                 throw (Exception) t;
178             }
179             else if ( t instanceof Error )
180             {
181                 throw (Error) t;
182             }
183         }
184     }
185
186     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
187         throws RepositoryException
188     {
189         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
190         nodeType.setMixin( true );
191         nodeType.setName( type );
192         nodeTypeManager.registerNodeType( nodeType, false );
193     }
194
195     @After
196     public void tearDown()
197         throws Exception
198     {
199         if ( repository != null )
200         {
201             try
202             {
203                 repository.close( );
204             } catch (Throwable e) {
205                 //
206             }
207         }
208         if (sessionFactory!=null) {
209             try
210             {
211                 sessionFactory.close( );
212             } catch (Throwable e) {
213                 //
214             }
215         }
216         super.tearDown();
217
218     }
219
220     @Test
221     public void testJcrStatisticsQuery()
222         throws Exception
223     {
224         try(RepositorySession repSession = sessionFactory.createSession()) {
225             Calendar cal = Calendar.getInstance();
226             Date endTime = cal.getTime();
227             cal.add(Calendar.HOUR, -1);
228             Date startTime = cal.getTime();
229
230             loadContentIntoRepo(repSession, TEST_REPO);
231             loadContentIntoRepo( repSession, "another-repo");
232
233             DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
234             testedStatistics.setNewFileCount(NEW_FILE_COUNT);
235             testedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
236             testedStatistics.setScanStartTime(startTime);
237             testedStatistics.setScanEndTime(endTime);
238
239
240             DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
241             expectedStatistics.setNewFileCount(NEW_FILE_COUNT);
242             expectedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
243             expectedStatistics.setScanEndTime(endTime);
244             expectedStatistics.setScanStartTime(startTime);
245             expectedStatistics.setTotalArtifactFileSize(95954585);
246             expectedStatistics.setTotalArtifactCount(269);
247             expectedStatistics.setTotalGroupCount(1);
248             expectedStatistics.setTotalProjectCount(43);
249             expectedStatistics.setTotalCountForType("zip", 1);
250             expectedStatistics.setTotalCountForType("gz", 1); // FIXME: should be tar.gz
251             expectedStatistics.setTotalCountForType("java-source", 10);
252             expectedStatistics.setTotalCountForType("jar", 108);
253             expectedStatistics.setTotalCountForType("xml", 3);
254             expectedStatistics.setTotalCountForType("war", 2);
255             expectedStatistics.setTotalCountForType("pom", 144);
256             expectedStatistics.setRepositoryId(TEST_REPO);
257
258             tryAssert( () -> {
259                 repository.populateStatistics(repSession, repository, TEST_REPO, testedStatistics);
260
261                 logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType());
262
263             assertEquals(NEW_FILE_COUNT, testedStatistics.getNewFileCount());
264             assertEquals(TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount());
265             assertEquals(endTime, testedStatistics.getScanEndTime());
266             assertEquals(startTime, testedStatistics.getScanStartTime());
267             assertEquals(269, testedStatistics.getTotalArtifactCount());
268             assertEquals(1, testedStatistics.getTotalGroupCount());
269             assertEquals(43, testedStatistics.getTotalProjectCount());
270             assertEquals(1, testedStatistics.getTotalCountForType("zip"));
271             assertEquals(1, testedStatistics.getTotalCountForType("gz"));
272             assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
273             assertEquals(108, testedStatistics.getTotalCountForType("jar"));
274             assertEquals(3, testedStatistics.getTotalCountForType("xml"));
275             assertEquals(2, testedStatistics.getTotalCountForType("war"));
276             assertEquals(144, testedStatistics.getTotalCountForType("pom"));
277             assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
278             assertEquals(95954585, testedStatistics.getTotalArtifactFileSize());
279         });
280
281         }
282     }
283
284     private void loadContentIntoRepo( RepositorySession repoSession, String repoId )
285         throws RepositoryException, IOException, MetadataRepositoryException
286     {
287             jcrSession = ((JcrRepositorySession) repoSession).getJcrSession();
288         Node n = JcrUtils.getOrAddNode( jcrSession.getRootNode( ), "repositories" );
289         n = JcrUtils.getOrAddNode( n, repoId );
290         n = JcrUtils.getOrAddNode( n, "content" );
291         n = JcrUtils.getOrAddNode( n, "org" );
292         n = JcrUtils.getOrAddNode( n, "apache" );
293
294         InputStream inputStream = getClass( ).getResourceAsStream( "/artifacts.xml" );
295         jcrSession.importXML( n.getPath( ), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
296         jcrSession.save( );
297     }
298 }