]> source.dussan.org Git - archiva.git/blob
4ae7aacd87ac4c3e62981d15ea169d6543f3cbf4
[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.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
25 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
27 import org.apache.archiva.metadata.repository.jcr.RepositoryFactory;
28 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.jackrabbit.commons.JcrUtils;
32 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.test.context.ContextConfiguration;
42
43 import javax.inject.Inject;
44 import javax.jcr.ImportUUIDBehavior;
45 import javax.jcr.Node;
46 import javax.jcr.Repository;
47 import javax.jcr.RepositoryException;
48 import javax.jcr.Session;
49 import javax.jcr.nodetype.NodeTypeManager;
50 import javax.jcr.nodetype.NodeTypeTemplate;
51 import java.io.File;
52 import java.io.IOException;
53 import java.util.Calendar;
54 import java.util.Date;
55 import java.util.Map;
56 import java.util.zip.GZIPInputStream;
57
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public class JcrRepositoryStatisticsGatheringTest
61     extends TestCase
62 {
63     private static final int TOTAL_FILE_COUNT = 1000;
64
65     private static final int NEW_FILE_COUNT = 500;
66
67     private static final String TEST_REPO = "test-repo";
68
69     JcrMetadataRepository repository;
70
71     @Inject
72     private RepositorySessionFactory repositorySessionFactory;
73
74     @Inject
75     private ApplicationContext applicationContext;
76
77     Session session;
78
79     private static Repository jcrRepository;
80
81     Logger logger = LoggerFactory.getLogger( getClass() );
82
83     @BeforeClass
84     public static void setupSpec()
85         throws IOException, InvalidFileStoreVersionException
86     {
87         File directory = new File( "target/test-repositories" );
88         if ( directory.exists() )
89         {
90             FileUtils.deleteDirectory( directory );
91         }
92         RepositoryFactory factory = new RepositoryFactory();
93         factory.setRepositoryPath( directory.getPath() );
94         factory.setStoreType( RepositoryFactory.StoreType.IN_MEMORY_TYPE );
95         jcrRepository = factory.createRepository();
96     }
97
98
99     @Before
100     public void setUp()
101         throws Exception
102     {
103
104         Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
105
106         assertNotNull( jcrRepository );
107         // TODO: probably don't need to use Spring for this
108         JcrMetadataRepository jcrMetadataRepository = new JcrMetadataRepository( factories, jcrRepository );
109
110         session = jcrMetadataRepository.getJcrSession();
111
112         try
113         {
114             session = jcrMetadataRepository.getJcrSession();
115
116             // set up namespaces, etc.
117             JcrMetadataRepository.initialize( session );
118
119             // removing content is faster than deleting and re-copying the files from target/jcr
120             session.getRootNode().getNode( "repositories" ).remove();
121         }
122         catch ( RepositoryException e )
123         {
124             // ignore
125         }
126
127         this.repository = jcrMetadataRepository;
128     }
129
130     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
131         throws RepositoryException
132     {
133         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
134         nodeType.setMixin( true );
135         nodeType.setName( type );
136         nodeTypeManager.registerNodeType( nodeType, false );
137     }
138
139     @After
140     public void tearDown()
141         throws Exception
142     {
143         if ( repository != null )
144         {
145             repository.close();
146         }
147
148     }
149
150     @Test
151     public void testJcrStatisticsQuery()
152         throws Exception
153     {
154         Calendar cal = Calendar.getInstance();
155         Date endTime = cal.getTime();
156         cal.add( Calendar.HOUR, -1 );
157         Date startTime = cal.getTime();
158
159         loadContentIntoRepo( TEST_REPO );
160         loadContentIntoRepo( "another-repo" );
161
162         DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
163         testedStatistics.setNewFileCount( NEW_FILE_COUNT );
164         testedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
165         testedStatistics.setScanStartTime( startTime );
166         testedStatistics.setScanEndTime( endTime );
167
168         repository.populateStatistics( repository, TEST_REPO, testedStatistics );
169
170         DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
171         expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
172         expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
173         expectedStatistics.setScanEndTime( endTime );
174         expectedStatistics.setScanStartTime( startTime );
175         expectedStatistics.setTotalArtifactFileSize( 95954585 );
176         expectedStatistics.setTotalArtifactCount( 269 );
177         expectedStatistics.setTotalGroupCount( 1 );
178         expectedStatistics.setTotalProjectCount( 43 );
179         expectedStatistics.setTotalCountForType( "zip", 1 );
180         expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
181         expectedStatistics.setTotalCountForType( "java-source", 10 );
182         expectedStatistics.setTotalCountForType( "jar", 108 );
183         expectedStatistics.setTotalCountForType( "xml", 3 );
184         expectedStatistics.setTotalCountForType( "war", 2 );
185         expectedStatistics.setTotalCountForType( "pom", 144 );
186         expectedStatistics.setRepositoryId( TEST_REPO );
187
188         logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType() );
189
190         assertEquals( NEW_FILE_COUNT, testedStatistics.getNewFileCount() );
191         assertEquals( TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount() );
192         assertEquals( endTime, testedStatistics.getScanEndTime() );
193         assertEquals( startTime, testedStatistics.getScanStartTime() );
194         assertEquals( 95954585, testedStatistics.getTotalArtifactFileSize() );
195         assertEquals( 269, testedStatistics.getTotalArtifactCount() );
196         assertEquals( 1, testedStatistics.getTotalGroupCount() );
197         assertEquals( 43, testedStatistics.getTotalProjectCount() );
198         assertEquals( 1, testedStatistics.getTotalCountForType( "zip" ) );
199         assertEquals( 1, testedStatistics.getTotalCountForType( "gz" ) );
200         assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
201         assertEquals( 108, testedStatistics.getTotalCountForType( "jar" ) );
202         assertEquals( 3, testedStatistics.getTotalCountForType( "xml" ) );
203         assertEquals( 2, testedStatistics.getTotalCountForType( "war" ) );
204         assertEquals( 144, testedStatistics.getTotalCountForType( "pom" ) );
205         assertEquals( 10, testedStatistics.getTotalCountForType( "java-source" ) );
206
207
208     }
209
210     private void loadContentIntoRepo( String repoId )
211         throws RepositoryException, IOException
212     {
213         Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
214         n = JcrUtils.getOrAddNode( n, repoId );
215         n = JcrUtils.getOrAddNode( n, "content" );
216         n = JcrUtils.getOrAddNode( n, "org" );
217         n = JcrUtils.getOrAddNode( n, "apache" );
218
219         GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
220         session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
221         session.save();
222     }
223 }