1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.xpath.iterators;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.api.DetailAST;
31 import com.puppycrawl.tools.checkstyle.xpath.AbstractNode;
32 import net.sf.saxon.om.NamespaceUri;
33 import net.sf.saxon.om.NodeInfo;
34 import net.sf.saxon.om.TreeInfo;
35
36 public class ReverseListIteratorTest {
37
38 @Test
39 public void testCorrectOrder() {
40 final List<AbstractNode> nodes = Arrays.asList(new TestNode(), new TestNode(),
41 new TestNode());
42
43 try (ReverseListIterator iterator = new ReverseListIterator(nodes)) {
44 for (int i = nodes.size() - 1; i >= 0; i--) {
45 assertWithMessage("Invalid node")
46 .that(iterator.next())
47 .isEqualTo(nodes.get(i));
48 }
49 assertWithMessage("Node should be null")
50 .that(iterator.next())
51 .isNull();
52 }
53 }
54
55 @Test
56 public void testNullList() {
57 try (ReverseListIterator iterator = new ReverseListIterator(null)) {
58 assertWithMessage("Node should be null")
59 .that(iterator.next())
60 .isNull();
61 }
62 }
63
64 private static class TestNode extends AbstractNode {
65
66 private TestNode() {
67 super(null);
68 }
69
70 protected TestNode(TreeInfo treeInfo) {
71 super(treeInfo);
72 }
73
74 @Override
75 public int getTokenType() {
76 return 0;
77 }
78
79 @Override
80 public DetailAST getUnderlyingNode() {
81 return null;
82 }
83
84 @Override
85 public int getDepth() {
86 return 0;
87 }
88
89 @Override
90 protected List<AbstractNode> createChildren() {
91 return new ArrayList<>();
92 }
93
94 @Override
95 public int getNodeKind() {
96 return 0;
97 }
98
99 @Override
100 public int compareOrder(NodeInfo other) {
101 return 0;
102 }
103
104 @Override
105 public String getLocalPart() {
106 return null;
107 }
108
109 @Override
110 public NodeInfo getParent() {
111 return null;
112 }
113
114 @Override
115 public String getAttributeValue(NamespaceUri uri, String local) {
116 return null;
117 }
118
119 @Override
120 public NodeInfo getRoot() {
121 return null;
122 }
123
124 @Override
125 public boolean hasChildNodes() {
126 return false;
127 }
128 }
129 }