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