package com.atlassian.plugin.loaders.classloading;

import com.atlassian.plugin.util.ClassLoaderUtils;
import com.atlassian.plugin.util.FileUtils;
import com.atlassian.plugin.loaders.TestClassPathPluginLoader;
import junit.framework.TestCase;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public abstract class AbstractTestClassLoader extends TestCase
{
    public static final String PADDINGTON_JAR = "paddington-test-plugin.jar";
    public static final String POOH_JAR = "pooh-test-plugin.jar";

    protected File pluginsDirectory;
    protected File tempDir;
    protected File pluginsTestDir;

    protected File getPluginsDirectory()
    {
        URL url = ClassLoaderUtils.getResource("plugins", TestClassPathPluginLoader.class);
        String path = url.toExternalForm().substring(5);
    	path = path.replace('/', File.separatorChar);
        pluginsDirectory = new File(path);
        System.out.println("path = " + path);
        return pluginsDirectory;
    }

    /**
     * Generate a random string of characters - including numbers
     */
    public static String randomString(int length)
    {
        StringBuffer b = new StringBuffer(length);

        for (int i = 0; i < length; i++)
        {
            b.append(randomAlpha());
        }

        return b.toString();
    }

    /**
     * Generate a random character from the alphabet - either a-z or A-Z
     */
    public static char randomAlpha()
    {
        int i = (int) (Math.random() * 52);

        if (i > 25)
            return (char) (97 + i - 26);
        else
            return (char) (65 + i);
    }

    protected void createFillAndCleanTempPluginDirectory() throws IOException
    {
        pluginsDirectory = getPluginsDirectory(); // hacky way of getting to the directoryPluginLoaderFiles classloading
        tempDir = new File(System.getProperty("java.io.tmpdir"));

        File pluginsDir = new File(tempDir.toString() + File.separator +  "plugins");
        pluginsTestDir = new File(pluginsDir, randomString(6));

        if (pluginsDir.exists() && pluginsDir.isDirectory())
            assertTrue(FileUtils.deleteDir(pluginsDir));

        pluginsTestDir.mkdirs();

        FileUtils.copyDirectory(pluginsDirectory, pluginsTestDir);

        // Clean up CVS directory in case we copied it over by mistake.
        File cvsDir = new File(pluginsTestDir, "CVS");
        if (cvsDir.exists())
            assertTrue(FileUtils.deleteDir(cvsDir));
    }
}

@@ -1069,16 +1069,124 @@
 numbers%0A
+     *%0A     * @param length the length of the string to return%0A     * @return a random string of characters%0A
      */%0A
@@ -1509,16 +1509,68 @@
  or A-Z%0A
+     *%0A     * @return a random alphabetic character%0A
      */%0A
@@ -2502,17 +2502,256 @@
     
-File cvsD
+removeSourceControlMetadata(%22CVS%22);%0A%0A        // Clean up SVN directory in case we copied it over by mistake.%0A        removeSourceControlMetadata(%22.svn%22);%0A    %7D%0A%0A    private void removeSourceControlMetadata(String directoryName)%0A    %7B%0A        File d
 ir =
@@ -2776,21 +2776,29 @@
 estDir, 
-%22CVS%22
+directoryName
 );%0A     
@@ -2804,20 +2804,17 @@
     if (
-cvsD
+d
 ir.exist
@@ -2865,12 +2865,9 @@
 Dir(
-cvsD
+d
 ir))

Remove .svn directory when cleaning up plugin store