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.jackrabbit.webdav.DavException;
24  import org.apache.jackrabbit.webdav.DavResource;
25  import org.apache.jackrabbit.webdav.DavResourceFactory;
26  import org.apache.jackrabbit.webdav.DavResourceLocator;
27  import org.apache.jackrabbit.webdav.DavServletRequest;
28  import org.apache.jackrabbit.webdav.DavServletResponse;
29  import org.apache.jackrabbit.webdav.DavSession;
30  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31  
32  import java.io.File;
33  import javax.activation.MimetypesFileTypeMap;
34  
35  public class ContinuumBuildAgentDavResourceTest
36      extends PlexusInSpringTestCase
37  {
38      private DavSession session;
39  
40      private DavResourceFactory resourceFactory;
41  
42      private ContinuumBuildAgentDavResourceLocator resourceLocator;
43  
44      private DavResource resource;
45  
46      private MimetypesFileTypeMap mimeTypes;
47  
48      private File baseDir;
49  
50      private File resourceFile;
51  
52      private final String RESOURCE_FILE = "resource.jar";
53  
54      @Override
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          session = new ContinuumBuildAgentDavSession();
61  
62          mimeTypes = new MimetypesFileTypeMap();
63          mimeTypes.addMimeTypes( "application/java-archive jar war ear" );
64          mimeTypes.addMimeTypes( "application/java-class class" );
65          mimeTypes.addMimeTypes( "image/png png" );
66  
67          baseDir = getTestFile( "target/DavResourceTest" );
68          baseDir.mkdirs();
69          resourceFile = new File( baseDir, RESOURCE_FILE );
70          resourceFile.createNewFile();
71  
72          resourceFactory = new RootContextDavResourceFactory();
73          resourceLocator = (ContinuumBuildAgentDavResourceLocator) new ContinuumBuildAgentDavLocatorFactory().
74              createResourceLocator( "/", RESOURCE_FILE );
75          resource = getDavResource( resourceLocator.getHref( false ), resourceFile );
76      }
77  
78      @Override
79      protected void tearDown()
80          throws Exception
81      {
82          if ( baseDir.exists() )
83          {
84              FileUtils.deleteDirectory( baseDir );
85          }
86  
87          super.tearDown();
88      }
89  
90      public void testAddResource()
91          throws Exception
92      {
93          File newResource = new File( baseDir, "newResource.jar" );
94          assertFalse( newResource.exists() );
95          try
96          {
97              resource.getCollection().addMember( resource, null );
98              fail( "Should have thrown an UnsupportedOperationException" );
99          }
100         catch ( UnsupportedOperationException e )
101         {
102             assertFalse( newResource.exists() );
103         }
104     }
105 
106     public void testDeleteCollection()
107         throws Exception
108     {
109         File dir = new File( baseDir, "testdir" );
110         try
111         {
112             assertTrue( dir.mkdir() );
113             DavResource directoryResource = getDavResource( "/testdir", dir );
114             directoryResource.getCollection().removeMember( directoryResource );
115             fail( "Should have thrown an UnsupportedOperationException" );
116         }
117         catch ( UnsupportedOperationException e )
118         {
119             assertTrue( dir.exists() );
120         }
121         finally
122         {
123             FileUtils.deleteDirectory( dir );
124         }
125     }
126 
127     public void testDeleteResource()
128         throws Exception
129     {
130         assertTrue( resourceFile.exists() );
131         try
132         {
133             resource.getCollection().removeMember( resource );
134             fail( "Should have thrown an UnsupportedOperationException" );
135         }
136         catch ( UnsupportedOperationException e )
137         {
138             assertTrue( resourceFile.exists() );
139         }
140     }
141 
142     private DavResource getDavResource( String logicalPath, File file )
143     {
144         return new ContinuumBuildAgentDavResource( file.getAbsolutePath(), logicalPath, session, resourceLocator,
145                                                    resourceFactory, mimeTypes );
146     }
147 
148     private class RootContextDavResourceFactory
149         implements DavResourceFactory
150     {
151         public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
152                                            DavServletResponse response )
153             throws DavException
154         {
155             throw new UnsupportedOperationException( "Not supported yet." );
156         }
157 
158         public DavResource createResource( DavResourceLocator locator, DavSession session )
159             throws DavException
160         {
161             return new ContinuumBuildAgentDavResource( baseDir.getAbsolutePath(), "/", session, resourceLocator,
162                                                        resourceFactory, mimeTypes );
163         }
164     }
165 }