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

leeonky / test-charm-java / 368

12 Oct 2025 01:55PM UTC coverage: 75.301% (+0.1%) from 75.185%
368

push

circleci

leeonky
one to many

13 of 13 new or added lines in 2 files covered. (100.0%)

10 existing lines in 4 files now uncovered.

8893 of 11810 relevant lines covered (75.3%)

0.75 hits per line

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

96.34
/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.*;
6
import java.util.function.BiConsumer;
7
import java.util.stream.Collectors;
8

9
import static com.github.leeonky.jfactory.PropertyChain.propertyChain;
10

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

17
    private Instance<T> instance;
18
    private BeanClass<T> type = null;
1✔
19
    //    TODO to private
20
    Optional<Association> association = Optional.empty();
1✔
21
    Optional<ReverseAssociation> reverseAssociation = Optional.empty();
1✔
22
    ObjectProducer<?> objectProducer;
23

24
    private ObjectFactory<T> objectFactory;
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

172
    public ListStructure<T, Coordinate> structure() {
173
        return structure(Coordinate.class);
1✔
174
    }
175

176
    public <C extends Coordinate> ListStructure<T, C> structure(Class<C> coordinateType) {
177
        DefaultListStructure<T, C> listStructure = new DefaultListStructure<>(coordinateType);
1✔
178
        appendSpec((jFactory, objectProducer) -> objectProducer.appendListStructure(listStructure));
1✔
179
        return listStructure;
1✔
180
    }
181

182
    void appendStructureDefinition(PropertyStructureDefinition<T> propertyStructureDefinition) {
183
        propertyStructureDefinitions.add(propertyStructureDefinition);
1✔
184
    }
1✔
185

186
    boolean isAssociation(String property) {
187
        return association.map(a -> a.matches(property)).orElse(false);
1✔
188
    }
189

190
    boolean isReverseAssociation(PropertyChain property) {
191
        return objectProducer.reverseAssociation(property)
1✔
192
                .map(s -> reverseAssociation.map(a -> a.matches(s)).orElse(false))
1✔
193
                .orElse(false);
1✔
194
    }
195
}
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