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.codehaus.plexus.PlexusTestCase;
29 import org.easymock.MockControl;
31 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
34 import java.util.Collections;
35 import java.util.List;
39 * RepositoryContentConsumerUtilTest
41 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44 public class RepositoryContentConsumerUtilTest
45 extends AbstractRepositoryLayerTestCase
47 private RepositoryContentConsumers lookupRepositoryConsumerUtil()
50 RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
52 assertNotNull( "RepositoryContentConsumerUtil should not be null.", consumerUtil );
56 public void testGetSelectedIds()
59 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
61 List knownConsumers = consumerutil.getSelectedKnownConsumerIds();
62 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
63 assertEquals( "Known Consumer IDs.size", 9, knownConsumers.size() );
65 List invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
66 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
67 assertEquals( "Invalid Consumer IDs.size", 1, invalidConsumers.size() );
70 public void testGetSelectedConsumersMaps()
73 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
75 Map knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
76 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
77 assertEquals( "Known Consumer Map.size", 1, knownConsumerMap.size() );
79 Object o = knownConsumerMap.get( "sample-known" );
80 assertNotNull( "Known[sample-known] should not be null.", o );
81 assertInstanceof( "Known[sample-known]", RepositoryContentConsumer.class, o );
82 assertInstanceof( "Known[sample-known]", KnownRepositoryContentConsumer.class, o );
84 Map invalidConsumerMap = consumerutil.getSelectedInvalidConsumersMap();
85 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
86 assertEquals( "Invalid Consumer Map.size", 0, invalidConsumerMap.size() );
89 private void assertInstanceof( String msg, Class clazz, Object o )
91 if ( clazz.isInstance( o ) == false )
93 fail( msg + ": Object [" + o.getClass().getName() + "] should have been an instanceof [" + clazz.getName() +
98 public void testGetAvailableLists()
101 RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
103 List knownConsumers = consumerutil.getAvailableKnownConsumers();
104 assertNotNull( "known consumers should not be null.", knownConsumers );
105 assertEquals( "known consumers", 1, knownConsumers.size() );
106 assertInstanceof( "Available Known Consumers", RepositoryContentConsumer.class, knownConsumers.get( 0 ) );
108 List invalidConsumers = consumerutil.getAvailableInvalidConsumers();
109 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
110 assertEquals( "invalid consumers", 0, invalidConsumers.size() );
113 public void testExecution()
116 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
117 RepositoryContentConsumers consumers = lookupRepositoryConsumerUtil();
118 KnownRepositoryContentConsumer knownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
119 consumers.setAvailableKnownConsumers( Collections.singletonList( knownConsumer ) );
121 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
122 InvalidRepositoryContentConsumer invalidConsumer = (InvalidRepositoryContentConsumer) invalidControl.getMock();
123 consumers.setAvailableInvalidConsumers( Collections.singletonList( invalidConsumer ) );
125 ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
126 File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
128 knownConsumer.beginScan( repo );
129 knownConsumer.getExcludes();
130 knownControl.setReturnValue( Collections.EMPTY_LIST );
131 knownConsumer.getIncludes();
132 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
133 knownConsumer.processFile( _OS("path/to/test-file.txt") );
134 // knownConsumer.completeScan();
135 knownControl.replay();
137 invalidConsumer.beginScan( repo );
138 // invalidConsumer.completeScan();
139 invalidControl.replay();
141 consumers.executeConsumers( repo, testFile );
143 knownControl.verify();
144 invalidControl.verify();
146 knownControl.reset();
147 invalidControl.reset();
149 File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
151 knownConsumer.beginScan( repo );
152 knownConsumer.getExcludes();
153 knownControl.setReturnValue( Collections.EMPTY_LIST );
154 knownConsumer.getIncludes();
155 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
156 // knownConsumer.completeScan();
157 knownControl.replay();
159 invalidConsumer.beginScan( repo );
160 invalidConsumer.processFile( _OS("path/to/test-file.xml") );
161 invalidConsumer.getId();
162 invalidControl.setReturnValue( "invalid" );
163 // invalidConsumer.completeScan();
164 invalidControl.replay();
166 consumers.executeConsumers( repo, notIncludedTestFile );
168 knownControl.verify();
169 invalidControl.verify();
171 knownControl.reset();
172 invalidControl.reset();
174 File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
176 knownConsumer.beginScan( repo );
177 knownConsumer.getExcludes();
178 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
179 // knownConsumer.completeScan();
180 knownControl.replay();
182 invalidConsumer.beginScan( repo );
183 invalidConsumer.processFile( _OS("path/to/test-file.txt") );
184 invalidConsumer.getId();
185 invalidControl.setReturnValue( "invalid" );
186 // invalidConsumer.completeScan();
187 invalidControl.replay();
189 consumers.executeConsumers( repo, excludedTestFile );
191 knownControl.verify();
192 invalidControl.verify();
196 * Create an OS specific version of the filepath.
197 * Provide path in unix "/" format.
199 private String _OS( String path )
201 if ( SystemUtils.IS_OS_WINDOWS )
203 return path.replace( '/', '\\' );