/hg/icedtea-web: PolicyEditor copy/paste/rename methods extracte...

aazores at icedtea.classpath.org aazores at icedtea.classpath.org
Fri Jun 6 13:22:03 UTC 2014


changeset 7f4465671398 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=7f4465671398
author: Andrew Azores <aazores at redhat.com>
date: Fri Jun 06 09:21:53 2014 -0400

	PolicyEditor copy/paste/rename methods extracted and unit tests added

	2014-06-06  Andrew Azores  <aazores at redhat.com>

	    PolicyEditor copy/paste/rename methods extracted and unit tests added
	    * netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
	    (codebaseClipboard): renamed to editorPermissionsClipboard
	    (customPermissionsClipboard): new field for copy/pasting custom permissions
	    (copyCodebaseButtonAction, pasteCodebaseButtonAction,
	    renameCodebaseButtonAction): refactor to use newly extracted methods
	    (copyCodebase, pasteCodebase, renameCodebase): methods extracted from
	    corresponding button action ActionListeners
	    (setPermission, addCustomPermission): new methods
	    * tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
	    (testRenameCodebase): new test for renaming codebase action
	    (testCopyPasteCodebase): new test for copying and pasting codebases


diffstat:

 ChangeLog                                                                        |  15 +
 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java                |  85 +++++++--
 tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java |  36 ++++
 3 files changed, 118 insertions(+), 18 deletions(-)

diffs (206 lines):

diff -r a4e068fd5b89 -r 7f4465671398 ChangeLog
--- a/ChangeLog	Thu Jun 05 11:22:31 2014 -0400
+++ b/ChangeLog	Fri Jun 06 09:21:53 2014 -0400
@@ -1,3 +1,18 @@
+2014-06-06  Andrew Azores  <aazores at redhat.com>
+
+	PolicyEditor copy/paste/rename methods extracted and unit tests added
+	* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
+	(codebaseClipboard): renamed to editorPermissionsClipboard
+	(customPermissionsClipboard): new field for copy/pasting custom permissions
+	(copyCodebaseButtonAction, pasteCodebaseButtonAction,
+	renameCodebaseButtonAction): refactor to use newly extracted methods
+	(copyCodebase, pasteCodebase, renameCodebase): methods extracted from
+	corresponding button action ActionListeners
+	(setPermission, addCustomPermission): new methods
+	* tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
+	(testRenameCodebase): new test for renaming codebase action
+	(testCopyPasteCodebase): new test for copying and pasting codebases
+
 2014-06-05  Andrew Azores  <aazores at redhat.com>
 
 	Added "Copy codebase to clipboard" action to PolicyEditor
diff -r a4e068fd5b89 -r 7f4465671398 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
--- a/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Thu Jun 05 11:22:31 2014 -0400
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Fri Jun 06 09:21:53 2014 -0400
@@ -65,6 +65,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -173,7 +174,8 @@
     private final JFileChooser fileChooser;
     private CustomPolicyViewer cpViewer = null;
     private final WeakReference<PolicyEditor> weakThis = new WeakReference<>(this);
-    private Map<PolicyEditorPermissions, Boolean> codebaseClipboard = null;
+    private Map<PolicyEditorPermissions, Boolean> editorPermissionsClipboard = null;
+    private Set<CustomPermission> customPermissionsClipboard = null;
 
     private final ActionListener okButtonAction, addCodebaseButtonAction,
             removeCodebaseButtonAction, openButtonAction, saveAsButtonAction, viewCustomButtonAction,
@@ -331,23 +333,14 @@
                         return;
                     }
                 }
-                final Map<PolicyEditorPermissions, Boolean> standardPermissions = policyFile.getCopyOfPermissions().get(oldCodebase);
-                final Set<CustomPermission> customPermissions = policyFile.getCopyOfCustomPermissions().get(oldCodebase);
-                removeCodebase(oldCodebase);
-                addNewCodebase(newCodebase);
-                for (final Map.Entry<PolicyEditorPermissions, Boolean> entry : standardPermissions.entrySet()) {
-                    policyFile.setPermission(newCodebase, entry.getKey(), entry.getValue());
-                }
-                policyFile.addCustomPermissions(newCodebase, customPermissions);
-                updateCheckboxes(newCodebase);
-                changesMade = true;
+                renameCodebase(oldCodebase, newCodebase);
             }
         };
 
         copyCodebaseButtonAction = new ActionListener() {
             @Override
             public void actionPerformed(final ActionEvent e) {
-                codebaseClipboard = new HashMap<>(policyFile.getCopyOfPermissions().get(getSelectedCodebase()));
+                copyCodebase(getSelectedCodebase());
             }
         };
 
@@ -361,12 +354,7 @@
                         return;
                     }
                 }
-                addNewCodebase(newCodebase);
-                for (final Map.Entry<PolicyEditorPermissions, Boolean> entry : codebaseClipboard.entrySet()) {
-                    policyFile.setPermission(newCodebase, entry.getKey(), entry.getValue());
-                }
-                updateCheckboxes(newCodebase);
-                changesMade = true;
+                pasteCodebase(newCodebase);
             }
         };
 
@@ -826,15 +814,76 @@
         changesMade = true;
     }
 
+    /**
+     * Rename a codebase, preserving its permissions
+     * @param oldCodebase the codebase to rename
+     * @param newCodebase the new name for the codebase
+     */
+    public void renameCodebase(final String oldCodebase, final String newCodebase) {
+        // Renaming a codebase shouldn't overwrite a previously copied clipboard
+        final Map<PolicyEditorPermissions, Boolean> editorPermissionsClipboardBackup = editorPermissionsClipboard;
+        final Set<CustomPermission> customPermissionsClipboardBackup = customPermissionsClipboard;
+
+        copyCodebase(oldCodebase);
+        pasteCodebase(newCodebase);
+        removeCodebase(oldCodebase);
+
+        editorPermissionsClipboard = editorPermissionsClipboardBackup;
+        customPermissionsClipboard = customPermissionsClipboardBackup;
+    }
+
+    /**
+     * Copy a codebase to the editor's "clipboard" (not the system clipboard), preserving its permissions
+     * @param codebase the codebase to copy
+     */
+    public void copyCodebase(final String codebase) {
+        if (!getCodebases().contains(codebase)) {
+            return;
+        }
+        editorPermissionsClipboard = new HashMap<>(policyFile.getCopyOfPermissions().get(codebase));
+
+        final Set<CustomPermission> customPermissions = policyFile.getCopyOfCustomPermissions().get(codebase);
+        if (customPermissions == null) {
+            customPermissionsClipboard = Collections.emptySet();
+        } else {
+            customPermissionsClipboard = new HashSet<>(policyFile.getCopyOfCustomPermissions().get(codebase));
+        }
+    }
+
+    /**
+     * Paste a codebase from the editor's "clipboard" (not the system clipboard)
+     * @param newCodebase the name to paste the codebase with
+     */
+    public void pasteCodebase(final String newCodebase) {
+        if (editorPermissionsClipboard == null || customPermissionsClipboard == null) {
+            return;
+        }
+        addNewCodebase(newCodebase);
+        for (final Map.Entry<PolicyEditorPermissions, Boolean> entry : editorPermissionsClipboard.entrySet()) {
+            policyFile.setPermission(newCodebase, entry.getKey(), entry.getValue());
+        }
+        policyFile.addCustomPermissions(newCodebase, customPermissionsClipboard);
+        updateCheckboxes(newCodebase);
+        changesMade = true;
+    }
+
     public Set<String> getCodebases() {
         return new HashSet<>(policyFile.getCopyOfPermissions().keySet());
     }
 
+    public void setPermission(final String codebase, final PolicyEditorPermissions permission, final boolean state) {
+        policyFile.setPermission(codebase, permission, state);
+    }
+
     public Map<PolicyEditorPermissions, Boolean> getPermissions(final String codebase) {
         policyFile.addCodebase(codebase);
         return new HashMap<>(policyFile.getCopyOfPermissions().get(codebase));
     }
 
+    public void addCustomPermission(final String codebase, final CustomPermission permission) {
+        policyFile.addCustomPermissions(codebase, Arrays.asList(permission));
+    }
+
     public Collection<CustomPermission> getCustomPermissions(final String codebase) {
         policyFile.addCodebase(codebase);
         return new HashSet<>(policyFile.getCopyOfCustomPermissions().get(codebase));
diff -r a4e068fd5b89 -r 7f4465671398 tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java	Thu Jun 05 11:22:31 2014 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java	Fri Jun 06 09:21:53 2014 -0400
@@ -106,6 +106,42 @@
     }
 
     @Test
+    public void testRenameCodebase() throws Exception {
+        final String originalUrl = "http://example.com";
+        final String renamedUrl = "http://example.com/example";
+        final PolicyEditorPermissions clipBoard = PolicyEditorPermissions.CLIPBOARD;
+        editor.addNewCodebase(originalUrl);
+        editor.setPermission(originalUrl, clipBoard, Boolean.TRUE);
+        final Collection<String> beforeRenameCodebases = editor.getCodebases();
+        assertTrue("Editor should contain " + originalUrl, beforeRenameCodebases.contains(originalUrl));
+        assertTrue(originalUrl + " should have " + clipBoard, editor.getPermissions(originalUrl).get(clipBoard));
+        editor.renameCodebase(originalUrl, renamedUrl);
+        final Collection<String> afterRenamedCodebases = editor.getCodebases();
+        assertFalse("Editor should not contain old codebase: " + originalUrl, afterRenamedCodebases.contains(originalUrl));
+        assertTrue("Editor should contain new codebase name: " + renamedUrl, afterRenamedCodebases.contains(renamedUrl));
+        assertTrue("Renamed " + renamedUrl + " should have " + clipBoard, editor.getPermissions(renamedUrl).get(clipBoard));
+    }
+
+    @Test
+    public void testCopyPasteCodebase() throws Exception {
+        final String copyUrl = "http://example.com";
+        final String pasteUrl = "http://example.com/example";
+        final PolicyEditorPermissions clipBoard = PolicyEditorPermissions.CLIPBOARD;
+        editor.addNewCodebase(copyUrl);
+        editor.setPermission(copyUrl, clipBoard, Boolean.TRUE);
+        final Collection<String> beforePasteCodebases = editor.getCodebases();
+        assertTrue("Editor should contain original codebase: " + copyUrl, beforePasteCodebases.contains(copyUrl));
+        assertTrue(copyUrl + " should have " + clipBoard, editor.getPermissions(copyUrl).get(clipBoard));
+        editor.copyCodebase(copyUrl);
+        editor.pasteCodebase(pasteUrl);
+        final Collection<String> afterPasteCodebases = editor.getCodebases();
+        assertTrue("Editor should still contain original codebase: " + copyUrl, afterPasteCodebases.contains(copyUrl));
+        assertTrue("Editor should also contain pasted codebase:" + pasteUrl, afterPasteCodebases.contains(pasteUrl));
+        assertTrue(copyUrl + " should have " + clipBoard, editor.getPermissions(copyUrl).get(clipBoard));
+        assertTrue(pasteUrl + " should have " + clipBoard, editor.getPermissions(pasteUrl).get(clipBoard));
+    }
+
+    @Test
     public void testReturnedCodebasesIsCopy() throws Exception {
         final Collection<String> original = editor.getCodebases();
         original.add("some invalid value");


More information about the distro-pkg-dev mailing list