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
23 import com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import org.apache.archiva.common.utils.FileUtils;
26 import org.apache.commons.lang3.ArrayUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.custommonkey.xmlunit.DetailedDiff;
29 import org.custommonkey.xmlunit.Diff;
30 import org.junit.Before;
32 import java.nio.file.Paths;
35 * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata.
39 public abstract class AbstractRepositoryServletProxiedMetadataTestCase
40 extends AbstractRepositoryServletProxiedTestCase
42 protected RemoteRepoInfo remotePrivateSnapshots;
44 protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
47 DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
48 if ( !detailedDiff.similar() )
50 // If it isn't similar, dump the difference.
51 assertEquals( expectedMetadata, actualMetadata );
53 // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
56 protected String requestMetadataOK( String path )
59 // process the response code later, not via an exception.
60 //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
62 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
63 WebResponse response = getServletUnitClient().getResponse( request );
64 assertResponseOK( response );
65 return response.getContentAsString();
68 protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
71 StringBuilder buf = new StringBuilder();
73 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
74 buf.append( "<metadata>\n" );
75 buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
76 buf.append( " <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
78 boolean hasLatest = StringUtils.isNotBlank( latest );
79 boolean hasRelease = StringUtils.isNotBlank( release );
80 boolean hasVersions = !ArrayUtils.isEmpty( versions );
82 if ( hasLatest || hasRelease || hasVersions )
84 buf.append( " <versioning>\n" );
87 buf.append( " <latest>" ).append( latest ).append( "</latest>\n" );
91 buf.append( " <release>" ).append( release ).append( "</release>\n" );
95 buf.append( " <versions>\n" );
96 for ( String availVersion : versions )
98 buf.append( " <version>" ).append( availVersion ).append( "</version>\n" );
100 buf.append( " </versions>\n" );
102 buf.append( " </versioning>\n" );
104 buf.append( "</metadata>" );
106 return buf.toString();
109 protected String createGroupMetadata( String groupId, String[] plugins )
111 StringBuilder buf = new StringBuilder();
113 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
114 buf.append( "<metadata>\n" );
115 buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
117 boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
121 buf.append( " <plugins>\n" );
122 for ( String plugin : plugins )
124 buf.append( " <plugin>\n" );
125 buf.append( " <prefix>" ).append( plugin ).append( "</prefix>\n" );
126 buf.append( " <artifactId>" ).append( plugin + "-maven-plugin" ).append( "</artifactId>\n" );
127 buf.append( " <name>" ).append( "The " + plugin + " Plugin" ).append( "</name>\n" );
128 buf.append( " </plugin>\n" );
130 buf.append( " </plugins>\n" );
132 buf.append( "</metadata>" );
134 return buf.toString();
137 protected void setupPrivateSnapshotsRemoteRepo()
140 remotePrivateSnapshots = createServer( "private-snapshots" );
142 assertServerSetupCorrectly( remotePrivateSnapshots );
143 archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
144 setupCleanRepo( remotePrivateSnapshots.root );
147 // private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
148 // long deltaManagedToRemoteTimestamp )
152 // setupSnapshotsRemoteRepo();
153 // setupCleanInternalRepo();
155 // String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/maven-metadata.xml";
156 // String expectedRemoteContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
157 // + " <groupId>org.apache.maven.plugins</groupId>\n" + " <artifactId>maven-assembly-plugin</artifactId>\n"
158 // + " <version>2.2-beta-2-SNAPSHOT</version>\n" + " <versioning>\n" + " <snapshot>\n"
159 // + " <timestamp>20071017.162810</timestamp>\n" + " <buildNumber>20</buildNumber>\n"
160 // + " </snapshot>\n" + " <lastUpdated>20071017162814</lastUpdated>\n" + " </versioning>\n"
162 // String expectedManagedContents = null;
163 // File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
165 // if ( hasManagedCopy )
167 // expectedManagedContents = "<metadata>\n" + " <groupId>org.apache.maven.plugins</groupId>\n"
168 // + " <artifactId>maven-assembly-plugin</artifactId>\n" + " <version>2.2-beta-2-SNAPSHOT</version>\n"
171 // File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
172 // managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
175 // setupConnector( REPOID_INTERNAL, remoteSnapshots );
176 // saveConfiguration();
179 // // process the response code later, not via an exception.
180 // HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
182 // WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
183 // WebResponse response = sc.getResponse( request );
185 // // --- Verification
187 // switch ( expectation )
189 // case EXPECT_MANAGED_CONTENTS:
190 // assertResponseOK( response );
191 // assertTrue( "Invalid Test Case: Can't expect managed contents with "
192 // + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
193 // String actualContents = response.getText();
194 // XMLAssert.assertXMLEqual( expectedManagedContents, actualContents );
195 // // assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
197 // case EXPECT_REMOTE_CONTENTS:
198 // assertResponseOK( response );
199 // assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
201 // case EXPECT_NOT_FOUND:
202 // assertResponseNotFound( response );
203 // assertManagedFileNotExists( repoRootInternal, resourcePath );
210 public void tearDown()
213 shutdownServer( remotePrivateSnapshots );
217 String appserverBase = System.getProperty( "appserver.base" );
218 if ( StringUtils.isNotEmpty( appserverBase ) )
220 FileUtils.deleteDirectory( Paths.get( appserverBase ) );