• 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

98.67
/jfactory/src/main/java/com/github/leeonky/jfactory/PropertySpec.java
1
package com.github.leeonky.jfactory;
2

3
import com.github.leeonky.util.BeanClass;
4
import com.github.leeonky.util.GenericBeanClass;
5
import com.github.leeonky.util.PropertyWriter;
6

7
import java.util.List;
8
import java.util.function.Consumer;
9
import java.util.function.Function;
10
import java.util.function.Supplier;
11
import java.util.stream.Collectors;
12

13
import static java.lang.String.format;
14
import static java.util.Collections.singletonList;
15

16
public class PropertySpec<T> {
17
    private final Spec<T> spec;
18
    private final PropertyChain property;
19

20
    PropertySpec(Spec<T> spec, PropertyChain property) {
1✔
21
        this.spec = spec;
1✔
22
        this.property = property;
1✔
23
    }
1✔
24

25
    public Spec<T> value(Object value) {
26
        return value(() -> value);
1✔
27
    }
28

29
    @SuppressWarnings("unchecked")
30
    public <V> Spec<T> value(Supplier<V> value) {
31
        if (value == null)
1✔
32
            return value(() -> null);
1✔
33
        return appendProducer((jFactory, producer, property) ->
1✔
34
                new UnFixedValueProducer<>(value, (BeanClass<V>) producer.getPropertyWriterType(property)));
1✔
35
    }
36

37
    @Deprecated
38
    /**
39
     * reference spec and trait via string
40
     */
41
    public <V> Spec<T> is(Class<? extends Spec<V>> specClass) {
42
        return appendProducer(jFactory -> createCreateProducer(jFactory.spec(specClass)));
1✔
43
    }
44

45
    public Spec<T> is(String... traitsAndSpec) {
46
        return appendProducer(jFactory -> createCreateProducer(jFactory.spec(traitsAndSpec)));
1✔
47
    }
48

49
    public <V> IsSpec2<V> from(String... traitsAndSpec) {
50
        return spec.newIsSpec(traitsAndSpec, this);
1✔
51
    }
52

53
    @Deprecated
54
    /**
55
     * reference spec and trait via string
56
     */
57
    public <V, S extends Spec<V>> IsSpec<V, S> from(Class<S> specClass) {
58
        return spec.newIsSpec(specClass, this);
1✔
59
    }
60

61
    public Spec<T> defaultValue(Object value) {
62
        return defaultValue(() -> value);
1✔
63
    }
64

65
    @SuppressWarnings("unchecked")
66
    public <V> Spec<T> defaultValue(Supplier<V> supplier) {
67
        if (supplier == null)
1✔
UNCOV
68
            return defaultValue((Object) null);
×
69
        return appendProducer((jFactory, producer, property) ->
1✔
70
                new DefaultValueProducer<>((BeanClass<V>) producer.getPropertyWriterType(property), supplier));
1✔
71
    }
72

73
    public Spec<T> byFactory() {
74
        return appendProducer((jFactory, producer, property) ->
1✔
75
                producer.createPropertyDefaultValueProducer(producer.getType().getPropertyWriter(property)).orElseGet(() ->
1✔
76
                        createCreateProducer(jFactory.type(producer.getPropertyWriterType(property).getType()))));
1✔
77
    }
78

79
    public Spec<T> byFactory(Function<Builder<?>, Builder<?>> builder) {
80
        return appendProducer((jFactory, producer, property) ->
1✔
81
                producer.createPropertyDefaultValueProducer(producer.getType().getPropertyWriter(property))
1✔
82
                        .orElseGet(() -> createQueryOrCreateProducer(builder.apply(jFactory.type(
1✔
83
                                producer.getPropertyWriterType(property).getType())))));
1✔
84
    }
85

86
    public Spec<T> dependsOn(String dependency, Function<Object, Object> rule) {
87
        return dependsOn(singletonList(dependency), objects -> rule.apply(objects[0]));
1✔
88
    }
89

90
    public Spec<T> dependsOn(List<String> dependencies, Function<Object[], Object> rule) {
91
        return spec.append((jFactory, objectProducer) ->
1✔
92
                objectProducer.addDependency(property, rule,
1✔
93
                        dependencies.stream().map(PropertyChain::propertyChain).collect(Collectors.toList())));
1✔
94
    }
95

96
    private Spec<T> appendProducer(Fuc<JFactory, Producer<?>, String, Producer<?>> producerFactory) {
97
        if (property.isSingle() || property.isTopLevelPropertyCollection())
1✔
98
            return spec.append((jFactory, objectProducer) -> {
1✔
99
                objectProducer.changeDescendant(property, ((nextToLast, property) -> producerFactory.apply(jFactory, nextToLast, property)));
1✔
100
            });
1✔
101
        if (property.isDefaultPropertyCollection()) {
1✔
102
            return spec.append((jFactory, objectProducer) -> {
1✔
103
                PropertyWriter<T> propertyWriter = objectProducer.getType().getPropertyWriter((String) property.head());
1✔
104
                if (!propertyWriter.getType().isCollection() && propertyWriter.getType().is(Object.class)) {
1✔
105
                    Producer<?> element = producerFactory.apply(jFactory, objectProducer, "0");
1✔
106
                    propertyWriter = propertyWriter.decorateType(GenericBeanClass.create(List.class, element.getType().getGenericType()));
1✔
107
                }
108
                CollectionProducer<?, ?> collectionProducer = BeanClass.cast(objectProducer.childOrDefaultCollection(propertyWriter, true),
1✔
109
                        CollectionProducer.class).orElseThrow(() ->
1✔
110
                        new IllegalArgumentException(format("%s.%s is not list", spec.getType().getName(), property.head())));
1✔
111
                collectionProducer.changeElementDefaultValueProducerFactory(index ->
1✔
112
                        producerFactory.apply(jFactory, collectionProducer, index.toString()));
1✔
113
            });
1✔
114
        }
115
        if (property.isTopLevelDefaultPropertyCollection()) {
1✔
116
            return spec.append((jFactory, objectProducer) -> {
1✔
117
                objectProducer.changeElementDefaultValueProducerFactory(propertyWriter ->
1✔
118
                        producerFactory.apply(jFactory, objectProducer, propertyWriter.getName()));
1✔
119
            });
1✔
120
        }
121
        throw new IllegalArgumentException(format("Not support property chain '%s' in current operation", property));
1✔
122
    }
123

124
    private Spec<T> appendProducer(Function<JFactory, Producer<?>> producerFactory) {
125
        return appendProducer((jFactory, producer, s) -> producerFactory.apply(jFactory));
1✔
126
    }
127

128
    @SuppressWarnings("unchecked")
129
    private <V> Producer<V> createQueryOrCreateProducer(Builder<V> builder) {
130
        Builder<V> builderWithArgs = builder.args(spec.params(property.toString()));
1✔
131
        return builderWithArgs.queryAll().stream().findFirst().<Producer<V>>map(object ->
1✔
132
                        new BuilderValueProducer<>((BeanClass<V>) BeanClass.create(object.getClass()), builderWithArgs))
1✔
133
                .orElseGet(builderWithArgs::createProducer);
1✔
134
    }
135

136
    private <V> Producer<V> createCreateProducer(Builder<V> builder) {
137
        return builder.args(spec.params(property.toString())).createProducer();
1✔
138
    }
139

140
    public Spec<T> reverseAssociation(String association) {
141
        return spec.append((jFactory, producer) -> producer.appendReverseAssociation(property, association));
1✔
142
    }
143

144
    public Spec<T> ignore() {
145
        return spec.append((jFactory, objectProducer) -> objectProducer.ignoreProperty(property.toString()));
1✔
146
    }
147

148
    @FunctionalInterface
149
    interface Fuc<P1, P2, P3, R> {
150
        R apply(P1 p1, P2 p2, P3 p3);
151
    }
152

153
    public class IsSpec<V, S extends Spec<V>> {
154
        private final Class<S> specClass;
155
        private final String position;
156

157
        public IsSpec(Class<S> spec) {
1✔
158
            position = Thread.currentThread().getStackTrace()[4].toString();
1✔
159
            specClass = spec;
1✔
160
        }
1✔
161

162
        public Spec<T> which(Consumer<S> trait) {
163
            spec.consume(this);
1✔
164
            return appendProducer(jFactory -> createCreateProducer(jFactory.spec(specClass, trait)));
1✔
165
        }
166

167
        public Spec<T> and(Function<Builder<V>, Builder<V>> builder) {
168
            spec.consume(this);
1✔
169
            return appendProducer(jFactory -> createQueryOrCreateProducer(builder.apply(jFactory.spec(specClass))));
1✔
170
        }
171

172
        public String getPosition() {
173
            return position;
1✔
174
        }
175
    }
176

177
    public class IsSpec2<V> {
178
        private final String[] spec;
179
        private final String position;
180

181
        public IsSpec2(String[] spec) {
1✔
182
            position = Thread.currentThread().getStackTrace()[4].toString();
1✔
183
            this.spec = spec;
1✔
184
        }
1✔
185

186
        public Spec<T> and(Function<Builder<V>, Builder<V>> builder) {
187
            PropertySpec.this.spec.consume(this);
1✔
188
            return appendProducer(jFactory -> createQueryOrCreateProducer(builder.apply(jFactory.spec(spec))));
1✔
189
        }
190

191
        public String getPosition() {
192
            return position;
1✔
193
        }
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