View Javadoc

1   package org.apache.maven.archiva.webdav;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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  
31  /**
32   * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata. 
33   *
34   * @version $Id: AbstractRepositoryServletProxiedMetadataTestCase.java 718864 2008-11-19 06:33:35Z brett $
35   */
36  public abstract class AbstractRepositoryServletProxiedMetadataTestCase
37      extends AbstractRepositoryServletProxiedTestCase
38  {
39      protected RemoteRepoInfo remotePrivateSnapshots;
40  
41      protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
42          throws Exception
43      {
44          DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
45          if ( !detailedDiff.similar() )
46          {
47              // If it isn't similar, dump the difference.
48              assertEquals( expectedMetadata, actualMetadata );
49          }
50          // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
51      }
52  
53      protected String requestMetadataOK( String path )
54          throws Exception
55      {
56          // process the response code later, not via an exception.
57          HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
58  
59          WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
60          WebResponse response = sc.getResponse( request );
61          assertResponseOK( response );
62          return response.getText();
63      }
64  
65      protected String createVersionMetadata( String groupId, String artifactId, String version )
66      {
67          return createVersionMetadata( groupId, artifactId, version, null, null, null );
68      }
69  
70      protected String createVersionMetadata( String groupId, String artifactId, String version, String timestamp,
71                                            String buildNumber, String lastUpdated )
72      {
73          StringBuffer buf = new StringBuffer();
74  
75          buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
76          buf.append( "<metadata>\n" );
77          buf.append( "  <groupId>" ).append( groupId ).append( "</groupId>\n" );
78          buf.append( "  <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
79          buf.append( "  <version>" ).append( version ).append( "</version>\n" );
80  
81          boolean hasSnapshot = StringUtils.isNotBlank( timestamp ) || StringUtils.isNotBlank( buildNumber );
82          boolean hasLastUpdated = StringUtils.isNotBlank( lastUpdated );
83  
84          if ( hasSnapshot || hasLastUpdated )
85          {
86              buf.append( "  <versioning>\n" );
87              if ( hasSnapshot )
88              {
89                  buf.append( "    <snapshot>\n" );
90                  buf.append( "      <buildNumber>" ).append( buildNumber ).append( "</buildNumber>\n" );
91                  buf.append( "      <timestamp>" ).append( timestamp ).append( "</timestamp>\n" );
92                  buf.append( "    </snapshot>\n" );
93              }
94              if ( hasLastUpdated )
95              {
96                  buf.append( "    <lastUpdated>" ).append( lastUpdated ).append( "</lastUpdated>\n" );
97              }
98              buf.append( "  </versioning>\n" );
99          }
100         buf.append( "</metadata>" );
101 
102         return buf.toString();
103     }
104 
105     protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
106                                           String[] versions )
107     {
108         StringBuffer buf = new StringBuffer();
109 
110         buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
111         buf.append( "<metadata>\n" );
112         buf.append( "  <groupId>" ).append( groupId ).append( "</groupId>\n" );
113         buf.append( "  <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
114 
115         boolean hasLatest = StringUtils.isNotBlank( latest );
116         boolean hasRelease = StringUtils.isNotBlank( release );
117         boolean hasVersions = !ArrayUtils.isEmpty( versions );
118 
119         if ( hasLatest || hasRelease || hasVersions )
120         {
121             buf.append( "  <versioning>\n" );
122             if ( hasLatest )
123             {
124                 buf.append( "    <latest>" ).append( latest ).append( "</latest>\n" );
125             }
126             if ( hasRelease )
127             {
128                 buf.append( "    <release>" ).append( release ).append( "</release>\n" );
129             }
130             if ( hasVersions )
131             {
132                 buf.append( "    <versions>\n" );
133                 for ( String availVersion : versions )
134                 {
135                     buf.append( "      <version>" ).append( availVersion ).append( "</version>\n" );
136                 }
137                 buf.append( "    </versions>\n" );
138             }
139             buf.append( "  </versioning>\n" );
140         }
141         buf.append( "</metadata>" );
142 
143         return buf.toString();
144     }
145 
146     protected String createGroupMetadata( String groupId, String[] plugins )
147     {
148         StringBuffer buf = new StringBuffer();
149 
150         buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
151         buf.append( "<metadata>\n" );
152         buf.append( "  <groupId>" ).append( groupId ).append( "</groupId>\n" );
153 
154         boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
155 
156         if ( hasPlugins )
157         {
158             buf.append( "  <plugins>\n" );
159             for ( String plugin : plugins )
160             {
161                 buf.append( "    <plugin>\n" );
162                 buf.append( "      <prefix>" ).append( plugin ).append( "</prefix>\n" );
163                 buf.append( "      <artifactId>" ).append( plugin + "-maven-plugin" ).append( "</artifactId>\n" );
164                 buf.append( "      <name>" ).append( "The " + plugin + " Plugin" ).append( "</name>\n" );
165                 buf.append( "    </plugin>\n" );
166             }
167             buf.append( "  </plugins>\n" );
168         }
169         buf.append( "</metadata>" );
170 
171         return buf.toString();
172     }
173 
174     protected void setupPrivateSnapshotsRemoteRepo()
175         throws Exception
176     {
177         remotePrivateSnapshots = createServer( "private-snapshots" );
178 
179         assertServerSetupCorrectly( remotePrivateSnapshots );
180         archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
181         setupCleanRepo( remotePrivateSnapshots.root );
182     }
183 
184 //    private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
185 //                                                   long deltaManagedToRemoteTimestamp )
186 //        throws Exception
187 //    {
188 //        // --- Setup
189 //        setupSnapshotsRemoteRepo();
190 //        setupCleanInternalRepo();
191 //
192 //        String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/maven-metadata.xml";
193 //        String expectedRemoteContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
194 //            + "  <groupId>org.apache.maven.plugins</groupId>\n" + "  <artifactId>maven-assembly-plugin</artifactId>\n"
195 //            + "  <version>2.2-beta-2-SNAPSHOT</version>\n" + "  <versioning>\n" + "    <snapshot>\n"
196 //            + "      <timestamp>20071017.162810</timestamp>\n" + "      <buildNumber>20</buildNumber>\n"
197 //            + "    </snapshot>\n" + "    <lastUpdated>20071017162814</lastUpdated>\n" + "  </versioning>\n"
198 //            + "</metadata>";
199 //        String expectedManagedContents = null;
200 //        File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
201 //
202 //        if ( hasManagedCopy )
203 //        {
204 //            expectedManagedContents = "<metadata>\n" + "  <groupId>org.apache.maven.plugins</groupId>\n"
205 //                + "  <artifactId>maven-assembly-plugin</artifactId>\n" + "  <version>2.2-beta-2-SNAPSHOT</version>\n"
206 //                + "</metadata>";
207 //
208 //            File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
209 //            managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
210 //        }
211 //
212 //        setupConnector( REPOID_INTERNAL, remoteSnapshots );
213 //        saveConfiguration();
214 //
215 //        // --- Execution
216 //        // process the response code later, not via an exception.
217 //        HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
218 //
219 //        WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
220 //        WebResponse response = sc.getResponse( request );
221 //
222 //        // --- Verification
223 //
224 //        switch ( expectation )
225 //        {
226 //            case EXPECT_MANAGED_CONTENTS:
227 //                assertResponseOK( response );
228 //                assertTrue( "Invalid Test Case: Can't expect managed contents with "
229 //                    + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
230 //                String actualContents = response.getText();
231 //                XMLAssert.assertXMLEqual( expectedManagedContents, actualContents );
232 //                // assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
233 //                break;
234 //            case EXPECT_REMOTE_CONTENTS:
235 //                assertResponseOK( response );
236 //                assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
237 //                break;
238 //            case EXPECT_NOT_FOUND:
239 //                assertResponseNotFound( response );
240 //                assertManagedFileNotExists( repoRootInternal, resourcePath );
241 //                break;
242 //        }
243 //    }
244 
245     protected void tearDown()
246         throws Exception
247     {
248         shutdownServer( remotePrivateSnapshots );
249 
250         super.tearDown();
251     }
252 }