KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > AbstractModelTest


1 package org.netbeans.modules.xml.xam;
2
3 import java.beans.PropertyChangeEvent JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import junit.framework.*;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8 import org.openide.util.WeakListeners;
9
10 /**
11  *
12  * @author Nam Nguyen
13  */

14 public class AbstractModelTest extends TestCase {
15     PropertyListener plistener;
16     TestComponentListener listener;
17     TestModel model;
18     TestComponent root;
19
20     static class PropertyListener implements PropertyChangeListener JavaDoc {
21         List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
22         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
23             events.add(evt);
24         }
25         
26         public void assertEvent(String JavaDoc propertyName, Object JavaDoc old, Object JavaDoc now) {
27             for (PropertyChangeEvent JavaDoc e : events) {
28                 if (propertyName.equals(e.getPropertyName())) {
29                     if (old != null && ! old.equals(e.getOldValue()) ||
30                         old == null && e.getOldValue() != null) {
31                         continue;
32                     }
33                     if (now != null && ! now.equals(e.getNewValue()) ||
34                         now == null && e.getNewValue() != null) {
35                         continue;
36                     }
37                     return; //matched
38
}
39             }
40             assertTrue("Expect property change event on "+propertyName+" with "+old+" and "+now, false);
41         }
42     }
43     
44     class TestComponentListener implements ComponentListener {
45         List JavaDoc<ComponentEvent> accu = new ArrayList JavaDoc<ComponentEvent>();
46         public void valueChanged(ComponentEvent evt) {
47             accu.add(evt);
48         }
49         public void childrenAdded(ComponentEvent evt) {
50             accu.add(evt);
51         }
52         public void childrenDeleted(ComponentEvent evt) {
53             accu.add(evt);
54         }
55         public void reset() { accu.clear(); }
56         public int getEventCount() { return accu.size(); }
57         public List JavaDoc<ComponentEvent> getEvents() { return accu; }
58     
59         private void assertEvent(ComponentEvent.EventType type, Component source) {
60             for (ComponentEvent e : accu) {
61                 if (e.getEventType().equals(type) &&
62                     e.getSource() == source) {
63                     return;
64                 }
65             }
66             assertTrue("Expect component change event " + type +" on source " + source +
67                     ". Instead received: " + accu, false);
68         }
69     }
70     
71     public AbstractModelTest(String JavaDoc testName) {
72         super(testName);
73     }
74
75     protected void setUp() throws Exception JavaDoc {
76         model = new TestModel();
77         listener = new TestComponentListener();
78         plistener = new PropertyListener();
79         model.addComponentListener(listener);
80         model.addPropertyChangeListener(plistener);
81     }
82
83     protected void tearDown() throws Exception JavaDoc {
84         model.removePropertyChangeListener(plistener);
85         model.removeComponentListener(listener);
86     }
87
88     public static Test suite() {
89         return new TestSuite(AbstractModelTest.class);
90     }
91     
92     public void testWeakListenerRemoval() throws Exception JavaDoc {
93         TestModel model = new TestModel();
94         TestComponent root = model.getRootComponent();
95         TestComponentListener listener1 = new TestComponentListener();
96         TestComponentListener listener2 = new TestComponentListener();
97         TestComponentListener listener3 = new TestComponentListener();
98         TestComponentListener listener4 = new TestComponentListener();
99         TestComponentListener listener5 = new TestComponentListener();
100         model.addComponentListener((ComponentListener)WeakListeners.create(ComponentListener.class, listener1, model));
101         model.addComponentListener((ComponentListener)WeakListeners.create(ComponentListener.class, listener2, model));
102         model.addComponentListener((ComponentListener)WeakListeners.create(ComponentListener.class, listener3, model));
103         model.addComponentListener((ComponentListener)WeakListeners.create(ComponentListener.class, listener4, model));
104         model.addComponentListener((ComponentListener)WeakListeners.create(ComponentListener.class, listener5, model));
105         
106         model.startTransaction();
107         model.addChildComponent(root, new TestComponent.B(model, 1), -1);
108         model.endTransaction();
109         
110         listener1.assertEvent(ComponentEvent.EventType.CHILD_ADDED, root);
111         listener2.assertEvent(ComponentEvent.EventType.CHILD_ADDED, root);
112         listener3.assertEvent(ComponentEvent.EventType.CHILD_ADDED, root);
113         listener4.assertEvent(ComponentEvent.EventType.CHILD_ADDED, root);
114         listener5.assertEvent(ComponentEvent.EventType.CHILD_ADDED, root);
115
116         listener2 = null;
117         listener3 = null;
118         listener4 = null;
119         listener5 = null;
120         System.gc();
121         Thread.sleep(50);
122      
123         assertEquals(1, model.getComponentListenerList().getListenerCount());
124     }
125     
126     public void testStateChangeEvent() throws Exception JavaDoc {
127         model.startTransaction();
128         model.setState(Model.State.NOT_WELL_FORMED);
129         model.endTransaction();
130         plistener.assertEvent(Model.STATE_PROPERTY, Model.State.VALID, Model.State.NOT_WELL_FORMED);
131     }
132     
133     private class FlushListener implements PropertyChangeListener JavaDoc {
134         long flushTime = 0;
135         public FlushListener() {
136             model.getAccess().addFlushListener(this);
137         }
138         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
139             if (evt.getSource() == model.getAccess() && evt.getPropertyName().equals("flushed")) {
140                 flushTime = ((Long JavaDoc)evt.getNewValue()).longValue();
141             }
142         }
143         public void assertFlushEvent(long since) {
144             assertTrue("Expect flush event after "+since, flushTime >= since);
145         }
146         public void assertNoFlushEvents(long since) {
147             assertTrue("Expect no flush events after "+since, flushTime < since);
148         }
149     }
150     
151     public void testReadOnlyTransactionSkipFlush() throws Exception JavaDoc {
152         FlushListener list = new FlushListener();
153         long since = System.currentTimeMillis();
154         model.startTransaction();
155         model.endTransaction();
156         list.assertNoFlushEvents(since);
157     }
158     
159     public void testWriteTransactionDidFlush() throws Exception JavaDoc {
160         FlushListener list = new FlushListener();
161         long since = System.currentTimeMillis();
162         model.startTransaction();
163         model.getRootComponent().setValue("newValue");
164         model.endTransaction();
165         list.assertFlushEvent(since);
166     }
167     
168     public void testModelFactoryListener() throws Exception JavaDoc {
169         TestModel2.factory().addPropertyChangeListener(plistener);
170         TestModel2 m = TestModel2.factory().getModel(Util.createModelSource(
171                 "resources/test1.xml"));
172         plistener.assertEvent(TestModel2.factory().MODEL_LOADED_PROPERTY, null, m);
173     }
174 }
175
Popular Tags