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;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class BaseTestCase extends BaseExpectations {
27 protected static final Logger LOG = LoggerFactory.getLogger(BaseTestCase.class);
29 protected InetAddress asInet(int intValue) {
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);
39 public static void fail(String message) {
43 public static void fail() {
47 public static void assertNotNull(String message, Object object) {
48 Assert.assertNotNull(message, object);
51 public static void assertNotNull(Object object) {
52 Assert.assertNotNull(object);
55 public static void assertNull(Object message, Object object) {
56 Assert.assertNull(message.toString(), object);
59 public static void assertNull(Object object) {
60 Assert.assertNull("" + object, object);
63 public static void assertSame(String message, Object expected, Object actual) {
64 Assert.assertSame(message, expected, actual);
67 public static void assertSame(Object expected, Object actual) {
68 Assert.assertSame(expected, actual);
71 public static void assertNotSame(String message, Object unexpected, Object actual) {
72 Assert.assertNotSame(message, unexpected, actual);
75 public static void assertNotSame(Object unexpected, Object actual) {
76 Assert.assertNotSame(unexpected, actual);
79 protected Object inject(Object testedObject, String memberName, Object member) throws Exception {
80 assertNotNull(testedObject);
81 assertNotNull(memberName);
82 assertNotNull(member);
85 for (Class<?> cls = testedObject.getClass(); (cls != null) && (field == null); cls = cls.getSuperclass()) {
86 field = cls.getDeclaredField(memberName);
88 assertNotNull("Couldn't find member '" + memberName + "' in " + testedObject.getClass().getSimpleName(), field);
89 return inject(testedObject, new FieldData(field, member));
92 protected Object inject(Object testedObject, FieldData fieldData) {
93 assertNotNull(fieldData.field);
94 Field field = fieldData.field;
95 if (fieldData.value != null) {
97 field.setAccessible(true);
98 Field modifiersField = Field.class.getDeclaredField("modifiers");
99 modifiersField.setAccessible(true);
100 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
101 field.set(testedObject, fieldData.value);
102 return fieldData.value;
103 } catch (Exception e) {
104 LOG.debug("Caught generic exception", e);
105 fail(e.getMessage());
111 protected Object injectStatic(Class<?> clazz, String memberName, Object member) throws Exception {
112 assertNotNull(clazz);
113 assertNotNull(memberName);
114 assertNotNull(member);
117 for (Class<?> cls = clazz; (cls != null) && (field == null); cls = cls.getSuperclass()) {
118 field = cls.getDeclaredField(memberName);
120 assertNotNull("Couldn't find member '" + memberName + "' in " + clazz.getSimpleName(), field);
121 return inject(null, new FieldData(field, member));
124 protected static final class FieldData {
125 public final Field field;
128 public FieldData(Field field, Object value) {
129 assertNotNull(field);
135 protected abstract class SimpleAction implements Action {
136 public void describeTo(Description arg0) {
139 public abstract Object invoke(Invocation invocation) throws Throwable;
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));
152 protected static void assertHexEquals(short expected, short actual) {
153 assertEquals(String.format("0x%04X", expected), String.format("0x%04X", actual));
156 protected static void assertHexEquals(byte expected, byte actual) {
157 assertEquals(String.format("0x%02X", expected), String.format("0x%02X", actual));