You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRepositoryServletProxiedMetadataTestCase.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package org.apache.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import com.gargoylesoftware.htmlunit.WebRequest;
  21. import com.gargoylesoftware.htmlunit.WebResponse;
  22. import org.apache.archiva.common.utils.FileUtils;
  23. import org.apache.commons.lang.ArrayUtils;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.custommonkey.xmlunit.DetailedDiff;
  26. import org.custommonkey.xmlunit.Diff;
  27. import org.junit.Before;
  28. import java.nio.file.Paths;
  29. /**
  30. * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata.
  31. *
  32. *
  33. */
  34. public abstract class AbstractRepositoryServletProxiedMetadataTestCase
  35. extends AbstractRepositoryServletProxiedTestCase
  36. {
  37. protected RemoteRepoInfo remotePrivateSnapshots;
  38. protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
  39. throws Exception
  40. {
  41. DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
  42. if ( !detailedDiff.similar() )
  43. {
  44. // If it isn't similar, dump the difference.
  45. assertEquals( expectedMetadata, actualMetadata );
  46. }
  47. // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
  48. }
  49. protected String requestMetadataOK( String path )
  50. throws Exception
  51. {
  52. // process the response code later, not via an exception.
  53. //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
  54. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
  55. WebResponse response = getServletUnitClient().getResponse( request );
  56. assertResponseOK( response );
  57. return response.getContentAsString();
  58. }
  59. protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
  60. String[] versions )
  61. {
  62. StringBuilder buf = new StringBuilder();
  63. buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
  64. buf.append( "<metadata>\n" );
  65. buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
  66. buf.append( " <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
  67. boolean hasLatest = StringUtils.isNotBlank( latest );
  68. boolean hasRelease = StringUtils.isNotBlank( release );
  69. boolean hasVersions = !ArrayUtils.isEmpty( versions );
  70. if ( hasLatest || hasRelease || hasVersions )
  71. {
  72. buf.append( " <versioning>\n" );
  73. if ( hasLatest )
  74. {
  75. buf.append( " <latest>" ).append( latest ).append( "</latest>\n" );
  76. }
  77. if ( hasRelease )
  78. {
  79. buf.append( " <release>" ).append( release ).append( "</release>\n" );
  80. }
  81. if ( hasVersions )
  82. {
  83. buf.append( " <versions>\n" );
  84. for ( String availVersion : versions )
  85. {
  86. buf.append( " <version>" ).append( availVersion ).append( "</version>\n" );
  87. }
  88. buf.append( " </versions>\n" );
  89. }
  90. buf.append( " </versioning>\n" );
  91. }
  92. buf.append( "</metadata>" );
  93. return buf.toString();
  94. }
  95. protected String createGroupMetadata( String groupId, String[] plugins )
  96. {
  97. StringBuilder buf = new StringBuilder();
  98. buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
  99. buf.append( "<metadata>\n" );
  100. buf.append( " <groupId>" ).append( groupId ).append( "</groupId>\n" );
  101. boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
  102. if ( hasPlugins )
  103. {
  104. buf.append( " <plugins>\n" );
  105. for ( String plugin : plugins )
  106. {
  107. buf.append( " <plugin>\n" );
  108. buf.append( " <prefix>" ).append( plugin ).append( "</prefix>\n" );
  109. buf.append( " <artifactId>" ).append( plugin + "-maven-plugin" ).append( "</artifactId>\n" );
  110. buf.append( " <name>" ).append( "The " + plugin + " Plugin" ).append( "</name>\n" );
  111. buf.append( " </plugin>\n" );
  112. }
  113. buf.append( " </plugins>\n" );
  114. }
  115. buf.append( "</metadata>" );
  116. return buf.toString();
  117. }
  118. protected void setupPrivateSnapshotsRemoteRepo()
  119. throws Exception
  120. {
  121. remotePrivateSnapshots = createServer( "private-snapshots" );
  122. assertServerSetupCorrectly( remotePrivateSnapshots );
  123. archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
  124. setupCleanRepo( remotePrivateSnapshots.root );
  125. }
  126. // private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
  127. // long deltaManagedToRemoteTimestamp )
  128. // throws Exception
  129. // {
  130. // // --- Setup
  131. // setupSnapshotsRemoteRepo();
  132. // setupCleanInternalRepo();
  133. //
  134. // String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/maven-metadata.xml";
  135. // String expectedRemoteContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
  136. // + " <groupId>org.apache.maven.plugins</groupId>\n" + " <artifactId>maven-assembly-plugin</artifactId>\n"
  137. // + " <version>2.2-beta-2-SNAPSHOT</version>\n" + " <versioning>\n" + " <snapshot>\n"
  138. // + " <timestamp>20071017.162810</timestamp>\n" + " <buildNumber>20</buildNumber>\n"
  139. // + " </snapshot>\n" + " <lastUpdated>20071017162814</lastUpdated>\n" + " </versioning>\n"
  140. // + "</metadata>";
  141. // String expectedManagedContents = null;
  142. // File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
  143. //
  144. // if ( hasManagedCopy )
  145. // {
  146. // expectedManagedContents = "<metadata>\n" + " <groupId>org.apache.maven.plugins</groupId>\n"
  147. // + " <artifactId>maven-assembly-plugin</artifactId>\n" + " <version>2.2-beta-2-SNAPSHOT</version>\n"
  148. // + "</metadata>";
  149. //
  150. // File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
  151. // managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
  152. // }
  153. //
  154. // setupConnector( REPOID_INTERNAL, remoteSnapshots );
  155. // saveConfiguration();
  156. //
  157. // // --- Execution
  158. // // process the response code later, not via an exception.
  159. // HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
  160. //
  161. // WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
  162. // WebResponse response = sc.getResponse( request );
  163. //
  164. // // --- Verification
  165. //
  166. // switch ( expectation )
  167. // {
  168. // case EXPECT_MANAGED_CONTENTS:
  169. // assertResponseOK( response );
  170. // assertTrue( "Invalid Test Case: Can't expect managed contents with "
  171. // + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
  172. // String actualContents = response.getText();
  173. // XMLAssert.assertXMLEqual( expectedManagedContents, actualContents );
  174. // // assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
  175. // break;
  176. // case EXPECT_REMOTE_CONTENTS:
  177. // assertResponseOK( response );
  178. // assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
  179. // break;
  180. // case EXPECT_NOT_FOUND:
  181. // assertResponseNotFound( response );
  182. // assertManagedFileNotExists( repoRootInternal, resourcePath );
  183. // break;
  184. // }
  185. // }
  186. @Before
  187. @Override
  188. public void tearDown()
  189. throws Exception
  190. {
  191. shutdownServer( remotePrivateSnapshots );
  192. super.tearDown();
  193. String appserverBase = System.getProperty( "appserver.base" );
  194. if ( StringUtils.isNotEmpty( appserverBase ) )
  195. {
  196. FileUtils.deleteDirectory( Paths.get( appserverBase ) );
  197. }
  198. }
  199. }