/*
 * Test byteseek handles senior bit of ~bitmask correctly
 */
package uk.gov.nationalarchives.droid.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.List;
import org.junit.*;
import static org.junit.Assert.*;
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationRequest;
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationResult;
import uk.gov.nationalarchives.droid.core.interfaces.IdentificationResultCollection;
import uk.gov.nationalarchives.droid.core.interfaces.RequestIdentifier;
import uk.gov.nationalarchives.droid.core.interfaces.resource.FileSystemIdentificationRequest;
import uk.gov.nationalarchives.droid.core.interfaces.resource.RequestMetaData;

/**
 *
 * @author rbrennan
 */
public class AnyBitmaskBugFixTest {
    
    public AnyBitmaskBugFixTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }
    
    @Test
    public void testAnyBitmask() throws Exception {
    
        BinarySignatureIdentifier droid = new BinarySignatureIdentifier();
        droid.setSignatureFile("test_sig_files/AnyBitmask_SignatureFile.xml");
        try {
            droid.init();
        } catch (SignatureParseException x) {
            assertEquals("Can't parse signature file", x.getMessage());
        }
        File file = new File("test_sig_files/test80.bit");
        assertTrue(file.exists());
        URI resourceUri = file.toURI();
  
        InputStream in = new FileInputStream(file);
        RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), "test80.bit");
        RequestIdentifier identifier = new RequestIdentifier(resourceUri);
        identifier.setParentId(1L);
        
        IdentificationRequest request = new FileSystemIdentificationRequest(metaData, identifier);
        request.open(in);

        IdentificationResultCollection resultsCollection = droid.matchBinarySignatures(request);
        List<IdentificationResult> results = resultsCollection.getResults();
        
        //byteseek 1.1
        assertEquals(0, results.size());
        
        //byteseek 1.2
//        assertEquals(1, results.size());
        
//        IdentificationResult result = results.getResults().iterator().next();
//        assertEquals("any/1", result.getPuid());
      }
}

@@ -864,334 +864,322 @@
     
-%0D%0A    public AnyBitmaskBugFixTest() %7B%0D%0A    %7D%0D%0A%0D%0A    @BeforeClass%0D%0A    public static void setUpClass() throws Exception %7B%0D%0A    %7D%0D%0A%0D%0A    @AfterClass%0D%0A    public static void tearDownClass() throws Exception %7B%0D%0A    %7D%0D%0A    %0D%0A    @Before%0D%0A    public void setUp() %7B%0D%0A    %7D%0D%0A
+private final String TESTAREA = %22test_sig_files/%22;%0D%0A    private final String SIGFILE = %22AnyBitmask_SignatureFile.xml%22;%0D%0A    private final String SCANFILE = %22AnyBitmask.bit%22;%0D%0A    %0D%0A    private final int EXPECTED_HITS = 0;  
     
-%0D%0A
    
- @After%0D%0A    public void tearDown() %7B%0D%0A    %7D%0D%0A    
+// change to 1 for byteseek 1.2%0D%0A    private final String EXPECTED_PUID = %22dev/1%22;%0D%0A
 %0D%0A  
@@ -1357,53 +1357,26 @@
 ile(
-%22test_sig_files/AnyBitmask_SignatureFile.xml%22
+TESTAREA + SIGFILE
 );%0D%0A
@@ -1581,35 +1581,27 @@
 ile(
-%22test_sig_files/test80.bit%22
+TESTAREA + SCANFILE
 );%0D%0A
@@ -1829,20 +1829,16 @@
 (), 
-%22test80.bit%22
+SCANFILE
 );%0D%0A
@@ -2287,156 +2287,116 @@
     
-//byteseek 1.1%0D%0A        assertEquals(0, results.size());%0D%0A        %0D%0A        //byteseek 1.2%0D%0A//        assertEquals(1, results.size());
+assertEquals(EXPECTED_HITS, results.size());%0D%0A        %0D%0A        while (results.iterator().hasNext()) %7B
 %0D%0A    
- 
-   %0D%0A//
     
@@ -2441,21 +2441,8 @@
 lts.
-getResults().
 iter
@@ -2461,18 +2461,16 @@
 );%0D%0A
-//
         
 asse
@@ -2457,32 +2457,36 @@
 ext();%0D%0A        
+    
 assertEquals(%22an
@@ -2486,15 +2486,21 @@
 als(
-%22any/1%22
+EXPECTED_PUID
 , re
@@ -2513,24 +2513,35 @@
 etPuid());%0D%0A
+        %7D%0D%0A
       %7D%0D%0A%7D%0D%0A

Generalised test