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;
43 import static org.mockito.Mockito.*;
45 public class JcrRepositoryStatisticsGatheringTest
48 private static final int TOTAL_FILE_COUNT = 1000;
50 private static final int NEW_FILE_COUNT = 500;
52 private static final String TEST_REPO = "test-repo";
54 private RepositoryStatisticsManager repositoryStatisticsManager;
56 private MetadataRepository metadataRepository;
58 private Session session;
61 protected void setUp()
66 File confFile = new File( "src/test/repository.xml" );
67 File dir = new File( "target/jcr" );
68 FileUtils.deleteDirectory( dir );
70 TransientRepository repository = new TransientRepository( confFile, dir );
71 session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
73 // TODO: perhaps have an archiva-jcr-utils module shared by these plugins that does this and can contain
74 // structure information
75 Workspace workspace = session.getWorkspace();
76 NamespaceRegistry registry = workspace.getNamespaceRegistry();
77 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
79 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
80 registerMixinNodeType( nodeTypeManager, "archiva:namespace" );
81 registerMixinNodeType( nodeTypeManager, "archiva:project" );
82 registerMixinNodeType( nodeTypeManager, "archiva:projectVersion" );
83 registerMixinNodeType( nodeTypeManager, "archiva:artifact" );
84 registerMixinNodeType( nodeTypeManager, "archiva:facet" );
86 metadataRepository = mock( MetadataRepository.class );
87 when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
88 when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
90 repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
93 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
94 throws RepositoryException
96 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
97 nodeType.setMixin( true );
98 nodeType.setName( type );
99 nodeTypeManager.registerNodeType( nodeType, false );
103 protected void tearDown()
111 public void testJcrStatisticsQuery()
114 Calendar cal = Calendar.getInstance();
115 Date endTime = cal.getTime();
116 cal.add( Calendar.HOUR, -1 );
117 Date startTime = cal.getTime();
119 loadContentIntoRepo( TEST_REPO );
120 loadContentIntoRepo( "another-repo" );
122 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
123 TOTAL_FILE_COUNT, NEW_FILE_COUNT );
125 RepositoryStatistics expectedStatistics = new RepositoryStatistics();
126 expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
127 expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
128 expectedStatistics.setScanEndTime( endTime );
129 expectedStatistics.setScanStartTime( startTime );
130 expectedStatistics.setTotalArtifactFileSize( 95954585 );
131 expectedStatistics.setTotalArtifactCount( 269 );
132 expectedStatistics.setTotalGroupCount( 1 );
133 expectedStatistics.setTotalProjectCount( 43 );
134 expectedStatistics.setTotalCountForType( "zip", 1 );
135 expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
136 expectedStatistics.setTotalCountForType( "java-source", 10 );
137 expectedStatistics.setTotalCountForType( "jar", 108 );
138 expectedStatistics.setTotalCountForType( "xml", 3 );
139 expectedStatistics.setTotalCountForType( "war", 2 );
140 expectedStatistics.setTotalCountForType( "pom", 144 );
142 verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
145 private void loadContentIntoRepo( String repoId )
146 throws RepositoryException, IOException
148 Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
149 n = JcrUtils.getOrAddNode( n, repoId );
150 n = JcrUtils.getOrAddNode( n, "content" );
151 n = JcrUtils.getOrAddNode( n, "org" );
152 n = JcrUtils.getOrAddNode( n, "apache" );
154 GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
155 session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );