Skip to content

Commit c47fe8d

Browse files
committed
chore: added unit test for BaseTreeNode
1 parent b82f24c commit c47fe8d

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package io.snyk.eclipse.plugin.views.snyktoolview;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.eclipse.jface.resource.ImageDescriptor;
10+
import org.eclipse.jface.viewers.TreeNode;
11+
12+
class BaseTreeNodeTest {
13+
private BaseTreeNode node;
14+
private ImageDescriptor mockImageDescriptor;
15+
16+
@BeforeEach
17+
void setUp() {
18+
node = new BaseTreeNode("root");
19+
mockImageDescriptor = mock(ImageDescriptor.class);
20+
}
21+
22+
@Test
23+
void testConstructorWithString() {
24+
assertEquals("root", node.getText());
25+
assertEquals("root", node.getValue());
26+
}
27+
28+
@Test
29+
void testConstructorWithNonString() {
30+
Integer value = 42;
31+
BaseTreeNode intNode = new BaseTreeNode(value);
32+
assertEquals("", intNode.getText());
33+
assertEquals(value, intNode.getValue());
34+
}
35+
36+
@Test
37+
void testSetValue() {
38+
String newValue = "newValue";
39+
node.setValue(newValue);
40+
assertEquals(newValue, node.getValue());
41+
}
42+
43+
@Test
44+
void testAddChildToEmptyNode() {
45+
BaseTreeNode child = new BaseTreeNode("child1");
46+
node.addChild(child);
47+
48+
TreeNode[] children = node.getChildren();
49+
assertNotNull(children);
50+
assertEquals(1, children.length);
51+
assertEquals(child, children[0]);
52+
}
53+
54+
@Test
55+
void testAddChildToNodeWithExistingChildren() {
56+
BaseTreeNode child1 = new BaseTreeNode("child1");
57+
BaseTreeNode child2 = new BaseTreeNode("child2");
58+
59+
node.addChild(child1);
60+
node.addChild(child2);
61+
62+
TreeNode[] children = node.getChildren();
63+
assertNotNull(children);
64+
assertEquals(2, children.length);
65+
assertEquals(child1, children[0]);
66+
assertEquals(child2, children[1]);
67+
}
68+
69+
@Test
70+
void testRemoveChildren() {
71+
BaseTreeNode child = new BaseTreeNode("child");
72+
node.addChild(child);
73+
74+
node.removeChildren();
75+
76+
TreeNode[] children = node.getChildren();
77+
assertNotNull(children);
78+
assertEquals(0, children.length);
79+
}
80+
81+
@Test
82+
void testSetAndGetImageDescriptor() {
83+
node.setImageDescriptor(mockImageDescriptor);
84+
assertEquals(mockImageDescriptor, node.getImageDescriptor());
85+
}
86+
87+
@Test
88+
void testSetAndGetText() {
89+
String newText = "newText";
90+
node.setText(newText);
91+
assertEquals(newText, node.getText());
92+
}
93+
94+
@Test
95+
void testToString() {
96+
assertEquals("root", node.toString());
97+
98+
Integer value = 42;
99+
BaseTreeNode intNode = new BaseTreeNode(value);
100+
assertEquals("42", intNode.toString());
101+
}
102+
103+
@Test
104+
void testReset() {
105+
// Setup initial state
106+
node.setText("someText");
107+
node.setImageDescriptor(mockImageDescriptor);
108+
node.addChild(new BaseTreeNode("child"));
109+
110+
// Reset the node
111+
node.reset();
112+
113+
// Verify all fields are reset
114+
assertNull(node.getValue());
115+
assertEquals("", node.getText());
116+
assertNull(node.getImageDescriptor());
117+
assertEquals(0, node.getChildren().length);
118+
}
119+
120+
@Test
121+
void testAddMultipleChildren() {
122+
BaseTreeNode[] children = new BaseTreeNode[] {
123+
new BaseTreeNode("child1"),
124+
new BaseTreeNode("child2"),
125+
new BaseTreeNode("child3")
126+
};
127+
128+
Arrays.stream(children).forEach(node::addChild);
129+
130+
TreeNode[] resultChildren = node.getChildren();
131+
assertNotNull(resultChildren);
132+
assertEquals(children.length, resultChildren.length);
133+
134+
for (int i = 0; i < children.length; i++) {
135+
assertEquals(children[i], resultChildren[i]);
136+
}
137+
}
138+
139+
@Test
140+
void testAddChildAfterRemovingChildren() {
141+
BaseTreeNode child1 = new BaseTreeNode("child1");
142+
node.addChild(child1);
143+
node.removeChildren();
144+
145+
BaseTreeNode child2 = new BaseTreeNode("child2");
146+
node.addChild(child2);
147+
148+
TreeNode[] children = node.getChildren();
149+
assertNotNull(children);
150+
assertEquals(1, children.length);
151+
assertEquals(child2, children[0]);
152+
}
153+
}

0 commit comments

Comments
 (0)