1 21 package org.snipsnap.test.label; 22 23 import java.util.Collection ; 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 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 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 { 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 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 102 protected void getLabels(Labels aLabels, String 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 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 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 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 } 156 157 protected void removeOneLabel(Labels aLabels, String name) { 158 try { 159 aLabels.removeLabel("any", "any"); 160 } catch (Exception 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 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 |