<commit_before>
package no.priv.garshol.duke.test;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

import no.priv.garshol.duke.Link;
import no.priv.garshol.duke.Record;
import no.priv.garshol.duke.Property;
import no.priv.garshol.duke.LinkKind;
import no.priv.garshol.duke.LinkStatus;
import no.priv.garshol.duke.RecordImpl;
import no.priv.garshol.duke.Configuration;
import no.priv.garshol.duke.JDBCLinkDatabase;
import no.priv.garshol.duke.LinkDatabaseMatchListener;

// Note that a fair amount of testing takes place already in
// LinkDatabaseMatchListenerTest. Here we only test what isn't tested
// there.

public class JDBCLinkDatabaseTest {
  private JDBCLinkDatabase linkdb;
  
  @Before
  public void setup() {
    linkdb = new JDBCLinkDatabase("org.h2.Driver", "jdbc:h2:mem", "h2",
                                  new Properties());
  }

  @After
  public void cleanup() {
    linkdb.clear();
    linkdb.close();
  }
  
  @Test
  public void testBadDatabase() {
    try {
      new JDBCLinkDatabase("org.h2.Driver", "jdbc:h2:mem", "unknowndb",
                           new Properties());
    } catch (Exception e) {
      // this is what we expect
    }
  }

  @Test
  public void testGetSinceForeverEmpty() {
    assertTrue(linkdb.getChangesSince(0).isEmpty());
  }

  @Test
  public void testGetSinceForever() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(0);
    assertEquals(2, links.size());
    assertEquals(l1, links.get(1));
    assertEquals(l2, links.get(0));
  }

  @Test
  public void testGetSinceOnlyHalf() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long thetime = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(thetime);
    assertEquals(1, links.size());
    assertEquals(l2, links.get(0));
  }

  @Test
  public void testGetBeforeOnlyHalf() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long thetime = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(0, thetime);
    assertEquals(1, links.size());
    assertEquals(l1, links.get(0));
  }

  @Test
  public void testGetBeforeAndSince() {
    long since = System.currentTimeMillis();
    pause();
    
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long before = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(since, before);
    assertEquals(1, links.size());
    assertEquals(l1, links.get(0));
  }

  @Test
  public void testGetPageOnlyHalf() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    pause();
    long thetime = System.currentTimeMillis();
    
    List<Link> links = linkdb.getChangesSince(0, thetime + 200, 1);
    assertEquals(1, links.size());
    LinkDatabaseMatchListenerTest.verifySame(l2, links.get(0));
  }
  
  @Test
  public void testOverride() {
    Link l1 = new Link("1", "2", LinkStatus.ASSERTED, LinkKind.SAME);
    linkdb.assertLink(l1);
    Link l2 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    Collection<Link> all = linkdb.getAllLinks();
    assertEquals(1, all.size());
    LinkDatabaseMatchListenerTest.verifySame(new Link("1", "2", LinkStatus.ASSERTED, LinkKind.SAME),
               all.iterator().next());
  }
  
  @Test
  public void testOverride2() {
    Link l1 = new Link("1", "2", LinkStatus.ASSERTED, LinkKind.DIFFERENT);
    linkdb.assertLink(l1);
    Link l2 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    Collection<Link> all = linkdb.getAllLinks();
    assertEquals(1, all.size());
    LinkDatabaseMatchListenerTest.verifySame(new Link("1", "2", LinkStatus.ASSERTED, LinkKind.DIFFERENT),
               all.iterator().next());
  }
  <commit_msg>One test kept failing on and off. Think it's fixed now.<commit_after>
package no.priv.garshol.duke.test;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

import no.priv.garshol.duke.Link;
import no.priv.garshol.duke.Record;
import no.priv.garshol.duke.Property;
import no.priv.garshol.duke.LinkKind;
import no.priv.garshol.duke.LinkStatus;
import no.priv.garshol.duke.RecordImpl;
import no.priv.garshol.duke.Configuration;
import no.priv.garshol.duke.JDBCLinkDatabase;
import no.priv.garshol.duke.LinkDatabaseMatchListener;

// Note that a fair amount of testing takes place already in
// LinkDatabaseMatchListenerTest. Here we only test what isn't tested
// there.

public class JDBCLinkDatabaseTest {
  private JDBCLinkDatabase linkdb;
  
  @Before
  public void setup() {
    linkdb = new JDBCLinkDatabase("org.h2.Driver", "jdbc:h2:mem", "h2",
                                  new Properties());
  }

  @After
  public void cleanup() {
    linkdb.clear();
    linkdb.close();
  }
  
  @Test
  public void testBadDatabase() {
    try {
      new JDBCLinkDatabase("org.h2.Driver", "jdbc:h2:mem", "unknowndb",
                           new Properties());
    } catch (Exception e) {
      // this is what we expect
    }
  }

  @Test
  public void testGetSinceForeverEmpty() {
    assertTrue(linkdb.getChangesSince(0).isEmpty());
  }

  @Test
  public void testGetSinceForever() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(0);
    assertEquals(2, links.size());
    assertEquals(l1, links.get(1));
    assertEquals(l2, links.get(0));
  }

  @Test
  public void testGetSinceOnlyHalf() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long thetime = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(thetime);
    assertEquals(1, links.size());
    assertEquals(l2, links.get(0));
  }

  @Test
  public void testGetBeforeOnlyHalf() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long thetime = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(0, thetime);
    assertEquals(1, links.size());
    assertEquals(l1, links.get(0));
  }

  @Test
  public void testGetBeforeAndSince() {
    long since = System.currentTimeMillis();
    pause();
    
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);

    pause();
    long before = System.currentTimeMillis();
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    List<Link> links = linkdb.getChangesSince(since, before);
    assertEquals(1, links.size());
    assertEquals(l1, links.get(0));
  }

  @Test
  public void testGetPageOnlyOne() {
    Link l1 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l1);
    pause();
    
    Link l2 = new Link("1", "3", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);
    pause();
    
    Link l3 = new Link("1", "4", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l3);
    pause();
    
    Link l4 = new Link("1", "5", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l4);
    pause();

    pause();
    long thetime = System.currentTimeMillis();
    
    List<Link> links = linkdb.getChangesSince(0, thetime + 200, 1);
    assertEquals(1, links.size());
    LinkDatabaseMatchListenerTest.verifySame(l4, links.get(0));
  }
  
  @Test
  public void testOverride() {
    Link l1 = new Link("1", "2", LinkStatus.ASSERTED, LinkKind.SAME);
    linkdb.assertLink(l1);
    Link l2 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    Collection<Link> all = linkdb.getAllLinks();
    assertEquals(1, all.size());
    LinkDatabaseMatchListenerTest.verifySame(new Link("1", "2", LinkStatus.ASSERTED, LinkKind.SAME),
               all.iterator().next());
  }
  
  @Test
  public void testOverride2() {
    Link l1 = new Link("1", "2", LinkStatus.ASSERTED, LinkKind.DIFFERENT);
    linkdb.assertLink(l1);
    Link l2 = new Link("1", "2", LinkStatus.INFERRED, LinkKind.SAME);
    linkdb.assertLink(l2);

    Collection<Link> all = linkdb.getAllLinks();
    assertEquals(1, all.size());
    LinkDatabaseMatchListenerTest.verifySame(new Link("1", "2", LinkStatus.ASSERTED, LinkKind.DIFFERENT),
               all.iterator().next());
  }
  
{'fraction_non_alphanumeric': 0.1431959679810258, 'fraction_numerical': 0.019171854926376124, 'mean_word_length': 3.4758956214064574, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3633242', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3438, 'n_words': 718}