]> source.dussan.org Git - archiva.git/blob
838163379e3baa40fd7f576bf2f5e4be24a0cf36
[archiva.git] /
1 package org.apache.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 import org.junit.Before;
31
32 /**
33  * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata. 
34  *
35  * @version $Id$
36  */
37 public abstract class AbstractRepositoryServletProxiedMetadataTestCase
38     extends AbstractRepositoryServletProxiedTestCase
39 {
40     protected RemoteRepoInfo remotePrivateSnapshots;
41
42     protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
43         throws Exception
44     {
45         DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
46         if ( !detailedDiff.similar() )
47         {
48             // If it isn't similar, dump the difference.
49             assertEquals( expectedMetadata, actualMetadata );
50         }
51         // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
52     }
53
54     protected String requestMetadataOK( String path )
55         throws Exception
56     {
57         // process the response code later, not via an exception.
58         HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
59
60         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
61         WebResponse response = getServletUnitClient().getResponse( request );
62         assertResponseOK( response );
63         return response.getText();
64     }
65
66     protected String createVersionMetadata( String groupId, String artifactId, String version )
67     {
68         return createVersionMetadata( groupId, artifactId, version, null, null, null );
69     }
70
71     protected String createVersionMetadata( String groupId, String artifactId, String version, String timestamp,
72                                           String buildNumber, String lastUpdated )
73     {
74         StringBuffer buf = new StringBuffer();
75
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" );
81
82         boolean hasSnapshot = StringUtils.isNotBlank( timestamp ) || StringUtils.isNotBlank( buildNumber );
83         boolean hasLastUpdated = StringUtils.isNotBlank( lastUpdated );
84
85         if ( hasSnapshot || hasLastUpdated )
86         {
87             buf.append( "  <versioning>\n" );
88             if ( hasSnapshot )
89             {
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" );
94             }
95             if ( hasLastUpdated )
96             {
97                 buf.append( "    <lastUpdated>" ).append( lastUpdated ).append( "</lastUpdated>\n" );
98             }
99             buf.append( "  </versioning>\n" );
100         }
101         buf.append( "</metadata>" );
102
103         return buf.toString();
104     }
105
106     protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
107                                           String[] versions )
108     {
109         StringBuffer buf = new StringBuffer();
110
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" );
115
116         boolean hasLatest = StringUtils.isNotBlank( latest );
117         boolean hasRelease = StringUtils.isNotBlank( release );
118         boolean hasVersions = !ArrayUtils.isEmpty( versions );
119
120         if ( hasLatest || hasRelease || hasVersions )
121         {
122             buf.append( "  <versioning>\n" );
123             if ( hasLatest )
124             {
125                 buf.append( "    <latest>" ).append( latest ).append( "</latest>\n" );
126             }
127             if ( hasRelease )
128             {
129                 buf.append( "    <release>" ).append( release ).append( "</release>\n" );
130             }
131             if ( hasVersions )
132             {
133                 buf.append( "    <versions>\n" );
134                 for ( String availVersion : versions )
135                 {
136                     buf.append( "      <version>" ).append( availVersion ).append( "</version>\n" );
137                 }
138                 buf.append( "    </versions>\n" );
139             }
140             buf.append( "  </versioning>\n" );
141         }
142         buf.append( "</metadata>" );
143
144         return buf.toString();
145     }
146
147     protected String createGroupMetadata( String groupId, String[] plugins )
148     {
149         StringBuffer buf = new StringBuffer();
150
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" );
154
155         boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
156
157         if ( hasPlugins )
158         {
159             buf.append( "  <plugins>\n" );
160             for ( String plugin : plugins )
161             {
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" );
167             }
168             buf.append( "  </plugins>\n" );
169         }
170         buf.append( "</metadata>" );
171
172         return buf.toString();
173     }
174
175     protected void setupPrivateSnapshotsRemoteRepo()
176         throws Exception
177     {
178         remotePrivateSnapshots = createServer( "private-snapshots" );
179
180         assertServerSetupCorrectly( remotePrivateSnapshots );
181         archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
182         setupCleanRepo( remotePrivateSnapshots.root );
183     }
184
185 //    private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
186 //                                                   long deltaManagedToRemoteTimestamp )
187 //        throws Exception
188 //    {
189 //        // --- Setup
190 //        setupSnapshotsRemoteRepo();
191 //        setupCleanInternalRepo();
192 //
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"
199 //            + "</metadata>";
200 //        String expectedManagedContents = null;
201 //        File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
202 //
203 //        if ( hasManagedCopy )
204 //        {
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"
207 //                + "</metadata>";
208 //
209 //            File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
210 //            managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
211 //        }
212 //
213 //        setupConnector( REPOID_INTERNAL, remoteSnapshots );
214 //        saveConfiguration();
215 //
216 //        // --- Execution
217 //        // process the response code later, not via an exception.
218 //        HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
219 //
220 //        WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
221 //        WebResponse response = sc.getResponse( request );
222 //
223 //        // --- Verification
224 //
225 //        switch ( expectation )
226 //        {
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() );
234 //                break;
235 //            case EXPECT_REMOTE_CONTENTS:
236 //                assertResponseOK( response );
237 //                assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
238 //                break;
239 //            case EXPECT_NOT_FOUND:
240 //                assertResponseNotFound( response );
241 //                assertManagedFileNotExists( repoRootInternal, resourcePath );
242 //                break;
243 //        }
244 //    }
245
246     @Before
247     public void tearDown()
248         throws Exception
249     {
250         shutdownServer( remotePrivateSnapshots );
251
252         super.tearDown();
253     }
254 }