/**
 * Copyright 2011 Google 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 com.google.bitcoin.store;

import static org.junit.Assert.assertEquals;

import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.StoredBlock;

import org.junit.Test;

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

public class DerbyBlockStoreTest {
    /**
     * This path will be deleted recursively!
     */
    private static final String DB_NAME = ".bitcoinj.unittest.derby";

    @Test
    public void testStorage() throws Exception {
        deleteRecursively(new File(DB_NAME));
        NetworkParameters params = NetworkParameters.unitTests();
        Address to = new ECKey().toAddress(params);
        DerbyBlockStore store = new DerbyBlockStore(params, DB_NAME);
        store.resetStore();
        store.dump();
        // Check the first block in a new store is the genesis block.
        StoredBlock genesis = store.getChainHead();
        assertEquals(params.genesisBlock, genesis.getHeader());

        // Build a new block.
        StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
        store.put(b1);
        store.setChainHead(b1);
        store.dump();
        // Check we can get it back out again if we rebuild the store object.
        store = new DerbyBlockStore(params, DB_NAME);
        StoredBlock b2 = store.get(b1.getHeader().getHash());
        assertEquals(b1, b2);
        // Check the chain head was stored correctly also.
        assertEquals(b1, store.getChainHead());
        
        StoredBlock g1 = store.get(params.genesisBlock.getHash());
        assertEquals(params.genesisBlock, g1.getHeader());
        store.dump();
    }
    
    void deleteRecursively(File f) throws IOException {
        if (f.isDirectory()) {
            for (File c : f.listFiles())
                deleteRecursively(c);
        }
        
        if (f.exists() && !f.delete())
            throw new FileNotFoundException("Failed to delete file: " + f);
    }
}

@@ -627,54 +627,8 @@
 e;%0A%0A
-import static org.junit.Assert.assertEquals;%0A%0A
 impo
@@ -663,16 +663,16 @@
 ddress;%0A
+
 import c
@@ -795,17 +795,40 @@
 dBlock;%0A
+import org.junit.After;
 %0A
-
 import o
@@ -931,16 +931,62 @@
 ption;%0A%0A
+import static org.junit.Assert.assertEquals;%0A%0A
 public c
@@ -1117,17 +1117,23 @@
 NAME = %22
-.
+target/
 bitcoinj
@@ -1151,16 +1151,240 @@
 erby%22;%0A%0A
+    @After%0A    public void clear() %7B%0A        try %7B%0A            deleteRecursively(new File(DB_NAME));%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            // Does not really matter.%0A        %7D%0A    %7D%0A%0A
     @Tes

Use temp dir under target/ for derby test, ensure we actually do delete the directory afterwards. Resolves issue 129.