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.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
42 import javax.jcr.ImportUUIDBehavior;
43 import javax.jcr.Node;
44 import javax.jcr.Repository;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.Session;
47 import javax.jcr.nodetype.NodeTypeManager;
48 import javax.jcr.nodetype.NodeTypeTemplate;
50 import java.io.IOException;
51 import java.util.Calendar;
52 import java.util.Date;
54 import java.util.zip.GZIPInputStream;
56 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
57 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
58 public class JcrRepositoryStatisticsGatheringTest extends TestCase
60 private static final int TOTAL_FILE_COUNT = 1000;
62 private static final int NEW_FILE_COUNT = 500;
64 private static final String TEST_REPO = "test-repo";
66 JcrMetadataRepository repository;
69 private RepositorySessionFactory repositorySessionFactory;
72 private ApplicationContext applicationContext;
77 private static Repository jcrRepository;
80 public static void setupSpec() throws IOException, InvalidFileStoreVersionException
82 File directory = new File( "target/test-repositories" );
83 if ( directory.exists() )
85 FileUtils.deleteDirectory( directory );
87 RepositoryFactory factory = new RepositoryFactory();
88 factory.setRepositoryPath( directory.getPath() );
89 factory.setStoreType( RepositoryFactory.StoreType.IN_MEMORY_TYPE );
90 jcrRepository = factory.createRepository();
99 Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
101 assertNotNull( jcrRepository );
102 // TODO: probably don't need to use Spring for this
103 JcrMetadataRepository jcrMetadataRepository = new JcrMetadataRepository( factories, jcrRepository );
106 session = jcrMetadataRepository.getJcrSession();
110 session = jcrMetadataRepository.getJcrSession();
112 // set up namespaces, etc.
113 JcrMetadataRepository.initialize( session );
115 // removing content is faster than deleting and re-copying the files from target/jcr
116 session.getRootNode().getNode( "repositories" ).remove();
118 catch ( RepositoryException e )
123 this.repository = jcrMetadataRepository;
126 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
127 throws RepositoryException
129 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
130 nodeType.setMixin( true );
131 nodeType.setName( type );
132 nodeTypeManager.registerNodeType( nodeType, false );
136 public void tearDown()
139 if (repository!=null)
147 public void testJcrStatisticsQuery()
150 Calendar cal = Calendar.getInstance();
151 Date endTime = cal.getTime();
152 cal.add( Calendar.HOUR, -1 );
153 Date startTime = cal.getTime();
155 loadContentIntoRepo( TEST_REPO );
156 loadContentIntoRepo( "another-repo" );
158 DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
159 testedStatistics.setNewFileCount( NEW_FILE_COUNT );
160 testedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
161 testedStatistics.setScanStartTime( startTime );
162 testedStatistics.setScanEndTime( endTime );
164 repository.populateStatistics( repository, TEST_REPO, testedStatistics );
166 DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
167 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
168 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
169 expectedStatistics.setScanEndTime( endTime );
170 expectedStatistics.setScanStartTime( startTime );
171 expectedStatistics.setTotalArtifactFileSize( 95954585 );
172 expectedStatistics.setTotalArtifactCount( 269 );
173 expectedStatistics.setTotalGroupCount( 1 );
174 expectedStatistics.setTotalProjectCount( 43 );
175 expectedStatistics.setTotalCountForType( "zip", 1 );
176 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
177 expectedStatistics.setTotalCountForType( "java-source", 10 );
178 expectedStatistics.setTotalCountForType( "jar", 108 );
179 expectedStatistics.setTotalCountForType( "xml", 3 );
180 expectedStatistics.setTotalCountForType( "war", 2 );
181 expectedStatistics.setTotalCountForType( "pom", 144 );
182 expectedStatistics.setRepositoryId( TEST_REPO );
184 System.out.println(testedStatistics.getTotalCountForType());
186 assertEquals( NEW_FILE_COUNT, testedStatistics.getNewFileCount());
187 assertEquals( TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount() );
188 assertEquals( endTime, testedStatistics.getScanEndTime() );
189 assertEquals( startTime, testedStatistics.getScanStartTime() );
190 assertEquals( 95954585, testedStatistics.getTotalArtifactFileSize() );
191 assertEquals( 269, testedStatistics.getTotalArtifactCount() );
192 assertEquals( 1, testedStatistics.getTotalGroupCount() );
193 assertEquals( 43, testedStatistics.getTotalProjectCount() );
194 assertEquals( 1, testedStatistics.getTotalCountForType( "zip" ) );
195 assertEquals( 1, testedStatistics.getTotalCountForType( "gz" ) );
196 assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
197 assertEquals( 108, testedStatistics.getTotalCountForType( "jar" ) );
198 assertEquals( 3, testedStatistics.getTotalCountForType( "xml" ) );
199 assertEquals( 2, testedStatistics.getTotalCountForType( "war" ) );
200 assertEquals( 144, testedStatistics.getTotalCountForType( "pom" ) );
201 assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
206 private void loadContentIntoRepo( String repoId )
207 throws RepositoryException, IOException
209 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
210 n = JcrUtils.getOrAddNode( n, repoId );
211 n = JcrUtils.getOrAddNode( n, "content" );
212 n = JcrUtils.getOrAddNode( n, "org" );
213 n = JcrUtils.getOrAddNode( n, "apache" );
215 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
216 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );