1 /**
2 * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of Konstanz nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 package org.perfidix.element;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.fail;
32
33 import java.lang.reflect.Method;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.Iterator;
38 import java.util.List;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.perfidix.annotation.Bench;
43
44 /**
45 * Testcase for no method arrangement.
46 *
47 * @author Sebastian Graf, University of Konstanz
48 */
49 public class NoMethodArrangementTest {
50
51 private transient List<BenchmarkElement> elemSet;
52
53 /**
54 * Before method to setUp Benchmarkables.
55 */
56 @Before
57 public void setUp() {
58 elemSet = new ArrayList<BenchmarkElement>();
59 final Class<?> testClazz = TestBenchClass.class;
60 for (final Method meth : testClazz.getDeclaredMethods()) {
61 if (BenchmarkMethod.isBenchmarkable(meth)) {
62 elemSet.add(new BenchmarkElement(new BenchmarkMethod(meth)));
63 }
64 }
65
66 // Put methods in a specific order, since Class.getDeclaredMethods()
67 // makes no such guarantees, causing the test to fail otherwise
68 // cf. http://bugs.sun.com/view_bug.do?bug_id=7023180
69 Collections.sort(elemSet, new Comparator<BenchmarkElement>() {
70
71 @Override
72 public int compare(BenchmarkElement a, BenchmarkElement b) {
73 return a.getMeth().getMethodWithClassName().compareTo(b.getMeth().getMethodWithClassName());
74 }
75 });
76 }
77
78 /**
79 * Test method for {@link org.perfidix.element.NoMethodArrangement} .
80 */
81 @Test
82 public void test() {
83 try {
84
85 final AbstractMethodArrangement arrangement =
86 AbstractMethodArrangement.getMethodArrangement(elemSet, KindOfArrangement.NoArrangement);
87 final String[] expectedNames = {
88 "bench1", "bench2", "bench4"
89 };
90 final Iterator<BenchmarkElement> iterBench = arrangement.iterator();
91 assertEquals("Method name for first element", expectedNames[0], iterBench.next().getMeth()
92 .getMethodToBench().getName());
93 assertEquals("Method name for second element", expectedNames[1], iterBench.next().getMeth()
94 .getMethodToBench().getName());
95 assertEquals("Method name for third element", expectedNames[2], iterBench.next().getMeth()
96 .getMethodToBench().getName());
97 assertFalse("No more elements avaliables", iterBench.hasNext());
98
99 } catch (final Exception e) {
100 fail(e.toString());
101 }
102 }
103
104 class TestBenchClass {
105
106 @Bench
107 public void bench1() {
108 // Just a method sekeleton
109 }
110
111 @Bench
112 public void bench2() {
113 // Just a method sekeleton
114 }
115
116 public void bench3() {
117 // Just a method sekeleton
118 }
119
120 @Bench
121 public void bench4() {
122 // Just a method sekeleton
123 }
124
125 }
126
127 }