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 java.io.File;
23  
24  import com.meterware.httpunit.GetMethodWebRequest;
25  import com.meterware.httpunit.HttpUnitOptions;
26  import com.meterware.httpunit.WebRequest;
27  import com.meterware.httpunit.WebResponse;
28  
29  /**
30   * RepositoryServlet Tests, Proxied, Get of resources that are not artifacts or metadata, with varying policy settings.
31   * 
32   * @version $Id: RepositoryServletProxiedReleasePolicyTest.java 661174 2008-05-29 01:49:41Z jdumay $
33   */
34  public class RepositoryServletProxiedPassthroughTest
35      extends AbstractRepositoryServletProxiedTestCase
36  {
37      private static final String CONTENT_SHA1 = "2aab0a51c04c9023636852f3e63a68034ba10142";
38  
39      private static final String PATH_SHA1 = "org/apache/archiva/test/1.0/test-1.0.jar.sha1";
40  
41      private static final String CONTENT_ASC =
42          "-----BEGIN PGP SIGNATURE-----\n" + "Version: GnuPG v1.4.8 (Darwin)\n" + "\n"
43              + "iEYEABECAAYFAkiAIVgACgkQxbsDNW2stZZjyACeK3LW+ZDeawCyJj4XgvUaJkNh\n"
44              + "qIEAoIUiijY4Iw82RWOT75Rt3yZuY6ZI\n" + "=WLkm\n" + "-----END PGP SIGNATURE-----\n";
45  
46      private static final String PATH_ASC = "org/apache/archiva/test/1.0/test-1.0.jar.asc";
47  
48      public void testGetProxiedManagedNewerSha1()
49          throws Exception
50      {
51          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, ( NEWER * OVER_ONE_DAY ), PATH_SHA1,
52                                    CONTENT_SHA1 );
53      }
54  
55      public void testGetProxiedManagedOlderSha1()
56          throws Exception
57      {
58          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, HAS_MANAGED_COPY, ( OLDER * OVER_ONE_DAY ), PATH_SHA1,
59                                    CONTENT_SHA1 );
60      }
61  
62      public void testGetProxiedNoManagedContentSha1()
63          throws Exception
64      {
65          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, NO_MANAGED_COPY, PATH_SHA1, CONTENT_SHA1 );
66      }
67  
68      public void testGetProxiedEqualSha1()
69          throws Exception
70      {
71          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, PATH_SHA1, CONTENT_SHA1 );
72      }
73  
74      public void testGetProxiedManagedNewerAsc()
75          throws Exception
76      {
77          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, ( NEWER * OVER_ONE_DAY ), PATH_ASC,
78                                    CONTENT_ASC );
79      }
80  
81      public void testGetProxiedManagedOlderAsc()
82          throws Exception
83      {
84          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, HAS_MANAGED_COPY, ( OLDER * OVER_ONE_DAY ), PATH_ASC,
85                                    CONTENT_ASC );
86      }
87  
88      public void testGetProxiedNoManagedContentAsc()
89          throws Exception
90      {
91          assertGetProxiedResource( EXPECT_REMOTE_CONTENTS, NO_MANAGED_COPY, PATH_ASC, CONTENT_ASC );
92      }
93  
94      public void testGetProxiedEqualAsc()
95          throws Exception
96      {
97          assertGetProxiedResource( EXPECT_MANAGED_CONTENTS, HAS_MANAGED_COPY, PATH_ASC, CONTENT_ASC );
98      }
99  
100     private void assertGetProxiedResource( int expectation, boolean hasManagedCopy, String path, String content )
101         throws Exception
102     {
103         assertGetProxiedResource( expectation, hasManagedCopy, 0, path, content );
104     }
105 
106     private void assertGetProxiedResource( int expectation, boolean hasManagedCopy, long deltaManagedToRemoteTimestamp,
107                                            String path, String contents )
108         throws Exception
109     {
110         // --- Setup
111         setupCentralRemoteRepo();
112         setupCleanInternalRepo();
113 
114         String expectedRemoteContents = contents;
115         String expectedManagedContents = null;
116         File remoteFile = populateRepo( remoteCentral, path, expectedRemoteContents );
117 
118         if ( hasManagedCopy )
119         {
120             expectedManagedContents = contents;
121             File managedFile = populateRepo( repoRootInternal, path, expectedManagedContents );
122             managedFile.setLastModified( remoteFile.lastModified() + deltaManagedToRemoteTimestamp );
123         }
124 
125         setupConnector( REPOID_INTERNAL, remoteCentral );
126         saveConfiguration();
127 
128         // --- Execution
129         // process the response code later, not via an exception.
130         HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
131 
132         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path );
133         WebResponse response = sc.getResponse( request );
134 
135         // --- Verification
136 
137         switch ( expectation )
138         {
139             case EXPECT_MANAGED_CONTENTS:
140                 assertResponseOK( response );
141                 assertTrue( "Invalid Test Case: Can't expect managed contents with "
142                     + "test that doesn't have a managed copy in the first place.", hasManagedCopy );
143                 assertEquals( "Expected managed file contents", expectedManagedContents, response.getText() );
144                 break;
145             case EXPECT_REMOTE_CONTENTS:
146                 assertResponseOK( response, path );
147                 assertEquals( "Expected remote file contents", expectedRemoteContents, response.getText() );
148                 break;
149             case EXPECT_NOT_FOUND:
150                 assertResponseNotFound( response );
151                 assertManagedFileNotExists( repoRootInternal, path );
152                 break;
153         }
154     }
155 }