Package net.sf.collabreview.reputation

Source Code of net.sf.collabreview.reputation.ReputationMetricTest

/*
   Copyright 2012 Christian Prause and Fraunhofer FIT

   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 net.sf.collabreview.reputation;

import net.sf.collabreview.core.ArtifactIdentifier;
import net.sf.collabreview.core.CollabReview;
import net.sf.collabreview.core.Repository;
import net.sf.collabreview.core.configuration.AutoConfigurator;
import net.sf.collabreview.core.users.Author;
import net.sf.collabreview.core.users.AuthorManager;
import net.sf.collabreview.importing.ImportProgressInfo;
import net.sf.collabreview.repository.Review;
import junit.framework.TestCase;

import java.util.*;

/**
* Test cases to test the ReputationMetricManager and several ReputationMetrics.
* <p/>
* The tests depend on the changes that previous tests applied to the repository. It is important to run them
* all in the same order when testing.
*
* @author Christian Prause (chris)
* @date 2010-08-02 10:55:08
*/
@SuppressWarnings({"deprecation"})
public class ReputationMetricTest extends TestCase {
  Author alice, bob, chris, daniel, vader, mrx;

  static CollabReview collabReview;

  static ReputationMetricManager rmm;

  AuthorManager authorManager;

  ArtifactIdentifier helloWorld = new ArtifactIdentifier("/src/com/foobar/research/HelloWorld.java", 10, "");
  ArtifactIdentifier oldHelloWorld = new ArtifactIdentifier("/src/com/foobar/research/HelloWorld.java", 3, "");
  ArtifactIdentifier deathStar = new ArtifactIdentifier("/src/com/foobar/research/DeathStar.java", 5, "");
  ArtifactIdentifier foomatic = new ArtifactIdentifier("/src/com/foobar/research/Foomatic.java", 8, "");
  ArtifactIdentifier foomatic2 = new ArtifactIdentifier(foomatic.getName(), 50, foomatic.getBranch());
  ArtifactIdentifier foomatic3 = new ArtifactIdentifier(foomatic.getName(), 55, foomatic.getBranch());
  ArtifactIdentifier newOne = new ArtifactIdentifier("anewone.java", 100, "");

  Repository repo;

  private void loadAuthors() {
    alice = authorManager.createAuthor("alice");
    assert alice != null;
    bob = authorManager.createAuthor("bob");
    assert bob != null;
    chris = authorManager.createAuthor("chris");
    assert chris != null;
    daniel = authorManager.createAuthor("daniel");
    assert daniel != null;
    vader = authorManager.createAuthor("vader");
    assert vader != null;
    mrx = authorManager.createAuthor("mrx");
  }

  @Override
  protected void setUp() throws Exception {
    System.out.println("Go!");
    Thread.sleep(50);
    if (collabReview == null) {
      collabReview = AutoConfigurator.newConfiguredApplication();
      rmm = collabReview.getReputationMetricManager();
      authorManager = collabReview.getAuthorManager();
      repo = collabReview.getRepository();
      loadAuthors();
      importDataFromDemoRepository();
    } else {
      authorManager = collabReview.getAuthorManager();
      repo = collabReview.getRepository();
      loadAuthors();
    }
  }

  public void tearDown() throws InterruptedException {
    repo.commit();
    System.out.println("Done!");
    Thread.sleep(50);
  }

  private void standardMetricTests(ReputationMetric m, String[] users, Float[] values) {
    // some output
    for (String user : m.listContributors()) {
      System.out.println(user + ": " + m.getAuthorScores().get(user));
    }
    System.out.println("Expected scores: " + Arrays.toString(values));
    // test results
    Map<String, Float> scores = m.getAuthorScores();
    assertEquals(users.length, scores.size());
    int index = 0;
    for (String user : scores.keySet()) {
      assertEquals(users[index], user);
      assertTrue("Expected=" + values[index] + ", actual=" + scores.get(user), Math.abs(values[index] - scores.get(user)) < 0.000125);
      index++;
    }
  }

  private ReputationMetric findMetric(String key) {
    ReputationMetric metric = rmm.findReputationMetric(key);
    if (metric != null && key.equals(metric.getName())) {
      return metric;
    }
    fail("metric \"" + key + "\" not found");
    return null;
  }


  /**
   * This method loads some data into the repository; taking it from the Demo repository.
   * It shall and needs only be invoked once before the tests are run.
   * Tests use data of revisions 1 to 10.
   *
   * @throws InterruptedException if sleep aborts
   */
  public void importDataFromDemoRepository() throws InterruptedException {
    AutoConfigurator.newConfiguredApplication().getRepository().reset();
    ImportProgressInfo.State state = collabReview.getImporter().start().getProgressInfo().getState();
    while (state != ImportProgressInfo.State.sleeping) {
      assertNotSame(ImportProgressInfo.State.retryAfterException, state);
      Thread.sleep(50);
      state = collabReview.getImporter().getProgressInfo().getState();
    }
    System.out.println("In the repository: " + repo.listNonObsoleteArtifacts(null));
    assertEquals("Too many revisions in demo repository. The other tests will probably fail!", 11, repo.size());
    loadAuthors();
    repo.setReview(helloWorld, alice, 10, "", true);
    repo.setReview(deathStar, alice, 10, "Oh yeah, baby!", true);
    repo.setReview(foomatic, alice, 10, "Perfect", false);
    repo.setReview(oldHelloWorld, bob, 5, "Well, well, it could be a bit better", true);
    repo.setReview(deathStar, bob, 8, "", true);
    repo.setReview(foomatic, chris, -10, "No no good for me, I don't need nobody", false);
    repo.setReview(deathStar, vader, 7, "Schnippdischnappdi", true);
    repo.commit();
    // Hack: there is a strange behavior from Hibernate to crash if setting a review to rejected and then getting another one from the DB
    Review r1 = repo.getReview(helloWorld.getName(), helloWorld.getBranch(), alice);
    Review r2 = repo.getReview(deathStar.getName(), deathStar.getBranch(), alice);
    r1.setReject(bob, "");
    r2.setReject(bob, "no comment on this");
    repo.commit();
  }

  public void testInitialization() {
    assertEquals(9, rmm.listReputationMetrics().size());
    assertEquals(MetricQuality.class, rmm.listReputationMetrics().get(0).getClass());
    assertEquals(MetricQuantity.class, rmm.listReputationMetrics().get(1).getClass());
    assertTrue(rmm.isUptodate());
    rmm.updateNow();
    assertTrue(rmm.isUptodate());
  }

  public void testQuantity() {
    ReputationMetric m = findMetric("quantity");
    standardMetricTests(m,
        new String[]{daniel.getName(), alice.getName(), vader.getName(), bob.getName(), chris.getName()},
        new Float[]{854f, 770f, 211f, 156f, 123.99999f});
  }

  public void testQuality() {
    ReputationMetric m = findMetric("quality");
    // vader: 7.5  chris: 5.0  alice: 4.7272725  bob: 4.519231  daniel: 2.8249414
    standardMetricTests(m,
        new String[]{vader.getName(), chris.getName(), alice.getName(), bob.getName(), daniel.getName()},
        new Float[]{7.5f, 5.0f, 4.7272725f, 4.519231f, 2.8249414f});
  }

  public void testBusyReview() {
    ReputationMetric m = findMetric("busyreview");
    standardMetricTests(m,
        new String[]{alice.getName(), bob.getName(), chris.getName(), vader.getName()},
        new Float[]{3.0f, 2.0f, 1.0f, 1.0f});
  }

  public void testRatingBias() {
    ReputationMetric m = findMetric("bias");
    standardMetricTests(m,
        new String[]{alice.getName(), vader.getName(), bob.getName(), chris.getName()},
        new Float[]{10.0f, 7.0f, 6.5f, -10f});
  }

  public void testReviewAgedCount() {
    ReputationMetric m = findMetric("agedcount");
    standardMetricTests(m,
        new String[]{alice.getName(), bob.getName(), chris.getName(), vader.getName()},
        new Float[]{3.0f, 1.0949612f, 1.0f, 1.0f});
  }

  public void testMetaMetric() {
    System.out.println(findMetric("quality").getAuthorScores());
    System.out.println(findMetric("agedcount").getAuthorScores());
    ReputationMetric m = findMetric("xxx");
    // vader: 8.5  alice: 7.7272725  chris: 6.0  bob: 5.614192  daniel: 2.8249414
    standardMetricTests(m,
        new String[]{vader.getName(), alice.getName(), chris.getName(), bob.getName(), daniel.getName()},
        new Float[]{8.5f, 7.7272725f, 6.0f, 5.614192f, 2.8249414f});
  }

  public void testUpdateOnReviewChange() {
    assertTrue(rmm.isUptodate());
    System.out.println("Original quality values: " + findMetric("quality").getAuthorScores());
    repo.setReview(deathStar, vader, 5, "Schnippdischnappdi", true);
    assertTrue(rmm.isUptodate());
    System.out.println("Quality values after new review: " + findMetric("quality").getAuthorScores());
    assertTrue(findMetric("quality").isUpToDate());
    assertTrue(findMetric("quantity").isUpToDate());
    assertTrue(findMetric("busyreview").isUpToDate());
    assertTrue(findMetric("xxx").isUpToDate());
    Map<String, Float> qualityThroughUpdate = new TreeMap<String, Float>(findMetric("quality").getAuthorScores());
    Map<String, Float> quantityThroughUpdate = new TreeMap<String, Float>(findMetric("quantity").getAuthorScores());
    Map<String, Float> busyreviewThroughUpdate = new TreeMap<String, Float>(findMetric("busyreview").getAuthorScores());
    Map<String, Float> xxxThroughUpdate = new TreeMap<String, Float>(findMetric("xxx").getAuthorScores());
    rmm.updateNow();
    assertTrue(rmm.isUptodate());
    assertEquals(findMetric("quality").getAuthorScores(), qualityThroughUpdate);
    assertEquals(findMetric("quantity").getAuthorScores(), quantityThroughUpdate);
    assertEquals(findMetric("busyreview").getAuthorScores(), busyreviewThroughUpdate);
    assertEquals(findMetric("xxx").getAuthorScores(), xxxThroughUpdate);
  }

  public void testRatingBiasUpdate() {
    ReputationMetric m = findMetric("bias");
    assertEquals(-10f, m.getAuthorScore(chris.getName()));
    assertEquals(10f, m.getAuthorScore(alice.getName()));
    repo.setReview(foomatic, chris, -8, "No, no still not so good", false);
    repo.setReview(helloWorld, alice, 7, "oh, it's a bit worse than I thought", true);
    assertEquals(-8f, m.getAuthorScore(chris.getName()));
    assertEquals(9f, m.getAuthorScore(alice.getName()));
  }

  public void testUpdateReviewAgedCount() throws Exception {
    String newContent = "// blub blub //\n" + repo.getArtifact(foomatic).getContent().substring(200);
    repo.getArtifact(foomatic).setObsoleteDate(new Date());
    repo.addArtifact(foomatic2, new Date(), newContent, mrx);
    repo.commit();
    ReputationMetric m = findMetric("agedcount");
    assertTrue(m.isUpToDate());
    compareTimelinessFor(m, alice);
    compareTimelinessFor(m, bob);
    compareTimelinessFor(m, vader);
    compareTimelinessFor(m, chris);
    standardMetricTests(m,
        new String[]{alice.getName(), bob.getName(), vader.getName(), chris.getName()},
        new Float[]{2.5670730f, 1.0949612f, 1.0f, 0.56707317f});
    assertTrue(findMetric("quality").isUpToDate());
  }

  private void compareTimelinessFor(ReputationMetric metric, Author author) {
    float sum = 0;
    for (Review review : repo.listReviewsBy(author)) {
      sum += review.getTimeliness();
    }
    assertEquals(sum, metric.getAuthorScore(author.getName()));
  }

  public void testMetaMetricAndItsUpdating() {
    if (!rmm.isUptodate()) {
      rmm.updateNow();
    }
    ReputationMetric m = findMetric("xxx");
    assertNotNull(m);
    assertTrue(m.isUpToDate());
    assertNotNull(m.getAuthorScore(mrx.getName()));
    assertTrue(m.getAuthorScore(mrx.getName()) < 1);
    // alice: 9.062282  bob: 7.8437605  vader: 7.5  chris: 7.3936224  daniel: 4.2239904  mrx: 0.9999999
    standardMetricTests(m,
        new String[]{alice.getName(), bob.getName(), vader.getName(), chris.getName(), daniel.getName(), mrx.getName()},
        new Float[]{9.062282f, 7.8437605f, 7.5f, 7.3936224f, 4.2239904f, 0.9999999f});
    repo.setReview(foomatic, mrx, 3, "habe nichts zu sagen...", true);
    repo.commit();
    rmm.updateNow();
    assertTrue(m.isUpToDate());
    // alice: 9.099705  bob: 7.8437605  vader: 7.5  chris: 7.3936224  daniel: 4.510418  mrx: 2.2337399
    standardMetricTests(m,
        new String[]{alice.getName(), bob.getName(), vader.getName(), chris.getName(), daniel.getName(), mrx.getName()},
        new Float[]{9.099705f, 7.8437605f, 7.5f, 7.3936224f, 4.510418f, 2.2337399f});
    assertTrue(m.getAuthorScore(mrx.getName()) > 2.2);
  }

  public void testDefaultScore() {
    ReputationMetric qualityMetric = findMetric("quality");
    ReputationMetric quantityMetric = findMetric("quantity");
    ReputationMetric scoreMetric = findMetric("score");
    assertEquals(qualityMetric.listContributors().size(), scoreMetric.listContributors().size());
    assertEquals(quantityMetric.listContributors().size(), scoreMetric.listContributors().size());
    for (String user : scoreMetric.listContributors()) {
      assertEquals(qualityMetric.getAuthorScore(user) * quantityMetric.getAuthorScore(user),
          scoreMetric.getAuthorScore(user));
    }
  }

  public void testBugReviewUpdateAfterArtifactUpdate() throws Exception {
    // Test to reproduce a bug that occurred when a review was updated after the respective artifact was updated.
    // First update an artifact than revise the review
    assert repo.getArtifact(foomatic2) != null;
    assert repo.getArtifact(foomatic2).getContent() != null;
    String newContent = "// The next revision //\n" + repo.getArtifact(foomatic2).getContent();
    repo.getArtifact(foomatic2).setObsoleteDate(new Date());
    repo.addArtifact(foomatic3, new Date(), newContent, vader);
    repo.commit();
    repo.setReview(foomatic3, chris, 10, "dat haste jut jemacht", true); // crashes without the bug fix
    repo.commit();
  }

  public void testAddArtifactUnderNewName() throws Exception {
    // test to reproduce bug that occured when an artifact with a new name was added
    assert rmm != null; // just that static analysis doesn't complain...
    repo.addArtifact(newOne, new Date(), "new content", alice);
    repo.commit();
  }

  public void testLoneRangerMetricWithUpdate() {
    ReputationMetric loneranger = rmm.findReputationMetric("loneranger");
    System.out.println(loneranger.getAuthorScores());
    assertEquals(0, loneranger.listContributors().size());
    repo.setReview(newOne, alice, 5, "tschakka!", true);
    repo.commit();
    System.out.println(loneranger.getAuthorScores());
    assertEquals(1, loneranger.listContributors().size());
    assertEquals(alice.getName(), loneranger.listContributors().iterator().next());
    repo.setReview(newOne, bob, 3, "hossa!", false);
    repo.commit();
    System.out.println(loneranger.getAuthorScores());
    assertEquals(1, loneranger.listContributors().size());
    assertEquals(0f, loneranger.getAuthorScore(alice.getName()));
  }

  public void testMetaMetricDivide() {
    ReputationMetric meta = rmm.findReputationMetric("averagereviewage");
    ReputationMetric aged = rmm.findReputationMetric("agedcount");
    ReputationMetric total = rmm.findReputationMetric("busyreview");
    System.out.println(meta.getAuthorScores());
    for (String user : meta.listContributors()) {
      System.out.println(String.format("*** %f / %f = %f ***", aged.getAuthorScore(user), total.getAuthorScore(user), meta.getAuthorScore(user)));
      assertEquals(aged.getAuthorScore(user) / total.getAuthorScore(user), meta.getAuthorScore(user));
    }
    // chris: 1.0  vader: 1.0  alice: 0.8824786  bob: 0.6983204  mrx: 0.5299145
    standardMetricTests(meta,
        new String[]{chris.getName(), vader.getName(), alice.getName(), bob.getName(), mrx.getName()},
        new Float[]{1.0f, 1.0f, 0.8824786f, 0.6983204f, 0.5299145f});
  }

  public void testTrivialQuality() {
    repo.reset();
    Repository oldRepo = repo;
    collabReview = AutoConfigurator.newConfiguredApplication();
    rmm = collabReview.getReputationMetricManager();
    authorManager = collabReview.getAuthorManager();
    repo = collabReview.getRepository();
    assert oldRepo != repo;
    repo.addArtifact(helloWorld, new Date(), "abc\n123\nbla\nblub", alice);
    repo.addArtifact(deathStar, new Date(), "xyz\n123\nbla", bob);
    repo.addArtifact(foomatic, new Date(), "123\nblub\ndfgh\nert\nasdf", chris);
    repo.setReview(helloWorld, alice, 6, "1", true);
    repo.setReview(deathStar, alice, 6, "2", true);
    repo.setReview(foomatic, alice, 6, "3", false);
    repo.setReview(deathStar, bob, 6, "4", true);
    repo.setReview(foomatic, chris, 6, "5", false);
    repo.commit();
    assertTrue(rmm.isUptodate());
    System.out.println(repo.listAllArtifacts());
    System.out.println(repo.listReviewsBy(alice));
    ReputationMetric m = findMetric("quality");
    standardMetricTests(m,
        new String[]{chris.getName(), alice.getName(), bob.getName()},
        new Float[]{6.0f, 6.0f, 6.0f});
  }
}
TOP

Related Classes of net.sf.collabreview.reputation.ReputationMetricTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.