]> source.dussan.org Git - archiva.git/blob
47c5d7ebfca7c9cd75d6e8a041540de976ea26d1
[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
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;
31
32 import java.nio.file.Paths;
33
34 /**
35  * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata. 
36  *
37  *
38  */
39 public abstract class AbstractRepositoryServletProxiedMetadataTestCase
40     extends AbstractRepositoryServletProxiedTestCase
41 {
42     protected RemoteRepoInfo remotePrivateSnapshots;
43
44     protected void assertExpectedMetadata( String expectedMetadata, String actualMetadata )
45         throws Exception
46     {
47         DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
48         if ( !detailedDiff.similar() )
49         {
50             // If it isn't similar, dump the difference.
51             assertEquals( expectedMetadata, actualMetadata );
52         }
53         // XMLAssert.assertXMLEqual( "Expected Metadata:", expectedMetadata, actualMetadata );
54     }
55
56     protected String requestMetadataOK( String path )
57         throws Exception
58     {
59         // process the response code later, not via an exception.
60         //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
61
62         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
63         WebResponse response = getServletUnitClient().getResponse( request );
64         assertResponseOK( response );
65         return response.getContentAsString();
66     }
67
68     protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
69                                           String[] versions )
70     {
71         StringBuilder buf = new StringBuilder();
72
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" );
77
78         boolean hasLatest = StringUtils.isNotBlank( latest );
79         boolean hasRelease = StringUtils.isNotBlank( release );
80         boolean hasVersions = !ArrayUtils.isEmpty( versions );
81
82         if ( hasLatest || hasRelease || hasVersions )
83         {
84             buf.append( "  <versioning>\n" );
85             if ( hasLatest )
86             {
87                 buf.append( "    <latest>" ).append( latest ).append( "</latest>\n" );
88             }
89             if ( hasRelease )
90             {
91                 buf.append( "    <release>" ).append( release ).append( "</release>\n" );
92             }
93             if ( hasVersions )
94             {
95                 buf.append( "    <versions>\n" );
96                 for ( String availVersion : versions )
97                 {
98                     buf.append( "      <version>" ).append( availVersion ).append( "</version>\n" );
99                 }
100                 buf.append( "    </versions>\n" );
101             }
102             buf.append( "  </versioning>\n" );
103         }
104         buf.append( "</metadata>" );
105
106         return buf.toString();
107     }
108
109     protected String createGroupMetadata( String groupId, String[] plugins )
110     {
111         StringBuilder buf = new StringBuilder();
112
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" );
116
117         boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
118
119         if ( hasPlugins )
120         {
121             buf.append( "  <plugins>\n" );
122             for ( String plugin : plugins )
123             {
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" );
129             }
130             buf.append( "  </plugins>\n" );
131         }
132         buf.append( "</metadata>" );
133
134         return buf.toString();
135     }
136
137     protected void setupPrivateSnapshotsRemoteRepo()
138         throws Exception
139     {
140         remotePrivateSnapshots = createServer( "private-snapshots" );
141
142         assertServerSetupCorrectly( remotePrivateSnapshots );
143         archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
144         setupCleanRepo( remotePrivateSnapshots.root );
145     }
146
147 //    private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
148 //                                                   long deltaManagedToRemoteTimestamp )
149 //        throws Exception
150 //    {
151 //        // --- Setup
152 //        setupSnapshotsRemoteRepo();
153 //        setupCleanInternalRepo();
154 //
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"
161 //            + "</metadata>";
162 //        String expectedManagedContents = null;
163 //        File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
164 //
165 //        if ( hasManagedCopy )
166 //        {
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"
169 //                + "</metadata>";
170 //
171 //            File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
172 //            managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
173 //        }
174 //
175 //        setupConnector( REPOID_INTERNAL, remoteSnapshots );
176 //        saveConfiguration();
177 //
178 //        // --- Execution
179 //        // process the response code later, not via an exception.
180 //        HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
181 //
182 //        WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
183 //        WebResponse response = sc.getResponse( request );
184 //
185 //        // --- Verification
186 //
187 //        switch ( expectation )
188 //        {
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() );
196 //                break;
197 //            case EXPECT_REMOTE_CONTENTS:
198 //                assertResponseOK( response );
199 //                assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
200 //                break;
201 //            case EXPECT_NOT_FOUND:
202 //                assertResponseNotFound( response );
203 //                assertManagedFileNotExists( repoRootInternal, resourcePath );
204 //                break;
205 //        }
206 //    }
207
208     @Before
209     @Override
210     public void tearDown()
211         throws Exception
212     {
213         shutdownServer( remotePrivateSnapshots );
214
215         super.tearDown();
216
217         String appserverBase = System.getProperty( "appserver.base" );
218         if ( StringUtils.isNotEmpty( appserverBase ) )
219         {
220             FileUtils.deleteDirectory( Paths.get( appserverBase ) );
221         }
222
223     }
224 }