]> source.dussan.org Git - archiva.git/blob
b8105daeca2353b9a19fd416c0a1e61516cd47df
[archiva.git] /
1 package org.apache.archiva.metadata.repository.stats;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
27
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;
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.Calendar;
40 import java.util.Date;
41 import java.util.zip.GZIPInputStream;
42
43 import static org.mockito.Mockito.*;
44
45 public class JcrRepositoryStatisticsGatheringTest
46     extends TestCase
47 {
48     private static final int TOTAL_FILE_COUNT = 1000;
49
50     private static final int NEW_FILE_COUNT = 500;
51
52     private static final String TEST_REPO = "test-repo";
53
54     private RepositoryStatisticsManager repositoryStatisticsManager;
55
56     private MetadataRepository metadataRepository;
57
58     private Session session;
59
60     @Override
61     protected void setUp()
62         throws Exception
63     {
64         super.setUp();
65
66         File confFile = new File( "src/test/repository.xml" );
67         File dir = new File( "target/jcr" );
68         FileUtils.deleteDirectory( dir );
69
70         TransientRepository repository = new TransientRepository( confFile, dir );
71         session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
72
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/" );
78
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" );
85
86         metadataRepository = mock( MetadataRepository.class );
87         when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
88         when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
89
90         repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
91     }
92
93     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
94         throws RepositoryException
95     {
96         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
97         nodeType.setMixin( true );
98         nodeType.setName( type );
99         nodeTypeManager.registerNodeType( nodeType, false );
100     }
101
102     @Override
103     protected void tearDown()
104         throws Exception
105     {
106         session.logout();
107
108         super.tearDown();
109     }
110
111     public void testJcrStatisticsQuery()
112         throws Exception
113     {
114         Calendar cal = Calendar.getInstance();
115         Date endTime = cal.getTime();
116         cal.add( Calendar.HOUR, -1 );
117         Date startTime = cal.getTime();
118
119         loadContentIntoRepo( TEST_REPO );
120         loadContentIntoRepo( "another-repo" );
121
122         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
123                                                             TOTAL_FILE_COUNT, NEW_FILE_COUNT );
124
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 );
141
142         verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
143     }
144
145     private void loadContentIntoRepo( String repoId )
146         throws RepositoryException, IOException
147     {
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" );
153
154         GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
155         session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
156         session.save();
157     }
158 }