2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.tools.junit;
11 import static org.junit.Assert.assertEquals;
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;
19 import org.hamcrest.Description;
20 import org.jmock.api.Action;
21 import org.jmock.api.Invocation;
22 import org.junit.Assert;
24 public abstract class BaseTestCase extends BaseExpectations {
25 protected InetAddress asInet(int intValue) {
27 return InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(intValue).array());
28 } catch (UnknownHostException e) {
34 public static void fail(String message) {
38 public static void fail() {
42 public static void assertNotNull(String message, Object object) {
43 Assert.assertNotNull(message, object);
46 public static void assertNotNull(Object object) {
47 Assert.assertNotNull(object);
50 public static void assertNull(Object message, Object object) {
51 Assert.assertNull(message.toString(), object);
54 public static void assertNull(Object object) {
55 Assert.assertNull("" + object, object);
58 public static void assertSame(String message, Object expected, Object actual) {
59 Assert.assertSame(message, expected, actual);
62 public static void assertSame(Object expected, Object actual) {
63 Assert.assertSame(expected, actual);
66 public static void assertNotSame(String message, Object unexpected, Object actual) {
67 Assert.assertNotSame(message, unexpected, actual);
70 public static void assertNotSame(Object unexpected, Object actual) {
71 Assert.assertNotSame(unexpected, actual);
74 protected Object inject(Object testedObject, String memberName, Object member) throws Exception {
75 assertNotNull(testedObject);
76 assertNotNull(memberName);
77 assertNotNull(member);
80 for (Class<?> cls = testedObject.getClass(); (cls != null) && (field == null); cls = cls.getSuperclass()) {
81 field = cls.getDeclaredField(memberName);
83 assertNotNull("Couldn't find member '" + memberName + "' in " + testedObject.getClass().getSimpleName(), field);
84 return inject(testedObject, new FieldData(field, member));
87 protected Object injectStatic(Class<?> clazz, String memberName, Object member) throws Exception {
89 assertNotNull(memberName);
90 assertNotNull(member);
93 for (Class<?> cls = clazz; (cls != null) && (field == null); cls = cls.getSuperclass()) {
94 field = cls.getDeclaredField(memberName);
96 assertNotNull("Couldn't find member '" + memberName + "' in " + clazz.getSimpleName(), field);
97 return inject(null, new FieldData(field, member));
100 protected Object inject(Object testedObject, FieldData fieldData) {
101 assertNotNull(fieldData.field);
102 Field field = fieldData.field;
103 if (fieldData.value != null) {
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());
118 static protected final class FieldData {
119 final public Field field;
122 public FieldData(Field field, Object value) {
123 assertNotNull(field);
129 protected abstract class SimpleAction implements Action {
130 public void describeTo(Description arg0) {
133 public abstract Object invoke(Invocation invocation) throws Throwable;
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));
146 protected static void assertHexEquals(short expected, short actual) {
147 assertEquals(String.format("0x%04X", expected), String.format("0x%04X", actual));
150 protected static void assertHexEquals(byte expected, byte actual) {
151 assertEquals(String.format("0x%02X", expected), String.format("0x%02X", actual));