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

leeonky / test-charm-java / 296

15 Sep 2025 02:01PM UTC coverage: 74.593% (+0.4%) from 74.22%
296

push

circleci

leeonky
checking during consistency merge

89 of 94 new or added lines in 6 files covered. (94.68%)

11 existing lines in 6 files now uncovered.

8435 of 11308 relevant lines covered (74.59%)

0.75 hits per line

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

95.38
/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
import java.util.stream.Stream;
12

13
import static com.github.leeonky.jfactory.PropertyChain.propertyChain;
14
import static java.util.stream.Collectors.toList;
15
import static java.util.stream.Stream.concat;
16
import static java.util.stream.Stream.of;
17

18
public class Spec<T> {
1✔
19
    private final List<BiConsumer<JFactory, ObjectProducer<T>>> operations = new ArrayList<>();
1✔
20
    private final Set<PropertySpec<T>.IsSpec<?, ? extends Spec<?>>> invalidIsSpecs = new LinkedHashSet<>();
1✔
21
    private final Set<PropertySpec<T>.IsSpec2<?>> invalidIsSpec2s = new LinkedHashSet<>();
1✔
22

23
    private Instance<T> instance;
24
    private BeanClass<T> type = null;
1✔
25

26
    private ObjectFactory<T> objectFactory;
27

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

39
    protected T construct() {
40
        return objectFactory.getBase().create(instance);
1✔
41
    }
42

43
    public void main() {
44
    }
1✔
45

46
    public PropertySpec<T> property(String property) {
47
        return new PropertySpec<>(this, propertyChain(property));
1✔
48
    }
49

50
    Spec<T> append(BiConsumer<JFactory, ObjectProducer<T>> operation) {
51
        operations.add(operation);
1✔
52
        return this;
1✔
53
    }
54

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

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

80
    protected String getName() {
81
        return getClass().getSimpleName();
1✔
82
    }
83

84
    @Deprecated
85
    public Spec<T> link(String property, String... others) {
86
        List<PropertyChain> linkProperties = concat(of(property), of(others)).map(PropertyChain::propertyChain).collect(toList());
1✔
87
        append((jFactory, objectProducer) -> objectProducer.link(linkProperties));
1✔
88
        return this;
1✔
89
    }
90

91
    Spec<T> setInstance(Instance<T> instance) {
92
        this.instance = instance;
1✔
93
        return this;
1✔
94
    }
95

96
    public <P> P param(String key) {
97
        return instance.param(key);
1✔
98
    }
99

100
    public <P> P param(String key, P defaultValue) {
UNCOV
101
        return instance.param(key, defaultValue);
×
102
    }
103

104
    public Arguments params(String property) {
105
        return instance.params(property);
1✔
106
    }
107

108
    public Arguments params() {
UNCOV
109
        return instance.params();
×
110
    }
111

112
    public Instance<T> instance() {
113
        return instance;
1✔
114
    }
115

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

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

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

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

140
    void consume(PropertySpec<T>.IsSpec2<?> isSpec) {
141
        invalidIsSpec2s.remove(isSpec);
1✔
142
    }
1✔
143

144
    void append(Spec<T> spec) {
145
        operations.addAll(spec.operations);
1✔
146
        invalidIsSpecs.addAll(spec.invalidIsSpecs);
1✔
147
    }
1✔
148

149
    public Spec<T> linkNew(String propertyChain1, String propertyChain2, String... others) {
150
        Consistency<?> consistency = consistent(Object.class);
1✔
151
        concat(Stream.of(propertyChain1, propertyChain2), Stream.of(others))
1✔
152
                .forEach(consistency::direct);
1✔
153
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
1✔
154
        for (ConsistencyItem<?> item : ((DefaultConsistency<?>) consistency).items()) {
1✔
155
            item.changeLocation(stackTrace[1]).changeComposerLocation(stackTrace[1]).changeDecomposerLocation(stackTrace[1]);
1✔
156
        }
1✔
157
        return this;
1✔
158
    }
159

160
    public <V> Consistency<V> consistent(Class<V> type) {
161
        DefaultConsistency<V> consistency = new DefaultConsistency<>(type);
1✔
162
        append((jFactory, objectProducer) -> objectProducer.appendLink(consistency));
1✔
163
        return consistency;
1✔
164
    }
165
}
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