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.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.jackrabbit.commons.JcrUtils;
27 import org.apache.jackrabbit.core.TransientRepository;
29 import javax.inject.Inject;
30 import javax.jcr.ImportUUIDBehavior;
31 import javax.jcr.NamespaceRegistry;
32 import javax.jcr.Node;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.Session;
35 import javax.jcr.SimpleCredentials;
36 import javax.jcr.Workspace;
37 import javax.jcr.nodetype.NodeTypeManager;
38 import javax.jcr.nodetype.NodeTypeTemplate;
40 import java.io.IOException;
41 import java.util.Calendar;
42 import java.util.Date;
43 import java.util.zip.GZIPInputStream;
45 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
51 import static org.mockito.Mockito.*;
53 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
54 public class JcrRepositoryStatisticsGatheringTest
57 private static final int TOTAL_FILE_COUNT = 1000;
59 private static final int NEW_FILE_COUNT = 500;
61 private static final String TEST_REPO = "test-repo";
63 private RepositoryStatisticsManager repositoryStatisticsManager;
65 private MetadataRepository metadataRepository;
68 private RepositorySessionFactory repositorySessionFactory;
70 private Session session;
79 File confFile = new File( "src/test/repository.xml" );
80 File dir = new File( "target/jcr" );
81 FileUtils.deleteDirectory( dir );
83 assertTrue( confFile.exists() );
84 assertFalse( dir.exists() );
86 TransientRepository repository = new TransientRepository( confFile, dir );
87 session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
89 // TODO: perhaps have an archiva-jcr-utils module shared by these plugins that does this and can contain
90 // structure information
91 Workspace workspace = session.getWorkspace();
92 NamespaceRegistry registry = workspace.getNamespaceRegistry();
93 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
95 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
96 registerMixinNodeType( nodeTypeManager, "archiva:namespace" );
97 registerMixinNodeType( nodeTypeManager, "archiva:project" );
98 registerMixinNodeType( nodeTypeManager, "archiva:projectVersion" );
99 registerMixinNodeType( nodeTypeManager, "archiva:artifact" );
100 registerMixinNodeType( nodeTypeManager, "archiva:facet" );
102 metadataRepository = mock( MetadataRepository.class );
103 when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
104 when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
106 repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
109 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
110 throws RepositoryException
112 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
113 nodeType.setMixin( true );
114 nodeType.setName( type );
115 nodeTypeManager.registerNodeType( nodeType, false );
120 public void tearDown()
123 if ( session != null )
132 public void testJcrStatisticsQuery()
135 Calendar cal = Calendar.getInstance();
136 Date endTime = cal.getTime();
137 cal.add( Calendar.HOUR, -1 );
138 Date startTime = cal.getTime();
140 loadContentIntoRepo( TEST_REPO );
141 loadContentIntoRepo( "another-repo" );
143 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
144 TOTAL_FILE_COUNT, NEW_FILE_COUNT );
146 RepositoryStatistics expectedStatistics = new RepositoryStatistics();
147 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
148 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
149 expectedStatistics.setScanEndTime( endTime );
150 expectedStatistics.setScanStartTime( startTime );
151 expectedStatistics.setTotalArtifactFileSize( 95954585 );
152 expectedStatistics.setTotalArtifactCount( 269 );
153 expectedStatistics.setTotalGroupCount( 1 );
154 expectedStatistics.setTotalProjectCount( 43 );
155 expectedStatistics.setTotalCountForType( "zip", 1 );
156 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
157 expectedStatistics.setTotalCountForType( "java-source", 10 );
158 expectedStatistics.setTotalCountForType( "jar", 108 );
159 expectedStatistics.setTotalCountForType( "xml", 3 );
160 expectedStatistics.setTotalCountForType( "war", 2 );
161 expectedStatistics.setTotalCountForType( "pom", 144 );
162 expectedStatistics.setRepositoryId( TEST_REPO );
164 verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
167 private void loadContentIntoRepo( String repoId )
168 throws RepositoryException, IOException
170 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
171 n = JcrUtils.getOrAddNode( n, repoId );
172 n = JcrUtils.getOrAddNode( n, "content" );
173 n = JcrUtils.getOrAddNode( n, "org" );
174 n = JcrUtils.getOrAddNode( n, "apache" );
176 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
177 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );