1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 |
|
---|
5 | import junit.framework.TestCase;
|
---|
6 |
|
---|
7 | import org.expeditee.items.Text;
|
---|
8 |
|
---|
9 | public class AttributeValuePairTest extends TestCase {
|
---|
10 |
|
---|
11 | public final void testExtractAttributes() {
|
---|
12 | assertEquals(null, AttributeUtils.extractAttributes(null));
|
---|
13 | }
|
---|
14 |
|
---|
15 | public final void testSetAttribute() {
|
---|
16 | Text attributes = new Text("");
|
---|
17 | Text text = new Text("Test");
|
---|
18 |
|
---|
19 | assertFalse(AttributeUtils.setAttribute(text, attributes));
|
---|
20 |
|
---|
21 | // Do a test with no attribute value pairs
|
---|
22 | attributes.setText("NO_AVP");
|
---|
23 | assertFalse(AttributeUtils.setAttribute(text, attributes));
|
---|
24 |
|
---|
25 | assertFalse(AttributeUtils.setAttribute(null, attributes));
|
---|
26 | assertFalse(AttributeUtils.setAttribute(attributes, null));
|
---|
27 |
|
---|
28 | attributes.setText("s:50");
|
---|
29 | assertTrue(AttributeUtils.setAttribute(attributes, attributes));
|
---|
30 | assertEquals(50.0F, attributes.getSize());
|
---|
31 | attributes.setText("size:25");
|
---|
32 | assertTrue(AttributeUtils.setAttribute(attributes, attributes));
|
---|
33 | assertEquals(25.0F, attributes.getSize());
|
---|
34 |
|
---|
35 | attributes.setText("c:red");
|
---|
36 | assertTrue(AttributeUtils.setAttribute(attributes, attributes));
|
---|
37 | assertEquals(Color.red, attributes.getColor());
|
---|
38 | attributes.setText("color:green");
|
---|
39 | assertTrue(AttributeUtils.setAttribute(attributes, attributes));
|
---|
40 | assertEquals(Color.green, attributes.getColor());
|
---|
41 | }
|
---|
42 |
|
---|
43 | public final void testGetValue() {
|
---|
44 | String[][] data = new String[][] {
|
---|
45 | { "Att:Value1", "Value1" },
|
---|
46 | { " Att : Value2 ", "Value2" },
|
---|
47 | { "@NoValue", "" },
|
---|
48 | { " @NoVaLuE ", "" },
|
---|
49 | { "@Value : : : value3", "" },
|
---|
50 | { "@Value: value3", "value3" },
|
---|
51 | { " ", "" },
|
---|
52 | {
|
---|
53 | "Att: value4 " + (char) Character.LINE_SEPARATOR
|
---|
54 | + "A comment", "value4" } };
|
---|
55 | for (int i = 0; i < data.length; i++) {
|
---|
56 | AttributeValuePair avp = new AttributeValuePair(data[i][0]);
|
---|
57 | assertEquals(data[i][1], avp.getValue());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public final void testGetAttribute() {
|
---|
62 | String[][] data = new String[][] { { "AtT1: Value1", "AtT1" },
|
---|
63 | { " Att2 : Value2 ", "Att2" }, { "@NoValue", "NoValue" },
|
---|
64 | { "@Att3 : Value", null }, { "@ Att3: Value", null },
|
---|
65 | { " @Att3: Value ", "Att3" }, { ": value3", null },
|
---|
66 | { "@1Test: value3", "1Test" }, { "@t est: value3", null },
|
---|
67 | { "Att4 : test : value4", "Att4 : test" }, { "@", null },
|
---|
68 | { "a", null }, { "*!*", null }, { "*!*: @%@", "*!*" } };
|
---|
69 | for (int i = 0; i < data.length; i++) {
|
---|
70 | // System.out.println("i = " + i);
|
---|
71 | AttributeValuePair avp = new AttributeValuePair(data[i][0]);
|
---|
72 | assertEquals(data[i][1], avp.getAttribute());
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public final void testReplaceValue() {
|
---|
77 | String[][] data = new String[][] {
|
---|
78 | { "AtT1: Value1", "NewValue", "AtT1: NewValue" },
|
---|
79 | { " Att2 : Value2 ", "Att2", "Att2: Att2" },
|
---|
80 | { " @NoValue ", " 15", "@NoValue: 15" },
|
---|
81 | { " 54 ", " 15", "54: 15" } };
|
---|
82 | Text test = new Text(-1);
|
---|
83 |
|
---|
84 | for (int i = 0; i < data.length; i++) {
|
---|
85 | test.setText(data[i][0]);
|
---|
86 | AttributeUtils.replaceValue(test, data[i][1]);
|
---|
87 | assertEquals(data[i][2], test.getText());
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public final void testGetDoubleValue() {
|
---|
92 | Object[][] validData = new Object[][] { { " Att2 : 3.5 ", 3.5 } };
|
---|
93 | for (int i = 0; i < validData.length; i++) {
|
---|
94 | AttributeValuePair avp = new AttributeValuePair(validData[i][0]
|
---|
95 | .toString());
|
---|
96 | assertEquals(validData[i][1], avp.getDoubleValue());
|
---|
97 | }
|
---|
98 | Object[][] invalidData = new Object[][] { { "AtT1: Value1", null },
|
---|
99 | { "@NoValue", null } };
|
---|
100 | for (int i = 0; i < invalidData.length; i++) {
|
---|
101 | try {
|
---|
102 | AttributeValuePair avp = new AttributeValuePair(
|
---|
103 | invalidData[i][0].toString());
|
---|
104 | avp.getDoubleValue();
|
---|
105 | } catch (Exception e) {
|
---|
106 | continue;
|
---|
107 | }
|
---|
108 | fail();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|