]> source.dussan.org Git - archiva.git/blob
9e51faeb9d9a49d1ed429a67817cdc4050246eab
[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 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;
47
48 import static org.mockito.Mockito.*;
49 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
50 public class JcrRepositoryStatisticsGatheringTest
51     extends TestCase
52 {
53     private static final int TOTAL_FILE_COUNT = 1000;
54
55     private static final int NEW_FILE_COUNT = 500;
56
57     private static final String TEST_REPO = "test-repo";
58
59     private RepositoryStatisticsManager repositoryStatisticsManager;
60
61     private MetadataRepository metadataRepository;
62
63     private Session session;
64
65     @Override
66     @Before
67     public void setUp()
68         throws Exception
69     {
70         super.setUp();
71
72         File confFile = new File( "src/test/repository.xml" );
73         File dir = new File( "target/jcr" );
74         FileUtils.deleteDirectory( dir );
75
76         TransientRepository repository = new TransientRepository( confFile, dir );
77         session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
78
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/" );
84
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" );
91
92         metadataRepository = mock( MetadataRepository.class );
93         when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
94         when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
95
96         repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
97     }
98
99     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
100         throws RepositoryException
101     {
102         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
103         nodeType.setMixin( true );
104         nodeType.setName( type );
105         nodeTypeManager.registerNodeType( nodeType, false );
106     }
107
108     @Override
109     @After
110     public void tearDown()
111         throws Exception
112     {
113         session.logout();
114
115         super.tearDown();
116     }
117
118     @Test
119     public void testJcrStatisticsQuery()
120         throws Exception
121     {
122         Calendar cal = Calendar.getInstance();
123         Date endTime = cal.getTime();
124         cal.add( Calendar.HOUR, -1 );
125         Date startTime = cal.getTime();
126
127         loadContentIntoRepo( TEST_REPO );
128         loadContentIntoRepo( "another-repo" );
129
130         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
131                                                             TOTAL_FILE_COUNT, NEW_FILE_COUNT );
132
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 );
149
150         verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
151     }
152
153     private void loadContentIntoRepo( String repoId )
154         throws RepositoryException, IOException
155     {
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" );
161
162         GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
163         session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
164         session.save();
165     }
166 }