• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leeonky / test-charm-java / 347

07 Oct 2025 09:20AM UTC coverage: 74.946% (+0.2%) from 74.7%
347

push

circleci

leeonky
raise error when dependent value changed

23 of 23 new or added lines in 3 files covered. (100.0%)

20 existing lines in 6 files now uncovered.

8723 of 11639 relevant lines covered (74.95%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/jfactory/src/main/java/com/github/leeonky/jfactory/Spec.java
1
package com.github.leeonky.jfactory;
2

3
import com.github.leeonky.util.BeanClass;
4

5
import java.util.ArrayList;
6
import java.util.LinkedHashSet;
7
import java.util.List;
8
import java.util.Set;
9
import java.util.function.BiConsumer;
10
import java.util.stream.Collectors;
11

12
import static com.github.leeonky.jfactory.PropertyChain.propertyChain;
13

14
public class Spec<T> {
1✔
15
    private final List<BiConsumer<JFactory, ObjectProducer<T>>> specDefinitions = new ArrayList<>();
1✔
16
    private final List<PropertyStructureDefinition<T>> propertyStructureDefinitions = new ArrayList<>();
1✔
17
    private final Set<PropertySpec<T>.IsSpec<?, ? extends Spec<?>>> invalidIsSpecs = new LinkedHashSet<>();
1✔
18
    private final Set<PropertySpec<T>.IsSpec2<?>> invalidIsSpec2s = new LinkedHashSet<>();
1✔
19

20
    private Instance<T> instance;
21
    private BeanClass<T> type = null;
1✔
22

23
    private ObjectFactory<T> objectFactory;
24

25
    T constructBy(ObjectFactory<T> factory) {
26
        objectFactory = factory;
1✔
27
        try {
28
            if (objectFactory == null)
1✔
UNCOV
29
                throw new IllegalStateException("Illegal construct context");
×
30
            return construct();
1✔
31
        } finally {
32
            objectFactory = null;
1✔
33
        }
34
    }
35

36
    protected T construct() {
37
        return objectFactory.getBase().create(instance);
1✔
38
    }
39

40
    public void main() {
41
    }
1✔
42

43
    public PropertySpec<T> property(String property) {
44
        return new PropertySpec<>(this, propertyChain(property));
1✔
45
    }
46

47
    Spec<T> appendSpec(BiConsumer<JFactory, ObjectProducer<T>> operation) {
48
        specDefinitions.add(operation);
1✔
49
        return this;
1✔
50
    }
51

52
    void applySpecs(JFactory jFactory, ObjectProducer<T> producer) {
53
        specDefinitions.forEach(o -> o.accept(jFactory, producer));
1✔
54
        type = producer.getType();
1✔
55
        if (!invalidIsSpecs.isEmpty())
1✔
56
            throw new InvalidSpecException("Invalid property spec:\n\t"
1✔
57
                    + invalidIsSpecs.stream().map(PropertySpec.IsSpec::getPosition).collect(Collectors.joining("\n\t"))
1✔
58
                    + "\nShould finish method chain with `and` or `which`:\n"
59
                    + "\tproperty().from().which()\n"
60
                    + "\tproperty().from().and()\n"
61
                    + "Or use property().is() to create object with only spec directly.");
62
        if (!invalidIsSpec2s.isEmpty())
1✔
63
            throw new InvalidSpecException("Invalid property spec:\n\t"
1✔
64
                    + invalidIsSpec2s.stream().map(PropertySpec.IsSpec2::getPosition).collect(Collectors.joining("\n\t"))
1✔
65
                    + "\nShould finish method chain with `and`:\n"
66
                    + "\tproperty().from().and()\n"
67
                    + "Or use property().is() to create object with only spec directly.");
68
    }
1✔
69

70
    void applyPropertyStructureDefinitions(JFactory jFactory, ObjectProducer<T> producer) {
71
        specDefinitions.clear();
1✔
72
        for (PropertyStructureDefinition<T> propertyStructureDefinition : propertyStructureDefinitions)
1✔
73
            propertyStructureDefinition.apply(this, producer);
1✔
74
        applySpecs(jFactory, producer);
1✔
75
    }
1✔
76

77
    @SuppressWarnings("unchecked")
78
    public BeanClass<T> getType() {
79
        return getClass().equals(Spec.class) ? type :
1✔
80
                (BeanClass<T>) BeanClass.create(getClass()).getSuper(Spec.class).getTypeArguments(0)
1✔
81
                        .orElseThrow(() -> new IllegalStateException("Cannot guess type via generic type argument, please override Spec::getType"));
1✔
82
    }
83

84
    protected String getName() {
85
        return getClass().getSimpleName();
1✔
86
    }
87

88
    Spec<T> setInstance(Instance<T> instance) {
89
        this.instance = instance;
1✔
90
        return this;
1✔
91
    }
92

93
    public <P> P param(String key) {
94
        return instance.param(key);
1✔
95
    }
96

97
    public <P> P param(String key, P defaultValue) {
98
        return instance.param(key, defaultValue);
×
99
    }
100

101
    public Arguments params(String property) {
102
        return instance.params(property);
1✔
103
    }
104

105
    public Arguments params() {
UNCOV
106
        return instance.params();
×
107
    }
108

109
    public Instance<T> instance() {
110
        return instance;
1✔
111
    }
112

113
    public Spec<T> ignore(String... properties) {
114
        for (String property : properties)
1✔
115
            property(property).ignore();
1✔
116
        return this;
1✔
117
    }
118

119
    @Deprecated
120
    <V, S extends Spec<V>> PropertySpec<T>.IsSpec<V, S> newIsSpec(Class<S> specClass, PropertySpec<T> propertySpec) {
121
        PropertySpec<T>.IsSpec<V, S> isSpec = propertySpec.new IsSpec<V, S>(specClass);
1✔
122
        invalidIsSpecs.add(isSpec);
1✔
123
        return isSpec;
1✔
124
    }
125

126
    @Deprecated
127
    void consume(PropertySpec<T>.IsSpec<?, ? extends Spec<?>> isSpec) {
128
        invalidIsSpecs.remove(isSpec);
1✔
129
    }
1✔
130

131
    <V> PropertySpec<T>.IsSpec2<V> newIsSpec(String[] traitsAndSpec, PropertySpec<T> propertySpec) {
132
        PropertySpec<T>.IsSpec2<V> isSpec = propertySpec.new IsSpec2<V>(traitsAndSpec);
1✔
133
        invalidIsSpec2s.add(isSpec);
1✔
134
        return isSpec;
1✔
135
    }
136

137
    void consume(PropertySpec<T>.IsSpec2<?> isSpec) {
138
        invalidIsSpec2s.remove(isSpec);
1✔
139
    }
1✔
140

141
    void append(Spec<T> spec) {
142
        specDefinitions.addAll(spec.specDefinitions);
1✔
143
        invalidIsSpecs.addAll(spec.invalidIsSpecs);
1✔
144
    }
1✔
145

146
    public Spec<T> link(String propertyChain1, String propertyChain2, String... others) {
147
        Consistency<?, ?> consistency = consistent(Object.class);
1✔
148
        consistency.direct(propertyChain1)
1✔
149
                .direct(propertyChain2);
1✔
150
        for (String string : others)
1✔
151
            consistency.direct(string);
1✔
152
        return this;
1✔
153
    }
154

155
    public <V> Consistency<V, Coordinate> consistent(Class<V> type) {
156
        DefaultConsistency<V, Coordinate> consistency = new DefaultConsistency<>(type, Coordinate.class);
1✔
157
        appendSpec((jFactory, objectProducer) -> objectProducer.appendLink(consistency));
1✔
158
        return consistency;
1✔
159
    }
160

161
    public <V, C extends Coordinate> Consistency<V, C> consistent(Class<V> type, Class<C> cType) {
162
        DefaultConsistency<V, C> consistency = new DefaultConsistency<>(type, cType);
1✔
163
        appendSpec((jFactory, objectProducer) -> objectProducer.appendLink(consistency));
1✔
164
        return consistency;
1✔
165
    }
166

167
    public PropertyStructureBuilder<T> structure(String property) {
168
        return new PropertyStructureBuilder<>(this, property);
1✔
169
    }
170

171
    void appendStructureDefinition(PropertyStructureDefinition<T> propertyStructureDefinition) {
172
        propertyStructureDefinitions.add(propertyStructureDefinition);
1✔
173
    }
1✔
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc