View Javadoc

1   /*******************************************************************************
2    * Copyright 2013 André Rouél and Dominik Seichter
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  package net.sf.qualitytest.blueprint.configuration;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import net.sf.qualitytest.blueprint.CycleHandlingStrategy;
22  import net.sf.qualitytest.blueprint.strategy.creation.BlueprintCreationStrategy;
23  import net.sf.qualitytest.blueprint.strategy.creation.BlueprintStringCreationStrategy;
24  import net.sf.qualitytest.blueprint.strategy.creation.RandomBooleanValueCreationStrategy;
25  import net.sf.qualitytest.blueprint.strategy.creation.RandomByteValueCreationStrategy;
26  import net.sf.qualitytest.blueprint.strategy.creation.RandomCharValueCreationStrategy;
27  import net.sf.qualitytest.blueprint.strategy.creation.RandomDoubleValueCreationStrategy;
28  import net.sf.qualitytest.blueprint.strategy.creation.RandomEnumCreationStrategy;
29  import net.sf.qualitytest.blueprint.strategy.creation.RandomFloatValueCreationStrategy;
30  import net.sf.qualitytest.blueprint.strategy.creation.RandomIntValueCreationStrategy;
31  import net.sf.qualitytest.blueprint.strategy.creation.RandomLongValueCreationStrategy;
32  import net.sf.qualitytest.blueprint.strategy.creation.RandomShortValueCreationStrategy;
33  import net.sf.qualitytest.blueprint.strategy.creation.ValueCreationStrategy;
34  import net.sf.qualitytest.blueprint.strategy.matching.InstanceOfTypeMatchingStrategy;
35  import net.sf.qualitytest.blueprint.strategy.matching.SetterMethodMatchingStrategy;
36  import net.sf.qualitytest.blueprint.strategy.matching.TypeMatchingStrategy;
37  
38  import com.google.common.collect.ImmutableList;
39  
40  /**
41   * {@code BlueprintConfiguration} which assigns all primitive types and their corresponding object types with a random
42   * value.
43   * 
44   * Additionally, support for the interfaces {@code java.util.List}, {@code java.util.Set} and {@code java.util.Map} is
45   * added. If these interface are encountered, empty lists, maps and sets are created.
46   * 
47   * @author Dominik Seichter
48   * 
49   */
50  public final class RandomBlueprintConfiguration extends ImmutableBlueprintConfiguration {
51  
52  	private static final ValueCreationStrategy<Long> LONG_DEFAULT = new RandomLongValueCreationStrategy();
53  	private static final ValueCreationStrategy<Integer> INTEGER_DEFAULT = new RandomIntValueCreationStrategy();
54  	private static final ValueCreationStrategy<Boolean> BOOLEAN_DEFAULT = new RandomBooleanValueCreationStrategy();
55  	private static final ValueCreationStrategy<Character> CHARACTER_DEFAULT = new RandomCharValueCreationStrategy();
56  	private static final ValueCreationStrategy<Short> SHORT_DEFAULT = new RandomShortValueCreationStrategy();
57  	private static final ValueCreationStrategy<Byte> BYTE_DEFAULT = new RandomByteValueCreationStrategy();
58  	private static final ValueCreationStrategy<Float> FLOAT_DEFAULT = new RandomFloatValueCreationStrategy();
59  	private static final ValueCreationStrategy<Double> DOUBLE_DEFAULT = new RandomDoubleValueCreationStrategy();
60  
61  	/**
62  	 * Add the default enum handling to a map.
63  	 * 
64  	 * @param map
65  	 */
66  	public static void addRandomEnumStrategy(final List<StrategyPair> list) {
67  		list.add(new StrategyPair(new InstanceOfTypeMatchingStrategy(Enum.class), new RandomEnumCreationStrategy()));
68  	}
69  
70  	private static List<StrategyPair> createDefaultAttributeMapping() {
71  		final List<StrategyPair> list = new ArrayList<StrategyPair>();
72  		list.add(new StrategyPair(new TypeMatchingStrategy(String.class), new BlueprintStringCreationStrategy()));
73  		list.add(new StrategyPair(new TypeMatchingStrategy(Long.class), LONG_DEFAULT));
74  		list.add(new StrategyPair(new TypeMatchingStrategy(long.class), LONG_DEFAULT));
75  		list.add(new StrategyPair(new TypeMatchingStrategy(Integer.class), INTEGER_DEFAULT));
76  		list.add(new StrategyPair(new TypeMatchingStrategy(int.class), INTEGER_DEFAULT));
77  		list.add(new StrategyPair(new TypeMatchingStrategy(Boolean.class), BOOLEAN_DEFAULT));
78  		list.add(new StrategyPair(new TypeMatchingStrategy(boolean.class), BOOLEAN_DEFAULT));
79  		list.add(new StrategyPair(new TypeMatchingStrategy(Character.class), CHARACTER_DEFAULT));
80  		list.add(new StrategyPair(new TypeMatchingStrategy(char.class), CHARACTER_DEFAULT));
81  		list.add(new StrategyPair(new TypeMatchingStrategy(Short.class), SHORT_DEFAULT));
82  		list.add(new StrategyPair(new TypeMatchingStrategy(short.class), SHORT_DEFAULT));
83  		list.add(new StrategyPair(new TypeMatchingStrategy(Byte.class), BYTE_DEFAULT));
84  		list.add(new StrategyPair(new TypeMatchingStrategy(byte.class), BYTE_DEFAULT));
85  		list.add(new StrategyPair(new TypeMatchingStrategy(Float.class), FLOAT_DEFAULT));
86  		list.add(new StrategyPair(new TypeMatchingStrategy(float.class), FLOAT_DEFAULT));
87  		list.add(new StrategyPair(new TypeMatchingStrategy(Double.class), DOUBLE_DEFAULT));
88  		list.add(new StrategyPair(new TypeMatchingStrategy(double.class), DOUBLE_DEFAULT));
89  
90  		addRandomEnumStrategy(list);
91  		DefaultBlueprintConfiguration.addDefaultArrayStrategy(list);
92  		DefaultBlueprintConfiguration.addDefaultCollections(list);
93  		list.add(new StrategyPair(new SetterMethodMatchingStrategy(), new BlueprintCreationStrategy()));
94  
95  		return list;
96  	}
97  
98  	public RandomBlueprintConfiguration() {
99  		super(createDefaultAttributeMapping(), ImmutableList.<CycleHandlingStrategy<?>> of(), false);
100 	}
101 }