[Japi-cvs] SF.net SVN: japi: [614] libs/swing-prefs/trunk
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-09-23 20:21:54
|
Revision: 614 http://japi.svn.sourceforge.net/japi/?rev=614&view=rev Author: christianhujer Date: 2007-09-23 13:21:52 -0700 (Sun, 23 Sep 2007) Log Message: ----------- Added tests for preferences. Fixed update bug. Modified Paths: -------------- libs/swing-prefs/trunk/libs-swing-prefs.iml libs/swing-prefs/trunk/src/net/sf/japi/swing/prefs/PreferencesPane.java Added Paths: ----------- libs/swing-prefs/trunk/src/test/ libs/swing-prefs/trunk/src/test/net/ libs/swing-prefs/trunk/src/test/net/sf/ libs/swing-prefs/trunk/src/test/net/sf/japi/ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockActionBuilder.java libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockPrefs.java libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/PreferencesPaneTest.java libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/package-info.java Modified: libs/swing-prefs/trunk/libs-swing-prefs.iml =================================================================== --- libs/swing-prefs/trunk/libs-swing-prefs.iml 2007-09-23 20:11:01 UTC (rev 613) +++ libs/swing-prefs/trunk/libs-swing-prefs.iml 2007-09-23 20:21:52 UTC (rev 614) @@ -4,6 +4,7 @@ <exclude-output /> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" packagePrefix="test" /> </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> Modified: libs/swing-prefs/trunk/src/net/sf/japi/swing/prefs/PreferencesPane.java =================================================================== --- libs/swing-prefs/trunk/src/net/sf/japi/swing/prefs/PreferencesPane.java 2007-09-23 20:11:01 UTC (rev 613) +++ libs/swing-prefs/trunk/src/net/sf/japi/swing/prefs/PreferencesPane.java 2007-09-23 20:21:52 UTC (rev 614) @@ -95,6 +95,9 @@ */ private final JPanel cardPanel = new JPanel(cards); + /** The JList that displays the list of prefs modules. */ + private final JList prefsList; + /** Show Preferences. * @param parentComponent determines the Frame in which the dialog is displayed; if <code>null</code>, or if the <code>parentComponent</code> has * no <code>Frame</code>, a default <code>Frame</code> is used @@ -119,8 +122,9 @@ /** Create a PreferencesPane. * @param prefs PreferencesGroup to create panel for */ - private PreferencesPane(final PreferencesGroup prefs) { + public PreferencesPane(final PreferencesGroup prefs) { this.prefs = prefs; + prefsList = new JList(prefs); setMessage(createMessage()); setOptions(createOptions()); } @@ -139,11 +143,10 @@ * @return list */ private JComponent createList() { - final JList list = new JList(prefs); - list.setCellRenderer(new PrefsListCellRenderer()); - list.setSelectedIndex(0); - list.addListSelectionListener(this); - return new JScrollPane(list); + prefsList.setCellRenderer(new PrefsListCellRenderer()); + prefsList.setSelectedIndex(0); + prefsList.addListSelectionListener(this); + return new JScrollPane(prefsList); } /** Create the Panel. @@ -196,6 +199,8 @@ currentPref.apply(); break; case NO_OPTION: + currentPref.revert(); + break; default: } } @@ -256,6 +261,13 @@ } } + /** Returns the JList for selecting the prefs modules. + * @return The JList for selecting the prefs modules. + */ + public JList getPrefsList() { + return prefsList; + } + /** Class for rendering preferences list items. */ private static final class PrefsListCellRenderer extends DefaultListCellRenderer { Added: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockActionBuilder.java =================================================================== --- libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockActionBuilder.java (rev 0) +++ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockActionBuilder.java 2007-09-23 20:21:52 UTC (rev 614) @@ -0,0 +1,30 @@ +/* + * JAPI libs-swing-prefs is a library for adding preferences dialogs to an application. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.prefs; + +import net.sf.japi.swing.DefaultActionBuilder; + +/** + * Created by IntelliJ IDEA. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class MockActionBuilder extends DefaultActionBuilder { + +} // class MockActionBuilder Property changes on: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockActionBuilder.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockPrefs.java =================================================================== --- libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockPrefs.java (rev 0) +++ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockPrefs.java 2007-09-23 20:21:52 UTC (rev 614) @@ -0,0 +1,121 @@ +/* + * JAPI libs-swing-prefs is a library for adding preferences dialogs to an application. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.prefs; + +import java.util.HashMap; +import java.util.Map; +import javax.swing.JTextField; +import net.sf.japi.swing.prefs.AbstractPrefs; +import net.sf.japi.swing.prefs.Prefs; +import org.jetbrains.annotations.NotNull; + +/** MockPrefs is a {@link Prefs} implementation for testing. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @todo Extract invocation counter. This probably would be a good use case for AOP. + */ +public class MockPrefs extends AbstractPrefs { + + /** Invocation Counter. */ + private final Map<String, int[]> invocationCounter = new HashMap<String, int[]>(); + + /** The Textfield for changes. */ + private JTextField textfield = new JTextField(); + + /** The text for changes. */ + private String text = ""; + + /** Creates MockPrefs. */ + public MockPrefs() { + // Initialize the invocation counter. + final String[] keys = { "apply", "defaults", "isChanged", "revert" }; + for (final String key : keys) { + invocationCounter.put(key, new int[1]); + } + add(textfield); + } + + /** {@inheritDoc} */ + public void apply() { + increment("apply"); + text = textfield.getText(); + } + + /** {@inheritDoc} */ + public void defaults() { + increment("defaults"); + text = ""; + textfield.setText(text); + } + + /** {@inheritDoc} */ + public boolean isChanged() { + increment("isChanged"); + return !text.equals(textfield.getText()); + } + + /** {@inheritDoc} */ + public void revert() { + increment("revert"); + textfield.setText(text); + } + + /** Increments an invocation count. + * @param key Key to increment. + */ + protected void increment(@NotNull final String key) { + invocationCounter.get(key)[0]++; + } + + /** Resets an invocation count. + * @param key Key to reset. + */ + public void reset(@NotNull final String key) { + invocationCounter.get(key)[0] = 0; + } + + /** Resets all invocation counts. */ + public void resetAll() { + for (final int[] val : invocationCounter.values()) { + val[0] = 0; + } + } + + /** Returns the text of this preferences. + * @return The text of this preferences. + */ + public String getText() { + return text; + } + + /** Sets the text of this preferences. + * @param text The text of this preferences. + */ + public void setText(final String text) { + this.text = text; + } + + /** Returns the textfield of this preferences. + * @return The textfield of this preferences. + */ + public JTextField getTextfield() { + return textfield; + } + +} // class MockPrefs Property changes on: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/MockPrefs.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/PreferencesPaneTest.java =================================================================== --- libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/PreferencesPaneTest.java (rev 0) +++ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/PreferencesPaneTest.java 2007-09-23 20:21:52 UTC (rev 614) @@ -0,0 +1,58 @@ +/* + * JAPI libs-swing-prefs is a library for adding preferences dialogs to an application. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.prefs; + +import javax.swing.JList; +import javax.swing.JTextField; +import net.sf.japi.swing.ActionBuilderFactory; +import net.sf.japi.swing.prefs.PreferencesGroup; +import net.sf.japi.swing.prefs.PreferencesPane; +import org.junit.BeforeClass; +import org.junit.Test; + +/** Test for {@link PreferencesPane}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class PreferencesPaneTest { + + @BeforeClass + public void setUpSuite() { + ActionBuilderFactory.getInstance().putActionBuilder("net.sf.japi.swing.prefs", new MockActionBuilder()); + } + + /** Tests that when selecting another preferences for changed preferences, "no" will revert the changes. + * @see <a href="http://sourceforge.net/tracker/index.php?func=detail&aid=1769634&group_id=149894&atid=776737">[ 1769634 ] Selecting "No" for changed preferences should revert</a> + */ + @Test + public void testSelectingNoForChangedPrefsReverts() { + final MockPrefs prefs1 = new MockPrefs(); + final MockPrefs prefs2 = new MockPrefs(); + final PreferencesGroup prefsGroup = new PreferencesGroup("", prefs1, prefs2); + final PreferencesPane pane = new PreferencesPane(prefsGroup); + final JList list = pane.getPrefsList(); + final JTextField tf1 = prefs1.getTextfield(); + tf1.setText(tf1.getText() + "_"); + list.setSelectedIndex(1); + // TODO: Check whether the dialog (apply changes? yes | no | cancel) is shown + // TODO: Tell Dialog to select "No" + // TODO: Check whether revert() was called and actually reverted the value. + } + +} // class PreferencesPaneTest Property changes on: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/PreferencesPaneTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/package-info.java =================================================================== --- libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/package-info.java (rev 0) +++ libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/package-info.java 2007-09-23 20:21:52 UTC (rev 614) @@ -0,0 +1,23 @@ +/* + * JAPI libs-swing-prefs is a library for adding preferences dialogs to an application. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** Tests for {@link net.sf.japi.swing.prefs}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +package test.net.sf.japi.swing.prefs; Property changes on: libs/swing-prefs/trunk/src/test/net/sf/japi/swing/prefs/package-info.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |