/*
* Copyright 2011 Daniel Josefsson
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package se.dannej.fakehttpserver.junit;
import static org.junit.Assert.fail;
import static se.dannej.fakehttpserver.expect.ExpectationState.clearExpectations;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import se.dannej.fakehttpserver.FakeServer;
import se.dannej.fakehttpserver.FakeServerException;
/**
* This is a {@link MethodRule} that finds all servers defined in fields.
*
* <p>For each server, it:
* <ul>
* <li>asserts that the expectations were met
* <li>shuts it down
* </ul>
*/
public class FakeServerRule implements MethodRule {
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
clearExpectations();
final List<FakeServer> fakeServers = locateFakeServers(target);
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
assertServers(fakeServers);
} finally {
shutdownServers(fakeServers);
}
}
};
}
private void assertServers(List<FakeServer> servers) {
StringBuilder failMessage = new StringBuilder();
Description mismatchDescription = new StringDescription(failMessage);
boolean allSatisfied = true;
for (FakeServer server : servers) {
boolean isSatisfied = server.isSatisfied(mismatchDescription);
allSatisfied &= isSatisfied;
}
if (!allSatisfied) {
fail(failMessage.toString());
}
}
private void shutdownServers(List<FakeServer> servers) {
for (FakeServer server : servers) {
server.stop();
}
}
private List<FakeServer> locateFakeServers(Object target) {
return recursivelyLocateFieldsOfClass(target, target.getClass(), FakeServer.class);
}
private static <T> List<T> recursivelyLocateFieldsOfClass(Object target, Class<? extends Object> targetClass, Class<T> classOfField) {
List<T> result = new ArrayList<T>();
Class<? extends Object> superClass = targetClass.getSuperclass();
if (superClass != null) {
result.addAll(recursivelyLocateFieldsOfClass(target, superClass, classOfField));
}
result.addAll(locateFieldsOfClass(target, targetClass, classOfField));
return result;
}
@SuppressWarnings("unchecked")
private static <T> List<T> locateFieldsOfClass(Object target, Class<? extends Object> targetClass,
Class<T> classOfField) {
List<T> result = new ArrayList<T>();
Field[] declaredFields = targetClass.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
if (classOfField.isAssignableFrom(field.getType())) {
try {
T found = (T) field.get(target);
result.add(found);
} catch (Exception e) {
throw new FakeServerException("Failed value of field " + field.getName() + " from " + target, e);
}
}
}
return result;
}
}