Rename static final variable 'logger' to 'LOG'
[lispflowmapping.git] / commons / unittest_tools / src / main / java / org / opendaylight / lispflowmapping / tools / junit / BaseTestCase.java
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
24 public abstract class BaseTestCase extends BaseExpectations {
25     protected InetAddress asInet(int intValue) {
26         try {
27             return InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(intValue).array());
28         } catch (UnknownHostException e) {
29             fail("");
30         }
31         return null;
32     }
33
34     public static void fail(String message) {
35         Assert.fail(message);
36     }
37
38     public static void fail() {
39         Assert.fail();
40     }
41
42     public static void assertNotNull(String message, Object object) {
43         Assert.assertNotNull(message, object);
44     }
45
46     public static void assertNotNull(Object object) {
47         Assert.assertNotNull(object);
48     }
49
50     public static void assertNull(Object message, Object object) {
51         Assert.assertNull(message.toString(), object);
52     }
53
54     public static void assertNull(Object object) {
55         Assert.assertNull("" + object, object);
56     }
57
58     public static void assertSame(String message, Object expected, Object actual) {
59         Assert.assertSame(message, expected, actual);
60     }
61
62     public static void assertSame(Object expected, Object actual) {
63         Assert.assertSame(expected, actual);
64     }
65
66     public static void assertNotSame(String message, Object unexpected, Object actual) {
67         Assert.assertNotSame(message, unexpected, actual);
68     }
69
70     public static void assertNotSame(Object unexpected, Object actual) {
71         Assert.assertNotSame(unexpected, actual);
72     }
73
74     protected Object inject(Object testedObject, String memberName, Object member) throws Exception {
75         assertNotNull(testedObject);
76         assertNotNull(memberName);
77         assertNotNull(member);
78
79         Field field = null;
80         for (Class<?> cls = testedObject.getClass(); (cls != null) && (field == null); cls = cls.getSuperclass()) {
81             field = cls.getDeclaredField(memberName);
82         }
83         assertNotNull("Couldn't find member '" + memberName + "' in " + testedObject.getClass().getSimpleName(), field);
84         return inject(testedObject, new FieldData(field, member));
85     }
86
87     protected Object injectStatic(Class<?> clazz, String memberName, Object member) throws Exception {
88         assertNotNull(clazz);
89         assertNotNull(memberName);
90         assertNotNull(member);
91
92         Field field = null;
93         for (Class<?> cls = clazz; (cls != null) && (field == null); cls = cls.getSuperclass()) {
94             field = cls.getDeclaredField(memberName);
95         }
96         assertNotNull("Couldn't find member '" + memberName + "' in " + clazz.getSimpleName(), field);
97         return inject(null, new FieldData(field, member));
98     }
99
100     protected Object inject(Object testedObject, FieldData fieldData) {
101         assertNotNull(fieldData.field);
102         Field field = fieldData.field;
103         if (fieldData.value != null) {
104             try {
105                 field.setAccessible(true);
106                 Field modifiersField = Field.class.getDeclaredField("modifiers");
107                 modifiersField.setAccessible(true);
108                 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
109                 field.set(testedObject, fieldData.value);
110                 return fieldData.value;
111             } catch (Exception e) {
112                 fail(e.getMessage());
113             }
114         }
115         return null;
116     }
117
118     static protected final class FieldData {
119         final public Field field;
120         public Object value;
121
122         public FieldData(Field field, Object value) {
123             assertNotNull(field);
124             this.field = field;
125             this.value = value;
126         }
127     }
128
129     protected abstract class SimpleAction implements Action {
130         public void describeTo(Description arg0) {
131         }
132
133         public abstract Object invoke(Invocation invocation) throws Throwable;
134     }
135
136     protected static ByteBuffer hexToByteBuffer(String hex) {
137         String[] hexBytes = hex.split(" ");
138         ByteBuffer bb = ByteBuffer.allocate(hexBytes.length);
139         for (String hexByte : hexBytes) {
140             bb.put((byte) Integer.parseInt(hexByte, 16));
141         }
142         bb.clear();
143         return bb;
144     }
145
146     protected static void assertHexEquals(short expected, short actual) {
147         assertEquals(String.format("0x%04X", expected), String.format("0x%04X", actual));
148     }
149
150     protected static void assertHexEquals(byte expected, byte actual) {
151         assertEquals(String.format("0x%02X", expected), String.format("0x%02X", actual));
152     }
153 }