Skip to content

Commit 2b7c080

Browse files
committed
add tests
1 parent bab9251 commit 2b7c080

File tree

11 files changed

+431
-231
lines changed

11 files changed

+431
-231
lines changed

core.loaders/maven/src/main/java/net/lecousin/core/loaders/maven/MavenPOM.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ else if (xml.event.text.equals(ELEMENT_ARTIFACT_ID))
564564
else
565565
xml.closeElement();
566566
}
567-
if (e.getValue1().equals("*")) e.setValue1(null);
568-
if (e.getValue2().equals("*")) e.setValue2(null);
567+
if ("*".equals(e.getValue1())) e.setValue1(null);
568+
if ("*".equals(e.getValue2())) e.setValue2(null);
569569
exclusions.add(e);
570570
} else {
571571
xml.closeElement();

core.loaders/maven/src/test/java/net/lecousin/core/loaders/maven/tests/TestLocalRepository.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,26 @@ public void testPOMWithRepositories() throws Exception {
236236
}
237237
}
238238

239+
@Test
240+
public void testPOMWithAdditionalFields() throws Exception {
241+
File outFile = new File("./test-additional-fields.pom.xml");
242+
outFile.createNewFile();
243+
outFile.deleteOnExit();
244+
try {
245+
new File("./target/test-out").mkdir();
246+
FileIO.WriteOnly out = new FileIO.WriteOnly(outFile, Task.PRIORITY_NORMAL);
247+
IOUtil.copy(
248+
((IOProvider.Readable)IOProviderFromURI.getInstance().get(new URI("classpath:test-maven/test-additional-fields.pom.xml"))).provideIOReadable(Task.PRIORITY_NORMAL),
249+
out,
250+
-1, true, null, 0).blockThrow(15000);
251+
252+
AsyncSupplier<MavenPOM, LibraryManagementException> load = MavenPOM.load(new File("./test-additional-fields.pom.xml").toURI(), Task.PRIORITY_NORMAL, pomLoader, false);
253+
load.blockResult(30000);
254+
} finally {
255+
outFile.delete();
256+
}
257+
}
258+
239259
private void testError(String errorName) throws IOException, CancelException, URISyntaxException {
240260
File outFile = new File("./test-error-" + errorName + ".pom.xml");
241261
outFile.createNewFile();
@@ -264,6 +284,7 @@ private void testError(String errorName) throws IOException, CancelException, UR
264284
public void testErrors() throws Exception {
265285
testError("invalid-root");
266286
testError("invalid-xml");
287+
testError("empty");
267288
}
268289

269290
@Test

core.loaders/maven/src/test/resources/test-maven/error/test-empty.pom.xml

Whitespace-only changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>test</groupId>
6+
<artifactId>maven</artifactId>
7+
<version>1</version>
8+
<packaging>bundle</packaging>
9+
10+
<parent>
11+
<unknown/>
12+
</parent>
13+
14+
<dependencyManagement>
15+
<unknown/>
16+
<dependencies>
17+
<unknown/>
18+
<dependency>
19+
<unknown/>
20+
<exclusions>
21+
<unknown/>
22+
<exclusion>
23+
<unknown/>
24+
</exclusion>
25+
</exclusions>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
30+
<profiles>
31+
<unknown/>
32+
<profile>
33+
<unknown/>
34+
<dependencyManagement>
35+
<unknown/>
36+
</dependencyManagement>
37+
<activation>
38+
<unknown/>
39+
<os>
40+
<name>windows</name>
41+
<arch>x</arch>
42+
<version>0</version>
43+
<unknown/>
44+
</os>
45+
<property>
46+
<unknown/>
47+
</property>
48+
<file>
49+
<unknown/>
50+
</file>
51+
</activation>
52+
</profile>
53+
</profiles>
54+
55+
<repositories>
56+
<unknown/>
57+
<repository>
58+
<unknown/>
59+
<releases>
60+
<unknown/>
61+
<enabled>false</enabled>
62+
</releases>
63+
<snapshots>
64+
<unknown/>
65+
<enabled>false</enabled>
66+
</snapshots>
67+
</repository>
68+
</repositories>
69+
70+
</project>

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/AttributeAnnotationToRuleOnType.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,15 @@ public interface AttributeAnnotationToRuleOnType<TAnnotation extends Annotation>
2323
/** Search for annotations on the given type, and try to convert them into
2424
* serialization rules.
2525
*/
26-
@SuppressWarnings({ "unchecked", "rawtypes" })
26+
@SuppressWarnings({ "rawtypes" })
2727
static List<SerializationRule> addRules(SerializationClass type, boolean onGet, List<SerializationRule> rules) {
2828
List<SerializationRule> newRules = new LinkedList<>();
2929
for (Attribute attr : type.getAttributes()) {
3030
if (onGet && !attr.canGet()) continue;
3131
if (!onGet && !attr.canSet()) continue;
3232
for (Annotation a : ClassUtil.expandRepeatableAnnotations(attr.getAnnotations(onGet))) {
33-
for (AttributeAnnotationToRuleOnType toRule : getAnnotationToRules(a)) {
34-
SerializationRule rule;
35-
try { rule = toRule.createRule(a, attr); }
36-
catch (Exception t) {
37-
LCCore.getApplication().getDefaultLogger().error(
38-
"Error creating rule from annotation " + a.annotationType().getName()
39-
+ " on attribute " + attr.getOriginalName() + " of type "
40-
+ attr.getOriginalType().getBase().getName(), t);
41-
continue;
42-
}
43-
if (rule != null) {
44-
boolean found = false;
45-
for (SerializationRule r : rules)
46-
if (r.isEquivalent(rule)) {
47-
found = true;
48-
break;
49-
}
50-
if (!found)
51-
for (SerializationRule r : newRules)
52-
if (r.isEquivalent(rule)) {
53-
found = true;
54-
break;
55-
}
56-
if (!found)
57-
newRules.add(rule);
58-
}
59-
}
33+
for (AttributeAnnotationToRuleOnType toRule : getAnnotationToRules(a))
34+
addRuleFromAttribute(attr, a, toRule, rules, newRules);
6035
}
6136
}
6237
if (newRules.isEmpty())
@@ -67,6 +42,38 @@ static List<SerializationRule> addRules(SerializationClass type, boolean onGet,
6742
return newList;
6843
}
6944

45+
/** Create a rule from annotation and add it to the new rules. */
46+
@SuppressWarnings({ "unchecked", "rawtypes" })
47+
static void addRuleFromAttribute(
48+
Attribute attr, Annotation a, AttributeAnnotationToRuleOnType toRule, List<SerializationRule> rules, List<SerializationRule> newRules
49+
) {
50+
SerializationRule rule;
51+
try { rule = toRule.createRule(a, attr); }
52+
catch (Exception t) {
53+
LCCore.getApplication().getDefaultLogger().error(
54+
"Error creating rule from annotation " + a.annotationType().getName()
55+
+ " on attribute " + attr.getOriginalName() + " of type "
56+
+ attr.getOriginalType().getBase().getName(), t);
57+
return;
58+
}
59+
if (rule != null) {
60+
boolean found = false;
61+
for (SerializationRule r : rules)
62+
if (r.isEquivalent(rule)) {
63+
found = true;
64+
break;
65+
}
66+
if (!found)
67+
for (SerializationRule r : newRules)
68+
if (r.isEquivalent(rule)) {
69+
found = true;
70+
break;
71+
}
72+
if (!found)
73+
newRules.add(rule);
74+
}
75+
}
76+
7077
/** Search for implementations to convert the given annotation into a rule.
7178
* It looks first on the annotation class if there is an inner class implementing AttributeAnnotationToRuleOnAttribute.
7279
* If none is found, it looks into the registry.

0 commit comments

Comments
 (0)