package com.universal.storage;

import junit.framework.TestCase;
import java.io.File;
import com.universal.error.UniversalStorageException;
import org.apache.commons.io.FileUtils;
import com.universal.util.FileUtil;
import com.universal.storage.settings.UniversalSettings;

/**
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Dynamicloud
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * This class is the implementation of a storage that will manage files as a default file system.
 * This implementation will manage file using a setting to store files like so.
 */
public class TestUniversalFileStorage extends TestCase {
    /**
     * This test will execute the store file process using a FILE_SYSTEM provider.
     */
    public void testStoreFileAsFileSystemProvider() {
        try {
            UniversalStorage us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));

            us.storeFile(new File("src/test/resources/settings.json"), "myfolder/innerfolder");
            us.storeFile(new File("src/test/resources/settings.json"));
            us.storeFile("src/test/resources/settings.json", "myfolder/innerfolder");
            us.storeFile("src/test/resources/settings.json");
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }
    }

    public void testRetrieveFileAsFileSystemProvider() {
        UniversalStorage us = null;
        try {
            us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));
            File file = us.retrieveFile("myfolder/innerfolder/settings.json");
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }

        try {
            File file = us.retrieveFile("myfolder/innerfolder/settings.jsontxt");
            fail("This method should throw an error.");
        } catch (UniversalStorageException ignore) {
            
        }

        try {
            FileUtils.copyInputStreamToFile(us.retrieveFileAsStream("myfolder/innerfolder/settings.json"), 
                new File(FileUtil.completeFileSeparator(System.getProperty("user.dir")) + "settings.json"));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    /**
     * This test will execute the remove file process using a FILE_SYSTEM provider.
     */
    public void testRemoveFileAsFileSystemProvider() {
        try {
            UniversalStorage us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));
            us.removeFile("settings.json");
            us.removeFile("myfolder/innerfolder/settings.json");
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }
    }

    /**
     * This test will execute the create folder process using a FILE_SYSTEM provider.
     */
    public void testCreateFolderAsFileSystemProvider() {
        try {
            UniversalStorage us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));
            us.createFolder("myNewFolder");
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }
    }

    /**
     * This test will execute the remove folder process using a FILE_SYSTEM provider.
     */
    public void testRemoveFolderAsFileSystemProvider() {
        try {
            UniversalStorage us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));
            us.removeFolder("myNewFolder");
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }
    }

    /**
     * This test will clean the storage's context.
     */
    public void testCleanStorageAsFileSystemProvider() {
        try {
            UniversalStorage us = UniversalStorage.Impl.
                getInstance(new UniversalSettings(new File("src/test/resources/settings.json")));
            us.clean();
        } catch (UniversalStorageException e) {
            fail(e.getMessage());
        }
    }
}
@@ -1814,32 +1814,335 @@
 %7B%0A        try %7B%0A
+            File newFile = new File(System.getProperty(%22user.home%22), %22target.txt%22);%0A            if (!newFile.exists()) %7B%0A                try %7B%0A                    newFile.createNewFile();%0A                %7D catch (Exception e) %7B%0A                    fail(e.getMessage());%0A                %7D%0A            %7D%0A%0A
             Univ
@@ -2319,41 +2319,52 @@
 ile(
-%22src/test/resources/settings.json
+System.getProperty(%22user.home%22), %22target.txt
 %22), 
@@ -2426,104 +2426,128 @@
 ile(
-%22src/test/resources/settings.json%22));%0A            us.storeFile(%22src/test/resources/settings.json
+System.getProperty(%22user.home%22), %22target.txt%22));%0A            us.storeFile(System.getProperty(%22user.home%22) + %22/target.txt
 %22, %22
@@ -2599,41 +2599,54 @@
 ile(
-%22src/test/resources/settings.json
+System.getProperty(%22user.home%22) + %22/target.txt
 %22);%0A
@@ -3047,37 +3047,34 @@
 innerfolder/
-settings.json
+target.txt
 %22);%0A        
@@ -3179,36 +3179,24 @@
 %0A           
- File file =
  us.retrieve
@@ -3479,29 +3479,26 @@
 rfolder/
-settings.json
+target.txt
 %22), %0A   
@@ -3579,31 +3579,29 @@
 ser.
-dir
+home
 %22)) + %22
-settings.json
+target.txt
 %22));
@@ -4035,29 +4035,26 @@
 veFile(%22
-settings.json
+target.txt
 %22);%0A    
@@ -4097,29 +4097,26 @@
 rfolder/
-settings.json
+target.txt
 %22);%0A    

Test fixes