Title: [40574] trunk/hudson/plugins/configurationslicing/src: adding multiple builders per items

Diff

Modified: trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlice.java (40573 => 40574)


--- trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlice.java	2012-05-18 14:46:07 UTC (rev 40573)
+++ trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlice.java	2012-05-18 19:33:10 UTC (rev 40574)
@@ -37,7 +37,16 @@
             for(String itemName : namesSplit) {
             	itemName = itemName.trim();
             	if (itemName.length() > 0) {
-            		addLine(nameToValues, itemName, value.trim());
+                	int index = 0;
+            		if (spec.isMultipleItemsAllowed()) {
+	                	int bracket = itemName.indexOf('[');
+	                	if (bracket > 0) {
+	                		String indexString = itemName.substring(bracket + 1, itemName.length() - 1);
+	                		index = Integer.parseInt(indexString);
+	                		itemName = itemName.substring(0, bracket);
+	                	}
+            		}
+            		addLine(nameToValues, itemName, value.trim(), index);
             	}
             }
         }
@@ -61,14 +70,19 @@
         Set<String> list= map.get(s);
         list.add(name);
     }
-    private static void addLine(Map<String, List<String>> map, String s, String name) {
+    private static void addLine(Map<String, List<String>> map, String s, String name, int index) {
         if(!map.containsKey(s)) {
             map.put(s, new ArrayList<String>());
         }
-        List<String> list= map.get(s);
-        list.add(name);
+        List<String> list = map.get(s);
+        while (list.size() < index + 1) {
+        	// add a blank - this could happen if one of the indexed names was simply removed
+        	list.add("");
+        }
+        
+        list.set(index, name);
     }
-
+    
     public List<String> get(String name) {
         return nameToValues.get(name);
     }

Modified: trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlicer.java (40573 => 40574)


--- trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlicer.java	2012-05-18 14:46:07 UTC (rev 40573)
+++ trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/UnorderedStringSlicer.java	2012-05-18 19:33:10 UTC (rev 40574)
@@ -1,5 +1,6 @@
 package configurationslicing;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public class UnorderedStringSlicer<I> implements Slicer<UnorderedStringSlice<I>, I>{
@@ -20,6 +21,12 @@
         public String getConfiguredValueDescription() {
         	return "Configured Value";
         }
+        /**
+         * Allows you to use "MyJob[0]" to indicate separate values
+         */
+        public boolean isMultipleItemsAllowed() {
+        	return false;
+        }
     }
 
     private UnorderedStringSlicerSpec<I> spec;
@@ -34,8 +41,19 @@
     	return true;
     }
 
-    public UnorderedStringSlice<I> accumulate(UnorderedStringSlice<I> t, I i) {
-        t.add(spec.getName(i), spec.getValues(i));
+    public UnorderedStringSlice<I> accumulate(UnorderedStringSlice<I> t, I item) {
+    	String name = spec.getName(item);
+    	List<String> values = spec.getValues(item);
+    	if (values.size() > 1 && spec.isMultipleItemsAllowed()) {
+	    	for (int i = 0; i < values.size(); i++) {
+	    		List<String> _oneValueList_ = new ArrayList<String>();
+	    		oneValueList.add(values.get(i));
+	    		String _oneName_ = name + "[" + i + "]";
+	    		t.add(oneName, oneValueList);
+	    	}
+    	} else {
+    		t.add(spec.getName(item), values);
+    	}
         return t;
     }
 

Modified: trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/executeshell/ExecuteShellSlicer.java (40573 => 40574)


--- trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/executeshell/ExecuteShellSlicer.java	2012-05-18 14:46:07 UTC (rev 40573)
+++ trunk/hudson/plugins/configurationslicing/src/main/java/configurationslicing/executeshell/ExecuteShellSlicer.java	2012-05-18 19:33:10 UTC (rev 40574)
@@ -30,7 +30,7 @@
 
     public static class ExecuteShellSliceSpec extends UnorderedStringSlicerSpec<AbstractProject<?,?>> {
 
-        private static final String NOTHING = "(nothing)";
+        public static final String NOTHING = "(nothing)";
 
         public String getDefaultValueString() {
             return NOTHING;
@@ -48,16 +48,22 @@
             return "executeshellslicestring";
         }
 
+        @Override
+        public boolean isMultipleItemsAllowed() {
+        	return true;
+        }
+        
         public List<String> getValues(AbstractProject<?, ?> item) {
             List<String> shellContent = new ArrayList<String>();
             DescribableList<Builder,Descriptor<Builder>> buildersList = getBuildersList(item);
 
-            Shell shell = (Shell)buildersList.get(Shell.class);
-            if(shell != null) {
+            List<Shell> shells = buildersList.getAll(Shell.class);
+            for (Shell shell: shells) {
                 shellContent.add(shell.getCommand());
-            } else {
-                shellContent.add(NOTHING);
             }
+            if (shellContent.isEmpty()) {
+            	shellContent.add(NOTHING);
+            }
 
             return shellContent;
         }
@@ -83,46 +89,66 @@
         	} else {
         		return null;
         	}
-            
         }
 
-        public boolean setValues(AbstractProject<?, ?> item, List<String> set) {
+        public boolean setValues(AbstractProject<?, ?> item, List<String> list) {
             DescribableList<Builder,Descriptor<Builder>> buildersList = getBuildersList(item);
-            String command = set.iterator().next();
+            List<Shell> shells = buildersList.getAll(Shell.class);
             
-            // if the command is empty or NOTHING, remove the shell builder from the job
-            if(command.equals(NOTHING) || command.equals("")) {
-                Shell shell = (Shell) buildersList.get(Shell.class);
-                if(shell != null) {
-                    try {
-                    	// the remove command will persist the project
-                        buildersList.remove(shell.getDescriptor());
-                    } catch(java.io.IOException e) {
-                        System.err.println("IOException Thrown removing shell value");
-                        return false;
-                    }
-                }
-            } else {
-        		Shell newShell = new Shell(command);
-            	// check to see if we need to replace the shell command.  This prevents persisting
-            	// an empty change, which is important for keeping audit trails clean.
-            	Shell currentShell = buildersList.get(Shell.class);
-            	if (currentShell != null) {
-            		String oldCommand = currentShell.getCommand();
-            		if (!command.equals(oldCommand)) {
-            			return replaceBuilder(buildersList, currentShell, newShell);
-            		}
-            	} else {
+            int maxLen = Math.max(list.size(), shells.size());
+            Shell[] oldShells = new Shell[maxLen];
+            Shell[] newShells = new Shell[maxLen];
+
+            for (int i = 0; i < shells.size(); i++) {
+	            oldShells[i] = shells.get(i);
+            }
+
+            for (int i = 0; i < list.size(); i++) {
+	            String command = list.get(i);
+	            if(!command.equals(NOTHING) && !command.equals("")) {
+	            	if (oldShells[i] != null && oldShells[i].getCommand().equals(command)) {
+	            		newShells[i] = oldShells[i];
+	            	} else {
+	            		newShells[i] = new Shell(command);
+	            	}
+	            }
+            }
+            
+            // perform any replacements
+            for (int i = 0; i < maxLen; i++) {
+				if (oldShells[i] != null && newShells[i] != null && oldShells[i] != newShells[i]) {
+					replaceBuilder(buildersList, oldShells[i], newShells[i]);
+				}
+			}
+            
+            // add any new ones (should always add to the end, but might not if the original command was empty)
+            for (int i = 0; i < maxLen; i++) {
+				if (oldShells[i] == null && newShells[i] != null) {
 	                try {
-	                    buildersList.add(newShell);
+	                    buildersList.add(newShells[i]);
 	                } catch(java.io.IOException e) {
-	                    System.err.println("IOException Thrown add shell value");
+	                    System.err.println("IOException Thrown add builder value");
 	                    return false;
 	                }
-            	}
-            }
-			return true;
+				}
+			}
+            
+            // delete any old ones
+            for (int i = 0; i < maxLen; i++) {
+				if (oldShells[i] != null && newShells[i] == null) {
+                    try {
+	                	// the remove command will persist the project
+	                    buildersList.remove(oldShells[i]);
+	                } catch(java.io.IOException e) {
+	                    System.err.println("IOException Thrown removing shell value");
+	                    return false;
+	                }
+				}
+			}
+            
+            return true;
         }
+
         /**
          * If we do other builders, publishers, etc - this should be the pattern to use.
          * @throws IOException 

Added: trunk/hudson/plugins/configurationslicing/src/test/java/configurationslicing/ShellTest.java (0 => 40574)


--- trunk/hudson/plugins/configurationslicing/src/test/java/configurationslicing/ShellTest.java	                        (rev 0)
+++ trunk/hudson/plugins/configurationslicing/src/test/java/configurationslicing/ShellTest.java	2012-05-18 19:33:10 UTC (rev 40574)
@@ -0,0 +1,122 @@
+package configurationslicing;
+
+import hudson.model.Project;
+import hudson.tasks.Shell;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.jvnet.hudson.test.HudsonTestCase;
+
+import configurationslicing.executeshell.ExecuteShellSlicer;
+
+public class ShellTest extends HudsonTestCase {
+
+	@SuppressWarnings("unchecked")
+	public void testGetMultipleShells() throws Exception {
+		ExecuteShellSlicer.ExecuteShellSliceSpec spec = new ExecuteShellSlicer.ExecuteShellSliceSpec();
+		
+		String command1 = "foo";
+		String command2 = "bar";
+		
+		Project project = createProject("shell-get", command1, command2);
+		
+		List<String> values = spec.getValues(project);
+		assertEquals(command1, values.get(0));
+		assertEquals(command2, values.get(1));
+	}
+	
+	public void testSetMultipleShells() throws Exception {
+		int count = 0;
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b" }, new String[] { "c", "d", "e"});
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b" }, new String[] { "a", "e", "b"});
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b", "c" }, new String[] { "a", "c"});
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b", "c" }, new String[] { "a", "", "c"});
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b", "c" }, new String[] { "" });
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b", "c" }, new String[] { "", "d", "", "e" });
+		doTestSetMultipleShells("shell-" + (count++), new String[] { "a", "b", "c" }, new String[] { "c", "b", "a" });
+		doTestSetMultipleShells("shell-" + (count++), new String[] { }, new String[] { "a", "b" });
+	}
+	@SuppressWarnings("unchecked")
+	public void doTestSetMultipleShells(String name, String[] oldCommands, String[] newCommands) throws Exception {
+		ExecuteShellSlicer.ExecuteShellSliceSpec spec = new ExecuteShellSlicer.ExecuteShellSliceSpec();
+		
+		Project project = createProject(name, oldCommands);
+
+		// smoke test that the create worked
+		List<String> oldValues = spec.getValues(project);
+		for (int i = 0; i < oldCommands.length; i++) {
+			assertEquals(oldCommands[i], oldValues.get(i));
+		}
+		
+		List<String> newShells = Arrays.asList(newCommands);
+		spec.setValues(project, newShells);
+		
+		List<String> newCommandsClean = new ArrayList<String>();
+		for (int i = 0; i < newCommands.length; i++) {
+			if (!"".equals(newCommands[i])) {
+				newCommandsClean.add(newCommands[i]);
+			}
+		}
+		if (newCommandsClean.isEmpty()) {
+			newCommandsClean.add(ExecuteShellSlicer.ExecuteShellSliceSpec.NOTHING);
+		}
+		
+		List<String> newValues = spec.getValues(project);
+		assertEquals(newCommandsClean.size(), newValues.size());
+		for (int i = 0; i < newCommandsClean.size(); i++) {
+			assertEquals(newCommandsClean.get(i), newValues.get(i));
+		}
+	}
+	
+	@SuppressWarnings("unchecked")
+	private Project createProject(String name, String... shells) throws Exception {
+		Project project = createFreeStyleProject(name);
+		for (String shell: shells) {
+			project.getBuildersList().add(new Shell(shell));
+		}
+		return project;
+	}
+	
+	@SuppressWarnings("unchecked")
+	public void testNoBracketNames() {
+		ExecuteShellSlicer.ExecuteShellSliceSpec spec = new ExecuteShellSlicer.ExecuteShellSliceSpec();
+		
+		String v1 = "v1";
+		List<String> configurationValues = new ArrayList<String>();
+		configurationValues.add(v1);
+		
+		String n1 = "n1";
+		List<String> itemNames = new ArrayList<String>();
+		itemNames.add(n1);
+		
+		UnorderedStringSlice slice = new UnorderedStringSlice(spec, configurationValues, itemNames);
+		
+		List<String> values = slice.get(n1);
+		assertEquals(1, values.size());
+		assertEquals(v1, values.get(0));
+	}
+	@SuppressWarnings("unchecked")
+	public void testBracketNames() {
+		ExecuteShellSlicer.ExecuteShellSliceSpec spec = new ExecuteShellSlicer.ExecuteShellSliceSpec();
+		
+		String v1 = "v1";
+		String v2 = "v2";
+		List<String> configurationValues = new ArrayList<String>();
+		configurationValues.add(v1);
+		configurationValues.add(v2);
+		
+		List<String> itemNames = new ArrayList<String>();
+		itemNames.add("a[1]\nb[2]");
+		itemNames.add("a[0]\nc[4]");
+		
+		UnorderedStringSlice slice = new UnorderedStringSlice(spec, configurationValues, itemNames);
+		
+		assertEquals(2, slice.get("a").size());
+		assertEquals(3, slice.get("b").size());
+		assertEquals(5, slice.get("c").size());
+	}
+}
Property changes on: trunk/hudson/plugins/configurationslicing/src/test/java/configurationslicing/ShellTest.java
___________________________________________________________________

Added: svn:mime-type

Reply via email to