/*
 * Copyright 2013 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 org.bitcoinj.store;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.params.UnitTestParams;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertEquals;

public class SPVBlockStoreTest {

    @Test
    public void basics() throws Exception {
        NetworkParameters params = UnitTestParams.get();
        File f = File.createTempFile("spvblockstore", null);
        f.delete();
        f.deleteOnExit();
        SPVBlockStore store = new SPVBlockStore(params, f);

        Address to = new ECKey().toAddress(params);
        // Check the first block in a new store is the genesis block.
        StoredBlock genesis = store.getChainHead();
        assertEquals(params.getGenesisBlock(), genesis.getHeader());
        assertEquals(0, genesis.getHeight());


        // Build a new block.
        StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
        store.put(b1);
        store.setChainHead(b1);
        store.close();

        // Check we can get it back out again if we rebuild the store object.
        store = new SPVBlockStore(params, f);
        StoredBlock b2 = store.get(b1.getHeader().getHash());
        assertEquals(b1, b2);
        // Check the chain head was stored correctly also.
        StoredBlock chainHead = store.getChainHead();
        assertEquals(b1, chainHead);
    }
}

@@ -26,16 +26,53 @@
 le Inc.%0A
+ * Copyright 2018 Andreas Schildbach%0A
  *%0A * Li
@@ -650,24 +650,92 @@
 inj.store;%0A%0A
+import static org.junit.Assert.assertEquals;%0A%0Aimport java.io.File;%0A%0A
 import org.b
@@ -934,51 +934,23 @@
 nit.
-Test;%0A%0Aimport java.io.Fil
+Befor
 e;%0A
-%0A
 import 
-static 
 org.
@@ -955,35 +955,20 @@
 g.junit.
-Assert.assertEquals
+Test
 ;%0A%0Apubli
@@ -999,70 +999,32 @@
 t %7B%0A
-%0A    @Test%0A    public void basics() throws Exception %7B%0A       
+    private static final
  Net
@@ -1038,22 +1038,24 @@
 ameters 
-params
+UNITTEST
  = UnitT
@@ -1079,18 +1079,115 @@
     
-    File f
+private File blockStoreFile;%0A%0A    @Before%0A    public void setup() throws Exception %7B%0A        blockStoreFile
  = F
@@ -1233,25 +1233,38 @@
 l);%0A        
-f
+blockStoreFile
 .delete();%0A 
@@ -1270,17 +1270,30 @@
         
-f
+blockStoreFile
 .deleteO
@@ -1297,24 +1297,85 @@
 teOnExit();%0A
+    %7D%0A%0A    @Test%0A    public void basics() throws Exception %7B%0A
         SPVB
@@ -1410,25 +1410,40 @@
 ckStore(
-params, f
+UNITTEST, blockStoreFile
 );%0A%0A    
@@ -1481,22 +1481,24 @@
 Address(
-params
+UNITTEST
 );%0A     
@@ -1635,22 +1635,24 @@
 tEquals(
-params
+UNITTEST
 .getGene
@@ -1732,17 +1732,16 @@
 ht());%0A%0A
-%0A
         
@@ -2054,17 +2054,32 @@
 ore(
-params, f
+UNITTEST, blockStoreFile
 );%0A 
@@ -2324,11 +2324,469 @@
 );%0A    %7D
+%0A%0A    @Test(expected = BlockStoreException.class)%0A    public void twoStores_onSameFile() throws Exception %7B%0A        new SPVBlockStore(UNITTEST, blockStoreFile);%0A        new SPVBlockStore(UNITTEST, blockStoreFile);%0A    %7D%0A%0A    @Test%0A    public void twoStores_butSequentially() throws Exception %7B%0A        SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile);%0A        store.close();%0A        store = new SPVBlockStore(UNITTEST, blockStoreFile);%0A    %7D
 %0A%7D%0A

Add tests for file locking.