/**
 * Copyright 2013 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.kernel.spring;

import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;

/**
 * @author Andrew Woods
 *         Date: 10/20/13
 */
public class DefaultPropertiesLoaderTest {

    private DefaultPropertiesLoader loader;

    private static final String PROP_FLAG = "integration-test";
    private static final String PROP_TEST = "fcrepo.ispn.repo.CacheDirPath";
    private static final String HOME_PROP = "fcrepo.home";

    @Before
    public void setUp() throws Exception {
        loader = new DefaultPropertiesLoader();
    }

    @After
    public void tearDown() throws Exception {
        clearProps();
    }

    private void clearProps() {
        System.clearProperty(PROP_FLAG);
        System.clearProperty(PROP_TEST);
        System.clearProperty(HOME_PROP);
    }

    @Test
    public void testLoadSystemProperties() throws Exception {
        System.setProperty(PROP_FLAG, "true");

        loader.loadSystemProperties();
        Assert.assertNotNull(System.getProperty(PROP_FLAG));
        Assert.assertNull(System.getProperty(PROP_TEST));
    }

    @Test
    public void testLoadSystemPropertiesProduction() throws Exception {
        loader.loadSystemProperties();
        Assert.assertNull(System.getProperty(PROP_FLAG));
        Assert.assertNotNull(System.getProperty(PROP_TEST));
    }

    @Test
    public void testContentIsInWorkingDir() {
       loader.loadSystemProperties();
       Assert.assertTrue("Default directories are within working directory.",
               containsPath(System.getProperty(PROP_TEST),
                       System.getProperty("user.dir")));
    }

    @Test
    public void testCustomHomeDirWithRelativeSubdirs() {
        System.setProperty(HOME_PROP, "/test");
        System.setProperty(PROP_TEST, "sub");

        loader.loadSystemProperties();

        File home = new File("/test");
        Assert.assertTrue("Relative subdirs are within fcrepo.home directory.",
                containsPath(System.getProperty(PROP_TEST),
                        home.getAbsolutePath()));
        clearProps();
    }

    @Test
    public void testCustomHomeDirWithAbsoluteSubdirs() {
        System.setProperty(HOME_PROP, "/tmp/test");
        System.setProperty(PROP_TEST, "/tmp/sub");

        loader.loadSystemProperties();

        File home = new File("/test");
        Assert.assertFalse("Absolute subdirs are idependent of fcrepo.home.",
                containsPath(System.getProperty(PROP_TEST),
                        home.getAbsolutePath()));
        clearProps();
    }

    @Test
    public void relativePathsWorkForFedoraHome() {
        System.setProperty(HOME_PROP, "test");
        System.setProperty(PROP_TEST, "sub");

        loader.loadSystemProperties();

        Assert.assertEquals(new File(new File("test"), "sub"),
                new File(System.getProperty(PROP_TEST)));
        clearProps();
    }

    private boolean containsPath(String path, String parentPath) {
        final File parent = new File(parentPath);
        for (File f = new File(path) ; f.getParentFile() != null ; f = f.getParentFile()) {
            if (f.getParentFile().equals(parent)) {
                return true;
            }
        }
        return false;
    }
}

@@ -2386,24 +2386,35 @@
 E_PROP, 
+asTempPath(
 %22
-/
 test%22)
+)
 ;%0A      
@@ -2519,32 +2519,43 @@
  = new File(
+asTempPath(
 %22
-/
 test%22)
+)
 ;%0A        As
@@ -2872,20 +2872,27 @@
 OP, 
-%22/tmp/
+asTempPath(%22
 test%22)
+)
 ;%0A  
@@ -2931,19 +2931,26 @@
 ST, 
-%22/tmp/
+asTempPath(%22
 sub%22)
+)
 ;%0A%0A 
@@ -3021,16 +3021,27 @@
 ile(
+asTempPath(
 %22
-/
 test%22)
+)
 ;%0A  
@@ -3941,11 +3941,167 @@
 e;%0A    %7D
+%0A%0A    private String asTempPath(final String file) %7B%0A       return System.getProperty(%22java.io.tmpdir%22) + System.getProperty(%22file.separator%22) + file;%0A    %7D
 %0A%7D%0A

Change file paths in org.fcrepo.kernel.spring.DefaultPropertiesLoaderTest to be platform-independent.