1 package org.apache.maven.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.SystemUtils;
23 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
25 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
27 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
28 import org.easymock.MockControl;
31 import java.util.Collections;
32 import java.util.List;
36 * RepositoryContentConsumerUtilTest
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class RepositoryContentConsumerUtilTest
42 extends AbstractRepositoryLayerTestCase
44 private RepositoryContentConsumers lookupRepositoryConsumerUtil()
47 RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
49 assertNotNull( "RepositoryContentConsumerUtil should not be null.", consumerUtil );
53 public void testGetSelectedIds()
56 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
58 List knownConsumers = consumerutil.getSelectedKnownConsumerIds();
59 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
60 assertEquals( "Known Consumer IDs.size", 9, knownConsumers.size() );
62 List invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
63 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
64 assertEquals( "Invalid Consumer IDs.size", 1, invalidConsumers.size() );
67 public void testGetSelectedConsumersMaps()
70 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
72 Map knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
73 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
74 assertEquals( "Known Consumer Map.size", 1, knownConsumerMap.size() );
76 Object o = knownConsumerMap.get( "sample-known" );
77 assertNotNull( "Known[sample-known] should not be null.", o );
78 assertInstanceof( "Known[sample-known]", RepositoryContentConsumer.class, o );
79 assertInstanceof( "Known[sample-known]", KnownRepositoryContentConsumer.class, o );
81 Map invalidConsumerMap = consumerutil.getSelectedInvalidConsumersMap();
82 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
83 assertEquals( "Invalid Consumer Map.size", 0, invalidConsumerMap.size() );
86 private void assertInstanceof( String msg, Class clazz, Object o )
88 if ( clazz.isInstance( o ) == false )
90 fail( msg + ": Object [" + o.getClass().getName() + "] should have been an instanceof [" + clazz.getName() +
95 public void testGetAvailableLists()
98 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
100 List knownConsumers = consumerutil.getAvailableKnownConsumers();
101 assertNotNull( "known consumers should not be null.", knownConsumers );
102 assertEquals( "known consumers", 1, knownConsumers.size() );
103 assertInstanceof( "Available Known Consumers", RepositoryContentConsumer.class, knownConsumers.get( 0 ) );
105 List invalidConsumers = consumerutil.getAvailableInvalidConsumers();
106 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
107 assertEquals( "invalid consumers", 0, invalidConsumers.size() );
110 public void testExecution()
113 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
114 RepositoryContentConsumers consumers = lookupRepositoryConsumerUtil();
115 KnownRepositoryContentConsumer knownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
116 consumers.setAvailableKnownConsumers( Collections.singletonList( knownConsumer ) );
118 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
119 InvalidRepositoryContentConsumer invalidConsumer = (InvalidRepositoryContentConsumer) invalidControl.getMock();
120 consumers.setAvailableInvalidConsumers( Collections.singletonList( invalidConsumer ) );
122 ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
123 File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
125 knownConsumer.beginScan( repo );
126 knownConsumer.getExcludes();
127 knownControl.setReturnValue( Collections.EMPTY_LIST );
128 knownConsumer.getIncludes();
129 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
130 knownConsumer.processFile( _OS("path/to/test-file.txt") );
131 // knownConsumer.completeScan();
132 knownControl.replay();
134 invalidConsumer.beginScan( repo );
135 // invalidConsumer.completeScan();
136 invalidControl.replay();
138 consumers.executeConsumers( repo, testFile );
140 knownControl.verify();
141 invalidControl.verify();
143 knownControl.reset();
144 invalidControl.reset();
146 File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
148 knownConsumer.beginScan( repo );
149 knownConsumer.getExcludes();
150 knownControl.setReturnValue( Collections.EMPTY_LIST );
151 knownConsumer.getIncludes();
152 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
153 // knownConsumer.completeScan();
154 knownControl.replay();
156 invalidConsumer.beginScan( repo );
157 invalidConsumer.processFile( _OS("path/to/test-file.xml") );
158 invalidConsumer.getId();
159 invalidControl.setReturnValue( "invalid" );
160 // invalidConsumer.completeScan();
161 invalidControl.replay();
163 consumers.executeConsumers( repo, notIncludedTestFile );
165 knownControl.verify();
166 invalidControl.verify();
168 knownControl.reset();
169 invalidControl.reset();
171 File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
173 knownConsumer.beginScan( repo );
174 knownConsumer.getExcludes();
175 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
176 // knownConsumer.completeScan();
177 knownControl.replay();
179 invalidConsumer.beginScan( repo );
180 invalidConsumer.processFile( _OS("path/to/test-file.txt") );
181 invalidConsumer.getId();
182 invalidControl.setReturnValue( "invalid" );
183 // invalidConsumer.completeScan();
184 invalidControl.replay();
186 consumers.executeConsumers( repo, excludedTestFile );
188 knownControl.verify();
189 invalidControl.verify();
193 * Create an OS specific version of the filepath.
194 * Provide path in unix "/" format.
196 private String _OS( String path )
198 if ( SystemUtils.IS_OS_WINDOWS )
200 return path.replace( '/', '\\' );