1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45
46
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
63
64
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
73
74
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
90
91
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 }