/*
 * Copyright (C) 2009 Google Inc.
 *
 * 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 com.google.caliper.functional;

import com.google.caliper.Benchmark;
import com.google.caliper.Runner;
import com.google.caliper.UserException.AbstractBenchmarkException;
import com.google.caliper.UserException.DoesntImplementBenchmarkException;
import com.google.caliper.UserException.ExceptionFromUserCodeException;
import com.google.caliper.UserException.NoParameterlessConstructorException;
import junit.framework.TestCase;

/**
 * Unit test covering common user mistakes.
 */
public class ErrorsInUserCodeTest extends TestCase {
  private Runner runner;

  @Override protected void setUp() throws Exception {
    runner = new Runner();
  }

  public void testDidntSubclassAnything() {
    try {
      runner.run(NotABenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException expected) {
      assertEquals(DoesntImplementBenchmarkException.class.getCanonicalName(),
    		  expected.getCause().getClass().getCanonicalName());
    }
  }

  static class NotABenchmark {
    public void timeSomething(int reps) {
      fail("" + reps);
    }
  }


  public void testAbstract() {
    try {
      runner.run(AbstractBenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException expected) {
        assertEquals(AbstractBenchmarkException.class.getCanonicalName(),
      		  expected.getCause().getClass().getCanonicalName());
    }
  }

  abstract static class AbstractBenchmark extends Benchmark {
    public void timeSomething(int reps) {
      fail("" + reps);
    }
  }


  public void testNoSuitableConstructor() {
    try {
      runner.run(BadConstructorBenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException expected) {
        assertEquals(NoParameterlessConstructorException.class.getCanonicalName(),
        		  expected.getCause().getClass().getCanonicalName());
    }
  }

  static class BadConstructorBenchmark extends Benchmark {
    BadConstructorBenchmark(String damnParam) {
      fail(damnParam);
    }

    public void timeSomething(int reps) {
      fail("" + reps);
    }
  }


  @SuppressWarnings("serial")
  static class SomeUserException extends RuntimeException {}

  private static void throwSomeUserException() {
    throw new SomeUserException();
  }

  public void testExceptionInInit() {
    try {
      runner.run(ExceptionInInitBenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException expected) {
    }
  }

  static class ExceptionInInitBenchmark extends Benchmark {
    static {
      throwSomeUserException();
    }

    public void timeSomething(int reps) {
      fail("" + reps);
    }
  }

  public void testExceptionInConstructor() {
    try {
      runner.run(ExceptionInConstructorBenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException expected) {
    }
  }

  static class ExceptionInConstructorBenchmark extends Benchmark {
    ExceptionInConstructorBenchmark() {
      throw new SomeUserException();
    }

    public void timeSomething(int reps) {
      fail("" + reps);
    }
  }

  public void testExceptionInMethod() {
    try {
      new Runner().run(ExceptionInMethodBenchmark.class.getName());
      fail();
    } catch (ExceptionFromUserCodeException ignored) {
    }
  }

  public static class ExceptionInMethodBenchmark extends Benchmark {
    public void timeSomething(int reps) {
      throw new SomeUserException();
    }
  }

  public void testUserCodePrintsOutput() {
    new Runner().run(UserCodePrintsBenchmark.class.getName(), "--debug",
        "--warmupMillis", "100", "--runMillis", "100");
  }

  public static class UserCodePrintsBenchmark extends Benchmark {
    public void timeSomething(int reps) throws InterruptedException {
      System.out.println("output to System.out!");
      Thread.sleep(reps);
    }
  }
}

@@ -988,24 +988,25 @@
 rException;%0A
+%0A
 import junit
@@ -1016,24 +1016,22 @@
 amework.
-TestCase
+Assert
 ;%0A%0A/**%0A 
@@ -1077,16 +1077,145 @@
 es.%0A */%0A
+// Disabled because they rely on the now defunct SimpleBenchmark%0A// TODO(gak): migrate to the new runner with the new exceptions%0A
 public c
@@ -1252,16 +1252,44 @@
 nds 
+Assert /* temporarily not a 
 TestCase
  %7B%0A 
@@ -1284,16 +1284,19 @@
 TestCase
+ */
  %7B%0A  pri
@@ -1322,18 +1322,8 @@
 %0A%0A  
-@Override 
 prot
@@ -1644,26 +1644,28 @@
 Name(),%0A    
-%09%09
+    
   expected.g
@@ -2065,26 +2065,28 @@
 me(),%0A      
-%09%09
+    
   expected.g
@@ -2555,10 +2555,10 @@
     
-%09%09
+  
   ex

Disable the failing ErrorsInUserCodeTest. It will be migrated with the other two tests in the package, but just needs to be disabled for now because it is failing on code that is about to be deleted. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=41439423