/*
 * 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.ui;

import java.io.File;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.junit.runner.RunWith;
import org.xwiki.extension.ExtensionId;
import org.xwiki.test.integration.XWikiExecutor;
import org.xwiki.test.integration.XWikiExecutorSuite;

/**
 * Runs all functional tests found in the classpath.
 * 
 * @version $Id$
 */
@RunWith(PageObjectSuite.class)
public class AllTests
{
    @XWikiExecutorSuite.PreStart
    public void preStart(List<XWikiExecutor> executors) throws Exception
    {
        XWikiExecutor executor = executors.get(0);

        // Put self as extensions repository
        Properties properties = executor.loadXWikiProperties();
        properties.setProperty("extension.repositories", "self:xwiki:http://localhost:8080/xwiki/rest");
        executor.saveXWikiProperties(properties);
    }

    @PageObjectSuite.PostStart
    public void postStart(PersistentTestContext context) throws Exception
    {
        // Import XR
        if (!context.getUtil().pageExists("Extension", "WebHome")) {
            context.getDriver().get(
                context.getUtil().getURLToLoginAsAdminAndGotoPage(context.getUtil().getURLToNonExistentPage()));
            context.getUtil().recacheSecretToken();
            context.getUtil().importXar(
                new File("target/dependency/xwiki-platform-repository-server-ui.xar"));
        }

        // Initialize extensions and repositories
        initExtensions(context);
    }

    public static void initExtensions(PersistentTestContext context) throws Exception
    {
        // Initialize extensions and repositories
        RepositoryTestUtils repositoryUtil = new RepositoryTestUtils(context.getUtil());
        repositoryUtil.init();

        // Set integration repository util
        context.getProperties().put(RepositoryTestUtils.PROPERTY_KEY, repositoryUtil);

        // Populate maven repository
        File extensionFile = repositoryUtil.getTestExtension(new ExtensionId("emptyjar", "1.0"), "jar").getFile().getFile();
        FileUtils.copyFile(extensionFile, new File(repositoryUtil.getRepositoryUtil().getMavenRepository(), "maven/extension/1.0/extension-1.0.jar"));
        FileUtils.copyFile(extensionFile, new File(repositoryUtil.getRepositoryUtil().getMavenRepository(), "maven/extension/2.0/extension-2.0.jar"));
        FileUtils.copyFile(extensionFile, new File(repositoryUtil.getRepositoryUtil().getMavenRepository(), "maven/oldextension/0.9/oldextension-0.9.jar"));
        FileUtils.copyFile(extensionFile, new File(repositoryUtil.getRepositoryUtil().getMavenRepository(), "maven/dependency/version/dependency-version.jar"));
    }
}

@@ -936,16 +936,41 @@
 o.File;%0A
+import java.util.Arrays;%0A
 import j
@@ -988,37 +988,73 @@
 st;%0A
+%0A
 import 
-java.util.Properties;%0A
+org.apache.commons.configuration.PropertiesConfiguration;
 %0Aimp
@@ -1163,16 +1163,64 @@
 sionId;%0A
+import org.xwiki.extension.test.RepositoryUtil;%0A
 import o
@@ -1613,24 +1613,87 @@
 rs.get(0);%0A%0A
+        RepositoryUtil repositoryUtil = new RepositoryUtil();%0A%0A
         // P
@@ -1743,16 +1743,29 @@
 operties
+Configuration
  propert
@@ -1798,16 +1798,29 @@
 operties
+Configuration
 ();%0A    
@@ -1846,16 +1846,29 @@
 roperty(
+%0A            
 %22extensi
@@ -1884,17 +1884,43 @@
 tories%22,
- 
+%0A            Arrays.asList(
 %22self:xw
@@ -1956,16 +1956,100 @@
 ki/rest%22
+, %22test-maven:maven:%22%0A                + repositoryUtil.getMavenRepository().toURI())
 );%0A     
@@ -2543,33 +2543,16 @@
 portXar(
-%0A                
 new File
@@ -3175,16 +3175,28 @@
 onFile =
+%0A           
  reposit
@@ -3383,32 +3383,44 @@
 venRepository(),
+%0A           
  %22maven/extensio
@@ -3546,32 +3546,44 @@
 venRepository(),
+%0A           
  %22maven/extensio
@@ -3709,32 +3709,44 @@
 venRepository(),
+%0A           
  %22maven/oldexten
@@ -3886,16 +3886,28 @@
 itory(),
+%0A           
  %22maven/

Add UI test for extension import in XR Properly init maven repository