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.archiva.metadata.repository.jcr.RepositoryFactory;
26 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.jackrabbit.commons.JcrUtils;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
34 import javax.inject.Inject;
35 import javax.jcr.ImportUUIDBehavior;
36 import javax.jcr.NamespaceRegistry;
37 import javax.jcr.Node;
38 import javax.jcr.RepositoryException;
39 import javax.jcr.Session;
40 import javax.jcr.SimpleCredentials;
41 import javax.jcr.Workspace;
42 import javax.jcr.nodetype.NodeTypeManager;
43 import javax.jcr.nodetype.NodeTypeTemplate;
45 import java.io.IOException;
46 import java.util.Calendar;
47 import java.util.Date;
48 import java.util.zip.GZIPInputStream;
50 import static org.mockito.Mockito.*;
52 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
53 public class JcrRepositoryStatisticsGatheringTest
56 private static final int TOTAL_FILE_COUNT = 1000;
58 private static final int NEW_FILE_COUNT = 500;
60 private static final String TEST_REPO = "test-repo";
62 private RepositoryStatisticsManager repositoryStatisticsManager;
64 private MetadataRepository metadataRepository;
67 private RepositorySessionFactory repositorySessionFactory;
69 private Session session;
78 File confFile = new File( "src/test/repository.xml" );
79 File dir = new File( "target/jcr" );
80 FileUtils.deleteDirectory( dir );
82 assertTrue( confFile.exists() );
83 assertFalse( dir.exists() );
85 RepositoryFactory repositoryFactory = new RepositoryFactory();
86 repositoryFactory.setRepositoryPath( dir.getPath() );
87 session = repositoryFactory.createRepository().login(new SimpleCredentials( "admin", "admin".toCharArray()));
90 // TODO: perhaps have an archiva-jcr-utils module shared by these plugins that does this and can contain
91 // structure information
92 Workspace workspace = session.getWorkspace();
93 NamespaceRegistry registry = workspace.getNamespaceRegistry();
94 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
96 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
97 registerMixinNodeType( nodeTypeManager, "archiva:namespace" );
98 registerMixinNodeType( nodeTypeManager, "archiva:project" );
99 registerMixinNodeType( nodeTypeManager, "archiva:projectVersion" );
100 registerMixinNodeType( nodeTypeManager, "archiva:artifact" );
101 registerMixinNodeType( nodeTypeManager, "archiva:facet" );
103 metadataRepository = mock( MetadataRepository.class );
104 when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
105 when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
107 repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
110 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
111 throws RepositoryException
113 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
114 nodeType.setMixin( true );
115 nodeType.setName( type );
116 nodeTypeManager.registerNodeType( nodeType, false );
121 public void tearDown()
124 if ( session != null )
133 public void testJcrStatisticsQuery()
136 Calendar cal = Calendar.getInstance();
137 Date endTime = cal.getTime();
138 cal.add( Calendar.HOUR, -1 );
139 Date startTime = cal.getTime();
141 loadContentIntoRepo( TEST_REPO );
142 loadContentIntoRepo( "another-repo" );
144 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
145 TOTAL_FILE_COUNT, NEW_FILE_COUNT );
147 RepositoryStatistics expectedStatistics = new RepositoryStatistics();
148 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
149 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
150 expectedStatistics.setScanEndTime( endTime );
151 expectedStatistics.setScanStartTime( startTime );
152 expectedStatistics.setTotalArtifactFileSize( 95954585 );
153 expectedStatistics.setTotalArtifactCount( 269 );
154 expectedStatistics.setTotalGroupCount( 1 );
155 expectedStatistics.setTotalProjectCount( 43 );
156 expectedStatistics.setTotalCountForType( "zip", 1 );
157 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
158 expectedStatistics.setTotalCountForType( "java-source", 10 );
159 expectedStatistics.setTotalCountForType( "jar", 108 );
160 expectedStatistics.setTotalCountForType( "xml", 3 );
161 expectedStatistics.setTotalCountForType( "war", 2 );
162 expectedStatistics.setTotalCountForType( "pom", 144 );
163 expectedStatistics.setRepositoryId( TEST_REPO );
165 verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
168 private void loadContentIntoRepo( String repoId )
169 throws RepositoryException, IOException
171 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
172 n = JcrUtils.getOrAddNode( n, repoId );
173 n = JcrUtils.getOrAddNode( n, "content" );
174 n = JcrUtils.getOrAddNode( n, "org" );
175 n = JcrUtils.getOrAddNode( n, "apache" );
177 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
178 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );