KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > test > label > LabelsTest


1 /*
2  * Copyright (c) 2004 Thomas Mohaupt
3  * All Rights Reserved.
4  *
5  * --LICENSE NOTICE--
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  * --LICENSE NOTICE--
20  */

21 package org.snipsnap.test.label;
22
23 import java.util.Collection JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28 import org.snipsnap.snip.Snip;
29 import org.snipsnap.snip.SnipImpl;
30 import org.snipsnap.snip.label.CategoryLabel;
31 import org.snipsnap.snip.label.DefaultLabel;
32 import org.snipsnap.snip.label.Label;
33 import org.snipsnap.snip.label.Labels;
34 import org.snipsnap.snip.label.TypeLabel;
35 import org.snipsnap.test.snip.SnipTestSupport;
36
37
38 /**
39  * LabelsTest
40  *
41  */

42 public class LabelsTest extends SnipTestSupport {
43   private Labels m_emptyLabels;
44   private Labels m_filledLabels;
45   private int m_numberOfLabels;
46   private Snip m_aSnip;
47
48   public LabelsTest(String JavaDoc name) {
49     super(name);
50   }
51
52   public static Test suite() {
53     return new TestSuite(LabelsTest.class);
54   }
55
56   protected void setUp() throws Exception JavaDoc {
57     m_aSnip = new SnipImpl("Labeled Snip", "Test Content");
58     m_emptyLabels = new Labels();
59     m_filledLabels = new Labels(m_aSnip, "TypeLabel:Type:Template"
60         + "|CategoryLabel:Category:zz"
61         + "|CategoryLabel:Category:yy"
62         + "|CategoryLabel:Category:xx"
63     );
64     m_numberOfLabels = 4;
65     super.setUp();
66   }
67
68   public void testInitLabels() {
69     Collection JavaDoc c = m_emptyLabels.getAll();
70     assertTrue(c.isEmpty());
71
72     c = m_filledLabels.getAll();
73     assertTrue(!c.isEmpty());
74   }
75
76   public void testGetAll() {
77     assertEquals(0, m_emptyLabels.getAll().size());
78     assertEquals(m_numberOfLabels, m_filledLabels.getAll().size());
79   }
80
81   public void testAddLabel() {
82     Label label = new DefaultLabel();
83     m_emptyLabels.addLabel(label);
84     m_filledLabels.addLabel(label);
85     assertEquals(1, m_emptyLabels.getAll().size());
86     assertEquals(m_numberOfLabels + 1, m_filledLabels.getAll().size());
87   }
88
89 // Searching by Value is not supported
90
//
91
// public void testGetLabel() {
92
// String value = "testcategory";
93
// Label label = new CategoryLabel(value);
94
// m_emptyLabels.addLabel(label);
95
// assertEquals(label, m_emptyLabels.getLabel(value));
96
//
97
// m_filledLabels.addLabel(label);
98
// m_filledLabels.addLabel(label);
99
// assertEquals(label, m_filledLabels.getLabel(value));
100
// }
101

102   protected void getLabels(Labels aLabels, String JavaDoc name) {
103     int before = aLabels.getAll().size();
104
105     Label c1 = new CategoryLabel("1");
106     Label c2 = new CategoryLabel("2");
107     Label c3 = new CategoryLabel("3");
108     Label t1 = new TypeLabel("1");
109     Label t2 = new TypeLabel("2");
110
111     aLabels.addLabel(c1);
112     aLabels.addLabel(c2);
113     aLabels.addLabel(c3);
114     aLabels.addLabel(t1);
115     aLabels.addLabel(t2);
116
117     Collection JavaDoc coll = aLabels.getAll();
118     assertEquals(before + 5, coll.size());
119
120     assertTrue(coll.contains(c1));
121     assertTrue(coll.contains(c2));
122     assertTrue(coll.contains(c3));
123     assertTrue(coll.contains(t1));
124     assertTrue(coll.contains(t2));
125   }
126
127   public void testGetLabels() {
128     getLabels(m_emptyLabels, "m_emptyLabels");
129     getLabels(m_filledLabels, "m_filledLabels");
130   }
131
132   protected void removeLabels(Labels aLabels, String JavaDoc name) {
133     int before = aLabels.getAll().size();
134
135     Label c1 = new CategoryLabel("1");
136     Label c2 = new CategoryLabel("2");
137     Label t1 = new TypeLabel("1");
138
139     aLabels.addLabel(c1);
140     aLabels.addLabel(c2);
141     aLabels.addLabel(t1);
142
143     aLabels.removeLabel(c2.getName(), c2.getValue());
144     aLabels.removeLabel(t1.getName(), t1.getValue());
145     aLabels.removeLabel(c1.getName(), c1.getValue());
146
147     Collection JavaDoc coll = aLabels.getAll();
148     assertEquals(before, coll.size());
149
150     assertTrue(name + ": remove labels", !coll.contains(c1));
151     assertTrue(name + ": remove labels", !coll.contains(c2));
152     assertTrue(name + ": remove labels", !coll.contains(t1));
153
154     //assertTrue(false);
155
}
156
157   protected void removeOneLabel(Labels aLabels, String JavaDoc name) {
158     try {
159       aLabels.removeLabel("any", "any");
160     } catch (Exception JavaDoc ex) {
161       assertTrue(name + ": remove not existing label", false);
162       ex.printStackTrace();
163     }
164
165     int before = aLabels.getAll().size();
166
167     Label c1 = new CategoryLabel("1");
168     aLabels.addLabel(c1);
169
170     aLabels.removeLabel(c1.getName(), c1.getValue());
171
172     Collection JavaDoc coll = aLabels.getAll();
173     assertEquals(name + ": remove a label", before, coll.size());
174
175     assertTrue(name + ": remove right label", !coll.contains(c1));
176   }
177
178   public void testRemoveOneLabel() {
179     removeOneLabel(m_emptyLabels, "m_emptyLabels");
180     removeOneLabel(m_filledLabels, "m_filledLabels");
181   }
182
183   public void testRemoveLabels() {
184     removeLabels(m_emptyLabels, "m_emptyLabels");
185     removeLabels(m_filledLabels, "m_filledLabels");
186   }
187
188 }
189
Popular Tags