Skip to content

Commit

Permalink
#98 PMD/checkstyle cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 13, 2020
1 parent ac46bf5 commit ca97b11
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/AddDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String toString() {
@Override
public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack) {
final Collection<Node> targets = new ArrayList<Node>(cursor.size());
final Collection<Node> targets = new ArrayList<>(cursor.size());
final String label = this.name.raw();
final Document doc;
if (dom.getOwnerDocument() == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/AddIfDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String toString() {
@Override
public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack) {
final Collection<Node> targets = new ArrayList<Node>(cursor.size());
final Collection<Node> targets = new ArrayList<>(cursor.size());
final String label = this.name.raw();
for (final Node node : cursor) {
final NodeList kids = node.getChildNodes();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/xembly/Directive.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface Stack {
*/
void push(Directive.Cursor cursor)
throws ImpossibleModificationException;

/**
* Pop cursor (runtime exception if stack is empty).
* @return Cursor recently added
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/xembly/Directives.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
/**
* Collection of {@link Directive}s, instantiable from {@link String}.
*
* <p>For example, to fetch directives from a string and apply to the
Expand Down Expand Up @@ -178,7 +178,11 @@ public Iterator<Directive> iterator() {
* @since 0.13
* @checkstyle CyclomaticComplexity (50 lines)
*/
@SuppressWarnings("PMD.StdCyclomaticComplexity")
@SuppressWarnings(
{
"PMD.StdCyclomaticComplexity", "PMD.InefficientEmptyStringCheck"
}
)
public static Iterable<Directive> copyOf(final Node node) {
final Directives dirs = new Directives();
if (node.hasAttributes()) {
Expand Down Expand Up @@ -293,9 +297,9 @@ public Directives add(final Object name) {
* exception will be thrown. To avoid this, it is recommended to use
* {@link Xembler#escape(String)}.
*
* @param nodes Names and values of nodes to add
* @param <K> Type of key
* @param <V> Type of value
* @param nodes Names and values of nodes to add
* @return This object
* @since 0.8
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/DomCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import lombok.EqualsAndHashCode;
import org.w3c.dom.Node;

/*
/**
* Cursor at DOM.
*
* <p>The class is immutable and thread-safe.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/DomStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class DomStack implements Directive.Stack {
* Queue of cursors.
*/
private final transient Deque<Directive.Cursor> cursors =
new LinkedList<Directive.Cursor>();
new LinkedList<>();

@Override
public void push(final Directive.Cursor cursor) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/xembly/NsDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*
* <p>The class is immutable and thread-safe.
*
* @author Dmitri Pisarenko (dp@altruix.co)
* @since 0.19.3
*/
@EqualsAndHashCode(of = { "namespace" })
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/RemoveDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String toString() {
@Override
public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack) {
final Collection<Node> parents = new HashSet<Node>(cursor.size());
final Collection<Node> parents = new HashSet<>(cursor.size());
for (final Node node : cursor) {
final Node parent;
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/UpDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String toString() {
public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack)
throws ImpossibleModificationException {
final Collection<Node> parents = new HashSet<Node>(cursor.size());
final Collection<Node> parents = new HashSet<>(cursor.size());
for (final Node node : cursor) {
final Node parent = node.getParentNode();
if (parent == null) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/xembly/Xembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public String xml() throws ImpossibleModificationException {
* @checkstyle CyclomaticComplexity (20 lines)
* @checkstyle BooleanExpressionComplexity (20 lines)
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static String escape(final String text) {
final StringBuilder output = new StringBuilder(text.length());
final char[] chars = text.toCharArray();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/xembly/XsetDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Directive.Cursor exec(final Node dom,
throws ImpossibleModificationException {
final XPath xpath = XsetDirective.FACTORY.newXPath();
final ConcurrentMap<Node, String> values =
new ConcurrentHashMap<Node, String>(0);
new ConcurrentHashMap<>(0);
for (final Node node : cursor) {
try {
values.put(
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/xembly/AddDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

/**
* Test case for {@link AddDirective}.
*
* @since 0.1
*/
public final class AddDirectiveTest {

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/xembly/AddIfDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

/**
* Test case for {@link AddIfDirective}.
*
* @since 0.1
*/
public final class AddIfDirectiveTest {

Expand Down
14 changes: 9 additions & 5 deletions src/test/java/org/xembly/ArgTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

/**
* Test case for {@link Arg}.
*
* @since 0.1
*/
public final class ArgTest {

Expand Down Expand Up @@ -65,18 +67,20 @@ public void escapesAndUnescaped() throws Exception {
*/
@Test
public void rejectsToEscapeInvalidXmlChars() {
Assertions.assertThrows(XmlContentException.class, () -> {
new Arg("\u001b\u0000").toString();
});
Assertions.assertThrows(
XmlContentException.class,
() -> new Arg("\u001b\u0000").toString()
);
}

/**
* Arg can reject to unescape invalid text.
*/
@Test
public void rejectsToUnEscapeInvalidXmlChars() {
Assertions.assertThrows(XmlContentException.class, () ->
Arg.unescape("&#27;&#0000;")
Assertions.assertThrows(
XmlContentException.class,
() -> Arg.unescape("&#27;&#0000;")
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/xembly/AttrDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

/**
* Test case for {@link AddDirective}.
*
* @since 0.1
*/
public final class AttrDirectiveTest {

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/xembly/CommentDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

/**
* Test case for {@link CommentDirective}.
*
* @since 0.1
*/
public final class CommentDirectiveTest {

Expand Down
29 changes: 16 additions & 13 deletions src/test/java/org/xembly/DirectivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

/**
* Test case for {@link Directives}.
*
* @since 0.1
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
public final class DirectivesTest {
Expand Down Expand Up @@ -83,40 +85,40 @@ public void parsesIncomingGrammar() throws Exception {
final Iterable<Directive> dirs = new Directives(
"XPATH '//orders[@id=\"152\"]'; SET 'test';"
);
MatcherAssert.assertThat(
dirs,
Matchers.<Directive>iterableWithSize(2)
);
MatcherAssert.assertThat(dirs, Matchers.iterableWithSize(2));
}

/**
* Directives can throw when grammar is broken.
*/
@Test
public void throwsOnBrokenGrammar() {
Assertions.assertThrows(SyntaxException.class, () -> {
new Directives("not a xembly at all");
});
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("not a xembly at all")
);
}

/**
* Directives can throw when XML content is broken.
*/
@Test
public void throwsOnBrokenXmlContent() {
Assertions.assertThrows(SyntaxException.class, () -> {
new Directives("ADD '\u001b';");
});
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("ADD '\u001b';")
);
}

/**
* Directives can throw when escaped XML content is broken.
*/
@Test
public void throwsOnBrokenEscapedXmlContent() {
Assertions.assertThrows(SyntaxException.class, () -> {
new Directives("ADD '&#27;';");
});
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("ADD '&#27;';")
);
}

/**
Expand Down Expand Up @@ -203,6 +205,7 @@ public void copiesExistingNode() throws Exception {
)
).node()
);
// @checkstyle MagicNumber (1 line)
MatcherAssert.assertThat(copy, Matchers.iterableWithSize(19));
new Xembler(new Directives().add("dudes").append(copy)).apply(dom);
MatcherAssert.assertThat(
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/xembly/DomStackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

/**
* Test case for {@link DomStack}.
*
* @since 0.21
*/
public final class DomStackTest {
Expand Down Expand Up @@ -68,8 +69,9 @@ public void addsAndRetrieves() throws Exception {
*/
@Test
public void throwsExceptionOnEmpty() {
Assertions.assertThrows(ImpossibleModificationException.class, () -> {
new DomStack().pop();
});
Assertions.assertThrows(
ImpossibleModificationException.class,
() -> new DomStack().pop()
);
}
}
9 changes: 4 additions & 5 deletions src/test/java/org/xembly/NsDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Validates NsDirective class.
* @author Dmitri Pisarenko (dp@altruix.co)
* @since 0.19.3
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
Expand All @@ -56,13 +54,14 @@ public void setsNsAttr() throws Exception {
.newDocumentBuilder().newDocument();
final Element root = dom.createElement("f");
dom.appendChild(root);
new NsDirective(new Arg("somens")).exec(
dom, new DomCursor(Collections.<Node>singletonList(root)),
final String name = "somens";
new NsDirective(new Arg(name)).exec(
dom, new DomCursor(Collections.singletonList(root)),
new DomStack()
);
MatcherAssert.assertThat(
new XMLDocument(dom).toString(),
XhtmlMatchers.hasXPath("/ns1:f", "somens")
XhtmlMatchers.hasXPath("/ns1:f", name)
);
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/xembly/RemoveDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

/**
* Test case for {@link RemoveDirective}.
*
* @since 0.1
*/
public final class RemoveDirectiveTest {

Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/xembly/SetDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

/**
* Test case for {@link SetDirective}.
*
* @since 0.1
*/
public final class SetDirectiveTest {

Expand All @@ -55,7 +57,8 @@ public void setsTextContentOfNodes() throws Exception {
StringUtils.join(
"ADD 'root'; ADD 'foo';",
"SET '&quot;Bonnie &amp; Clyde&quot;';",
"UP; ADD 'cops'; SET 'everywhere';")
"UP; ADD 'cops'; SET 'everywhere';"
)
);
final Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Expand All @@ -74,9 +77,10 @@ public void setsTextContentOfNodes() throws Exception {
*/
@Test
public void rejectsContentWithInvalidXmlCharacters() {
Assertions.assertThrows(SyntaxException.class, () -> {
new Directives("ADD 'alpha'; SET 'illegal: &#27;&#00;&#03;';");
});
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("ADD 'alpha'; SET 'illegal: &#27;&#00;&#03;';")
);
}

/**
Expand Down

0 comments on commit ca97b11

Please sign in to comment.