01: package net.sourceforge.cruisecontrol.distributed.util;
02:
03: import junit.framework.TestCase;
04:
05: import java.util.List;
06: import java.util.ArrayList;
07: import java.util.prefs.Preferences;
08: import java.util.prefs.BackingStoreException;
09: import java.awt.Window;
10:
11: import net.sourceforge.cruisecontrol.builders.DistributedMasterBuilderTest;
12: import net.sourceforge.cruisecontrol.distributed.core.PreferencesHelper;
13: import org.apache.log4j.Logger;
14:
15: /**
16: * @author Dan Rollo
17: * Date: Mar 22, 2007
18: * Time: 9:23:58 PM
19: */
20: public class BuildAgentUtilityTest extends TestCase {
21:
22: private static final Logger LOG = Logger
23: .getLogger(BuildAgentUtilityTest.class);
24:
25: private static final class MockUI implements
26: PreferencesHelper.UIPreferences,
27: BuildAgentUtility.UISetInfo {
28:
29: public Preferences getPrefsBase() {
30: return Preferences
31: .userNodeForPackage(BuildAgentUtility.UI.class);
32: }
33:
34: public Window getWindow() {
35: final String msg = "Dummy unit test method not implemented, and should NOT have been called.";
36: LOG.error(msg);
37: throw new RuntimeException(msg);
38: }
39:
40: private String infoTextSet;
41:
42: public void setInfo(final String infoText) {
43: infoTextSet = infoText;
44: }
45:
46: public String getLastInfo() {
47: return infoTextSet;
48: }
49: }
50:
51: private MockUI mockUI;
52:
53: protected void setUp() throws Exception {
54: DistributedMasterBuilderTest.setupInsecurePolicy();
55:
56: mockUI = new MockUI();
57: }
58:
59: protected void tearDown() throws Exception {
60: // clear all agent util prefs
61: mockUI.getPrefsBase().removeNode();
62:
63: // @todo Revmove this if/when java 6 prefs impl is fixed on Unix (and annotate which jre contains the fix)
64: // see Sun Bug ID: 6568540 - (prefs) Preferences not saved in Webstart app, even after synch()
65: try {
66: mockUI.getPrefsBase().flush();
67: } catch (BackingStoreException e) {
68: if ((e.getCause() instanceof IllegalArgumentException)
69: && Double.parseDouble(System
70: .getProperty("java.specification.version")) == 1.6) {
71: LOG
72: .error("Warning: Unit Test Exception ignored due to known Java 6 bug (Sun Bug ID: 6568540)");
73: } else {
74: throw e;
75: }
76: }
77: }
78:
79: public void testDiscoveryInstanceReuse() throws Exception {
80:
81: final BuildAgentUtility buildAgentUtility = new BuildAgentUtility(
82: mockUI);
83: final List agents = new ArrayList();
84:
85: assertNull(mockUI.getLastInfo());
86: buildAgentUtility.getAgentInfoAll(agents);
87: assertNotNull(mockUI.getLastInfo());
88:
89: // reset
90: mockUI.setInfo(null);
91: assertNull(mockUI.getLastInfo());
92:
93: // call again to make sure disovery instance is not terminated
94: buildAgentUtility.getAgentInfoAll(agents);
95: assertNotNull(mockUI.getLastInfo());
96: }
97:
98: }
|