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.RepositorySessionFactory;
26 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
27 import org.apache.archiva.metadata.repository.jcr.RepositoryFactory;
28 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.jackrabbit.commons.JcrUtils;
32 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.test.context.ContextConfiguration;
43 import javax.inject.Inject;
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;
52 import java.io.IOException;
53 import java.util.Calendar;
54 import java.util.Date;
56 import java.util.zip.GZIPInputStream;
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public class JcrRepositoryStatisticsGatheringTest
63 private static final int TOTAL_FILE_COUNT = 1000;
65 private static final int NEW_FILE_COUNT = 500;
67 private static final String TEST_REPO = "test-repo";
69 JcrMetadataRepository repository;
72 private RepositorySessionFactory repositorySessionFactory;
75 private ApplicationContext applicationContext;
79 private static Repository jcrRepository;
81 Logger logger = LoggerFactory.getLogger( getClass() );
84 public static void setupSpec()
85 throws IOException, InvalidFileStoreVersionException
87 File directory = new File( "target/test-repositories" );
88 if ( directory.exists() )
90 FileUtils.deleteDirectory( directory );
92 RepositoryFactory factory = new RepositoryFactory();
93 factory.setRepositoryPath( directory.getPath() );
94 factory.setStoreType( RepositoryFactory.StoreType.IN_MEMORY_TYPE );
95 jcrRepository = factory.createRepository();
104 Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
106 assertNotNull( jcrRepository );
107 // TODO: probably don't need to use Spring for this
108 JcrMetadataRepository jcrMetadataRepository = new JcrMetadataRepository( factories, jcrRepository );
110 session = jcrMetadataRepository.getJcrSession();
114 session = jcrMetadataRepository.getJcrSession();
116 // set up namespaces, etc.
117 JcrMetadataRepository.initialize( session );
119 // removing content is faster than deleting and re-copying the files from target/jcr
120 session.getRootNode().getNode( "repositories" ).remove();
122 catch ( RepositoryException e )
127 this.repository = jcrMetadataRepository;
130 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
131 throws RepositoryException
133 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
134 nodeType.setMixin( true );
135 nodeType.setName( type );
136 nodeTypeManager.registerNodeType( nodeType, false );
140 public void tearDown()
143 if ( repository != null )
151 public void testJcrStatisticsQuery()
154 Calendar cal = Calendar.getInstance();
155 Date endTime = cal.getTime();
156 cal.add( Calendar.HOUR, -1 );
157 Date startTime = cal.getTime();
159 loadContentIntoRepo( TEST_REPO );
160 loadContentIntoRepo( "another-repo" );
162 DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
163 testedStatistics.setNewFileCount( NEW_FILE_COUNT );
164 testedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
165 testedStatistics.setScanStartTime( startTime );
166 testedStatistics.setScanEndTime( endTime );
168 repository.populateStatistics( repository, TEST_REPO, testedStatistics );
170 DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
171 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
172 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
173 expectedStatistics.setScanEndTime( endTime );
174 expectedStatistics.setScanStartTime( startTime );
175 expectedStatistics.setTotalArtifactFileSize( 95954585 );
176 expectedStatistics.setTotalArtifactCount( 269 );
177 expectedStatistics.setTotalGroupCount( 1 );
178 expectedStatistics.setTotalProjectCount( 43 );
179 expectedStatistics.setTotalCountForType( "zip", 1 );
180 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
181 expectedStatistics.setTotalCountForType( "java-source", 10 );
182 expectedStatistics.setTotalCountForType( "jar", 108 );
183 expectedStatistics.setTotalCountForType( "xml", 3 );
184 expectedStatistics.setTotalCountForType( "war", 2 );
185 expectedStatistics.setTotalCountForType( "pom", 144 );
186 expectedStatistics.setRepositoryId( TEST_REPO );
188 logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType() );
190 assertEquals( NEW_FILE_COUNT, testedStatistics.getNewFileCount() );
191 assertEquals( TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount() );
192 assertEquals( endTime, testedStatistics.getScanEndTime() );
193 assertEquals( startTime, testedStatistics.getScanStartTime() );
194 assertEquals( 95954585, testedStatistics.getTotalArtifactFileSize() );
195 assertEquals( 269, testedStatistics.getTotalArtifactCount() );
196 assertEquals( 1, testedStatistics.getTotalGroupCount() );
197 assertEquals( 43, testedStatistics.getTotalProjectCount() );
198 assertEquals( 1, testedStatistics.getTotalCountForType( "zip" ) );
199 assertEquals( 1, testedStatistics.getTotalCountForType( "gz" ) );
200 assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
201 assertEquals( 108, testedStatistics.getTotalCountForType( "jar" ) );
202 assertEquals( 3, testedStatistics.getTotalCountForType( "xml" ) );
203 assertEquals( 2, testedStatistics.getTotalCountForType( "war" ) );
204 assertEquals( 144, testedStatistics.getTotalCountForType( "pom" ) );
205 assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
210 private void loadContentIntoRepo( String repoId )
211 throws RepositoryException, IOException
213 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
214 n = JcrUtils.getOrAddNode( n, repoId );
215 n = JcrUtils.getOrAddNode( n, "content" );
216 n = JcrUtils.getOrAddNode( n, "org" );
217 n = JcrUtils.getOrAddNode( n, "apache" );
219 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
220 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );