001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.DummyTreeCellRenderer;
006: import org.uispec4j.utils.Functor;
007:
008: import javax.swing.tree.DefaultMutableTreeNode;
009: import java.awt.Color;
010:
011: public class TreeContentTest extends TreeTestCase {
012: public void testContentCheck() throws Exception {
013: assertTrue(tree.contentEquals("root\n" + " child1\n"
014: + " child1_1\n" + " child2"));
015: }
016:
017: public void testContentCheckWithEmptyExpectedStringError() {
018: checkContainmentError(" ",
019: "Expected tree description should not be empty");
020: }
021:
022: public void testContentCheckWithErrors() throws Exception {
023: checkContainmentError("root\n" + " child1\n" + " error\n"
024: + " child2");
025: }
026:
027: public void testExpectedContentStringIsTrimmedInContainmentCheck()
028: throws Exception {
029: assertTrue(tree.contentEquals(" root\n" + " child1\n"
030: + " child1_1\n" + " child2\n"));
031: assertTrue(tree.contentEquals(" root\n" + " child1\n"
032: + " child1_1\n" + " child2\n \t "));
033: }
034:
035: public void testContentCheckWithPropertiesSpecification()
036: throws Exception {
037: child1_1.setBold(true);
038: child1_1.setColor(Color.RED);
039: child2.setColor(Color.BLUE);
040: assertTrue(tree.contentEquals("root\n" + " child1\n"
041: + " child1_1 #(bold,color=red)\n"
042: + " child2 #(color=blue)"));
043: }
044:
045: public void testContentCheckWithMissingBoldnessError()
046: throws Exception {
047: child1.setBold(true);
048: checkContainmentError("root\n" + " child1\n"
049: + " child1_1\n" + " child2");
050: }
051:
052: public void testContentCheckWithBoldnessError() throws Exception {
053: child1.setBold(false);
054: checkContainmentError("root\n" + " child1 #(bold)\n"
055: + " child1_1\n" + " child2");
056: }
057:
058: public void testContentCheckAcceptsBothNumericAndHumanReadableValues()
059: throws Exception {
060: child2.setColor(Color.BLUE);
061: tree.contentEquals("root\n" + " child1\n" + " child1_1\n"
062: + " child2 #(color=blue)");
063: assertTrue(tree.contentEquals("root\n" + " child1\n"
064: + " child1_1\n" + " child2 #(color=0000ff)"));
065: }
066:
067: public void testContentCheckWithMissingColorError()
068: throws Exception {
069: child1.setColor(Color.BLUE);
070: checkContainmentError("root\n" + " child1\n"
071: + " child1_1\n" + " child2");
072: }
073:
074: public void testCheckContentsWithColorNameError() throws Exception {
075: child1_1.setColor(Color.BLUE);
076: checkAssertionFailedError(new Functor() {
077: public void run() throws Exception {
078: assertTrue(tree.contentEquals("root\n" + " child1\n"
079: + " child1_1 #(color=ERROR)\n" + " child2"));
080: }
081: }, "'ERROR' does not seem to be a color");
082: }
083:
084: public void testAssertContains() throws Exception {
085: assertTrue(tree.contains("child1/child1_1"));
086: try {
087: assertTrue(tree.contains("child1/unknown"));
088: throw new AssertionFailureNotDetectedError();
089: } catch (AssertionFailedError e) {
090: assertEquals("Could not find element 'child1/unknown'", e
091: .getMessage());
092: }
093: }
094:
095: public void testAssertContainsReallyChecksTheWholePath()
096: throws Exception {
097: child1Node.add(new DefaultMutableTreeNode(
098: new DummyTreeCellRenderer.UserObject("child1_2")));
099: assertTrue(tree.contains("child1/child1_2"));
100: }
101:
102: public void testSeparatorCustomisation() throws Exception {
103: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode(
104: new DummyTreeCellRenderer.UserObject("child/3"));
105: rootNode.add(child3);
106: child3.add(new DefaultMutableTreeNode(
107: new DummyTreeCellRenderer.UserObject("child/3 3")));
108: tree.setSeparator(" | ");
109: checkPath("child/3 | child/3 3");
110: tree.setSeparator(" ## ");
111: checkPath("child/3 ## child/3 3");
112: tree.setSeparator("-");
113: checkPath("child/3-child/3 3");
114: }
115:
116: private void checkPath(String path) {
117: tree.contains(path);
118: tree.select(path);
119: assertTrue(tree.selectionEquals(path));
120: tree.clearSelection();
121: tree.click(path);
122: assertTrue(tree.selectionEquals(path));
123: }
124:
125: public void testSeparatorCanBeSpecifiedAtTreeCreationTime()
126: throws Exception {
127: String previousSeparator = Tree.defaultSeparator;
128: System.getProperties().remove(Tree.SEPARATOR_PROPERTY);
129: Tree.setDefaultSeparator("-*-");
130:
131: Tree tree = new Tree(jTree);
132: assertTrue(tree.contains("child1-*-child1_1"));
133: System.setProperty(Tree.SEPARATOR_PROPERTY, "#");
134: assertTrue(tree.contains("child1-*-child1_1"));
135: tree = new Tree(jTree);
136: assertTrue(tree.contains("child1#child1_1"));
137:
138: System.getProperties().remove(Tree.SEPARATOR_PROPERTY);
139: Tree.setDefaultSeparator(previousSeparator);
140: }
141:
142: public void testSeparatorCannotBeEmpty() throws Exception {
143: try {
144: tree.setSeparator("");
145: throw new AssertionFailureNotDetectedError();
146: } catch (IllegalArgumentException e) {
147: assertEquals("Separator must not be empty", e.getMessage());
148: }
149: try {
150: Tree.setDefaultSeparator("");
151: throw new AssertionFailureNotDetectedError();
152: } catch (IllegalArgumentException e) {
153: assertEquals("Separator must not be empty", e.getMessage());
154: }
155:
156: System.setProperty(Tree.SEPARATOR_PROPERTY, "");
157: Tree tree = new Tree(jTree);
158: assertEquals("/", tree.getSeparator());
159: System.getProperties().remove(Tree.SEPARATOR_PROPERTY);
160: }
161:
162: public void testSeparatorCannotBeNull() throws Exception {
163: try {
164: tree.setSeparator(null);
165: throw new AssertionFailureNotDetectedError();
166: } catch (IllegalArgumentException e) {
167: assertEquals("Separator must not be null", e.getMessage());
168: }
169: try {
170: Tree.setDefaultSeparator(null);
171: throw new AssertionFailureNotDetectedError();
172: } catch (IllegalArgumentException e) {
173: assertEquals("Separator must not be null", e.getMessage());
174: }
175: }
176:
177: public void testPathDefinitionsGivePriorityToExactNames()
178: throws Exception {
179: rootNode.add(new DefaultMutableTreeNode(
180: new DummyTreeCellRenderer.UserObject("child1bis")));
181: checkPath("child1/child1_1");
182: }
183:
184: public void testUsingASpecificConverter() throws Exception {
185: tree.setCellValueConverter(new DummyTreeCellValueConverter());
186: assertTrue(tree.contentEquals("_obj:root_\n"
187: + " _obj:child1_\n" + " _obj:child1_1_\n"
188: + " _obj:child2_"));
189: assertTrue(tree.contains("_obj:child1_/_obj:child1_1_"));
190: }
191:
192: public void testUsingASpecificConverterForColor() throws Exception {
193: DummyTreeCellValueConverter converter = new DummyTreeCellValueConverter();
194: converter.setRedFontPattern("child1");
195: tree.setCellValueConverter(converter);
196: assertTrue(tree.contentEquals("_obj:root_\n"
197: + " _obj:child1_ #(color=FF0000)\n"
198: + " _obj:child1_1_ #(color=FF0000)\n"
199: + " _obj:child2_"));
200: }
201:
202: public void testUsingASpecificConverterForTextStyle()
203: throws Exception {
204: DummyTreeCellValueConverter converter = new DummyTreeCellValueConverter();
205: converter.setBoldPattern("child");
206: tree.setCellValueConverter(converter);
207: assertTrue(tree.contentEquals("_obj:root_\n"
208: + " _obj:child1_ #(bold)\n"
209: + " _obj:child1_1_ #(bold)\n"
210: + " _obj:child2_ #(bold)"));
211: }
212:
213: public void testGetChildCount() throws Exception {
214: assertEquals(2, tree.getChildCount(""));
215: assertEquals(1, tree.getChildCount("child1"));
216: }
217:
218: public void testCheckForegroundColor() throws Exception {
219: assertTrue(tree.foregroundEquals("", "black"));
220: child1.setColor(new Color(250, 10, 10));
221: assertTrue(tree.foregroundEquals("child1", "red"));
222: try {
223: assertTrue(tree.foregroundEquals("child1", "green"));
224: throw new AssertionFailureNotDetectedError();
225: } catch (AssertionFailedError e) {
226: assertEquals("expected:<GREEN> but was:<FA0A0A>", e
227: .getMessage());
228: }
229: }
230:
231: public void testToString() throws Exception {
232: assertEquals("root\n" + " child1\n" + " child1_1\n"
233: + " child2\n", tree.toString());
234: }
235:
236: private void checkContainmentError(String expectedTree) {
237: checkContainmentError(expectedTree, null);
238: }
239:
240: private void checkContainmentError(String expectedTree,
241: String message) {
242: try {
243: assertTrue(tree.contentEquals(expectedTree));
244: throw new AssertionFailureNotDetectedError();
245: } catch (AssertionFailedError e) {
246: if (message != null) {
247: assertEquals(message, e.getMessage());
248: }
249: }
250: }
251: }
|