// @Test
  public void testCountByMemberCenterDate() throws Exception {
    initTestContext("../test-data/collections/test-collections.csv");

    Membership membership = null;
    for (final Membership m : DAIRY.getMemberships()) {
      if (m.getMemberNumber().equals("1975") || m.getMemberNumber().equals("01975")) {
        membership = m;
        break;
      }
    }

    final DairyLocation centerBad = DairyFactory.eINSTANCE.createDairyLocation();
    centerBad.setName("test");
    centerBad.setLocation(DairyUtil.createLocation(null, null, null));
    centerBad.setCode("RXXX");
    DAIRY.getBranchLocations().add(centerBad);
    repo.save(DAIRY);

    final DairyLocation centerGood = centers.get("R012");
    final Date date = DateFormat.getDateInstance().parse("June 20, 2010");

    assertEquals(1, repo.countByMemberCenterDate(membership, centerGood, date));
    assertEquals(0, repo.countByMemberCenterDate(membership, centerBad, date));
    // assertEquals(0, repo.countByMemberCenterDate(null, null, null));
  }
  // @Test
  public void testGetMembersWithDeliveriesFor() throws Exception {
    initTestContext("../test-data/collections/test-collections.csv");

    createMember("newMember");

    List<Membership> members = repo.getMembersWithDeliveriesFor(6, 2010);
    assertEquals(3, members.size());
    members = repo.getMembersWithDeliveriesFor(7, 2010);
    assertEquals(2, members.size());
  }
  @Test
  public void testStatistics() throws Exception {
    //		repo = liveInjector.getInstance(ICollectionJournalLineRepository.class);
    Map<String, Double> statMap;

    statMap = repo.collectionStatistics(createDate(2010, 5, 1), createDate(2010, 5, 2), null, null);
    assertNotNull(statMap);

    statMap = repo.collectionStatistics(createDate(2010, 5, 1), createDate(2010, 5, 1), null, null);
    assertNotNull(statMap);

    System.err.println(statMap.entrySet());
  }
  private void initTestContext(String testFile) throws Exception {
    // testPM = new HsqldbMemoryPersistenceManager();
    // System.setProperty("riena.test", "true");
    // PersistenceManager.reset(testPM);

    initSampleDairy();

    if (testFile != null) {
      System.err.println("importing " + testFile);
      final ScaleImporter importer = new ScaleImporter(new File(testFile));
      importer.readRecords();
      final List<ScaleRecord> results = importer.getResults();

      for (final ScaleRecord record : results) {
        final CollectionJournalLine line = DairyFactory.eINSTANCE.createCollectionJournalLine();
        line.setRecordedMember(record.getMemberNumber());
        line.setValidatedMember(getMembership(record.getMemberNumber()));
        line.setQuantity(new BigDecimal(record.getQuantity()));

        final CollectionGroup group =
            getCollectionGroup(
                record.getRouteNumber(), record.getSessionCode(), record.getValidDate());
        group.getEntries().add(line);
        group.setEntryCount(group.getEntryCount() + 1);
        group.setRecordTotal(group.getRecordTotal().add(line.getQuantity()));
      }
      for (final CollectionGroup group : groups.values()) {
        repo.save(group);
      }
    }
  }
  protected Membership createMember(String accountNo) {
    final Farmer farmer = DairyUtil.createFarmer(accountNo, "", "", "", (Farm) null);
    farmer.setNickName(accountNo);

    final Membership member = DairyUtil.createMembership(new Date(), new Date(), farmer);
    member.setMemberNumber(accountNo);
    member.setStatus(MembershipStatus.ACTIVE);

    DAIRY.getMemberships().add(member);
    repo.save(DAIRY);

    return member;
  }
 private DairyLocation getCenter(String key) {
   DairyLocation center = centers.get(key);
   if (center == null) {
     center = DairyFactory.eINSTANCE.createDairyLocation();
     center.setCode(key);
     center.setName(key);
     center.setLocation(DairyUtil.createLocation(null, null, null));
     DAIRY.getBranchLocations().add(center);
     repo.save(center);
     centers.put(key, center);
   }
   return center;
 }
  // @Test
  public void testGetPayableDeliveriesForMember() throws Exception {
    initTestContext("../test-data/collections/test-collections.csv");
    // ICollectionJournalLineRepository repo = new
    // MilkCollectionJournalLineRepository();

    Membership member = null;
    for (final Membership m : DAIRY.getMemberships()) {
      if (m.getMemberNumber().equals("1975") || m.getMemberNumber().equals("01975")) {
        member = m;
        break;
      }
    }
    assertNotNull(member);

    List<CollectionJournalLine> collections;

    collections = repo.getPayableDeliveriesForMember(member, 6, 2010);
    assertEquals(6, collections.size());
    collections = repo.getPayableDeliveriesForMember(member, 7, 2010);
    assertEquals(0, collections.size());
    collections = repo.getPayableDeliveriesForMember(member, 8, 2010);
    assertEquals(17, collections.size());
  }
  @Test
  public void testGetMilkPrice() throws Exception {
    initTestContext();

    // initially empty
    assertEquals(0, DAIRY.getPriceHistory().size());

    // create some data
    final long queryDate = 100000;
    final MemberPayment milkPrice = DairyFactory.eINSTANCE.createMemberPayment();
    milkPrice.setEnteredBy(DEFAULT_DRIVER);
    milkPrice.setEntryDate(new Date(queryDate));
    milkPrice.setMonth(1);
    milkPrice.setYear(1990);
    milkPrice.setPaymentRate(new BigDecimal("23.22"));

    DAIRY.getPriceHistory().add(milkPrice);
    repo.save(DAIRY);

    assertEquals(1, DAIRY.getPriceHistory().size());
    final BigDecimal rate = repo.getMilkPrice(1, 1990);
    assertEquals(new BigDecimal("23.22"), rate);
  }
  // @Test
  public void testGetSumOfPayableDeliveries() throws Exception {
    initTestContext("../test-data/collections/test-collections.csv");
    // ICollectionJournalLineRepository repo = new
    // MilkCollectionJournalLineRepository();

    Membership member = null;
    for (final Membership m : DAIRY.getMemberships()) {
      if (m.getMemberNumber().equals("1975") || m.getMemberNumber().equals("01975")) {
        member = m;
        break;
      }
    }
    assertNotNull(member);

    assertEquals(new BigDecimal("88.6"), repo.getSumOfPayableDeliveries(member, 6, 2010));
  }