1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.webdav.test

File AbstractWebDAVTest.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
39
9
1
221
95
9
0.23
4.33
9
1

Classes

Class Line # Actions
AbstractWebDAVTest 45 39 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.webdav.test;
21   
22    import java.io.ByteArrayInputStream;
23   
24    import org.apache.commons.httpclient.HttpClient;
25    import org.apache.commons.httpclient.HttpMethod;
26    import org.apache.commons.httpclient.UsernamePasswordCredentials;
27    import org.apache.commons.httpclient.auth.AuthScope;
28    import org.apache.commons.httpclient.methods.DeleteMethod;
29    import org.apache.commons.httpclient.methods.GetMethod;
30    import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
31    import org.apache.commons.httpclient.methods.PutMethod;
32    import org.apache.webdav.lib.methods.MkcolMethod;
33    import org.apache.webdav.lib.methods.MoveMethod;
34    import org.apache.webdav.lib.methods.PropFindMethod;
35    import org.junit.Before;
36   
37    import static org.junit.Assert.*;
38   
39    /**
40    * Abstract test class for all webdav tests.
41    *
42    * @version $Id: 5c050ed0e2f7281462d89f17ecfeb39dfd18ff8d $
43    * @since 1.8RC1
44    */
 
45    public class AbstractWebDAVTest
46    {
47    /**
48    * Root webdav view.
49    */
50    public static final String ROOT = "http://localhost:8080/xwiki/webdav";
51   
52    /**
53    * location of the home view.
54    */
55    public static final String HOME = ROOT + "/home";
56   
57    /**
58    * location of the spaces view.
59    */
60    public static final String SPACES = ROOT + "/spaces";
61   
62    /**
63    * location of the attachments view.
64    */
65    public static final String ATTACHMENTS = ROOT + "/attachments";
66   
67    /**
68    * location of the orphans view.
69    */
70    public static final String ORPHANS = ROOT + "/orphans";
71   
72    /**
73    * location of the whatsnew view.
74    */
75    public static final String WHATSNEW = ROOT + "/whatsnew";
76   
77    /**
78    * Array of all baseview locations.
79    */
80    public static String[] BASE_VIEWS = new String[] {SPACES, ATTACHMENTS, HOME, ORPHANS, WHATSNEW};
81   
82    /**
83    * The {@link HttpClient} used to invoke various methods on the webdav server.
84    */
85    private HttpClient client;
86   
87    /**
88    * Initializes the http client.
89    */
 
90  15 toggle @Before
91    public void setUp() throws Exception
92    {
93  15 client = new HttpClient();
94  15 client.getState().setCredentials(
95    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME),
96    new UsernamePasswordCredentials("superadmin", "pass"));
97  15 client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
98  15 client.getParams().setAuthenticationPreemptive(true);
99    }
100   
101    /**
102    * @return the {@link HttpClient}.
103    */
 
104  77 toggle protected HttpClient getHttpClient()
105    {
106  77 return client;
107    }
108   
109    /**
110    * Executes the given {@link HttpMethod} and tests for the expected return status.
111    *
112    * @param method the {@link HttpMethod}.
113    * @param expect expected return status.
114    */
 
115  40 toggle protected void testMethod(HttpMethod method, int expect) throws Exception
116    {
117  40 int status = getHttpClient().executeMethod(method);
118  40 assertEquals(expect, status);
119    }
120   
121    /**
122    * Tests the PROPFIND method on the given url.
123    *
124    * @param url the target url.
125    * @param depth depth parameter for the {@link PropFindMethod}.
126    * @param expect the return status expected.
127    * @return the {@link HttpMethod} which contains the response.
128    */
 
129  1 toggle protected HttpMethod propFind(String url, int depth, int expect) throws Exception
130    {
131  1 PropFindMethod propFindMethod = new PropFindMethod(url);
132  1 propFindMethod.setDoAuthentication(true);
133  1 propFindMethod.setDepth(depth);
134  1 testMethod(propFindMethod, expect);
135  1 return propFindMethod;
136    }
137   
138    /**
139    * Tests the MKCOL method on the given url.
140    *
141    * @param url the target url.
142    * @param expect the return status expected.
143    * @return the {@link HttpMethod} which contains the response.
144    */
 
145  5 toggle protected HttpMethod mkCol(String url, int expect) throws Exception
146    {
147  5 MkcolMethod mkColMethod = new MkcolMethod();
148  5 mkColMethod.setDoAuthentication(true);
149  5 mkColMethod.setPath(url);
150  5 testMethod(mkColMethod, expect);
151  5 return mkColMethod;
152    }
153   
154    /**
155    * Tests the PUT method on the given url.
156    *
157    * @param url the target url.
158    * @param content the content for the {@link PutMethod}.
159    * @param expect the return status expected.
160    * @return the {@link HttpMethod} which contains the response.
161    */
 
162  3 toggle protected HttpMethod put(String url, String content, int expect) throws Exception
163    {
164  3 PutMethod putMethod = new PutMethod();
165  3 putMethod.setDoAuthentication(true);
166  3 putMethod.setPath(url);
167  3 putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
168  3 testMethod(putMethod, expect);
169  3 return putMethod;
170    }
171   
172    /**
173    * Tests the DELETE method on the given url.
174    *
175    * @param url the target url.
176    * @param expect the return status expected.
177    * @return the {@link HttpMethod} which contains the response.
178    */
 
179  11 toggle protected HttpMethod delete(String url, int expect) throws Exception
180    {
181  11 DeleteMethod deleteMethod = new DeleteMethod();
182  11 deleteMethod.setDoAuthentication(true);
183  11 deleteMethod.setPath(url);
184  11 testMethod(deleteMethod, expect);
185  11 return deleteMethod;
186    }
187   
188    /**
189    * Tests the MOVE method on the given url.
190    *
191    * @param url the target url.
192    * @param destination the destination parameter for the {@link MoveMethod}.
193    * @param expect the return status expected.
194    * @return the {@link HttpMethod} which contains the response.
195    */
 
196  16 toggle protected HttpMethod move(String url, String destination, int expect) throws Exception
197    {
198  16 MoveMethod moveMethod = new MoveMethod();
199  16 moveMethod.setDoAuthentication(true);
200  16 moveMethod.setPath(url);
201  16 moveMethod.setDestination(destination);
202  16 testMethod(moveMethod, expect);
203  16 return moveMethod;
204    }
205   
206    /**
207    * Tests the GET method on the given url.
208    *
209    * @param url the target url.
210    * @param expect the return status expected.
211    * @return the {@link HttpMethod} which contains the response.
212    */
 
213  4 toggle protected HttpMethod get(String url, int expect) throws Exception
214    {
215  4 GetMethod getMethod = new GetMethod();
216  4 getMethod.setDoAuthentication(true);
217  4 getMethod.setPath(url);
218  4 testMethod(getMethod, expect);
219  4 return getMethod;
220    }
221    }