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.socketadapter;
28  
29  // import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  import static org.mockito.Mockito.mock;
32  import static org.mockito.Mockito.when;
33  
34  import java.lang.reflect.Method;
35  
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.perfidix.element.BenchmarkMethod;
39  import org.perfidix.exceptions.SocketViewException;
40  import org.perfidix.meter.AbstractMeter;
41  import org.perfidix.meter.Time;
42  import org.perfidix.meter.TimeMeter;
43  
44  /**
45   * This class tests the java class {@link org.perfidix.socketadapter.SocketListener}.
46   * 
47   * @author Lewandowski Lukas, DiSy, University of Konstanz
48   */
49  
50  public class SocketListenerTest {
51  
52      private transient SocketListener socketListener;
53  
54      private IUpdater updater;
55  
56      /**
57       * Simple setUp
58       * 
59       * @throws java.lang.Exception
60       */
61      @Before
62      public void setUp() throws Exception {
63          updater = mock(IUpdater.class);
64          socketListener = new SocketListener(updater);
65      }
66  
67      /**
68       * Test method for
69       * {@link org.perfidix.socketadapter.SocketListener#visitBenchmark(org.perfidix.result.BenchmarkResult)} .
70       */
71      @Test(expected = UnsupportedOperationException.class)
72      public void testVisitBenchmark() {
73          socketListener = new SocketListener(updater);
74          socketListener.visitBenchmark(null);
75      }
76  
77      /**
78       * Test method for
79       * {@link org.perfidix.socketadapter.SocketListener#listenToResultSet(java.lang.reflect.Method, org.perfidix.meter.AbstractMeter, double)}
80       * .
81       * 
82       * @throws InterruptedException
83       *             Thread sleep exception.
84       * @throws SocketViewException
85       */
86      @Test
87      public void testListenToResultSet() throws InterruptedException, SocketViewException {
88          final Method[] methods = BenchWithException.class.getMethods();
89          BenchmarkMethod method1 = null;
90          for (Method method : methods) {
91              if (method.getName().equals("benchMe")) {
92                  method1 = new BenchmarkMethod(method);
93              }
94          }
95          final AbstractMeter meter = new TimeMeter(Time.MilliSeconds);
96          final String methodName =
97              method1.getMethodToBench().getDeclaringClass().getName() + "."
98                  + method1.getMethodToBench().getName();
99  
100         when(updater.updateCurrentElement(meter, methodName)).thenReturn(true);
101 
102         SocketListener myInstance = new SocketListener(updater);
103         assertTrue(myInstance.listenToResultSet(method1.getMethodToBench(), meter, 0));
104     }
105 
106     /**
107      * Test method for
108      * {@link org.perfidix.socketadapter.SocketListener#listenToException (org.perfidix.exceptions.AbstractPerfidixMethodException)}
109      * .
110      * 
111      * @throws InterruptedException
112      *             Thread exception occurred.
113      */
114     @Test
115     public void testListenToException() throws InterruptedException, SocketViewException {
116         final Method[] methods = BenchWithException.class.getMethods();
117         BenchmarkMethod method1 = null;
118         for (Method method : methods) {
119             if (method.getName().equals("benchMe")) {
120                 method1 = new BenchmarkMethod(method);
121             }
122         }
123         final AbstractMeter meter = new TimeMeter(Time.MilliSeconds);
124         final String methodName =
125             method1.getMethodToBench().getDeclaringClass().getName() + "."
126                 + method1.getMethodToBench().getName();
127 
128         when(updater.updateCurrentElement(meter, methodName)).thenReturn(true);
129 
130         SocketListener myInstance = new SocketListener(updater);
131         assertTrue(myInstance.listenToResultSet(method1.getMethodToBench(), meter, 0));
132 
133     }
134 }