View Javadoc
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.commons.lang.ArrayUtils;
26  import org.apache.commons.lang.StringUtils;
27  import org.custommonkey.xmlunit.DetailedDiff;
28  import org.custommonkey.xmlunit.Diff;
29  import org.junit.Before;
30  
31  /**
32   * Abstract TestCase for RepositoryServlet Tests, Proxied, Get of Metadata. 
33   *
34   *
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 = getServletUnitClient().getResponse( request );
61          assertResponseOK( response );
62          return response.getContentAsString();
63      }
64  
65      protected String createProjectMetadata( String groupId, String artifactId, String latest, String release,
66                                            String[] versions )
67      {
68          StringBuilder buf = new StringBuilder();
69  
70          buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" );
71          buf.append( "<metadata>\n" );
72          buf.append( "  <groupId>" ).append( groupId ).append( "</groupId>\n" );
73          buf.append( "  <artifactId>" ).append( artifactId ).append( "</artifactId>\n" );
74  
75          boolean hasLatest = StringUtils.isNotBlank( latest );
76          boolean hasRelease = StringUtils.isNotBlank( release );
77          boolean hasVersions = !ArrayUtils.isEmpty( versions );
78  
79          if ( hasLatest || hasRelease || hasVersions )
80          {
81              buf.append( "  <versioning>\n" );
82              if ( hasLatest )
83              {
84                  buf.append( "    <latest>" ).append( latest ).append( "</latest>\n" );
85              }
86              if ( hasRelease )
87              {
88                  buf.append( "    <release>" ).append( release ).append( "</release>\n" );
89              }
90              if ( hasVersions )
91              {
92                  buf.append( "    <versions>\n" );
93                  for ( String availVersion : versions )
94                  {
95                      buf.append( "      <version>" ).append( availVersion ).append( "</version>\n" );
96                  }
97                  buf.append( "    </versions>\n" );
98              }
99              buf.append( "  </versioning>\n" );
100         }
101         buf.append( "</metadata>" );
102 
103         return buf.toString();
104     }
105 
106     protected String createGroupMetadata( String groupId, String[] plugins )
107     {
108         StringBuilder buf = new StringBuilder();
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 
114         boolean hasPlugins = !ArrayUtils.isEmpty( plugins );
115 
116         if ( hasPlugins )
117         {
118             buf.append( "  <plugins>\n" );
119             for ( String plugin : plugins )
120             {
121                 buf.append( "    <plugin>\n" );
122                 buf.append( "      <prefix>" ).append( plugin ).append( "</prefix>\n" );
123                 buf.append( "      <artifactId>" ).append( plugin + "-maven-plugin" ).append( "</artifactId>\n" );
124                 buf.append( "      <name>" ).append( "The " + plugin + " Plugin" ).append( "</name>\n" );
125                 buf.append( "    </plugin>\n" );
126             }
127             buf.append( "  </plugins>\n" );
128         }
129         buf.append( "</metadata>" );
130 
131         return buf.toString();
132     }
133 
134     protected void setupPrivateSnapshotsRemoteRepo()
135         throws Exception
136     {
137         remotePrivateSnapshots = createServer( "private-snapshots" );
138 
139         assertServerSetupCorrectly( remotePrivateSnapshots );
140         archivaConfiguration.getConfiguration().addRemoteRepository( remotePrivateSnapshots.config );
141         setupCleanRepo( remotePrivateSnapshots.root );
142     }
143 
144 //    private void assertGetProxiedSnapshotMetadata( int expectation, boolean hasManagedCopy,
145 //                                                   long deltaManagedToRemoteTimestamp )
146 //        throws Exception
147 //    {
148 //        // --- Setup
149 //        setupSnapshotsRemoteRepo();
150 //        setupCleanInternalRepo();
151 //
152 //        String resourcePath = "org/apache/archiva/archivatest-maven-plugin/4.0-alpha-1-SNAPSHOT/maven-metadata.xml";
153 //        String expectedRemoteContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n"
154 //            + "  <groupId>org.apache.maven.plugins</groupId>\n" + "  <artifactId>maven-assembly-plugin</artifactId>\n"
155 //            + "  <version>2.2-beta-2-SNAPSHOT</version>\n" + "  <versioning>\n" + "    <snapshot>\n"
156 //            + "      <timestamp>20071017.162810</timestamp>\n" + "      <buildNumber>20</buildNumber>\n"
157 //            + "    </snapshot>\n" + "    <lastUpdated>20071017162814</lastUpdated>\n" + "  </versioning>\n"
158 //            + "</metadata>";
159 //        String expectedManagedContents = null;
160 //        File remoteFile = populateRepo( remoteSnapshots, resourcePath, expectedRemoteContents );
161 //
162 //        if ( hasManagedCopy )
163 //        {
164 //            expectedManagedContents = "<metadata>\n" + "  <groupId>org.apache.maven.plugins</groupId>\n"
165 //                + "  <artifactId>maven-assembly-plugin</artifactId>\n" + "  <version>2.2-beta-2-SNAPSHOT</version>\n"
166 //                + "</metadata>";
167 //
168 //            File managedFile = populateRepo( repoRootInternal, resourcePath, expectedManagedContents );
169 //            managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
170 //        }
171 //
172 //        setupConnector( REPOID_INTERNAL, remoteSnapshots );
173 //        saveConfiguration();
174 //
175 //        // --- Execution
176 //        // process the response code later, not via an exception.
177 //        HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
178 //
179 //        WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + resourcePath );
180 //        WebResponse response = sc.getResponse( request );
181 //
182 //        // --- Verification
183 //
184 //        switch ( expectation )
185 //        {
186 //            case EXPECT_MANAGED_CONTENTS:
187 //                assertResponseOK( response );
188 //                assertTrue( "Invalid Test Case: Can't expect managed contents with "
189 //                    + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
190 //                String actualContents = response.getText();
191 //                XMLAssert.assertXMLEqual( expectedManagedContents, actualContents );
192 //                // assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
193 //                break;
194 //            case EXPECT_REMOTE_CONTENTS:
195 //                assertResponseOK( response );
196 //                assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
197 //                break;
198 //            case EXPECT_NOT_FOUND:
199 //                assertResponseNotFound( response );
200 //                assertManagedFileNotExists( repoRootInternal, resourcePath );
201 //                break;
202 //        }
203 //    }
204 
205     @Before
206     @Override
207     public void tearDown()
208         throws Exception
209     {
210         shutdownServer( remotePrivateSnapshots );
211 
212         super.tearDown();
213     }
214 }