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.Collection;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import net.sf.qualitycheck.Check;
27  import net.sf.qualitytest.blueprint.CycleHandlingStrategy;
28  import net.sf.qualitytest.blueprint.strategy.creation.BlueprintCreationStrategy;
29  import net.sf.qualitytest.blueprint.strategy.creation.DefaultArrayCreationStrategy;
30  import net.sf.qualitytest.blueprint.strategy.creation.DefaultEnumCreationStrategy;
31  import net.sf.qualitytest.blueprint.strategy.creation.SingleValueCreationStrategy;
32  import net.sf.qualitytest.blueprint.strategy.matching.ArrayTypeMatchingStrategy;
33  import net.sf.qualitytest.blueprint.strategy.matching.InstanceOfTypeMatchingStrategy;
34  import net.sf.qualitytest.blueprint.strategy.matching.SetterMethodMatchingStrategy;
35  import net.sf.qualitytest.blueprint.strategy.matching.TypeMatchingStrategy;
36  
37  import com.google.common.collect.ImmutableList;
38  
39  /**
40   * Default {@code BlueprintConfiguration} which assigns all primitive types and their corresponding object types which a
41   * default value, which is usually {@code 0}.
42   * 
43   * Additionally, support for the interfaces {@code java.util.List}, {@code java.util.Set} and {@code java.util.list} is
44   * added. If these interface are encountered, empty lists, lists and sets are created.
45   * 
46   * @author Dominik Seichter
47   * 
48   */
49  public final class DefaultBlueprintConfiguration extends ImmutableBlueprintConfiguration {
50  
51  	private static final String STRING_DEFAULT = "";
52  	private static final Long LONG_DEFAULT = Long.valueOf(0l);
53  	private static final Integer INTEGER_DEFAULT = Integer.valueOf(0);
54  	private static final Boolean BOOLEAN_DEFAULT = Boolean.FALSE;
55  	private static final Character CHARACTER_DEFAULT = Character.valueOf((char) 0);
56  	private static final Short SHORT_DEFAULT = Short.valueOf((short) 0);
57  	private static final Byte BYTE_DEFAULT = Byte.valueOf((byte) 0);
58  	private static final Float FLOAT_DEFAULT = Float.valueOf(0.0f);
59  	private static final Double DOUBLE_DEFAULT = Double.valueOf(0.0);
60  
61  	/**
62  	 * Add the default array handling to a list.
63  	 * 
64  	 * @param list
65  	 */
66  	public static void addDefaultArrayStrategy(final List<StrategyPair> list) {
67  		list.add(new StrategyPair(new ArrayTypeMatchingStrategy(), new DefaultArrayCreationStrategy(
68  				DefaultArrayCreationStrategy.DEFAULT_LENGTH)));
69  	}
70  
71  	/**
72  	 * Add the default implementations for collection interfaces to a list.
73  	 * 
74  	 * @param list
75  	 */
76  	public static void addDefaultCollections(final List<StrategyPair> list) {
77  		Check.notNull(list, "list");
78  
79  		list.add(new StrategyPair(new TypeMatchingStrategy(Map.class), new SingleValueCreationStrategy<Map<Object, Object>>(
80  				new HashMap<Object, Object>())));
81  		list.add(new StrategyPair(new TypeMatchingStrategy(Set.class), new SingleValueCreationStrategy<Set<Object>>(new HashSet<Object>())));
82  		list.add(new StrategyPair(new TypeMatchingStrategy(List.class), new SingleValueCreationStrategy<List<Object>>(
83  				new ArrayList<Object>())));
84  		list.add(new StrategyPair(new TypeMatchingStrategy(Collection.class), new SingleValueCreationStrategy<Collection<Object>>(
85  				new ArrayList<Object>())));
86  	}
87  
88  	/**
89  	 * Add the default enum handling to a list.
90  	 * 
91  	 * @param list
92  	 */
93  	public static void addDefaultEnumStrategy(final List<StrategyPair> list) {
94  		list.add(new StrategyPair(new InstanceOfTypeMatchingStrategy(Enum.class), new DefaultEnumCreationStrategy()));
95  	}
96  
97  	private static List<StrategyPair> createDefaultAttributeMapping() {
98  		final List<StrategyPair> list = new ArrayList<StrategyPair>();
99  		list.add(createStrategyPair(String.class, STRING_DEFAULT));
100 		list.add(createStrategyPair(Long.class, LONG_DEFAULT));
101 		list.add(createStrategyPair(long.class, LONG_DEFAULT));
102 		list.add(createStrategyPair(Integer.class, INTEGER_DEFAULT));
103 		list.add(createStrategyPair(int.class, INTEGER_DEFAULT));
104 		list.add(createStrategyPair(Boolean.class, BOOLEAN_DEFAULT));
105 		list.add(createStrategyPair(boolean.class, BOOLEAN_DEFAULT));
106 		list.add(createStrategyPair(Character.class, CHARACTER_DEFAULT));
107 		list.add(createStrategyPair(char.class, CHARACTER_DEFAULT));
108 		list.add(createStrategyPair(Short.class, SHORT_DEFAULT));
109 		list.add(createStrategyPair(short.class, SHORT_DEFAULT));
110 		list.add(createStrategyPair(Byte.class, BYTE_DEFAULT));
111 		list.add(createStrategyPair(byte.class, BYTE_DEFAULT));
112 		list.add(createStrategyPair(Float.class, FLOAT_DEFAULT));
113 		list.add(createStrategyPair(float.class, FLOAT_DEFAULT));
114 		list.add(createStrategyPair(Double.class, DOUBLE_DEFAULT));
115 		list.add(createStrategyPair(double.class, DOUBLE_DEFAULT));
116 
117 		addDefaultEnumStrategy(list);
118 		addDefaultArrayStrategy(list);
119 		addDefaultCollections(list);
120 		list.add(new StrategyPair(new SetterMethodMatchingStrategy(), new BlueprintCreationStrategy()));
121 
122 		return list;
123 	}
124 
125 	private static <T> StrategyPair createStrategyPair(final Class<T> clazz, final T defaultValue) {
126 		return new StrategyPair(new TypeMatchingStrategy(clazz), new SingleValueCreationStrategy<T>(defaultValue));
127 	}
128 
129 	public DefaultBlueprintConfiguration() {
130 		super(createDefaultAttributeMapping(), ImmutableList.<CycleHandlingStrategy<?>> of(), false);
131 	}
132 }