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