]> source.dussan.org Git - archiva.git/blob
6094a631c7a2ef135ffc964582ceadbfac9670f6
[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.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;
33
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;
44 import java.io.File;
45 import java.io.IOException;
46 import java.util.Calendar;
47 import java.util.Date;
48 import java.util.zip.GZIPInputStream;
49
50 import static org.mockito.Mockito.*;
51
52 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
53 public class JcrRepositoryStatisticsGatheringTest
54     extends TestCase
55 {
56     private static final int TOTAL_FILE_COUNT = 1000;
57
58     private static final int NEW_FILE_COUNT = 500;
59
60     private static final String TEST_REPO = "test-repo";
61
62     private RepositoryStatisticsManager repositoryStatisticsManager;
63
64     private MetadataRepository metadataRepository;
65
66     @Inject
67     private RepositorySessionFactory repositorySessionFactory;
68
69     private Session session;
70
71     @Override
72     @Before
73     public void setUp()
74         throws Exception
75     {
76         super.setUp();
77
78         File confFile = new File( "src/test/repository.xml" );
79         File dir = new File( "target/jcr" );
80         FileUtils.deleteDirectory( dir );
81
82         assertTrue( confFile.exists() );
83         assertFalse( dir.exists() );
84
85         RepositoryFactory repositoryFactory = new RepositoryFactory();
86         repositoryFactory.setRepositoryPath( dir.getPath() );
87         session = repositoryFactory.createRepository().login(new SimpleCredentials( "admin", "admin".toCharArray()));
88
89
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/" );
95
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" );
102
103         metadataRepository = mock( MetadataRepository.class );
104         when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
105         when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
106
107         repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
108     }
109
110     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
111         throws RepositoryException
112     {
113         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
114         nodeType.setMixin( true );
115         nodeType.setName( type );
116         nodeTypeManager.registerNodeType( nodeType, false );
117     }
118
119     @Override
120     @After
121     public void tearDown()
122         throws Exception
123     {
124         if ( session != null )
125         {
126             session.logout();
127         }
128
129         super.tearDown();
130     }
131
132     @Test
133     public void testJcrStatisticsQuery()
134         throws Exception
135     {
136         Calendar cal = Calendar.getInstance();
137         Date endTime = cal.getTime();
138         cal.add( Calendar.HOUR, -1 );
139         Date startTime = cal.getTime();
140
141         loadContentIntoRepo( TEST_REPO );
142         loadContentIntoRepo( "another-repo" );
143
144         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
145                                                             TOTAL_FILE_COUNT, NEW_FILE_COUNT );
146
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 );
164
165         verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
166     }
167
168     private void loadContentIntoRepo( String repoId )
169         throws RepositoryException, IOException
170     {
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" );
176
177         GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
178         session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
179         session.save();
180     }
181 }