Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper.runner; |
| 18 | |
| 19 | import com.google.caliper.Param; |
| 20 | import com.google.common.collect.ImmutableSortedMap; |
| 21 | import junit.framework.TestCase; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.runners.JUnit4; |
| 25 | |
| 26 | /** |
| 27 | * Test for {@link BenchmarkCreator} |
| 28 | */ |
| 29 | @RunWith(JUnit4.class) |
| 30 | public class BenchmarkCreatorTest extends TestCase { |
| 31 | |
| 32 | @Test |
| 33 | public void publicDefaultConstructorNoParamBenchmark() { |
| 34 | BenchmarkCreator creator = new BenchmarkCreator(PublicDefaultConstructorNoParamBenchmark.class, |
| 35 | ImmutableSortedMap.<String, String>of()); |
| 36 | |
| 37 | Object benchmarkInstance = creator.createBenchmarkInstance(); |
| 38 | assertTrue(benchmarkInstance instanceof PublicDefaultConstructorNoParamBenchmark); |
| 39 | } |
| 40 | |
| 41 | public static class PublicDefaultConstructorNoParamBenchmark { |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void publicDefaultConstructorWithParamBenchmark() { |
| 46 | BenchmarkCreator creator = new BenchmarkCreator( |
| 47 | PublicDefaultConstructorWithParamBenchmark.class, |
| 48 | ImmutableSortedMap.of("byteField", "1", "intField", "2", "stringField", "string")); |
| 49 | |
| 50 | Object benchmarkInstance = creator.createBenchmarkInstance(); |
| 51 | assertTrue(benchmarkInstance instanceof PublicDefaultConstructorWithParamBenchmark); |
| 52 | PublicDefaultConstructorWithParamBenchmark benchmark = |
| 53 | (PublicDefaultConstructorWithParamBenchmark) benchmarkInstance; |
| 54 | assertEquals(1, benchmark.byteField); |
| 55 | assertEquals(2, benchmark.intField); |
| 56 | assertEquals("string", benchmark.stringField); |
| 57 | } |
| 58 | |
| 59 | public static class PublicDefaultConstructorWithParamBenchmark { |
| 60 | @Param |
| 61 | byte byteField; |
| 62 | |
| 63 | @Param |
| 64 | int intField; |
| 65 | |
| 66 | @Param |
| 67 | String stringField; |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void publicNoSuitableConstructorBenchmark() { |
| 72 | try { |
| 73 | new BenchmarkCreator( |
| 74 | PublicNoSuitableConstructorBenchmark.class, |
| 75 | ImmutableSortedMap.<String, String>of()); |
| 76 | } catch (UserCodeException e) { |
| 77 | assertEquals("Benchmark class " |
| 78 | + PublicNoSuitableConstructorBenchmark.class.getName() |
| 79 | + " does not have a publicly visible default constructor", e.getMessage()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public static class PublicNoSuitableConstructorBenchmark { |
| 84 | @Param |
| 85 | byte byteField; |
| 86 | |
| 87 | @Param |
| 88 | int intField; |
| 89 | |
| 90 | @Param |
| 91 | String stringField; |
| 92 | |
| 93 | public PublicNoSuitableConstructorBenchmark( |
| 94 | byte byteField, int intField, String stringField) { |
| 95 | this.byteField = byteField; |
| 96 | this.intField = intField; |
| 97 | this.stringField = stringField; |
| 98 | } |
| 99 | } |
| 100 | } |