public void testArchivedStorage()
      throws MalformedURLException, CoreException, IOException, NoSuchFieldException,
          IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
          InvocationTargetException {
    IssueStorage storage = IssueStorage.getInstance();
    long ts = System.currentTimeMillis();
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";

    storage.storeArchivedQueryIssues(url, qName, new String[] {id1, id2});
    Map<String, Long> read = storage.readArchivedQueryIssues(url, qName);

    assertEquals(2, read.size());
    assertTrue(ts <= read.get(id1));
    assertTrue(ts <= read.get(id2));

    // wait a sec and set TTL so that the archived issues shuld be cleaned up
    try {
      Thread.currentThread().sleep(1000);
    } catch (InterruptedException ex) {
      Exceptions.printStackTrace(ex);
    }
    Field f = BugtrackingConfig.class.getDeclaredField("DEFAULT_ARCHIVED_TTL");
    f.setAccessible(true);
    f.set(BugtrackingConfig.getInstance(), new Long(0)); // zero time to live

    read = storage.readArchivedQueryIssues(url, qName);
    assertEquals(0, read.size());
  }
  public void testStorage() throws MalformedURLException, CoreException, IOException {
    final IssueStorage storage = IssueStorage.getInstance();

    Map<String, String> attr1 = new HashMap<String, String>();
    attr1.put("dummy1", "dummy3");
    attr1.put("dummy2", "dummy2");
    attr1.put("dummy3", "dummy1");
    Map<String, String> attr2 = new HashMap<String, String>();
    attr2.put("dummy5", "dummy7");
    attr2.put("dummy6", "dummy6");
    attr2.put("dummy7", "dummy5");
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";
    DummyIssue i1 = new DummyIssue(id1, attr1);
    DummyIssue i2 = new DummyIssue(id2, attr2);

    storage.storeQuery(url, qName, new String[] {id1, id2});

    long lm = System.currentTimeMillis();
    IssueCache<DummyIssue, Object> cache = getCache();
    IssueEntry ie1 = cache.new IssueEntry(i1, i1.id, attr1, -1, -1, false, lm);
    IssueEntry ie2 = cache.new IssueEntry(i2, i2.id, attr2, -1, -1, false, lm);

    storage.storeIssue(url, ie1);
    storage.storeIssue(url, ie2);

    List<String> issues = storage.readQuery(url, qName);
    assertTrue(issues.contains(id1));
    assertTrue(issues.contains(id2));

    ie1 = cache.new IssueEntry(i1, i1.id, null, -1, -1, false, lm);
    ie2 = cache.new IssueEntry(i2, i2.id, null, -1, -1, false, lm);
    storage.readIssue(url, ie1);
    if (ie1.getSeenAttributes() == null) fail("missing issue id [" + id1 + "]");
    assertAttribute(ie1.getSeenAttributes(), "dummy1", "dummy3");
    assertAttribute(ie1.getSeenAttributes(), "dummy2", "dummy2");
    assertAttribute(ie1.getSeenAttributes(), "dummy3", "dummy1");
    storage.readIssue(url, ie2);
    if (ie2.getSeenAttributes() == null) fail("missing issue id [" + id2 + "]");
    assertAttribute(ie2.getSeenAttributes(), "dummy5", "dummy7");
    assertAttribute(ie2.getSeenAttributes(), "dummy6", "dummy6");
    assertAttribute(ie2.getSeenAttributes(), "dummy7", "dummy5");

    // create another query
    String qName2 = "SomeQuery2";
    storage.storeQuery(url, qName2, new String[] {id1, id2});
    issues = storage.readQuery(url, qName2);
    assertEquals(2, issues.size());

    // remove it
    storage.removeQuery(url, qName2);
    issues = storage.readQuery(url, qName2);
    // it's gone
    assertEquals(0, issues.size());
    // first query still exists
    issues = storage.readQuery(url, qName);
    assertEquals(2, issues.size());
  }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   System.setProperty("netbeans.user", getWorkDir().getAbsolutePath());
   emptyStorage();
 }
  public void testCleanup()
      throws MalformedURLException, CoreException, IOException, NoSuchFieldException,
          IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
          InvocationTargetException {
    IssueStorage storage = IssueStorage.getInstance();
    long ts = System.currentTimeMillis();
    Map<String, String> attr = new HashMap<String, String>();
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";

    DummyIssue i1 = new DummyIssue(id1, attr);
    DummyIssue i2 = new DummyIssue(id2, attr);

    long lm = System.currentTimeMillis();
    IssueCache<DummyIssue, Object> cache = getCache();
    IssueEntry ie1 = cache.new IssueEntry(i1, i1.id, attr, -1, -1, false, lm);
    IssueEntry ie2 = cache.new IssueEntry(i2, i2.id, attr, -1, -1, false, lm);

    storage.storeIssue(url, ie1);
    storage.storeIssue(url, ie2);

    // store query
    storage.storeQuery(url, qName, new String[] {id1, id2});
    List<String> stored = storage.readQuery(url, qName);

    assertEquals(2, stored.size());
    File folder = getNameSpaceFolder(url);
    File[] issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are there

    // cleanup, yet issues are living in a stored query
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are still there

    // cleanup, yet issues are living in archive
    storage.storeQuery(url, qName, new String[] {});
    storage.storeArchivedQueryIssues(url, qName, new String[] {id1, id2});
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are still there

    // cleanup, issues aren't in a query or archived
    storage.storeQuery(url, qName, new String[] {});
    storage.storeArchivedQueryIssues(url, qName, new String[] {});
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(0, issueFiles.length); // issues are still there
  }