All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.qualitytest.blueprint.strategy.creation.DefaultArrayCreationStrategy Maven / Gradle / Ivy

Go to download

The goal of quality-test is to provide a small Java library for basic code quality checks within unit tests. It is discouraged to use quality-test in production code. The checks provided in this library are designed to be used in unit-tests. The checks and utilities provided in this package check static properties of classes, for example whether classes are marked final or constructors are private. Additionally, there are utils to give additional code coverage for private constructors. Quality-Test often can be used together with Google Reflections (http://code.google.com/p/reflections/) to perform checks such as, assure that every class in package *.dto.* is final or make sure that no class in the package *.controller.* contains any non-final static variables.

There is a newer version: 1.3
Show newest version
/*******************************************************************************
 * 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.strategy.creation;

import java.lang.reflect.Array;

import net.sf.qualitycheck.Check;
import net.sf.qualitytest.blueprint.Blueprint;
import net.sf.qualitytest.blueprint.BlueprintConfiguration;
import net.sf.qualitytest.blueprint.BlueprintSession;
import net.sf.qualitytest.blueprint.CreationStrategy;

public class DefaultArrayCreationStrategy implements CreationStrategy {

	public static final int DEFAULT_LENGTH = 7;

	/**
	 * Initalize an array using blueprinted objects.
	 * 
	 * @param 
	 *            Type parameter of the array.
	 * @param array
	 *            class of the array
	 * @param arraySize
	 *            size of the array
	 * @param value
	 *            object where the array is stored
	 * @param config
	 *            A {@code BlueprintConfiguration}
	 * @param session
	 *            A {@code BlueprintSession}
	 */
	private static  void initializeArray(final Class array, final int arraySize, final Object value,
			final BlueprintConfiguration config, final BlueprintSession session) {
		for (int i = 0; i < arraySize; i++) {
			final Object blueprint = Blueprint.construct(array.getComponentType(), config, session);
			Array.set(value, i, blueprint);
		}
	}

	private final int length;

	public DefaultArrayCreationStrategy(final int length) {
		this.length = length;
	}

	@Override
	public Object createValue(final Class expectedClass, final BlueprintConfiguration config, final BlueprintSession session) {
		Check.notNull(expectedClass, "expectedClass");

		final Object value = Array.newInstance(expectedClass.getComponentType(), length);

		if (!expectedClass.getComponentType().isPrimitive()) {
			initializeArray(expectedClass, length, value, config, session);
		}

		return value;
	}

}