]> source.dussan.org Git - archiva.git/blob
6fe7da2b46654d8ca0796529faa4ab92726131a3
[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.DefaultMetadataResolver;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.jcr.JcrMetadataRepository;
29 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySessionFactory;
30 import org.apache.archiva.metadata.repository.jcr.JcrRepositorySession;
31 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.jackrabbit.commons.JcrUtils;
34 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.test.context.ContextConfiguration;
43
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.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.Calendar;
56 import java.util.Date;
57 import java.util.Map;
58 import java.util.zip.GZIPInputStream;
59
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class JcrRepositoryStatisticsGatheringTest
63     extends TestCase
64 {
65     private static final int TOTAL_FILE_COUNT = 1000;
66
67     private static final int NEW_FILE_COUNT = 500;
68
69     private static final String TEST_REPO = "test-repo";
70
71     static JcrMetadataRepository repository;
72     static JcrRepositorySessionFactory sessionFactory;
73
74     Session jcrSession;
75
76     private static Repository jcrRepository;
77
78     Logger logger = LoggerFactory.getLogger( getClass() );
79
80     @BeforeClass
81     public static void setupSpec()
82         throws IOException, InvalidFileStoreVersionException
83     {
84         Path directory = Paths.get( "target/test-repositories" );
85         if ( Files.exists(directory) )
86         {
87             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
88         }
89         directory = Paths.get( "target/jcr" );
90         if (Files.exists( directory )) {
91             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
92         }
93
94         Map<String, MetadataFacetFactory> factories = AbstractMetadataRepositoryTest.createTestMetadataFacetFactories();
95
96
97         JcrRepositorySessionFactory jcrSessionFactory = new JcrRepositorySessionFactory();
98         jcrSessionFactory.setMetadataResolver(new DefaultMetadataResolver());
99         jcrSessionFactory.setMetadataFacetFactories(factories);
100
101         jcrSessionFactory.open();
102         sessionFactory = jcrSessionFactory;
103         repository = jcrSessionFactory.getMetadataRepository();
104     }
105
106
107     @AfterClass
108     public static void stopSpec() {
109         try
110         {
111             repository.close();
112         }
113         catch ( MetadataRepositoryException e )
114         {
115             e.printStackTrace( );
116         }
117         sessionFactory.close();
118     }
119
120
121     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
122         throws RepositoryException
123     {
124         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
125         nodeType.setMixin( true );
126         nodeType.setName( type );
127         nodeTypeManager.registerNodeType( nodeType, false );
128     }
129
130     @After
131     public void tearDown()
132         throws Exception
133     {
134         if ( repository != null )
135         {
136             try
137             {
138                 repository.close( );
139             } catch (Throwable e) {
140                 //
141             }
142         }
143         if (sessionFactory!=null) {
144             try
145             {
146                 sessionFactory.close( );
147             } catch (Throwable e) {
148                 //
149             }
150         }
151         super.tearDown();
152
153     }
154
155     @Test
156     public void testJcrStatisticsQuery()
157         throws Exception
158     {
159         try(RepositorySession repSession = sessionFactory.createSession()) {
160             Calendar cal = Calendar.getInstance();
161             Date endTime = cal.getTime();
162             cal.add(Calendar.HOUR, -1);
163             Date startTime = cal.getTime();
164
165             loadContentIntoRepo(repSession, TEST_REPO);
166             loadContentIntoRepo( repSession, "another-repo");
167
168             DefaultRepositoryStatistics testedStatistics = new DefaultRepositoryStatistics();
169             testedStatistics.setNewFileCount(NEW_FILE_COUNT);
170             testedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
171             testedStatistics.setScanStartTime(startTime);
172             testedStatistics.setScanEndTime(endTime);
173
174             repository.populateStatistics(repSession, repository, TEST_REPO, testedStatistics);
175
176             DefaultRepositoryStatistics expectedStatistics = new DefaultRepositoryStatistics();
177             expectedStatistics.setNewFileCount(NEW_FILE_COUNT);
178             expectedStatistics.setTotalFileCount(TOTAL_FILE_COUNT);
179             expectedStatistics.setScanEndTime(endTime);
180             expectedStatistics.setScanStartTime(startTime);
181             expectedStatistics.setTotalArtifactFileSize(95954585);
182             expectedStatistics.setTotalArtifactCount(269);
183             expectedStatistics.setTotalGroupCount(1);
184             expectedStatistics.setTotalProjectCount(43);
185             expectedStatistics.setTotalCountForType("zip", 1);
186             expectedStatistics.setTotalCountForType("gz", 1); // FIXME: should be tar.gz
187             expectedStatistics.setTotalCountForType("java-source", 10);
188             expectedStatistics.setTotalCountForType("jar", 108);
189             expectedStatistics.setTotalCountForType("xml", 3);
190             expectedStatistics.setTotalCountForType("war", 2);
191             expectedStatistics.setTotalCountForType("pom", 144);
192             expectedStatistics.setRepositoryId(TEST_REPO);
193
194             logger.info("getTotalCountForType: {}", testedStatistics.getTotalCountForType());
195
196             assertEquals(NEW_FILE_COUNT, testedStatistics.getNewFileCount());
197             assertEquals(TOTAL_FILE_COUNT, testedStatistics.getTotalFileCount());
198             assertEquals(endTime, testedStatistics.getScanEndTime());
199             assertEquals(startTime, testedStatistics.getScanStartTime());
200             assertEquals(95954585, testedStatistics.getTotalArtifactFileSize());
201             assertEquals(269, testedStatistics.getTotalArtifactCount());
202             assertEquals(1, testedStatistics.getTotalGroupCount());
203             assertEquals(43, testedStatistics.getTotalProjectCount());
204             assertEquals(1, testedStatistics.getTotalCountForType("zip"));
205             assertEquals(1, testedStatistics.getTotalCountForType("gz"));
206             assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
207             assertEquals(108, testedStatistics.getTotalCountForType("jar"));
208             assertEquals(3, testedStatistics.getTotalCountForType("xml"));
209             assertEquals(2, testedStatistics.getTotalCountForType("war"));
210             assertEquals(144, testedStatistics.getTotalCountForType("pom"));
211             assertEquals(10, testedStatistics.getTotalCountForType("java-source"));
212
213         }
214     }
215
216     private void loadContentIntoRepo( RepositorySession repoSession, String repoId )
217         throws RepositoryException, IOException, MetadataRepositoryException
218     {
219             jcrSession = ((JcrRepositorySession) repoSession).getJcrSession();
220         Node n = JcrUtils.getOrAddNode( jcrSession.getRootNode( ), "repositories" );
221         n = JcrUtils.getOrAddNode( n, repoId );
222         n = JcrUtils.getOrAddNode( n, "content" );
223         n = JcrUtils.getOrAddNode( n, "org" );
224         n = JcrUtils.getOrAddNode( n, "apache" );
225
226         GZIPInputStream inputStream = new GZIPInputStream( getClass( ).getResourceAsStream( "/artifacts.xml.gz" ) );
227         jcrSession.importXML( n.getPath( ), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
228         jcrSession.save( );
229     }
230 }