/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.storage.framework;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.FilePart;

/**
 * Test saving and downloading of attachments.
 *
 * @version $Id$
 * @since 3.2M1
 */
public final class TestUtils
{
    public static final String[] ADMIN_CREDENTIALS = new String[] {"Admin", "admin"};

    public static String getPageAsString(final String address) throws IOException
    {
        final HttpMethod ret = doPost(address, null, null);
        return new String(ret.getResponseBody(), "UTF-8");
    }

    /** Method to easily do a post request to the site. */
    public static HttpMethod doPost(final String address,
                                    final String[] userNameAndPassword,
                                    final Map<String, String> parameters)
        throws IOException
    {
        final HttpClient client = new HttpClient();
        final PostMethod method = new PostMethod(address);

        if (userNameAndPassword != null && userNameAndPassword.length == 2) {
            client.getState().setCredentials(null,
                                             null,
                                             new UsernamePasswordCredentials(userNameAndPassword[0],
                                                                             userNameAndPassword[1]));
        }

        if (parameters != null) {
            for (Map.Entry<String, String> e : parameters.entrySet()) {
                method.addParameter(e.getKey(), e.getValue());
            }
        }
        client.executeMethod(method);
        return method;
    }

    public static HttpMethod doUpload(final String address,
                                      final String[] userNameAndPassword,
                                      final Map<String, byte[]> uploads)
        throws IOException
    {
        final HttpClient client = new HttpClient();
        final MultipartPostMethod method = new MultipartPostMethod(address);

        if (userNameAndPassword != null && userNameAndPassword.length == 2) {
            client.getState().setCredentials(null,
                                             null,
                                             new UsernamePasswordCredentials(userNameAndPassword[0],
                                                                             userNameAndPassword[1]));
        }

        for (Map.Entry<String, byte[]> e : uploads.entrySet()) {
            method.addPart(new FilePart("filepath",
                           new ByteArrayPartSource(e.getKey(), e.getValue())));
        }
        client.executeMethod(method);
        return method;
    }

    /**
     * Encodes a given string so that it may be used as a URL component. Compatable with javascript decodeURIComponent,
     * though more strict than encodeURIComponent: all characters except [a-zA-Z0-9], '.', '-', '*', '_' are converted
     * to hexadecimal, and spaces are substituted by '+'.
     * 
     * @param s
     * @since 3.2M1
     */
    public static String escapeURL(String s)
    {
        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // should not happen
            throw new RuntimeException(e);
        }
    }
}

@@ -1259,35 +1259,22 @@
 ent.
-methods.MultipartPostMethod
+auth.AuthScope
 ;%0Aim
@@ -1468,16 +1468,156 @@
 ilePart;
+%0Aimport org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;%0Aimport org.apache.commons.httpclient.methods.multipart.Part;
 %0A%0A/**%0A *
@@ -1659,24 +1659,25 @@
 achments.%0A *
+ 
 %0A * @version
@@ -2154,44 +2154,8 @@
 ess,
-%0A                                   
  fin
@@ -2191,36 +2191,8 @@
 rd,%0A
-                            
     
@@ -2232,24 +2232,16 @@
 ameters)
-%0A       
  throws 
@@ -2485,36 +2485,45 @@
 .setCredentials(
-null
+AuthScope.ANY
 ,%0A              
@@ -2528,244 +2528,153 @@
     
-                             null,%0A                                             new UsernamePasswordCredentials(userNameAndPassword%5B0%5D,%0A                                                                             userNameAndPassword%5B1%5D)
+new UsernamePasswordCredentials(userNameAndPassword%5B0%5D, userNameAndPassword%5B1%5D));%0A            client.getParams().setAuthenticationPreemptive(true
 );%0A 
@@ -3007,46 +3007,8 @@
 ess,
-%0A                                     
  fin
@@ -3044,38 +3044,8 @@
 rd,%0A
-                              
     
@@ -3082,24 +3082,16 @@
 uploads)
-%0A       
  throws 
@@ -3174,25 +3174,16 @@
   final 
-Multipart
 PostMeth
@@ -3198,25 +3198,16 @@
 d = new 
-Multipart
 PostMeth
@@ -3343,20 +3343,29 @@
 entials(
-null
+AuthScope.ANY
 ,%0A      
@@ -3378,210 +3378,164 @@
     
-                             null,%0A                                             new UsernamePasswordCredentials(userNameAndPassword%5B0%5D,%0A                                                          
+new UsernamePasswordCredentials(userNameAndPassword%5B0%5D, userNameAndPassword%5B1%5D));%0A            client.getParams().setAuthenticationPreemptive(true);%0A
         
     
@@ -3530,24 +3530,27 @@
         
+%7D%0A%0A
         
    userN
@@ -3545,47 +3545,67 @@
     
-   userNameAndPassword%5B1%5D));%0A        %7D%0A
+Part%5B%5D parts = new Part%5Buploads.size()%5D;%0A        int i = 0;
 %0A   
@@ -3682,23 +3682,21 @@
     
-method.addPart(
+parts%5Bi++%5D = 
 new 
@@ -3719,35 +3719,8 @@
 th%22,
-%0A                          
  new
@@ -3766,17 +3766,16 @@
 alue()))
-)
 ;%0A      
@@ -3770,32 +3770,169 @@
 ()));%0A        %7D%0A
+        MultipartRequestEntity entity = new MultipartRequestEntity(parts, method.getParams());%0A        method.setRequestEntity(entity);%0A%0A
         client.e

Fix various deprecated calls and apply codestyle