001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.web.webmodule;
030:
031: import java.awt.Component;
032: import java.io.File;
033: import java.util.HashSet;
034: import java.util.Set;
035: import javax.swing.JPanel;
036: import javax.swing.event.ChangeListener;
037: import org.netbeans.junit.NbTestCase;
038: import org.netbeans.modules.web.api.webmodule.ExtenderController;
039: import org.netbeans.modules.web.api.webmodule.WebModule;
040: import org.netbeans.modules.web.spi.webmodule.FrameworkConfigurationPanel;
041: import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
042: import org.netbeans.modules.web.spi.webmodule.WebModuleExtender;
043: import org.netbeans.modules.web.spi.webmodule.WebModuleFactory;
044: import org.openide.WizardDescriptor;
045: import org.openide.util.ChangeSupport;
046: import org.openide.util.HelpCtx;
047: import org.openide.util.test.MockChangeListener;
048:
049: /**
050: *
051: * @author Andrei Badea
052: */
053: @SuppressWarnings("deprecation")
054: public class WebModuleExtenderBridgeTest extends NbTestCase {
055:
056: private WebModule webModule;
057: private ExtenderController controller;
058:
059: @Override
060: protected void setUp() {
061: webModule = WebModuleFactory
062: .createWebModule(new SimpleWebModuleImpl());
063: controller = ExtenderController.create();
064: }
065:
066: public WebModuleExtenderBridgeTest(String testName) {
067: super (testName);
068: }
069:
070: public void testBasic() {
071:
072: PanelImpl panel = new PanelImpl();
073: FrameworkImpl framework = new FrameworkImpl(panel);
074: WebModuleExtender extender = framework.createWebModuleExtender(
075: webModule, controller);
076: MockChangeListener listener = new MockChangeListener();
077:
078: extender.update();
079: extender.addChangeListener(listener);
080: assertFalse(extender.isValid());
081: assertEquals("Not valid", controller.getErrorMessage());
082:
083: assertTrue(extender.getComponent() instanceof JPanel);
084:
085: controller.getProperties().setProperty("prop", "foo");
086: extender.update();
087: assertTrue(extender.isValid());
088: assertNull(controller.getErrorMessage());
089:
090: panel.forceInvalid(true);
091: listener.assertEventCount(1);
092: assertFalse(extender.isValid());
093: assertEquals("Not valid", controller.getErrorMessage());
094:
095: panel.forceInvalid(false);
096: listener.assertEventCount(1);
097: assertTrue(extender.isValid());
098:
099: controller.getProperties().setProperty("prop", null);
100: extender.update();
101: assertFalse(extender.isValid());
102:
103: assertFalse(framework.extendCalled);
104: extender.extend(webModule);
105: assertTrue(framework.extendCalled);
106: }
107:
108: public void testConfigurationPanelCanBeNullIssue121712() {
109: FrameworkImpl framework = new FrameworkImpl(null);
110: WebModuleExtender extender = framework.createWebModuleExtender(
111: webModule, controller);
112: extender.update(); // should not throw NPE
113: }
114:
115: private static final class PanelImpl implements
116: FrameworkConfigurationPanel {
117:
118: private final ChangeSupport changeSupport = new ChangeSupport(
119: this );
120: private WizardDescriptor wizard;
121: private boolean forcedInvalid;
122:
123: public void enableComponents(boolean enable) {
124: }
125:
126: public void addChangeListener(ChangeListener l) {
127: changeSupport.addChangeListener(l);
128: }
129:
130: public Component getComponent() {
131: return new JPanel();
132: }
133:
134: public HelpCtx getHelp() {
135: return new HelpCtx("help me");
136: }
137:
138: public boolean isValid() {
139: boolean valid = "foo".equals(wizard.getProperty("prop"))
140: && !forcedInvalid;
141: wizard.putProperty("WizardPanel_errorMessage", valid ? " "
142: : "Not valid");
143: return valid;
144: }
145:
146: public void readSettings(Object settings) {
147: wizard = (WizardDescriptor) settings;
148: }
149:
150: public void removeChangeListener(ChangeListener l) {
151: changeSupport.removeChangeListener(l);
152: }
153:
154: public void storeSettings(Object settings) {
155: }
156:
157: void forceInvalid(boolean value) {
158: forcedInvalid = value;
159: changeSupport.fireChange();
160: }
161: }
162:
163: @SuppressWarnings("deprecation")
164: private static final class FrameworkImpl extends
165: WebFrameworkProvider {
166:
167: private final PanelImpl panel;
168: private boolean extendCalled;
169:
170: public FrameworkImpl(PanelImpl panel) {
171: super ("name", "description");
172: this .panel = panel;
173: }
174:
175: @Override
176: public FrameworkConfigurationPanel getConfigurationPanel(
177: WebModule wm) {
178: return panel;
179: }
180:
181: @Override
182: public Set extend(WebModule wm) {
183: extendCalled = true;
184: return new HashSet();
185: }
186:
187: public File[] getConfigurationFiles(WebModule wm) {
188: return new File[0];
189: }
190:
191: public boolean isInWebModule(WebModule wm) {
192: return false;
193: }
194: }
195: }
|