View Javadoc

1   /*
2    * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6    * and is available at http://www.eclipse.org/legal/epl-v10.html
7    */
8   
9   package org.opendaylight.lispflowmapping.tools.junit;
10  
11  import static org.junit.Assert.assertEquals;
12  
13  import java.lang.reflect.Field;
14  import java.lang.reflect.Modifier;
15  import java.net.InetAddress;
16  import java.net.UnknownHostException;
17  import java.nio.ByteBuffer;
18  
19  import org.hamcrest.Description;
20  import org.jmock.api.Action;
21  import org.jmock.api.Invocation;
22  import org.junit.Assert;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  public abstract class BaseTestCase extends BaseExpectations {
27      protected static final Logger LOG = LoggerFactory.getLogger(BaseTestCase.class);
28  
29      protected InetAddress asInet(int intValue) {
30          try {
31              return InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(intValue).array());
32          } catch (UnknownHostException e) {
33              LOG.debug("Unknown host {}", ByteBuffer.allocate(4).putInt(intValue).array(), e);
34              fail("");
35          }
36          return null;
37      }
38  
39      public static void fail(String message) {
40          Assert.fail(message);
41      }
42  
43      public static void fail() {
44          Assert.fail();
45      }
46  
47      public static void assertNotNull(String message, Object object) {
48          Assert.assertNotNull(message, object);
49      }
50  
51      public static void assertNotNull(Object object) {
52          Assert.assertNotNull(object);
53      }
54  
55      public static void assertNull(Object message, Object object) {
56          Assert.assertNull(message.toString(), object);
57      }
58  
59      public static void assertNull(Object object) {
60          Assert.assertNull("" + object, object);
61      }
62  
63      public static void assertSame(String message, Object expected, Object actual) {
64          Assert.assertSame(message, expected, actual);
65      }
66  
67      public static void assertSame(Object expected, Object actual) {
68          Assert.assertSame(expected, actual);
69      }
70  
71      public static void assertNotSame(String message, Object unexpected, Object actual) {
72          Assert.assertNotSame(message, unexpected, actual);
73      }
74  
75      public static void assertNotSame(Object unexpected, Object actual) {
76          Assert.assertNotSame(unexpected, actual);
77      }
78  
79      protected Object inject(Object testedObject, String memberName, Object member) throws Exception {
80          assertNotNull(testedObject);
81          assertNotNull(memberName);
82          assertNotNull(member);
83  
84          Field field = null;
85          for (Class<?> cls = testedObject.getClass(); (cls != null) && (field == null); cls = cls.getSuperclass()) {
86              field = cls.getDeclaredField(memberName);
87          }
88          assertNotNull("Couldn't find member '" + memberName + "' in " + testedObject.getClass().getSimpleName(), field);
89          return inject(testedObject, new FieldData(field, member));
90      }
91  
92      protected Object injectStatic(Class<?> clazz, String memberName, Object member) throws Exception {
93          assertNotNull(clazz);
94          assertNotNull(memberName);
95          assertNotNull(member);
96  
97          Field field = null;
98          for (Class<?> cls = clazz; (cls != null) && (field == null); cls = cls.getSuperclass()) {
99              field = cls.getDeclaredField(memberName);
100         }
101         assertNotNull("Couldn't find member '" + memberName + "' in " + clazz.getSimpleName(), field);
102         return inject(null, new FieldData(field, member));
103     }
104 
105     protected Object inject(Object testedObject, FieldData fieldData) {
106         assertNotNull(fieldData.field);
107         Field field = fieldData.field;
108         if (fieldData.value != null) {
109             try {
110                 field.setAccessible(true);
111                 Field modifiersField = Field.class.getDeclaredField("modifiers");
112                 modifiersField.setAccessible(true);
113                 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
114                 field.set(testedObject, fieldData.value);
115                 return fieldData.value;
116             } catch (Exception e) {
117                 LOG.debug("Caught generic exception", e);
118                 fail(e.getMessage());
119             }
120         }
121         return null;
122     }
123 
124     static protected final class FieldData {
125         final public Field field;
126         public Object value;
127 
128         public FieldData(Field field, Object value) {
129             assertNotNull(field);
130             this.field = field;
131             this.value = value;
132         }
133     }
134 
135     protected abstract class SimpleAction implements Action {
136         public void describeTo(Description arg0) {
137         }
138 
139         public abstract Object invoke(Invocation invocation) throws Throwable;
140     }
141 
142     protected static ByteBuffer hexToByteBuffer(String hex) {
143         String[] hexBytes = hex.split(" ");
144         ByteBuffer bb = ByteBuffer.allocate(hexBytes.length);
145         for (String hexByte : hexBytes) {
146             bb.put((byte) Integer.parseInt(hexByte, 16));
147         }
148         bb.clear();
149         return bb;
150     }
151 
152     protected static void assertHexEquals(short expected, short actual) {
153         assertEquals(String.format("0x%04X", expected), String.format("0x%04X", actual));
154     }
155 
156     protected static void assertHexEquals(byte expected, byte actual) {
157         assertEquals(String.format("0x%02X", expected), String.format("0x%02X", actual));
158     }
159 }