/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * 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,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * (C) 2012,
 * @author JBoss Inc.
 */
package com.hp.mwtests.ts.arjuna.objectstore;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import org.junit.Before;
import org.junit.Test;

import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.common.arjPropertyManager;
import com.arjuna.ats.arjuna.exceptions.ObjectStoreException;
import com.arjuna.ats.arjuna.objectstore.*;
import com.arjuna.ats.arjuna.state.OutputObjectState;

import java.io.File;
import java.io.IOException;

/**
 * Test the filesystem is not changed when using a non file based store
 */
public class AltStoreTest
{
    String defaultStoreDir = null;

    @Before
    public void setPropertiesFileAndRemoveStore() {
        // set up a different properties file specific to this test
        // (the properties change the store type to one that does not touch the filesystem)
        System.setProperty("com.arjuna.ats.arjuna.common.propertiesFile", "alt-jbossts-properties.xml");

        defaultStoreDir = arjPropertyManager.getObjectStoreEnvironmentBean().getObjectStoreDir();
        assertFalse(defaultStoreDir == null);

        // delete the existing file based object store if it exists
        removeContents(new File(defaultStoreDir));

        System.out.println("Asserting that " + defaultStoreDir + " does not exist before the test");
        assertFalse(new File(defaultStoreDir).exists());
    }

    @Test
    public void test() throws IOException, ObjectStoreException
    {
        final OutputObjectState buff = new OutputObjectState();
        final String tn = "/StateManager/junit";

        // add a record to the object store
        assertTrue(StoreManager.getRecoveryStore().write_committed(new Uid(), tn, buff));

        // validate that there was no change to the filesystem
        System.out.println("Chacking that " + defaultStoreDir + " was not created by the test");
        assertFalse(new File(defaultStoreDir).exists());
    }

    /**
     * Delete a directory hierarchy
     * @param directory the point at which to start deleting (directory is also removed)
     * @return
     */
    private boolean removeContents(File directory)
    {
        boolean emptied = true;

        if ((directory != null) &&
                directory.isDirectory() &&
                (!directory.getName().equals("")) &&
                (!directory.getName().equals("/")) &&
                (!directory.getName().equals("\\")) &&
                (!directory.getName().equals(".")) &&
                (!directory.getName().equals("src/test")))
        {
            File[] contents = directory.listFiles();

            for (File f : contents) {
                if (f.isDirectory()) {
                    removeContents(f);

                    if (emptied)
                        emptied = f.delete();
                } else {
                    if (emptied)
                        emptied = f.delete();
                }
            }
        }

        if (directory != null && emptied)
            emptied = directory.delete();

        return emptied;
    }
}

@@ -3133,41 +3133,8 @@
   %7B%0A
-        boolean emptied = true;%0A%0A
     
@@ -3140,17 +3140,16 @@
      if 
-(
 (directo
@@ -3154,46 +3154,8 @@
 tory
- != null) &&%0A                directory
 .isD
@@ -3164,19 +3164,17 @@
 ectory()
- &&
+)
 %0A       
@@ -3182,411 +3182,110 @@
     
-    (!directory.getName().equals(%22%22)) &&%0A                (!directory.getName().equals(%22/%22)) &&%0A                (!directory.getName().equals(%22%5C%5C%22)) &&%0A                (!directory.getName().equals(%22.%22)) &&%0A                (!directory.getName().equals(%22src/test%22)))%0A        %7B%0A            File%5B%5D contents = directory.listFiles();%0A%0A            for (File f : contents) %7B%0A                if (f.isDirecto
+for (String entry : directory.list())%0A                if (!removeContents(new File(directory, ent
 ry
-(
 ))
- %7B
+)
 %0A   
@@ -3307,359 +3307,54 @@
   re
-moveContents(f);%0A%0A                    if (emptied)%0A                        emptied = f.delete();%0A                %7D else %7B%0A                    if (emptied)%0A                        emptied = f.delete();%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (directory != null && emptied)%0A            emptied = directory.delete();%0A%0A        return emptied
+turn false;%0A%0A        return directory.delete()
 ;%0A  

Change algorithm for deleting a directory