View Javadoc

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.assertFalse;
30  import static org.junit.Assert.fail;
31  
32  import java.lang.reflect.Method;
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.perfidix.annotation.Bench;
40  
41  /**
42   * Testcase for shuffle method arrangement.
43   * 
44   * @author Sebastian Graf, University of Konstanz
45   */
46  public class SequentialMethodArrangementTest {
47  
48      private transient List<BenchmarkElement> elemSet;
49  
50      private final static String BENCH1NAME = "bench1";
51      private final static String BENCH2NAME = "bench2";
52      private final static String BENCH4NAME = "bench4";
53  
54      /**
55       * Before method to setUp Benchmarkables.
56       * 
57       * @throws Exception
58       *             of any kind
59       */
60      @Before
61      public void setUp() throws Exception {
62          elemSet = new ArrayList<BenchmarkElement>();
63          final Class<?> testClazz = TestBenchClass.class;
64          for (final Method meth : testClazz.getDeclaredMethods()) {
65              if (BenchmarkMethod.isBenchmarkable(meth)) {
66                  elemSet.add(new BenchmarkElement(new BenchmarkMethod(meth)));
67              }
68          }
69          Method meth = testClazz.getMethod(BENCH2NAME);
70          elemSet.add(new BenchmarkElement(new BenchmarkMethod(meth)));
71          meth = testClazz.getMethod(BENCH2NAME);
72          elemSet.add(new BenchmarkElement(new BenchmarkMethod(meth)));
73          meth = testClazz.getMethod(BENCH4NAME);
74          elemSet.add(new BenchmarkElement(new BenchmarkMethod(meth)));
75      }
76  
77      /**
78       * Test method for {@link org.perfidix.element.SequentialMethodArrangement} .
79       */
80      @Test
81      public void test() {
82          try {
83  
84              final AbstractMethodArrangement arrangement =
85                  AbstractMethodArrangement.getMethodArrangement(elemSet,
86                      KindOfArrangement.SequentialMethodArrangement);
87              final String[] expectedNames = {
88                  BENCH2NAME, BENCH2NAME, BENCH2NAME, BENCH4NAME, BENCH4NAME, BENCH1NAME
89              };
90              final Iterator<BenchmarkElement> iterBench = arrangement.iterator();
91              final BenchmarkElement elem1 = iterBench.next();
92              final BenchmarkElement elem2 = iterBench.next();
93              final BenchmarkElement elem3 = iterBench.next();
94              final BenchmarkElement elem4 = iterBench.next();
95              final BenchmarkElement elem5 = iterBench.next();
96              final BenchmarkElement elem6 = iterBench.next();
97              if ((expectedNames[0].equals(elem1.getMeth().getMethodToBench().getName()))
98                  && (expectedNames[1].equals(elem2.getMeth().getMethodToBench().getName()))
99                  && (expectedNames[2].equals(elem3.getMeth().getMethodToBench().getName()))
100                 && (expectedNames[3].equals(elem4.getMeth().getMethodToBench().getName()))
101                 && (expectedNames[4].equals(elem5.getMeth().getMethodToBench().getName()))
102                 && (expectedNames[5].equals(elem6.getMeth().getMethodToBench().getName()))) {
103                 fail("Something has to be arranged in a different way!");
104             }
105 
106             assertFalse("No more elements should be there", iterBench.hasNext());
107 
108         } catch (final Exception e) {
109             fail(e.toString());
110         }
111     }
112 
113     class TestBenchClass {
114 
115         @Bench
116         public void bench1() {
117             // Just a method sekeleton
118         }
119 
120         @Bench
121         public void bench2() {
122             // Just a method sekeleton
123         }
124 
125         public void bench3() {
126             // Just a method sekeleton
127         }
128 
129         @Bench
130         public void bench4() {
131             // Just a method sekeleton
132         }
133 
134     }
135 
136 }