Skip to content

Commit 30c1fc2

Browse files
committed
More unit tests.
1 parent 41bc68d commit 30c1fc2

10 files changed

Lines changed: 123 additions & 165 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
Norconex Commons Lang
22
=======
33

4-
Generic library shared between several projects.
4+
Generic library complementing the Java APi with useful utilities.
55

66
https://opensource.norconex.com/commons/lang/
77

8-
Supports Java 8+. Custom taglets depend on JDK `tools.jar` being
9-
in system scope to compile.
8+
Supports Java 11+.

TODO.txt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ TODO:
2222

2323
- XSD: Always have a type and an element extending it to improve reuse.
2424

25-
- Replace/refactor process execution to use new process features of Java 8+.
26-
2725
- Modify Jar copier to handle cases where snapshot are timestamped instead and
2826
not being considered the latest when they should. Like:
2927
norconex-commons-lang-1.13.0-20170328.184247-17.jar vs
3028
norconex-commons-lang-1.13.0-SNAPSHOT.jar
3129
https://github.com/Norconex/collector-http/issues/331#issuecomment-290196986
3230

33-
- Is Pipeline really needed since Java 8. Could functional interfaces do it?
34-
3531
- Deploy javadoc for each version but have latest also accessible as "current"
3632

3733
- Check why URLNormalizer.removeFragment is so expensive compared to
@@ -45,9 +41,6 @@ LOWER:
4541

4642
- Have loadFromXML return validationError list.
4743

48-
- Do download page so more than one snapshot can be shown for different
49-
major versions.
50-
5144
- DONE? Add feature and tests for when parent element is specified but has no
5245
value, which means blanking a list
5346
(e.g. HTTP Collector: StandardSitemapResolverTest).
@@ -61,8 +54,8 @@ LOWER:
6154

6255
- Remove all classes that are already part of Apache Commons.
6356

64-
- Consider using Apache Commons Exec to replace SystemCommand if Java 8 features
65-
are not enough.
57+
- Consider using Apache Commons Exec to replace SystemCommand if latest Java
58+
features are not enough.
6659

6760
- Allows to specify a velocity-tools.properties or equivalent in classpath
6861
root (e.g. classes/ folder) to define custom classes/tools to be used
@@ -72,7 +65,7 @@ LOWER:
7265
MAYBE/IDEAS:
7366
--------------
7467

75-
- Make deployment part of CID tool.
68+
- Make deployment part of CICD tool.
7669

7770
- MAYBE modify assembly to use licenses.xml instead of THIRD-PARTY?
7871
(the second is easier to read for non-techies)

src/main/java/com/norconex/commons/lang/xml/flow/XMLCondition.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2021 Norconex Inc.
1+
/* Copyright 2021-2022 Norconex Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -20,24 +20,25 @@
2020
import java.util.function.Predicate;
2121

2222
import org.apache.commons.lang3.StringUtils;
23-
import org.apache.commons.lang3.builder.EqualsBuilder;
2423
import org.apache.commons.lang3.builder.EqualsExclude;
25-
import org.apache.commons.lang3.builder.HashCodeBuilder;
2624
import org.apache.commons.lang3.builder.HashCodeExclude;
27-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
2825
import org.apache.commons.lang3.builder.ToStringExclude;
29-
import org.apache.commons.lang3.builder.ToStringStyle;
3026

3127
import com.norconex.commons.lang.function.Predicates;
3228
import com.norconex.commons.lang.xml.IXMLConfigurable;
3329
import com.norconex.commons.lang.xml.XML;
3430

31+
import lombok.EqualsAndHashCode;
32+
import lombok.ToString;
33+
3534
/**
3635
* Represents a single "condition" tag or a "conditions" tag with nested
3736
* "condition" and/or "conditions".
3837
* @author Pascal Essiembre
3938
* @since 2.0.0
4039
*/
40+
@ToString
41+
@EqualsAndHashCode
4142
class XMLCondition<T> implements IXMLConfigurable, Predicate<T> {
4243

4344
enum Operator {
@@ -135,9 +136,7 @@ private Predicate<T> loadConditionFromXML(XML xml) {
135136
Predicate<T> parseConditionGroup(XML xml) {
136137
List<Predicate<T>> predicateList = new ArrayList<>();
137138
Operator operator = Operator.of(xml.getString("operator"));
138-
xml.forEach("*", x -> {
139-
predicateList.add(loadConditionFromXML(x));
140-
});
139+
xml.forEach("*", x -> predicateList.add(loadConditionFromXML(x)));
141140
if (predicateList.size() == 1) {
142141
return predicateList.get(0);
143142
}
@@ -149,12 +148,14 @@ Predicate<T> parseSingleCondition(XML predicateXML) {
149148
if (flow.getPredicateAdapter() != null) {
150149
// If a predicate adapter is set, use it to parse the XML
151150
try {
152-
//TODO throw XMLValidationException if there are any errors?
151+
//MAYBE: throw XMLValidationException if there are any errors?
153152
IXMLFlowPredicateAdapter<T> adapter =
154-
flow.getPredicateAdapter().newInstance();
153+
flow.getPredicateAdapter()
154+
.getDeclaredConstructor()
155+
.newInstance();
155156
adapter.loadFromXML(predicateXML);
156157
p = adapter;
157-
} catch (InstantiationException | IllegalAccessException e) {
158+
} catch (Exception e) {
158159
throw new XMLFlowException("Predicate adapter "
159160
+ flow.getPredicateAdapter().getName() + " could not "
160161
+ "resolve this XML: " + predicateXML, e);
@@ -173,18 +174,4 @@ Predicate<T> parseSingleCondition(XML predicateXML) {
173174
}
174175
return p;
175176
}
176-
177-
@Override
178-
public boolean equals(final Object other) {
179-
return EqualsBuilder.reflectionEquals(this, other);
180-
}
181-
@Override
182-
public int hashCode() {
183-
return HashCodeBuilder.reflectionHashCode(this);
184-
}
185-
@Override
186-
public String toString() {
187-
return new ReflectionToStringBuilder(
188-
this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
189-
}
190177
}

src/main/java/com/norconex/commons/lang/xml/flow/XMLFlow.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2021 Norconex Inc.
1+
/* Copyright 2021-2022 Norconex Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -19,15 +19,13 @@
1919
import java.util.function.Consumer;
2020
import java.util.function.Predicate;
2121

22-
import org.apache.commons.lang3.builder.EqualsBuilder;
23-
import org.apache.commons.lang3.builder.HashCodeBuilder;
24-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
25-
import org.apache.commons.lang3.builder.ToStringStyle;
26-
2722
import com.norconex.commons.lang.function.Consumers;
2823
import com.norconex.commons.lang.function.FunctionUtil;
2924
import com.norconex.commons.lang.xml.XML;
3025

26+
import lombok.EqualsAndHashCode;
27+
import lombok.ToString;
28+
3129
/**
3230
* <p>
3331
* Treats an XML block as being a mix of predicate and consumer classes
@@ -77,6 +75,8 @@
7775
* @param <T> type of the object to be submitted to the flow.
7876
* @since 2.0.0
7977
*/
78+
@ToString
79+
@EqualsAndHashCode
8080
public final class XMLFlow<T> {
8181

8282
public static final String DEFAULT_CONSUMERS_WRITE_TAG_NAME = "consumer";
@@ -140,11 +140,12 @@ private Consumer<T> parseConsumer(XML consumerXML) {
140140
if (consumerAdapter != null) {
141141
// If a consumer adapter is set, use it to parse the XML
142142
try {
143-
//TODO throw XMLValidationException if there are any errors?
144-
IXMLFlowConsumerAdapter<T> c = consumerAdapter.newInstance();
143+
//MAYBE: throw XMLValidationException if there are any errors?
144+
IXMLFlowConsumerAdapter<T> c =
145+
consumerAdapter.getDeclaredConstructor().newInstance();
145146
c.loadFromXML(consumerXML);
146147
consumer = c;
147-
} catch (InstantiationException | IllegalAccessException e) {
148+
} catch (Exception e) {
148149
throw new XMLFlowException("Consumer adapter "
149150
+ consumerAdapter.getName() + " could not resolve "
150151
+ "this XML: " + consumerXML, e);
@@ -191,18 +192,4 @@ private void writeSingleConsumer(XML xml, Consumer<T> consumer) {
191192
xml.addElement(DEFAULT_CONSUMERS_WRITE_TAG_NAME, consumer);
192193
}
193194
}
194-
195-
@Override
196-
public boolean equals(final Object other) {
197-
return EqualsBuilder.reflectionEquals(this, other);
198-
}
199-
@Override
200-
public int hashCode() {
201-
return HashCodeBuilder.reflectionHashCode(this);
202-
}
203-
@Override
204-
public String toString() {
205-
return new ReflectionToStringBuilder(
206-
this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
207-
}
208195
}
Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2018 Norconex Inc.
1+
/* Copyright 2018-2022 Norconex Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -14,44 +14,13 @@
1414
*/
1515
package com.norconex.commons.lang.xml.flow;
1616

17+
import lombok.experimental.StandardException;
18+
1719
/**
1820
* Runtime exception for XML-related issues.
1921
* @author Pascal Essiembre
2022
*/
23+
@StandardException
2124
public class XMLFlowException extends RuntimeException {
22-
2325
private static final long serialVersionUID = 8484839654375152232L;
24-
25-
/**
26-
* Constructor.
27-
*/
28-
public XMLFlowException() {
29-
super();
30-
}
31-
32-
/**
33-
* Constructor.
34-
* @param message exception message
35-
*/
36-
public XMLFlowException(String message) {
37-
super(message);
38-
}
39-
40-
/**
41-
* Constructor.
42-
* @param cause exception root cause
43-
*/
44-
public XMLFlowException(Throwable cause) {
45-
super(cause);
46-
}
47-
48-
/**
49-
* Constructor.
50-
* @param message exception message
51-
* @param cause exception root cause
52-
*/
53-
public XMLFlowException(String message, Throwable cause) {
54-
super(message, cause);
55-
}
56-
5726
}

src/main/java/com/norconex/commons/lang/xml/flow/XMLIf.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2021 Norconex Inc.
1+
/* Copyright 2021-2022 Norconex Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -19,17 +19,16 @@
1919
import java.util.function.Predicate;
2020

2121
import org.apache.commons.lang3.StringUtils;
22-
import org.apache.commons.lang3.builder.EqualsBuilder;
2322
import org.apache.commons.lang3.builder.EqualsExclude;
24-
import org.apache.commons.lang3.builder.HashCodeBuilder;
2523
import org.apache.commons.lang3.builder.HashCodeExclude;
26-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
2724
import org.apache.commons.lang3.builder.ToStringExclude;
28-
import org.apache.commons.lang3.builder.ToStringStyle;
2925

3026
import com.norconex.commons.lang.xml.IXMLConfigurable;
3127
import com.norconex.commons.lang.xml.XML;
3228

29+
import lombok.EqualsAndHashCode;
30+
import lombok.ToString;
31+
3332
/**
3433
* <p>
3534
* Treats an XML block as being an "if" statement, using a mix of predicate
@@ -86,6 +85,8 @@
8685
* @since 2.0.0
8786
* @see XMLIfNot
8887
*/
88+
@ToString
89+
@EqualsAndHashCode
8990
class XMLIf<T> implements Consumer<T>, IXMLConfigurable {
9091

9192
@ToStringExclude @EqualsExclude @HashCodeExclude
@@ -114,10 +115,8 @@ public void accept(T t) {
114115
if (thenConsumer != null) {
115116
thenConsumer.accept(t);
116117
}
117-
} else {
118-
if (elseConsumer != null) {
119-
elseConsumer.accept(t);
120-
}
118+
} else if (elseConsumer != null) {
119+
elseConsumer.accept(t);
121120
}
122121
}
123122

@@ -143,7 +142,7 @@ public void loadFromXML(XML xml) {
143142

144143
// There must be exactly one top-level child named "then"
145144
// and one optional "else".
146-
// TODO enforce in schema that there must be one "then".
145+
//MAYBE enforce in schema that there must be one "then".
147146
this.thenConsumer = flow.parse(xml.getXML(Tag.THEN.toString()));
148147
this.elseConsumer = flow.parse(xml.getXML(Tag.ELSE.toString()));
149148
}
@@ -159,18 +158,4 @@ public void saveToXML(XML xml) {
159158
flow.write(xml.addElement("else"), elseConsumer);
160159
}
161160
}
162-
163-
@Override
164-
public boolean equals(final Object other) {
165-
return EqualsBuilder.reflectionEquals(this, other);
166-
}
167-
@Override
168-
public int hashCode() {
169-
return HashCodeBuilder.reflectionHashCode(this);
170-
}
171-
@Override
172-
public String toString() {
173-
return new ReflectionToStringBuilder(
174-
this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
175-
}
176161
}
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2021 Norconex Inc.
1+
/* Copyright 2021-2022 Norconex Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -14,10 +14,8 @@
1414
*/
1515
package com.norconex.commons.lang.xml.flow;
1616

17-
import org.apache.commons.lang3.builder.EqualsBuilder;
18-
import org.apache.commons.lang3.builder.HashCodeBuilder;
19-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
20-
import org.apache.commons.lang3.builder.ToStringStyle;
17+
import lombok.EqualsAndHashCode;
18+
import lombok.ToString;
2119

2220
/**
2321
* <p>
@@ -27,6 +25,8 @@
2725
* @since 2.0.0
2826
* @see XMLIfNot
2927
*/
28+
@ToString
29+
@EqualsAndHashCode
3030
class XMLIfNot<T> extends XMLIf<T> {
3131

3232
XMLIfNot(XMLFlow<T> flow) {
@@ -37,18 +37,4 @@ class XMLIfNot<T> extends XMLIf<T> {
3737
protected boolean conditionPasses(T t) {
3838
return !super.conditionPasses(t);
3939
}
40-
41-
@Override
42-
public boolean equals(final Object other) {
43-
return EqualsBuilder.reflectionEquals(this, other);
44-
}
45-
@Override
46-
public int hashCode() {
47-
return HashCodeBuilder.reflectionHashCode(this);
48-
}
49-
@Override
50-
public String toString() {
51-
return new ReflectionToStringBuilder(
52-
this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
53-
}
5440
}

0 commit comments

Comments
 (0)