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.RepositorySession;
26 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
28 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.jcr.RepositoryFactory;
30 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.jackrabbit.commons.JcrUtils;
33 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.inject.Inject;
45 import javax.jcr.ImportUUIDBehavior;
46 import javax.jcr.Node;
47 import javax.jcr.Repository;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Session;
50 import javax.jcr.nodetype.NodeTypeManager;
51 import javax.jcr.nodetype.NodeTypeTemplate;
52 import java.io.IOException;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.nio.file.Paths;
56 import java.util.Calendar;
57 import java.util.Date;
59 import java.util.zip.GZIPInputStream;
61 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
63 public class JcrRepositoryStatisticsGatheringTest
66 private static final int TOTAL_FILE_COUNT = 1000;
68 private static final int NEW_FILE_COUNT = 500;
70 private static final String TEST_REPO = "test-repo";
72 JcrMetadataRepository repository;
73 JcrRepositorySessionFactory sessionFactory;
77 private RepositorySessionFactory repositorySessionFactory;
80 private ApplicationContext applicationContext;
84 private static Repository jcrRepository;
86 Logger logger = LoggerFactory.getLogger( getClass() );
89 public static void setupSpec()
90 throws IOException, InvalidFileStoreVersionException
92 Path directory = Paths.get( "target/test-repositories" );
93 if ( Files.exists(directory) )
95 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
97 RepositoryFactory factory = new RepositoryFactory();
98 factory.setRepositoryPath( directory.toString() );
99 factory.setStoreType( RepositoryFactory.StoreType.IN_MEMORY_TYPE );
100 jcrRepository = factory.createRepository();
109 Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
111 assertNotNull( jcrRepository );
112 // TODO: probably don't need to use Spring for this
113 JcrMetadataRepository jcrMetadataRepository = new JcrMetadataRepository( factories, jcrRepository );
115 jcrSession = jcrMetadataRepository.login();
119 jcrSession = jcrMetadataRepository.login();
121 // set up namespaces, etc.
122 JcrMetadataRepository.initializeNodeTypes(jcrSession);
124 // removing content is faster than deleting and re-copying the files from target/jcr
125 jcrSession.getRootNode().getNode( "repositories" ).remove();
127 catch ( RepositoryException e )
132 this.repository = jcrMetadataRepository;
133 this.sessionFactory = new JcrRepositorySessionFactory();
136 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
137 throws RepositoryException
139 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
140 nodeType.setMixin( true );
141 nodeType.setName( type );
142 nodeTypeManager.registerNodeType( nodeType, false );
146 public void tearDown()
149 if ( repository != null )
157 public void testJcrStatisticsQuery()
160 try(RepositorySession repSession = sessionFactory.createSession()) {
161 Calendar cal = Calendar.getInstance();
162 Date endTime = cal.getTime();
163 cal.add(Calendar.HOUR, -1);
164 Date startTime = cal.getTime();
166 loadContentIntoRepo(TEST_REPO);
167 loadContentIntoRepo("another-repo");
169 DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
170 testedStatistics.setNewFileCount(NEW_FILE_COUNT);
171 testedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
172 testedStatistics.setScanStartTime(startTime);
173 testedStatistics.setScanEndTime(endTime);
175 repository.populateStatistics(repSession, repository, TEST_REPO, testedStatistics);
177 DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
178 expectedStatistics.setNewFileCount(NEW_FILE_COUNT);
179 expectedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
180 expectedStatistics.setScanEndTime(endTime);
181 expectedStatistics.setScanStartTime(startTime);
182 expectedStatistics.setTotalArtifactFileSize(95954585);
183 expectedStatistics.setTotalArtifactCount(269);
184 expectedStatistics.setTotalGroupCount(1);
185 expectedStatistics.setTotalProjectCount(43);
186 expectedStatistics.setTotalCountForType("zip", 1);
187 expectedStatistics.setTotalCountForType("gz", 1); // FIXME: should be tar.gz
188 expectedStatistics.setTotalCountForType("java-source", 10);
189 expectedStatistics.setTotalCountForType("jar", 108);
190 expectedStatistics.setTotalCountForType("xml", 3);
191 expectedStatistics.setTotalCountForType("war", 2);
192 expectedStatistics.setTotalCountForType("pom", 144);
193 expectedStatistics.setRepositoryId(TEST_REPO);
195 logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType());
197 assertEquals(NEW_FILE_COUNT, testedStatistics.getNewFileCount());
198 assertEquals(TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount());
199 assertEquals(endTime, testedStatistics.getScanEndTime());
200 assertEquals(startTime, testedStatistics.getScanStartTime());
201 assertEquals(95954585, testedStatistics.getTotalArtifactFileSize());
202 assertEquals(269, testedStatistics.getTotalArtifactCount());
203 assertEquals(1, testedStatistics.getTotalGroupCount());
204 assertEquals(43, testedStatistics.getTotalProjectCount());
205 assertEquals(1, testedStatistics.getTotalCountForType("zip"));
206 assertEquals(1, testedStatistics.getTotalCountForType("gz"));
207 assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
208 assertEquals(108, testedStatistics.getTotalCountForType("jar"));
209 assertEquals(3, testedStatistics.getTotalCountForType("xml"));
210 assertEquals(2, testedStatistics.getTotalCountForType("war"));
211 assertEquals(144, testedStatistics.getTotalCountForType("pom"));
212 assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
217 private void loadContentIntoRepo( String repoId )
218 throws RepositoryException, IOException
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 );