View Javadoc

1   package org.apache.continuum.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 org.apache.commons.io.FileUtils;
23  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
24  import org.apache.jackrabbit.webdav.DavException;
25  import org.apache.jackrabbit.webdav.DavResourceLocator;
26  import org.apache.jackrabbit.webdav.DavServletRequest;
27  import org.apache.jackrabbit.webdav.DavServletResponse;
28  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
29  import org.easymock.MockControl;
30  
31  import java.io.File;
32  
33  public class ContinuumBuildAgentDavResourceFactoryTest
34      extends PlexusInSpringTestCase
35  {
36      private MockControl requestControl;
37  
38      private DavServletRequest request;
39  
40      private MockControl responseControl;
41  
42      private DavServletResponse response;
43  
44      private MockControl buildAgentConfigurationServiceControl;
45  
46      private BuildAgentConfigurationService buildAgentConfigurationService;
47  
48      private ContinuumBuildAgentDavResourceFactory resourceFactory;
49  
50      private File workingDirectory;
51  
52      @Override
53      public void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          requestControl = MockControl.createControl( DavServletRequest.class );
59          request = (DavServletRequest) requestControl.getMock();
60  
61          responseControl = MockControl.createControl( DavServletResponse.class );
62          response = (DavServletResponse) responseControl.getMock();
63          responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
64  
65          buildAgentConfigurationServiceControl = MockControl.
66              createControl( BuildAgentConfigurationService.class );
67          buildAgentConfigurationService =
68              (BuildAgentConfigurationService) buildAgentConfigurationServiceControl.getMock();
69  
70          resourceFactory = new ContinuumBuildAgentDavResourceFactory();
71          resourceFactory.setBuildAgentConfigurationService( buildAgentConfigurationService );
72  
73          String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
74          System.setProperty( "appserver.base", appserverBase );
75  
76          workingDirectory = new File( appserverBase, "data/working-directory" );
77  
78          new File( workingDirectory, "1/target" ).mkdirs();
79          new File( workingDirectory, "1/target/continuum-artifact-1.0.jar" ).createNewFile();
80      }
81  
82      @Override
83      public void tearDown()
84          throws Exception
85      {
86          if ( workingDirectory.exists() )
87          {
88              FileUtils.deleteDirectory( workingDirectory );
89          }
90  
91          super.tearDown();
92      }
93  
94      public void testRequestArtifact()
95          throws Exception
96      {
97          DavResourceLocator locator = new ContinuumBuildAgentDavResourceLocator( "http://myhost/",
98                                                                                  "/workingcopy/1/target/continuum-artifact-1.0.jar",
99                                                                                  new ContinuumBuildAgentDavLocatorFactory(),
100                                                                                 1 );
101 
102         try
103         {
104             requestControl.expectAndReturn( request.getMethod(), "GET" );
105             buildAgentConfigurationServiceControl.
106                 expectAndReturn( buildAgentConfigurationService.getWorkingDirectory( 1 ), getWorkingDirectory( 1 ) );
107             requestControl.expectAndReturn( request.getDavSession(), new ContinuumBuildAgentDavSession() );
108 
109             requestControl.replay();
110             buildAgentConfigurationServiceControl.replay();
111 
112             resourceFactory.createResource( locator, request, response );
113 
114             requestControl.verify();
115             buildAgentConfigurationServiceControl.verify();
116         }
117         catch ( DavException e )
118         {
119             fail( "A DavException should not have been thrown!" );
120         }
121     }
122 
123     public void testRequestArtifactDoesNotExist()
124         throws Exception
125     {
126         DavResourceLocator locator = new ContinuumBuildAgentDavResourceLocator( "http://myhost/",
127                                                                                 "/workingcopy/1/pom.xml",
128                                                                                 new ContinuumBuildAgentDavLocatorFactory(),
129                                                                                 1 );
130 
131         try
132         {
133             requestControl.expectAndReturn( request.getMethod(), "GET", 1 );
134             buildAgentConfigurationServiceControl.
135                 expectAndReturn( buildAgentConfigurationService.getWorkingDirectory( 1 ), getWorkingDirectory( 1 ) );
136 
137             requestControl.replay();
138             buildAgentConfigurationServiceControl.replay();
139 
140             resourceFactory.createResource( locator, request, response );
141 
142             requestControl.verify();
143             buildAgentConfigurationServiceControl.verify();
144 
145             fail( "A DavException with 404 error code should have been thrown." );
146         }
147         catch ( DavException e )
148         {
149             assertEquals( 404, e.getErrorCode() );
150         }
151     }
152 
153     private File getWorkingDirectory( int projectId )
154     {
155         return new File( workingDirectory, String.valueOf( projectId ) );
156     }
157 }