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

leeonky / test-charm-java / 305

21 Sep 2025 03:53PM UTC coverage: 74.692% (+7.6%) from 67.074%
305

push

circleci

leeonky
Refactor

9 of 10 new or added lines in 3 files covered. (90.0%)

8482 of 11356 relevant lines covered (74.69%)

0.75 hits per line

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

93.65
/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
import static java.util.stream.Collectors.toList;
14
import static java.util.stream.Stream.concat;
15
import static java.util.stream.Stream.of;
16

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

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

25
    private ObjectFactory<T> objectFactory;
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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