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 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.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
29 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySessionFactory;
30 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySession;
31 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.jackrabbit.commons.JcrUtils;
34 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.jcr.ImportUUIDBehavior;
45 import javax.jcr.Node;
46 import javax.jcr.Repository;
47 import javax.jcr.RepositoryException;
48 import javax.jcr.Session;
49 import javax.jcr.nodetype.NodeTypeManager;
50 import javax.jcr.nodetype.NodeTypeTemplate;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.Calendar;
56 import java.util.Date;
58 import java.util.zip.GZIPInputStream;
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class JcrRepositoryStatisticsGatheringTest
65 private static final int TOTAL_FILE_COUNT = 1000;
67 private static final int NEW_FILE_COUNT = 500;
69 private static final String TEST_REPO = "test-repo";
71 static JcrMetadataRepository repository;
72 static JcrRepositorySessionFactory sessionFactory;
76 private static Repository jcrRepository;
78 Logger logger = LoggerFactory.getLogger( getClass() );
81 public static void setupSpec()
82 throws IOException, InvalidFileStoreVersionException
84 Path directory = Paths.get( "target/test-repositories" );
85 if ( Files.exists(directory) )
87 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
89 directory = Paths.get( "target/jcr" );
90 if (Files.exists( directory )) {
91 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
94 Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
97 JcrRepositorySessionFactory jcrSessionFactory = new JcrRepositorySessionFactory();
98 jcrSessionFactory.setMetadataResolver(new DefaultMetadataResolver());
99 jcrSessionFactory.setMetadataFacetFactories(factories);
101 jcrSessionFactory.open();
102 sessionFactory = jcrSessionFactory;
103 repository = jcrSessionFactory.getMetadataRepository();
108 public static void stopSpec() {
113 catch ( MetadataRepositoryException e )
115 e.printStackTrace( );
117 sessionFactory.close();
121 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
122 throws RepositoryException
124 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
125 nodeType.setMixin( true );
126 nodeType.setName( type );
127 nodeTypeManager.registerNodeType( nodeType, false );
131 public void tearDown()
134 if ( repository != null )
139 } catch (Throwable e) {
143 if (sessionFactory!=null) {
146 sessionFactory.close( );
147 } catch (Throwable e) {
156 public void testJcrStatisticsQuery()
159 try(RepositorySession repSession = sessionFactory.createSession()) {
160 Calendar cal = Calendar.getInstance();
161 Date endTime = cal.getTime();
162 cal.add(Calendar.HOUR, -1);
163 Date startTime = cal.getTime();
165 loadContentIntoRepo(repSession, TEST_REPO);
166 loadContentIntoRepo( repSession, "another-repo");
168 DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
169 testedStatistics.setNewFileCount(NEW_FILE_COUNT);
170 testedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
171 testedStatistics.setScanStartTime(startTime);
172 testedStatistics.setScanEndTime(endTime);
174 repository.populateStatistics(repSession, repository, TEST_REPO, testedStatistics);
176 DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
177 expectedStatistics.setNewFileCount(NEW_FILE_COUNT);
178 expectedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
179 expectedStatistics.setScanEndTime(endTime);
180 expectedStatistics.setScanStartTime(startTime);
181 expectedStatistics.setTotalArtifactFileSize(95954585);
182 expectedStatistics.setTotalArtifactCount(269);
183 expectedStatistics.setTotalGroupCount(1);
184 expectedStatistics.setTotalProjectCount(43);
185 expectedStatistics.setTotalCountForType("zip", 1);
186 expectedStatistics.setTotalCountForType("gz", 1); // FIXME: should be tar.gz
187 expectedStatistics.setTotalCountForType("java-source", 10);
188 expectedStatistics.setTotalCountForType("jar", 108);
189 expectedStatistics.setTotalCountForType("xml", 3);
190 expectedStatistics.setTotalCountForType("war", 2);
191 expectedStatistics.setTotalCountForType("pom", 144);
192 expectedStatistics.setRepositoryId(TEST_REPO);
194 logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType());
196 assertEquals(NEW_FILE_COUNT, testedStatistics.getNewFileCount());
197 assertEquals(TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount());
198 assertEquals(endTime, testedStatistics.getScanEndTime());
199 assertEquals(startTime, testedStatistics.getScanStartTime());
200 assertEquals(95954585, testedStatistics.getTotalArtifactFileSize());
201 assertEquals(269, testedStatistics.getTotalArtifactCount());
202 assertEquals(1, testedStatistics.getTotalGroupCount());
203 assertEquals(43, testedStatistics.getTotalProjectCount());
204 assertEquals(1, testedStatistics.getTotalCountForType("zip"));
205 assertEquals(1, testedStatistics.getTotalCountForType("gz"));
206 assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
207 assertEquals(108, testedStatistics.getTotalCountForType("jar"));
208 assertEquals(3, testedStatistics.getTotalCountForType("xml"));
209 assertEquals(2, testedStatistics.getTotalCountForType("war"));
210 assertEquals(144, testedStatistics.getTotalCountForType("pom"));
211 assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
216 private void loadContentIntoRepo( RepositorySession repoSession, String repoId )
217 throws RepositoryException, IOException, MetadataRepositoryException
219 jcrSession = ((JcrRepositorySession) repoSession).getJcrSession();
220 Node n = JcrUtils.getOrAddNode( jcrSession.getRootNode( ), "repositories" );
221 n = JcrUtils.getOrAddNode( n, repoId );
222 n = JcrUtils.getOrAddNode( n, "content" );
223 n = JcrUtils.getOrAddNode( n, "org" );
224 n = JcrUtils.getOrAddNode( n, "apache" );
226 GZIPInputStream inputStream = new GZIPInputStream( getClass( ).getResourceAsStream( "/artifacts.xml.gz" ) );
227 jcrSession.importXML( n.getPath( ), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );