KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > Manager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.output;
21
22 import java.awt.event.ComponentEvent JavaDoc;
23 import java.awt.event.ComponentListener JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28 import org.apache.tools.ant.module.spi.AntSession;
29 import org.openide.util.Mutex;
30 import org.openide.util.NbBundle;
31
32 /**
33  * This class gets informed about started and finished JUnit test sessions
34  * and manages that the result windows and reports in them are appropriately
35  * displayed, closed etc.
36  *
37  * @author Marian Petras
38  */

39 final class Manager {
40     
41     /**
42      * reference to the singleton of this class.
43      * Strong references to the singleton are kept in instances of
44      * {@link JUnitOutputReader JUnitOutputReader}.
45      */

46     private static Reference JavaDoc<Manager> instanceRef;
47     
48     /** list of sessions without windows displayed */
49     //private List/*<AntSession>*/ pendingSessions;
50
/** list of sessions with windows displayed */
51     //private List/*<AntSession>*/ displayedSessions;
52
/** */
53     //private List/*<Report>*/ displayedReports;
54
/**
55      * registry of Ant sessions.
56      * Each entry has a value of <code>Integer</code> whose value is
57      * holds information about type of the session
58      * (see {@link AntSessionInfo#sessionType}).
59      * If the value is negative (opposite to the <code>AntSessionInfo</code>
60      * constant), it means that method {@link #reportStarted} method
61      * has not yet been called for the session.
62      */

63     private final Map JavaDoc<AntSession, TaskType> junitSessions
64             = new WeakHashMap JavaDoc<AntSession, TaskType>(5);
65
66     
67     /**
68      * Returns a singleton instance of this class.
69      * If no instance exists at the moment, a new instance is created.
70      *
71      * @return singleton of this class
72      */

73     static Manager getInstance() {
74         Manager instance = (instanceRef != null) ? instanceRef.get() : null;
75         if (instance == null) {
76             instance = new Manager();
77             instanceRef = new WeakReference JavaDoc<Manager>(instance);
78         }
79         return instance;
80     }
81     
82     /**
83      * Called when an Ant task running JUnit tests is started.
84      * Displays a message in the JUnit results window.
85      */

86     void testStarted(final AntSession session, final TaskType sessionType) {
87         displayMessage(
88                 session,
89                 sessionType,
90                 NbBundle.getMessage(getClass(), "LBL_RunningTests")); //NOI18N
91
}
92     
93     /**
94      */

95     void sessionFinished(final AntSession session,
96                          final TaskType sessionType) {
97         Object JavaDoc o = junitSessions.get(session);
98         if (o == null) {
99             /* This session did not run the "junit" task. */
100             return;
101         }
102         
103         displayMessage(session, sessionType, null); //updates the display
104
junitSessions.remove(session); //must be after displayMessage(...)
105
//otherwise the window would get
106
//activated
107

108         //<editor-fold defaultstate="collapsed" desc="disabled code">
109
/*
110         assert ((pendingSessions != null
111                         && pendingSessions.contains(session)))
112                ^ ((displayedSessions != null)
113                         && (displayedSessions.contains(session)));
114         
115         int indexDisp = -1;
116         if (displayedSessions != null) {
117             indexDisp = displayedSessions.indexOf(session);
118         }
119         int indexPend = -1;
120         if (indexDisp == -1) {
121             indexPend = pendingSessions.indexOf(session);
122         }
123          */

124         
125         /*
126          * Display windows for pending sessions started earlier,
127          * display the report in a new window ...
128          */

129         /*
130         final int index = (indexDisp != -1)
131                           ? indexDisp
132                           : (displayedSessions == null)
133                             ? indexPend
134                             : displayedSessions.size() + indexPend;
135          */

136         //</editor-fold>
137

138         //<editor-fold defaultstate="collapsed" desc="disabled code">
139
// Mutex.EVENT.writeAccess(new Runnable() {
140
// public void run() {
141
// final ResultWindow win = ResultWindow.getInstance();
142
// //<editor-fold defaultstate="collapsed" desc="disabled code">
143
// /*
144
// final int emptyViewsToOpen
145
// = Math.max(0, index - win.getViewsCount());
146
// for (int i = 0; i < emptyViewsToOpen; i++) {
147
// win.openEmptyView();
148
// }
149
// */
150
// //win.displayReport(index, report);
151
// //</editor-fold>
152
// win.displayReport(
153
// 0,
154
// report,
155
// sessionType != AntSessionInfo.SESSION_TYPE_TEST);
156
// }
157
// });
158
//</editor-fold>
159

160         //<editor-fold defaultstate="collapsed" desc="disabled code">
161

162         /* ... and update information about displayed and pending sessions: */
163         /*
164         if (indexDisp != -1) {
165             displayedReports.set(indexDisp, report);
166         } else { //(indexPend != -1)
167             if (displayedSessions == null) {
168                 displayedSessions = new ArrayList(4);
169             }
170             if (indexPend == pendingSessions.size() - 1) {
171                 displayedSessions.addAll(pendingSessions);
172                 pendingSessions = null;
173             } else {
174                 displayedSessions.addAll(
175                         pendingSessions.subList(0,
176                                                 indexPend + 1));
177                 pendingSessions = new ArrayList(
178                         pendingSessions.subList(indexPend + 1,
179                                                 pendingSessions.size()));
180             }
181             
182             if (displayedReports == null) {
183                 displayedReports = new ArrayList(4);
184             }
185             for (int i = 0; i < indexPend; i++) {
186                 displayedReports.add(null);
187             }
188             displayedReports.add(report);
189         }
190          */

191         //</editor-fold>
192
}
193     
194     /**
195      */

196     void displayOutput(final AntSession session,
197                        final TaskType sessionType,
198                        final String JavaDoc text,
199                        final boolean error) {
200
201         /* Called from the AntLogger's thread */
202
203         final ResultDisplayHandler displayHandler = getDisplayHandler(session);
204         displayHandler.displayOutput(text, error);
205         displayInWindow(session, sessionType, displayHandler);
206     }
207     
208     /**
209      *
210      * @param suiteName name of the running suite; or {@code null} in the case
211      * of anonymous suite
212      */

213     void displaySuiteRunning(final AntSession session,
214                              final TaskType sessionType,
215                              final String JavaDoc suiteName) {
216
217         /* Called from the AntLogger's thread */
218         
219         final ResultDisplayHandler displayHandler = getDisplayHandler(session);
220         displayHandler.displaySuiteRunning(suiteName);
221         displayInWindow(session, sessionType, displayHandler);
222     }
223     
224     /**
225      */

226     void displayReport(final AntSession session,
227                        final TaskType sessionType,
228                        final Report report) {
229
230         /* Called from the AntLogger's thread */
231         
232         final ResultDisplayHandler displayHandler = getDisplayHandler(session);
233         displayHandler.displayReport(report);
234         displayInWindow(session, sessionType, displayHandler);
235     }
236     
237     /**
238      * Displays a message in the JUnit results window.
239      * If this is the first display in the window, it also promotes
240      * (displays, activates) it.
241      *
242      * @param message message to be displayed
243      */

244     private void displayMessage(final AntSession session,
245                                 final TaskType sessionType,
246                                 final String JavaDoc message) {
247
248         /* Called from the AntLogger's thread */
249
250         final ResultDisplayHandler displayHandler = getDisplayHandler(session);
251         displayHandler.displayMessage(message);
252         displayInWindow(session, sessionType, displayHandler);
253         
254         //<editor-fold defaultstate="collapsed" desc="disabled code">
255
/*
256          * This method is called only from method taskStarted(AntSession)
257          * which is synchronized.
258          */

259         
260         /*
261         if (pendingSessions == null) {
262             pendingSessions = new ArrayList(4);
263         }
264         pendingSessions.add(session);
265          */

266         
267         /* Close all windows with reports displayed: */
268         /*
269         assert (displayedSessions == null) == (displayedReports == null);
270         if (displayedSessions != null) {
271             assert displayedReports.size() == displayedSessions.size();
272             
273             ListIterator iDispRep
274                     = displayedReports.listIterator(displayedReports.size());
275             ListIterator iDispSes
276                     = displayedSessions.listIterator(displayedSessions.size());
277             final List indexes
278                     = new ArrayList(displayedReports.size());
279             while (iDispRep.hasPrevious()) {
280                 int index = iDispSes.previousIndex();
281                 Object r = iDispRep.previous();
282                 Object s = iDispSes.previous();
283                 if (r == null) {
284                     indexes.add(new Integer(index));
285                     iDispRep.remove();
286                     iDispSes.remove();
287                 }
288             }
289
290             assert displayedSessions.size() == displayedReports.size();
291
292             if (displayedSessions.isEmpty()) {
293                 displayedSessions = null;
294                 displayedReports = null;
295             }
296             Mutex.EVENT.writeAccess(new Runnable() {
297                 public void run() {
298                     ResultWindow win = ResultWindow.getInstance();
299                     for (Iterator i = indexes.iterator(); i.hasNext(); ) {
300                         win.removeView(((Integer) i.next()).intValue());
301                     }
302                 }
303             });
304         }
305          */

306         //</editor-fold>
307
}
308     
309     /**
310      */

311     private void displayInWindow(final AntSession session,
312                                  final TaskType sessionType,
313                                  final ResultDisplayHandler displayHandler) {
314         final boolean promote =
315                 (junitSessions.put(session, sessionType) == null)
316                 && (sessionType == TaskType.TEST_TASK);
317         
318         int displayIndex = getDisplayIndex(session);
319         if (displayIndex == -1) {
320             addDisplay(session);
321             
322             Mutex.EVENT.writeAccess(new Displayer(displayHandler, promote));
323         } else if (promote) {
324             Mutex.EVENT.writeAccess(new Displayer(null, promote));
325         }
326     }
327     
328     /**
329      *
330      */

331     private class Displayer implements Runnable JavaDoc {
332         private final ResultDisplayHandler displayHandler;
333         private final boolean promote;
334         Displayer(final ResultDisplayHandler displayHandler,
335                   final boolean promote) {
336             this.displayHandler = displayHandler;
337             this.promote = promote;
338         }
339         public void run() {
340             final ResultWindow window = ResultWindow.getInstance();
341             if (displayHandler != null) {
342                window.addDisplayComponent(displayHandler.getDisplayComponent());
343             }
344             if (promote) {
345                window.promote();
346             }
347         }
348     }
349     
350     /** singleton of the <code>ResultDisplayHandler</code> */
351     private Map JavaDoc<AntSession,ResultDisplayHandler> displayHandlers;
352     
353     /**
354      */

355     private ResultDisplayHandler getDisplayHandler(final AntSession session) {
356         ResultDisplayHandler displayHandler = (displayHandlers != null)
357                                               ? displayHandlers.get(session)
358                                               : null;
359         if (displayHandler == null) {
360             if (displayHandlers == null) {
361                 displayHandlers = new WeakHashMap JavaDoc<AntSession,ResultDisplayHandler>(7);
362             }
363             displayHandler = new ResultDisplayHandler();
364             displayHandlers.put(session, displayHandler);
365         }
366         return displayHandler;
367     }
368     
369     /** */
370     private Map JavaDoc<AntSession,Boolean JavaDoc> displaysMap;
371     
372     /**
373      */

374     private int getDisplayIndex(final AntSession session) {
375         if (displaysMap == null) {
376             return -1;
377         }
378         Boolean JavaDoc o = displaysMap.get(session);
379         return (o != null) ? 0 : -1;
380     }
381     
382     /**
383      */

384     private void addDisplay(final AntSession session) {
385         if (displaysMap == null) {
386             displaysMap = new WeakHashMap JavaDoc<AntSession,Boolean JavaDoc>(4);
387         }
388         displaysMap.put(session, Boolean.TRUE);
389     }
390     
391 }
392
Popular Tags