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.commons.io.FileUtils;
25 import org.apache.jackrabbit.commons.JcrUtils;
26 import org.apache.jackrabbit.core.TransientRepository;
28 import javax.jcr.ImportUUIDBehavior;
29 import javax.jcr.NamespaceRegistry;
30 import javax.jcr.Node;
31 import javax.jcr.RepositoryException;
32 import javax.jcr.Session;
33 import javax.jcr.SimpleCredentials;
34 import javax.jcr.Workspace;
35 import javax.jcr.nodetype.NodeTypeManager;
36 import javax.jcr.nodetype.NodeTypeTemplate;
38 import java.io.IOException;
39 import java.util.Calendar;
40 import java.util.Date;
41 import java.util.zip.GZIPInputStream;
42 import org.apache.archiva.test.ArchivaBlockJUnit4ClassRunner;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
48 import static org.mockito.Mockito.*;
49 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
50 public class JcrRepositoryStatisticsGatheringTest
53 private static final int TOTAL_FILE_COUNT = 1000;
55 private static final int NEW_FILE_COUNT = 500;
57 private static final String TEST_REPO = "test-repo";
59 private RepositoryStatisticsManager repositoryStatisticsManager;
61 private MetadataRepository metadataRepository;
63 private Session session;
72 File confFile = new File( "src/test/repository.xml" );
73 File dir = new File( "target/jcr" );
74 FileUtils.deleteDirectory( dir );
76 TransientRepository repository = new TransientRepository( confFile, dir );
77 session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
79 // TODO: perhaps have an archiva-jcr-utils module shared by these plugins that does this and can contain
80 // structure information
81 Workspace workspace = session.getWorkspace();
82 NamespaceRegistry registry = workspace.getNamespaceRegistry();
83 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
85 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
86 registerMixinNodeType( nodeTypeManager, "archiva:namespace" );
87 registerMixinNodeType( nodeTypeManager, "archiva:project" );
88 registerMixinNodeType( nodeTypeManager, "archiva:projectVersion" );
89 registerMixinNodeType( nodeTypeManager, "archiva:artifact" );
90 registerMixinNodeType( nodeTypeManager, "archiva:facet" );
92 metadataRepository = mock( MetadataRepository.class );
93 when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
94 when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
96 repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
99 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
100 throws RepositoryException
102 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
103 nodeType.setMixin( true );
104 nodeType.setName( type );
105 nodeTypeManager.registerNodeType( nodeType, false );
110 public void tearDown()
119 public void testJcrStatisticsQuery()
122 Calendar cal = Calendar.getInstance();
123 Date endTime = cal.getTime();
124 cal.add( Calendar.HOUR, -1 );
125 Date startTime = cal.getTime();
127 loadContentIntoRepo( TEST_REPO );
128 loadContentIntoRepo( "another-repo" );
130 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
131 TOTAL_FILE_COUNT, NEW_FILE_COUNT );
133 RepositoryStatistics expectedStatistics = new RepositoryStatistics();
134 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
135 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
136 expectedStatistics.setScanEndTime( endTime );
137 expectedStatistics.setScanStartTime( startTime );
138 expectedStatistics.setTotalArtifactFileSize( 95954585 );
139 expectedStatistics.setTotalArtifactCount( 269 );
140 expectedStatistics.setTotalGroupCount( 1 );
141 expectedStatistics.setTotalProjectCount( 43 );
142 expectedStatistics.setTotalCountForType( "zip", 1 );
143 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
144 expectedStatistics.setTotalCountForType( "java-source", 10 );
145 expectedStatistics.setTotalCountForType( "jar", 108 );
146 expectedStatistics.setTotalCountForType( "xml", 3 );
147 expectedStatistics.setTotalCountForType( "war", 2 );
148 expectedStatistics.setTotalCountForType( "pom", 144 );
150 verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
153 private void loadContentIntoRepo( String repoId )
154 throws RepositoryException, IOException
156 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
157 n = JcrUtils.getOrAddNode( n, repoId );
158 n = JcrUtils.getOrAddNode( n, "content" );
159 n = JcrUtils.getOrAddNode( n, "org" );
160 n = JcrUtils.getOrAddNode( n, "apache" );
162 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
163 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );