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