/**
  * Executes the test.
  *
  * @since 1.00
  */
 private static void execute() {
   try {
     Thread.sleep(2000);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   ThreadedTest listener = new ThreadedTest();
   r1 = new Runnable1(listener, 0 /*8 * MILLI_SEC*/);
   Thread t1 = new Thread(r1);
   r2 = new Runnable2(listener, 0 /*19 * MILLI_SEC*/);
   Thread t2 = new Thread(r2);
   r3 = new Runnable3(listener, 0 /*25 * MILLI_SEC*/);
   Thread t3 = new Thread(r3);
   TestEnvironment.notice("Starting runnable 1");
   t1.start();
   TestEnvironment.notice("Starting runnable 2");
   t2.start();
   TestEnvironment.notice("Starting runnable 3");
   t3.start();
   while (finished < 3 || t1.isAlive() || t2.isAlive() || t3.isAlive()) {
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
     }
   }
 }
  /**
   * The automated tests using assert statements. This method is called by SPASS-meter at end of
   * monitoring upon the <code>EndSystem</code> annotation.
   *
   * @since 1.00
   */
  public static void asserts() {
    long tolerance = 100 * 1000 * 1000; // ms
    boolean includeOuter = true;
    if (TestEnvironment.isIndirectTest()) {
      // accounted by setting
      includeOuter = false;
    }
    assertChain(MonitoringGroupValue.ALLOCATED_MEMORY, includeOuter);
    // no statement about used memory - this depends on the JVM
    //        assertChain(MonitoringGroupValue.SYSTEM_TIME, false);

    // chain sequence is not ensured
    // due to unknown scheduling policy
    TestEnvironment.assertEquals(
        Runnable1.class.getName(),
        MonitoringGroupValue.SYSTEM_TIME,
        r1.getElapsedSystemTime(),
        tolerance);
    TestEnvironment.assertEquals(
        Runnable2.class.getName(),
        MonitoringGroupValue.SYSTEM_TIME,
        r2.getElapsedSystemTime(),
        tolerance);
    TestEnvironment.assertEquals(
        Runnable3.class.getName(),
        MonitoringGroupValue.SYSTEM_TIME,
        r3.getElapsedSystemTime(),
        tolerance);

    TestEnvironment.assertEquals(
        Runnable1.class.getName(),
        MonitoringGroupValue.CPU_TIME,
        r1.getElapsedCpuTime(),
        tolerance);
    TestEnvironment.assertEquals(
        Runnable2.class.getName(),
        MonitoringGroupValue.CPU_TIME,
        r2.getElapsedCpuTime(),
        tolerance);
    TestEnvironment.assertEquals(
        Runnable3.class.getName(),
        MonitoringGroupValue.CPU_TIME,
        r3.getElapsedCpuTime(),
        tolerance);

    assertZero(MonitoringGroupValue.FILE_READ);
    assertZero(MonitoringGroupValue.FILE_WRITE);
    assertZero(MonitoringGroupValue.NET_READ);
    assertZero(MonitoringGroupValue.NET_WRITE);
    assertZero(MonitoringGroupValue.TOTAL_READ);
    assertZero(MonitoringGroupValue.TOTAL_WRITE);
    TestEnvironment.success(ThreadedTest.class.getName());
  }
 /**
  * Starts the test.
  *
  * @param args ignored
  * @since 1.00
  */
 @StartSystem
 @EndSystem(invoke = "asserts")
 public static void main(String[] args) {
   TestEnvironment.notice(ThreadedTest.class.getName());
   if (args.length > 0) {
     if (args[0].equals("continue")) {
       for (int i = 0; i < 15; i++) {
         execute();
       }
     } else {
       System.out.println("invalid parameter: " + args[0]);
     }
   } else {
     execute();
     /*            try {
         Thread.sleep(1000);
     } catch (InterruptedException e) {
     }*/
   }
   TestEnvironment.notice("------------------ done: ThreadedTest");
 }
 /**
  * Starts the test.
  *
  * @param args command line arguments (ignored)
  * @since 1.00
  */
 @StartSystem
 @EndSystem(invoke = "asserts")
 public static void main(String[] args) {
   TestEnvironment.initialize(); // explicit due to events
   TestEnvironment.notice(TimerTest.class.getName());
   if (args.length > 0) {
     if (args[0].equals("continue")) {
       for (int i = 0; i < 15; i++) {
         execute();
         try {
           Thread.sleep(2000);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }
     } else {
       System.out.println("invalid parameter: " + args[0]);
     }
   } else {
     execute();
   }
   TestEnvironment.notice("------------------ done: TimerTest");
 }
 /**
  * The automated tests using assert statements. This method is called by SPASS-meter at end of
  * monitoring upon the <code>EndSystem</code> annotation.
  *
  * @since 1.00
  */
 public static void asserts() {
   TestEnvironment.assertEqualsTimer(ID_TIMER_START_FINISH, WAIT_TIME, TOLERANCE);
   // just to be sure - same as ID_TIMER_START_FINISH
   TestEnvironment.assertEqualsTimer(recIdStartStopOneStep, WAIT_TIME, TOLERANCE);
   // cannot use real timer id as value is being overridden
   TestEnvironment.assertEqualsTimer(recIdStartStopTwoSteps, WAIT_TIME, TOLERANCE);
   TestEnvironment.assertEqualsTimer(recIdRestartStopResumeTwoSteps, WAIT_TIME, TOLERANCE);
   TestEnvironment.assertEqualsTimer(recIdRestartStopResumeOneStep, WAIT_TIME, TOLERANCE);
   TestEnvironment.success(AnnotationId.ID_TIMER);
 }
  /**
   * Executes the test.
   *
   * @since 1.00
   */
  private static void execute() {
    // start and stop in one step
    long start = System.currentTimeMillis();
    timerInOneMethod();
    long end = System.currentTimeMillis();
    timeStartStopOneStep = end - start;
    recIdStartStopOneStep = TestEnvironment.storeTimerData(ID_TIMER_START_FINISH);
    TestEnvironment.notice("should be approx. 2000 (vs. real time " + timeStartStopOneStep + ")");

    // start and stop in two steps
    start = System.currentTimeMillis();
    startProcessing();
    endProcessing();
    end = System.currentTimeMillis();
    timeStartStopTwoSteps = end - start;
    recIdStartStopTwoSteps = TestEnvironment.storeTimerData(ID_TIMER_START_FINISH2);
    TestEnvironment.notice("should be approx. 2000 (vs. real time " + timeStartStopTwoSteps + ")");

    // restart, stop and resume in two steps
    start = System.currentTimeMillis();
    startProcessing();
    interruptProcessing();
    continueProcessing();
    endProcessing();
    end = System.currentTimeMillis();
    timeRestartStopResumeTwoSteps = end - start;
    recIdRestartStopResumeTwoSteps = TestEnvironment.storeTimerData(ID_TIMER_START_FINISH2);
    TestEnvironment.notice(
        "should be approx. 2000 (vs. real time " + timeRestartStopResumeTwoSteps + ")");

    // restart and stop and resume in one step
    start = System.currentTimeMillis();
    startProcessing();
    interruptAndContinueProcessing();
    endProcessing();
    end = System.currentTimeMillis();
    timeRestartStopResumeOneStep = end - start;
    recIdRestartStopResumeOneStep = TestEnvironment.storeTimerData(ID_TIMER_START_FINISH2);
    TestEnvironment.notice(
        "should be approx. 2000 (vs. real time " + timeRestartStopResumeOneStep + ")");
  }
 /**
  * Asserts the value 0 for all monitoring groups.
  *
  * @param value the value to test
  * @since 1.00
  */
 private static void assertZero(MonitoringGroupValue value) {
   TestEnvironment.assertEquals(ThreadedTest.class.getName(), value, 0);
   TestEnvironment.assertEquals(Runnable1.class.getName(), value, 0);
   TestEnvironment.assertEquals(Runnable2.class.getName(), value, 0);
   TestEnvironment.assertEquals(Runnable3.class.getName(), value, 0);
 }
 /**
  * Asserts a chain of values among the monitoring groups.
  *
  * @param value the value to consider
  * @param includeOuter include the outer group
  * @since 1.00
  */
 private static void assertChain(MonitoringGroupValue value, boolean includeOuter) {
   Long val1 = TestEnvironment.getValue(Runnable1.class.getName(), value);
   TestEnvironment.assertNotNull(Runnable1.class.getName(), val1);
   if (includeOuter) {
     Long val = TestEnvironment.getValue(ThreadedTest.class.getName(), value);
     TestEnvironment.assertNotNull(ThreadedTest.class.getName(), val);
     TestEnvironment.assertTrue(Runnable1.class.getName(), val < val1);
   }
   Long val2 = TestEnvironment.getValue(Runnable2.class.getName(), value);
   TestEnvironment.assertNotNull(Runnable2.class.getName(), val2);
   TestEnvironment.assertTrue(Runnable2.class.getName(), val1 < val2);
   Long val3 = TestEnvironment.getValue(Runnable3.class.getName(), value);
   TestEnvironment.assertNotNull(Runnable3.class.getName(), val3);
   TestEnvironment.assertTrue(Runnable3.class.getName(), val2 < val3);
 }