Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2013 André Rouél and Dominik Seichter
*
* 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 net.sf.qualitytest.blueprint;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.sf.qualitycheck.Check;
import net.sf.qualitycheck.Throws;
import net.sf.qualitycheck.exception.IllegalNullArgumentException;
import net.sf.qualitytest.ModifierBits;
import net.sf.qualitytest.blueprint.SafeInvoke.ExceptionRunnable;
import net.sf.qualitytest.blueprint.configuration.DefaultBlueprintConfiguration;
import net.sf.qualitytest.blueprint.configuration.RandomBlueprintConfiguration;
import net.sf.qualitytest.exception.BlueprintException;
import net.sf.qualitytest.exception.NoPublicConstructorException;
/**
* Blueprinting is a technique that makes writing test easier. For unit-testing you often need data-objects, where the
* actual content of the objects does not matter. {@code Blueprint} creates data-objects filled with random or defined
* data automatically based on the "Blue-Print" which is the Class itself.
*
* Blueprinting makes tests more maintainable as they depend less on test-data. Imagine, you add a new required
* attribute to a class. Usually, you have to add this to all tests using this class. With blueprinting you just have to
* add it to certain tests where the contents of the logic does actually matter. Most of the time the randomly generated
* value by {@code Blueprint} is just fine.
*
* {@code Blueprint} is similar to C#'s AutoFixture (https://github.com/AutoFixture/AutoFixture#readme).
*
* A simple example:
*
*
* final BlueprintConfiguration config = new RandomBlueprintConfiguration().with("email", "[email protected]");
* final User user = Blueprint.construct(User.class, config);
*
*
* or simpler
*
*
* final User user = Blueprint.random().with("email", "[email protected]").construct(User.class);
*
*
* {@code Blueprint} offers two custom configurations. A {@code DefaultBlueprintConfiguration} which fills any object
* using default, empty or 0 values. The second configuration, {@code RandomBlueprintConfiguration} will always generate
* a random value. Both fill child objects using a deep-tree-search.
*
* Utilities for collections can be found in {@code CollectionBlueprint}.
*
* @see DefaultBlueprintConfiguration
* @see RandomBlueprintConfiguration
*
* @author Dominik Seichter
*/
public final class Blueprint {
private static final BlueprintConfiguration DEFAULT_CONFIG = new DefaultBlueprintConfiguration();
/**
* Blueprint a Java-Bean.
*
* This method will call the default constructor and fill all setters using blueprints.
*
* @param
* @param clazz
* a class, which must have a default constructor.
* @param config
* a BlueprintConfiguration
* @param session
* A {@code BlueprintSession}
* @return a blue printed instance of {@code T}
*/
@Throws(IllegalNullArgumentException.class)
private static T bean(@Nonnull final Class clazz, @Nonnull final BlueprintConfiguration config,
@Nonnull final BlueprintSession session) {
Check.notNull(clazz, "clazz");
Check.notNull(config, "config");
Check.notNull(session, "sesion");
final T obj = safeNewInstance(clazz);
blueprintPublicMethods(obj, clazz, config, session);
blueprintPublicAttributes(obj, clazz, config, session);
return obj;
}
/**
* Blueprint a field.
*
* @param that
* Instance of the object
* @param f
* Accessible Field
* @param config
* configuration to use.
* @param session
* A {@code BlueprintSession}
*/
private static void blueprintField(final Object that, final Field f, final BlueprintConfiguration config, final BlueprintSession session) {
final CreationStrategy> creator = config.findCreationStrategyForType(f.getType());
final Object value = blueprintObject(f.getType(), config, creator, session);
SafeInvoke.invoke(new ExceptionRunnable