gt:         StringWriter out = new StringWriter();
context: /*******************************************************************************
   * This file is part of OpenNMS(R).
   *
   * Copyright (C) 2010-2014 The OpenNMS Group, Inc.
   * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License,
   * or (at your option) any later version.
   *
   * OpenNMS(R) is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with OpenNMS(R).  If not, see:
   *      http://www.gnu.org/licenses/
   *
   * For more information contact:
   *     OpenNMS(R) Licensing <license@opennms.org>
   *     http://www.opennms.org/
   *     http://www.opennms.com/
   *******************************************************************************/
  
  package org.opennms.util.ilr;
  
  import static org.junit.Assert.*;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.List;
  
  
  
  
  import org.junit.Ignore;
  import org.junit.Test;
  import org.opennms.util.ilr.Collector;
  import org.opennms.util.ilr.Collector.SortColumn;
  import org.opennms.util.ilr.Collector.SortOrder;
  
  
  public class CollectorTest {
      private Date getDate(String dateString) throws ParseException {
          return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss,S").parse(dateString);
      }
      @Test
      public void testStartNotSetEnd() throws ParseException {
          Collector c = new Collector();
          c.addLog("2010-05-26 12:12:38,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");	
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:50,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          assertEquals(getDate("2010-05-26 12:12:40,883"), c.getStartTime());
          assertEquals(getDate("2010-05-26 12:12:48,027"), c.getEndTime());
          assertEquals(7144, c.getDuration());
      }
      @Test
      public void testStartAndEndTime() throws ParseException {
          Collector c = new Collector();
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");	
  
          assertEquals(getDate("2010-05-26 12:12:40,883"), c.getStartTime());
          assertEquals(getDate("2010-05-26 12:12:48,027"), c.getEndTime());
          assertEquals(7144, c.getDuration());
      }
      @Test
      public void testServiceCount(){
          Collector c = new Collector();
          assertEquals(0, c.getServiceCount());
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          assertEquals(1, c.getServiceCount());
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: begin: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,995 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: end: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-06-01 09:36:02,541 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: end:example1/7/192.0.2.12/SNMP");
          c.addLog("2010-06-01 09:36:02,542 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: begin:example1/27/192.0.2.6/SNMP");
          c.addLog("2010-06-01 09:36:02,544 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: begin:example1/27/192.0.2.6/SNMP");
          c.addLog("2010-06-01 09:36:03,508 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: end:example1/27/192.0.2.6/SNMP");
          c.addLog("2010-06-01 09:36:02,541 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: error: example1/7/192.0.2.12/SNMP: org.opennms.netmgt.collectd.CollectionTimedOut: Timeout retrieving SnmpCollectors for 192.0.2.12 for kenny.internal.opennms.com/192.0.2.12: SnmpCollectors for 192.0.2.12: snmpTimeoutError for: kenny.internal.opennms.com/192.0.2.12");
          c.addLog("2010-06-01 09:36:27,644 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: begin:example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: end:example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:33:56,292 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: begin:example1/83/192.0.2.15/SNMP");
          c.addLog("2010-06-01 09:33:56,440 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: end:example1/83/192.0.2.15/SNMP");
          assertEquals(5, c.getServiceCount());
      }
  
      @Test
      public void testThreadcount(){
          Collector c = new Collector();
          assertEquals(0, c.getThreadCount());
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          assertEquals(1, c.getThreadCount());
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: begin: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,995 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: end: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:33:31,964 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: begin:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 09:33:32,477 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: end:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: begin: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: end: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:39:46,648 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          c.addLog("2010-06-01 08:39:46,650 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          assertEquals(5, c.getThreadCount());
      }
  
      @Test 
      public void testCollectionsPerService() {
          Collector c = new Collector();
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: begin: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,995 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: end: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:33:31,964 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: begin:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 09:33:32,477 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: end:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: begin: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: end: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:39:46,648 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          c.addLog("2010-06-01 08:39:46,650 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          assertEquals(1,c.getCollectionsPerService("example1/24/216.216.217.254/SNMP"));
          assertEquals(0,c.getCollectionsPerService("example1/19/209.61.128.9/SNMP"));
          assertEquals(1,c.getCollectionsPerService("example1/60/192.0.2.202/SNMP"));
          assertEquals(0,c.getCollectionsPerService("example1/86/192.0.2.25/WMI"));
          assertEquals(0,c.getCollectionsPerService("example1/58/192.0.2.201/SNMP"));
      }
      @Test
      public void testAverageCollectionTimePerService() {
          Collector c = new Collector();
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: begin: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,995 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: end: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:33:31,964 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: begin:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 09:33:32,477 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: end:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: begin: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: end: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:39:46,648 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          c.addLog("2010-06-01 08:39:46,650 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          assertEquals(7144,c.getAverageCollectionTimePerService("example1/24/216.216.217.254/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/19/209.61.128.9/SNMP"));
          assertEquals(513,c.getAverageCollectionTimePerService("example1/60/192.0.2.202/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/86/192.0.2.25/WMI"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/58/192.0.2.201/SNMP"));
      }
      @Test
      public void testTotalCollectionTimePerService() {
          Collector c = new Collector();
          c.addLog("2010-05-26 12:12:40,883 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: begin:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-05-26 12:12:48,027 INFO [CollectdScheduler-50 Pool-fiber11] collector.collect: end:example1/24/216.216.217.254/SNMP");
          c.addLog("2010-06-01 09:36:28,950 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: begin: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:36:28,995 INFO [CollectdScheduler-50 Pool-fiber1] collector.collect: persistDataQueueing: end: example1/19/209.61.128.9/SNMP");
          c.addLog("2010-06-01 09:33:31,964 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: begin:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 09:33:32,477 INFO [CollectdScheduler-50 Pool-fiber0] collector.collect: end:example1/60/192.0.2.202/SNMP");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: begin: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:45:12,104 INFO [CollectdScheduler-50 Pool-fiber2] collector.collect: persistDataQueueing: end: example1/86/192.0.2.25/WMI");
          c.addLog("2010-06-01 08:39:46,648 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          c.addLog("2010-06-01 08:39:46,650 INFO [CollectdScheduler-50 Pool-fiber3] collector.collect: begin:example1/58/192.0.2.201/SNMP");
          assertEquals(7144,c.getAverageCollectionTimePerService("example1/24/216.216.217.254/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/19/209.61.128.9/SNMP"));
          assertEquals(513,c.getAverageCollectionTimePerService("example1/60/192.0.2.202/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/86/192.0.2.25/WMI"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/58/192.0.2.201/SNMP"));
      }
  
      @Test
      public void testTotalCollectionTime() {
          Collector c = new Collector();
          c.addLog("2010-03-13 02:20:30,525 INFO [CollectdScheduler-400 Pool-fiber51] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:21:09,976 INFO [CollectdScheduler-400 Pool-fiber51] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:32:42,641 INFO [CollectdScheduler-400 Pool-fiber25] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:33:01,973 INFO [CollectdScheduler-400 Pool-fiber25] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:43:27,749 INFO [CollectdScheduler-400 Pool-fiber112] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:43:31,517 INFO [CollectdScheduler-400 Pool-fiber112] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:54:13,334 INFO [CollectdScheduler-400 Pool-fiber166] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 02:54:35,223 INFO [CollectdScheduler-400 Pool-fiber166] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 03:05:47,554 INFO [CollectdScheduler-400 Pool-fiber307] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 03:06:28,926 INFO [CollectdScheduler-400 Pool-fiber307] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 03:18:27,559 INFO [CollectdScheduler-400 Pool-fiber264] collector.collect: begin:example1/32028/209.219.9.78/SNMP");
          c.addLog("2010-03-13 03:19:06,934 INFO [CollectdScheduler-400 Pool-fiber264] collector.collect: end:example1/32028/209.219.9.78/SNMP");
          assertEquals(39451+19332+3768+21889+41372+39375,c.getTotalCollectionTimePerService("example1/32028/209.219.9.78/SNMP"));
      }
  
      @Test 
      public void testReadLogMessagesFromFile() throws IOException {
          Collector c = new Collector();
          c.readLogMessagesFromFile("target/test-classes/TestLogFile.log");
          assertEquals(7144,c.getAverageCollectionTimePerService("example1/24/216.216.217.254/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/19/209.61.128.9/SNMP"));
          assertEquals(513,c.getAverageCollectionTimePerService("example1/60/192.0.2.202/SNMP"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/86/192.0.2.25/WMI"));
          assertEquals(0,c.getAverageCollectionTimePerService("example1/58/192.0.2.201/SNMP"));
      }
  
      @Test
      public void testReadLogMessagesFromPostLog4jRefactorFile() throws Exception {
          final Collector c = new Collector();
          c.readLogMessagesFromFile("target/test-classes/instrumentation.log");
          final LogMessage logMessage = c.getFirstValidLogMessage();
          assertNotNull(logMessage);
          assertEquals("example1/380/10.151.117.34/SNMP", logMessage.getServiceID());
          assertEquals(timestamp("2013-07-23 11:39:22,287"), logMessage.getDate());
          assertEquals("LegacyScheduler-Thread-20-of-50", logMessage.getThread());
      }
  
      @Test
      public void testPrintGlobalStats() throws IOException {
          Collector c = new Collector ();
          c.readLogMessagesFromFile("target/test-classes/TestLogFile.log");
          StringWriter out = new StringWriter();
          c.printGlobalStats(new PrintWriter(out, true));
          String actualOutput = out.toString(); 
          assertTrue(actualOutput.contains("Start Time: 2010-05-26 12:12:40,883"));
          assertTrue(actualOutput.contains("End Time: 2010-06-01 08:45:12,104"));
          assertTrue(actualOutput.contains("Duration: 5d20h32m31.221s"));
          assertTrue(actualOutput.contains("Total Services: 5"));
          assertTrue(actualOutput.contains("Threads Used: 5"));
      }
      @Test 
      public void testPrintServiceStats () throws IOException {
          Collector c = new Collector ();
          c.readLogMessagesFromFile("target/test-classes/TestLogFile.log");
          String expectedOutput = String.format(Collector.SERVICE_DATA_FORMAT, 
                                                "example1/24/216.216.217.254/SNMP",
                                                "7.144s",
                                                1,
                                                "7.144s",
                                                100.0,
                                                "0s",
                                                0.0,
                                                "0s",
                                                "7.144s",
                                                "0s",
                                                "0s"
                  );
  