<commit_before>/*
 * Copyright 2008 the original author or authors.
 * 
 * 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.openehealth.ipf.commons.lbs.store;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openehealth.ipf.commons.lbs.store.DiskStore;
import org.openehealth.ipf.commons.lbs.store.FileSystemLayoutStrategy;
import org.openehealth.ipf.commons.lbs.store.FlatFileSystemLayout;
import org.openehealth.ipf.commons.lbs.store.FlatUriUuidConversion;
import org.openehealth.ipf.commons.lbs.store.ResourceIOException;


/**
 * @author <NAME>
 */
public class DiskStoreTest extends LargeBinaryStoreTest {
    private File storeLocation;
    private DiskStore diskStore;
    private static final URI baseUri = URI.create("http://localhost/");
    private FlatUriUuidConversion diskStoreStrategy;
    private FlatFileSystemLayout layoutStrategy;
    
    @Before
    public void setUp() throws Exception {
        StoreRegistration.reset();
        
        File temp = File.createTempFile(getClass().getName(), "");
        temp.delete();
        storeLocation = new File(temp.getAbsolutePath());
        storeLocation.mkdir();
        
        diskStoreStrategy = new FlatUriUuidConversion(baseUri);
        layoutStrategy = new FlatFileSystemLayout(storeLocation);
        
        diskStore = new DiskStore(layoutStrategy, diskStoreStrategy);
        setStore(diskStore);
    }
    
    @After
    public void tearDown() throws Exception {
        if (diskStore != null) {
            diskStore.deleteAll();
            if (storeLocation.exists()) {
                storeLocation.setWritable(true);
                FileUtils.deleteDirectory(storeLocation);
            }
        }

        StoreRegistration.reset();
    }
    
    @Test
    public void testConstructorWithNull() throws Exception {
        assertIllegalArg(new Code() { public void run() {
            new DiskStore(layoutStrategy, null);
        }});

        assertIllegalArg(new Code() { public void run() {
            new DiskStore(null, diskStoreStrategy);
        }});
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testConstructorWithNonExistingLocation() {        
        File nonExistingLocation = new File("/" + UUID.randomUUID().toString());
        FlatFileSystemLayout invalidLayout = 
            new FlatFileSystemLayout(nonExistingLocation);
        
        new DiskStore(invalidLayout, diskStoreStrategy);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructorWithFileAsLocation() throws IOException {
        File file = File.createTempFile(getClass().getName(), "txt");
        final FlatFileSystemLayout invalidLayout = 
            new FlatFileSystemLayout(file);
        
        try {
            new DiskStore(invalidLayout, diskStoreStrategy);
        }
        finally {
            file.delete();
        }
    }
     
    @Test(expected = IllegalArgumentException.class)
    @Ignore
    public void testConstructorWithReadOnlyLocation() {
        storeLocation.setWritable(false);
        final FlatFileSystemLayout invalidLayout = 
            new FlatFileSystemLayout(storeLocation);

        try {
            new DiskStore(invalidLayout, diskStoreStrategy);
        }
        finally {
            storeLocation.setWritable(true);
        }
    }
    
    @Ignore
    @Test(expected = ResourceIOException.class)
    public void testDeleteResourceButCannotDeleteFileOnDisk() throws Exception {
        byte[] binary = new byte[] { 0, 1 };
        URI resourceUri = diskStore.add(binary);
        File resource = getFileFromUri(resourceUri);
        
        // Open the file to ensure that it cannot be removed
        InputStream stream = new FileInputStream(resource);
        try {
            diskStore.delete(resourceUri);
        }
        finally {
            stream.close();
        }
    }

    @Ignore
    @Test(expected = ResourceIOException.class)
    public void testDeleteAllResourcesButCannotDeleteFileOnDisk() throws Exception {
        byte[] binary = new byte[] { 0, 1 };
        URI resourceUri = diskStore.add(binary);
        File resource = getFileFromUri(resourceUri);
        
        // Open the file to ensure that it cannot be removed<commit_msg>Better impl for testConstructorWithReadOnlyLocation (fails on some Windows systems)<commit_after>/*
 * Copyright 2008 the original author or authors.
 * 
 * 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.openehealth.ipf.commons.lbs.store;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;


/**
 * @author <NAME>
 */
public class DiskStoreTest extends LargeBinaryStoreTest {
    private File storeLocation;
    private DiskStore diskStore;
    private static final URI baseUri = URI.create("http://localhost/");
    private FlatUriUuidConversion diskStoreStrategy;
    private FlatFileSystemLayout layoutStrategy;
    private Log log = LogFactory.getLog(DiskStoreTest.class);
    
    @Before
    public void setUp() throws Exception {
        StoreRegistration.reset();
        
        File temp = File.createTempFile(getClass().getName(), "");
        temp.delete();
        storeLocation = new File(temp.getAbsolutePath());
        if (!storeLocation.mkdir()){
            log.error("Unable to create the directory in " + storeLocation);
        }
        
        
        diskStoreStrategy = new FlatUriUuidConversion(baseUri);
        layoutStrategy = new FlatFileSystemLayout(storeLocation);
        
        diskStore = new DiskStore(layoutStrategy, diskStoreStrategy);
        setStore(diskStore);
    }
    
    @After
    public void tearDown() throws Exception {
        if (diskStore != null) {
            diskStore.deleteAll();
            if (storeLocation.exists()) {
                storeLocation.setWritable(true);
                FileUtils.deleteDirectory(storeLocation);
            }
        }

        StoreRegistration.reset();
    }
    
    @Test
    public void testConstructorWithNull() throws Exception {
        assertIllegalArg(new Code() { public void run() {
            new DiskStore(layoutStrategy, null);
        }});

        assertIllegalArg(new Code() { public void run() {
            new DiskStore(null, diskStoreStrategy);
        }});
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testConstructorWithNonExistingLocation() {        
        File nonExistingLocation = new File("/" + UUID.randomUUID().toString());
        FlatFileSystemLayout invalidLayout = 
            new FlatFileSystemLayout(nonExistingLocation);
        
        new DiskStore(invalidLayout, diskStoreStrategy);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructorWithFileAsLocation() throws IOException {
        File file = File.createTempFile(getClass().getName(), "txt");
        final FlatFileSystemLayout invalidLayout = 
            new FlatFileSystemLayout(file);
        
        try {
            new DiskStore(invalidLayout, diskStoreStrategy);
        }
        finally {
            file.delete();
        }
    }
     
    @Test(expected = IllegalArgumentException.class)
    public void testConstructorWithReadOnlyLocation() {
        if(storeLocation.setWritable(false) && storeLocation.canWrite()){
            //In this case both storeLocation.setWritable(false) returned true
            //and storeLocation.canWrite() returns true, 
            //which is obviously wrong!
            String msg = "Unable to make a location " 
                         + storeLocation 
                         + " non-writable "
                         + "on platform " 
                         + System.getProperty("os.name");
            log.error(msg);
            throw new IllegalArgumentException(msg);     
            
        } else {
            storeLocation.setWritable(false);
            final FlatFileSystemLayout invalidLayout = new FlatFileSystemLayout(storeLocation);
            try {
                new DiskStore(invalidLayout, diskStoreStrategy);
            } 
            finally {
                storeLocation.setWritable(true);
            }
        } 
    }
    
    @Ignore
    @Test(expected = ResourceIOException.class)
    public void testDeleteResourceButCannotDeleteFileOnDisk() throws Exception {
        byte[] binary = new byte[] { 0, 1 };
        URI resourceUri = diskStore.add(binary);
        File resource = getFileFromUri(resourceUri);
        
        // Open the file to ensure that it cannot be removed
        InputStream stream = new FileInputStream(resource);
        try {
            diskStore.delete(resourceUri);
        }
        finally {
            stream.close();
        }
    }

    @Ignore
    @Test(expected = ResourceIOException.class)
    public void testDeleteAllResourcesButCannotDeleteFileOnDisk() throws Exception {
        byte[] binary = new byte[] { 0, 1 };
        URI resourceUri = diskStore.add(binary);
        File resource = getFileFromUri(resourceUri);
        
        // Open the file to ensure that it cannot be removed
{'fraction_non_alphanumeric': 0.08758181985802527, 'fraction_numerical': 0.0022125933437816907, 'mean_word_length': 2.5602231703314735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2408422', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2712, 'n_words': 726}