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

leeonky / test-charm-java / 320

28 Sep 2025 03:56PM UTC coverage: 74.512% (+0.07%) from 74.442%
320

push

circleci

leeonky
one list one property

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

13 existing lines in 7 files now uncovered.

8466 of 11362 relevant lines covered (74.51%)

0.75 hits per line

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

97.75
/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

12
import static java.lang.String.format;
13

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

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

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

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

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

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

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

51
    public Spec<T> optional(String... traitsAndSpec) {
52
        if (property.isSingle()) {
1✔
53
            return spec.append((jFactory, objectProducer) -> {
1✔
54
                PropertyWriter<T> propertyWriter = objectProducer.getType().getPropertyWriter(property.toString());
1✔
55
                objectProducer.changeChild(property.toString(), new OptionalSpecDefaultValueProducer<>(propertyWriter.getType(), traitsAndSpec));
1✔
56
            });
1✔
57
        }
UNCOV
58
        throw new IllegalArgumentException(format("Not support property chain '%s' in current operation", property));
×
59
    }
60

61
    @Deprecated
62
    /**
63
     * reference spec and trait via string
64
     */
65
    public <V, S extends Spec<V>> IsSpec<V, S> from(Class<S> specClass) {
66
        return spec.newIsSpec(specClass, this);
1✔
67
    }
68

69
    public Spec<T> defaultValue(Object value) {
70
        return defaultValue(() -> value);
1✔
71
    }
72

73
    @SuppressWarnings("unchecked")
74
    public <V> Spec<T> defaultValue(Supplier<V> supplier) {
75
        if (supplier == null)
1✔
UNCOV
76
            return defaultValue((Object) null);
×
77
        return appendProducer((jFactory, producer, property) ->
1✔
78
                new DefaultValueProducer<>((BeanClass<V>) producer.getPropertyWriterType(property), supplier));
1✔
79
    }
80

81
    public Spec<T> byFactory() {
82
        return appendProducer((jFactory, producer, property) ->
1✔
83
                producer.createPropertyDefaultValueProducer(producer.getType().getPropertyWriter(property)).orElseGet(() ->
1✔
84
                        createCreateProducer(jFactory.type(producer.getPropertyWriterType(property).getType()))));
1✔
85
    }
86

87
    public Spec<T> byFactory(Function<Builder<?>, Builder<?>> builder) {
88
        return appendProducer((jFactory, producer, property) ->
1✔
89
                producer.createPropertyDefaultValueProducer(producer.getType().getPropertyWriter(property))
1✔
90
                        .orElseGet(() -> createQueryOrCreateProducer(builder.apply(jFactory.type(
1✔
91
                                producer.getPropertyWriterType(property).getType())))));
1✔
92
    }
93

94
    public Spec<T> dependsOn(String dependency) {
95
        spec.consistent(Object.class)
1✔
96
                .property(property.toString()).write(Function.identity())
1✔
97
                .property(dependency).read(Function.identity());
1✔
98
        return spec;
1✔
99
    }
100

101
    public Spec<T> dependsOn(String dependency, Function<Object, Object> rule) {
102
        spec.consistent(Object.class)
1✔
103
                .property(property.toString()).write(Function.identity())
1✔
104
                .property(dependency).read(rule);
1✔
105
        return spec;
1✔
106
    }
107

108
    public Spec<T> dependsOn(List<String> dependencies, Function<Object[], Object> rule) {
109
        spec.consistent(Object.class)
1✔
110
                .property(property.toString()).write(Function.identity())
1✔
111
                .properties(dependencies.toArray(new String[0])).read(rule);
1✔
112
        return spec;
1✔
113
    }
114

115
    private Spec<T> appendProducer(Fuc<JFactory, Producer<?>, String, Producer<?>> producerFactory) {
116
        if (property.isSingle() || property.isTopLevelPropertyCollection())
1✔
117
            return spec.append((jFactory, objectProducer) -> {
1✔
118
                objectProducer.changeDescendant(property, ((nextToLast, property) -> producerFactory.apply(jFactory, nextToLast, property)));
1✔
119
            });
1✔
120
        if (property.isDefaultPropertyCollection()) {
1✔
121
            return spec.append((jFactory, objectProducer) -> {
1✔
122
                PropertyWriter<T> propertyWriter = objectProducer.getType().getPropertyWriter((String) property.head());
1✔
123
                if (!propertyWriter.getType().isCollection() && propertyWriter.getType().is(Object.class)) {
1✔
124
                    Producer<?> element = producerFactory.apply(jFactory, objectProducer, "0");
1✔
125
                    propertyWriter = propertyWriter.decorateType(GenericBeanClass.create(List.class, element.getType().getGenericType()));
1✔
126
                }
127
                CollectionProducer<?, ?> collectionProducer = BeanClass.cast(objectProducer.childOrDefaultCollection(propertyWriter, true),
1✔
128
                        CollectionProducer.class).orElseThrow(() ->
1✔
129
                        new IllegalArgumentException(format("%s.%s is not list", spec.getType().getName(), property.head())));
1✔
130
                collectionProducer.changeElementDefaultValueProducerFactory(index ->
1✔
131
                        producerFactory.apply(jFactory, collectionProducer, index.toString()));
1✔
132
            });
1✔
133
        }
134
        if (property.isTopLevelDefaultPropertyCollection()) {
1✔
135
            return spec.append((jFactory, objectProducer) -> {
1✔
136
                objectProducer.changeElementDefaultValueProducerFactory(propertyWriter ->
1✔
137
                        producerFactory.apply(jFactory, objectProducer, propertyWriter.getName()));
1✔
138
            });
1✔
139
        }
140
        throw new IllegalArgumentException(format("Not support property chain '%s' in current operation", property));
1✔
141
    }
142

143
    private Spec<T> appendProducer(Function<JFactory, Producer<?>> producerFactory) {
144
        return appendProducer((jFactory, producer, s) -> producerFactory.apply(jFactory));
1✔
145
    }
146

147
    @SuppressWarnings("unchecked")
148
    private <V> Producer<V> createQueryOrCreateProducer(Builder<V> builder) {
149
        Builder<V> builderWithArgs = builder.args(spec.params(property.toString()));
1✔
150
        return builderWithArgs.queryAll().stream().findFirst().<Producer<V>>map(object ->
1✔
151
                        new BuilderValueProducer<>((BeanClass<V>) BeanClass.create(object.getClass()), builderWithArgs))
1✔
152
                .orElseGet(builderWithArgs::createProducer);
1✔
153
    }
154

155
    private <V> Producer<V> createCreateProducer(Builder<V> builder) {
156
        return builder.args(spec.params(property.toString())).createProducer();
1✔
157
    }
158

159
    public Spec<T> reverseAssociation(String association) {
160
        return spec.append((jFactory, producer) -> producer.appendReverseAssociation(property, association));
1✔
161
    }
162

163
    public Spec<T> ignore() {
164
        return spec.append((jFactory, objectProducer) -> objectProducer.ignoreProperty(property.toString()));
1✔
165
    }
166

167
    @FunctionalInterface
168
    interface Fuc<P1, P2, P3, R> {
169
        R apply(P1 p1, P2 p2, P3 p3);
170
    }
171

172
    public class IsSpec<V, S extends Spec<V>> {
173
        private final Class<S> specClass;
174
        private final String position;
175

176
        public IsSpec(Class<S> spec) {
1✔
177
            position = Thread.currentThread().getStackTrace()[4].toString();
1✔
178
            specClass = spec;
1✔
179
        }
1✔
180

181
        public Spec<T> which(Consumer<S> trait) {
182
            spec.consume(this);
1✔
183
            return appendProducer(jFactory -> createCreateProducer(jFactory.spec(specClass, trait)));
1✔
184
        }
185

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

191
        public String getPosition() {
192
            return position;
1✔
193
        }
194
    }
195

196
    public class IsSpec2<V> {
197
        private final String[] spec;
198
        private final String position;
199

200
        public IsSpec2(String[] spec) {
1✔
201
            position = Thread.currentThread().getStackTrace()[4].toString();
1✔
202
            this.spec = spec;
1✔
203
        }
1✔
204

205
        public Spec<T> and(Function<Builder<V>, Builder<V>> builder) {
206
            PropertySpec.this.spec.consume(this);
1✔
207
            return appendProducer(jFactory -> createQueryOrCreateProducer(builder.apply(jFactory.spec(spec))));
1✔
208
        }
209

210
        public String getPosition() {
211
            return position;
1✔
212
        }
213
    }
214
}
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