1 package org.apache.archiva.webdav;
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 com.meterware.httpunit.GetMethodWebRequest;
23 import com.meterware.httpunit.HttpUnitOptions;
24 import com.meterware.httpunit.WebRequest;
25 import com.meterware.httpunit.WebResponse;
26 import org.apache.commons.lang.ArrayUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.custommonkey.xmlunit.DetailedDiff;
29 import org.custommonkey.xmlunit.Diff;
30 import org.junit.Before;
33 * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata.
37 public abstract class AbstractRepositoryServletProxiedMetadataTestCase
38 extends AbstractRepositoryServletProxiedTestCase
40 protected RemoteRepoInfo remotePrivateSnapshots;
42 protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
45 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
46 if ( !detailedDiff.similar() )
48 // If it isn't similar, dump the difference.
49 assertEquals( expectedMetadata, actualMetadata );
51 // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
54 protected String requestMetadataOK( String path )
57 // process the response code later, not via an exception.
58 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
60 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
61 WebResponse response = getServletUnitClient().getResponse( request );
62 assertResponseOK( response );
63 return response.getText();
66 protected String createVersionMetadata( String groupId, String artifactId, String version )
68 return createVersionMetadata( groupId, artifactId, version, null, null, null );
71 protected String createVersionMetadata( String groupId, String artifactId, String version, String timestamp,
72 String buildNumber, String lastUpdated )
74 StringBuffer buf = new StringBuffer();
76 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
77 buf.append( "<metadata>\n" );
78 buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
79 buf.append( " <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
80 buf.append( " <version>" ).append( version ).append( "</version>\n" );
82 boolean hasSnapshot = StringUtils.isNotBlank( timestamp ) || StringUtils.isNotBlank( buildNumber );
83 boolean hasLastUpdated = StringUtils.isNotBlank( lastUpdated );
85 if ( hasSnapshot || hasLastUpdated )
87 buf.append( " <versioning>\n" );
90 buf.append( " <snapshot>\n" );
91 buf.append( " <buildNumber>" ).append( buildNumber ).append( "</buildNumber>\n" );
92 buf.append( " <timestamp>" ).append( timestamp ).append( "</timestamp>\n" );
93 buf.append( " </snapshot>\n" );
97 buf.append( " <lastUpdated>" ).append( lastUpdated ).append( "</lastUpdated>\n" );
99 buf.append( " </versioning>\n" );
101 buf.append( "</metadata>" );
103 return buf.toString();
106 protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
109 StringBuffer buf = new StringBuffer();
111 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
112 buf.append( "<metadata>\n" );
113 buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
114 buf.append( " <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
116 boolean hasLatest = StringUtils.isNotBlank( latest );
117 boolean hasRelease = StringUtils.isNotBlank( release );
118 boolean hasVersions = !ArrayUtils.isEmpty( versions );
120 if ( hasLatest || hasRelease || hasVersions )
122 buf.append( " <versioning>\n" );
125 buf.append( " <latest>" ).append( latest ).append( "</latest>\n" );
129 buf.append( " <release>" ).append( release ).append( "</release>\n" );
133 buf.append( " <versions>\n" );
134 for ( String availVersion : versions )
136 buf.append( " <version>" ).append( availVersion ).append( "</version>\n" );
138 buf.append( " </versions>\n" );
140 buf.append( " </versioning>\n" );
142 buf.append( "</metadata>" );
144 return buf.toString();
147 protected String createGroupMetadata( String groupId, String[] plugins )
149 StringBuffer buf = new StringBuffer();
151 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
152 buf.append( "<metadata>\n" );
153 buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
155 boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
159 buf.append( " <plugins>\n" );
160 for ( String plugin : plugins )
162 buf.append( " <plugin>\n" );
163 buf.append( " <prefix>" ).append( plugin ).append( "</prefix>\n" );
164 buf.append( " <artifactId>" ).append( plugin + "-maven-plugin" ).append( "</artifactId>\n" );
165 buf.append( " <name>" ).append( "The " + plugin + " Plugin" ).append( "</name>\n" );
166 buf.append( " </plugin>\n" );
168 buf.append( " </plugins>\n" );
170 buf.append( "</metadata>" );
172 return buf.toString();
175 protected void setupPrivateSnapshotsRemoteRepo()
178 remotePrivateSnapshots = createServer( "private-snapshots" );
180 assertServerSetupCorrectly( remotePrivateSnapshots );
181 archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
182 setupCleanRepo( remotePrivateSnapshots.root );
185 // private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
186 // long deltaManagedToRemoteTimestamp )
190 // setupSnapshotsRemoteRepo();
191 // setupCleanInternalRepo();
193 // String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/maven-metadata.xml";
194 // String expectedRemoteContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
195 // + " <groupId>org.apache.maven.plugins</groupId>\n" + " <artifactId>maven-assembly-plugin</artifactId>\n"
196 // + " <version>2.2-beta-2-SNAPSHOT</version>\n" + " <versioning>\n" + " <snapshot>\n"
197 // + " <timestamp>20071017.162810</timestamp>\n" + " <buildNumber>20</buildNumber>\n"
198 // + " </snapshot>\n" + " <lastUpdated>20071017162814</lastUpdated>\n" + " </versioning>\n"
200 // String expectedManagedContents = null;
201 // File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
203 // if ( hasManagedCopy )
205 // expectedManagedContents = "<metadata>\n" + " <groupId>org.apache.maven.plugins</groupId>\n"
206 // + " <artifactId>maven-assembly-plugin</artifactId>\n" + " <version>2.2-beta-2-SNAPSHOT</version>\n"
209 // File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
210 // managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
213 // setupConnector( REPOID_INTERNAL, remoteSnapshots );
214 // saveConfiguration();
217 // // process the response code later, not via an exception.
218 // HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
220 // WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
221 // WebResponse response = sc.getResponse( request );
223 // // --- Verification
225 // switch ( expectation )
227 // case EXPECT_MANAGED_CONTENTS:
228 // assertResponseOK( response );
229 // assertTrue( "Invalid Test Case: Can't expect managed contents with "
230 // + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
231 // String actualContents = response.getText();
232 // XMLAssert.assertXMLEqual( expectedManagedContents, actualContents );
233 // // assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
235 // case EXPECT_REMOTE_CONTENTS:
236 // assertResponseOK( response );
237 // assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
239 // case EXPECT_NOT_FOUND:
240 // assertResponseNotFound( response );
241 // assertManagedFileNotExists( repoRootInternal, resourcePath );
247 public void tearDown()
250 shutdownServer( remotePrivateSnapshots );