org.xj4.jmock.MockeryLifecycle Maven / Gradle / Ivy
/**
* Copyright 2008 Peach Jean Solutions
*
* Licensed 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 org.xj4.jmock;
import java.lang.reflect.Field;
import java.util.List;
import org.xj4.lifecycle.Lifecycle;
import org.xj4.XJ4TestClass;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.Mockery;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/**
* @author jbunting
* @TODO Document Me!
* Created by IntelliJ IDEA.
* Date: Mar 5, 2008
* Time: 9:03:36 AM
*/
public class MockeryLifecycle implements Lifecycle {
private static final Log logger = LogFactory.getLog(MockeryLifecycle.class);
public boolean supportsClassLevel() {
// this could work, but it's really inadvisable to do it this way...
return false;
}
public boolean supportsInstanceLevel() {
return true;
}
public void start(XJ4TestClass testClass, Field field, Object target) {
if(!field.getType().isAssignableFrom(JUnit4Mockery.class)) {
throw new IllegalArgumentException("This lifecycle can only manage field " + field.getName() + " if it is of type " + Mockery.class.getName());
}
field.setAccessible(true);
try {
if(field.get(target) == null) {
Mockery mockery = new JUnit4Mockery();
field.set(target, mockery);
}
} catch(IllegalAccessException e) {
throw new IllegalArgumentException("This lifecycle does not have permission to access field " + field.getName());
}
}
public void stop(XJ4TestClass testClass, Field field, Object target) {
try {
Mockery mockery = (Mockery) field.get(target);
mockery.assertIsSatisfied();
} catch(IllegalAccessException e) {
throw new IllegalArgumentException("This lifecycle does not have permission to access field " + field.getName());
}
}
public Class> requiredType() {
return Mockery.class;
}
public boolean acceptsNull() {
return true;
}
public void validate(List errors, Field field) {
// we have nothing to validate
}
}