1 29 30 package nextapp.echo2.testapp.thousandmonkeys; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import nextapp.echo2.app.CheckBox; 36 import nextapp.echo2.app.Column; 37 import nextapp.echo2.app.Component; 38 import nextapp.echo2.app.ContentPane; 39 import nextapp.echo2.app.Grid; 40 import nextapp.echo2.app.IllegalChildException; 41 import nextapp.echo2.app.ListBox; 42 import nextapp.echo2.app.PasswordField; 43 import nextapp.echo2.app.RadioButton; 44 import nextapp.echo2.app.Row; 45 import nextapp.echo2.app.SelectField; 46 import nextapp.echo2.app.SplitPane; 47 import nextapp.echo2.app.TextArea; 48 import nextapp.echo2.app.TextField; 49 import nextapp.echo2.app.WindowPane; 50 import nextapp.echo2.testapp.thousandmonkeys.factories.ButtonFactory; 51 import nextapp.echo2.testapp.thousandmonkeys.factories.GenericFactory; 52 import nextapp.echo2.testapp.thousandmonkeys.factories.LabelFactory; 53 54 60 public class Monkey { 61 62 private static final ComponentFactory[] componentFactories = new ComponentFactory[]{ 63 new ButtonFactory(), 64 new GenericFactory(CheckBox.class), 65 new GenericFactory(Column.class), 66 new GenericFactory(ContentPane.class), 67 new GenericFactory(Grid.class), 68 new LabelFactory(), 69 new GenericFactory(ListBox.class), 70 new GenericFactory(PasswordField.class), 71 new GenericFactory(RadioButton.class), 72 new GenericFactory(Row.class), 73 new GenericFactory(SelectField.class), 74 new GenericFactory(SplitPane.class), 75 new GenericFactory(TextField.class), 76 new GenericFactory(TextArea.class), 77 new GenericFactory(WindowPane.class) 78 }; 79 80 84 private static final int MAX_CONTENT_PANE_CHILDREN = 5; 85 86 89 private static final int MIN_ADDS = 0; 90 91 94 private static final int MAX_ADDS = 20; 95 96 99 private static final int MIN_REMOVES = 0; 100 101 104 private static final int MAX_REMOVES = 12; 105 106 110 private ContentPane contentPane; 111 112 115 private List components = new ArrayList (); 116 117 123 public Monkey(ContentPane contentPane) { 124 this.contentPane = contentPane; 125 } 126 127 131 private void doAdd() { 132 int addParentIndex; 133 if (components.size() < MAX_CONTENT_PANE_CHILDREN) { 134 addParentIndex = ((int) Math.random() * (components.size() + 1)) - 1; 135 } else { 136 addParentIndex = ((int) Math.random() * components.size()); 137 } 138 ComponentFactory componentFactory = componentFactories[((int) (Math.random() * componentFactories.length))]; 139 Component child = componentFactory.newInstance(); 140 141 for (int i = 0; i < 5; ++i) { 142 Component parent = addParentIndex == -1 ? contentPane : (Component) components.get(addParentIndex); 143 try { 144 parent.add(child); 145 components.add(child); 146 return; 147 } catch (IllegalChildException ex) { } 148 } 149 } 150 151 154 private void doRemove() { 155 if (components.size() == 0) { 156 return; 158 } 159 Component child = (Component) components.get((int) (Math.random() * components.size())); 160 child.getParent().remove(child); 161 recursiveRemoveComponentFromList(child); 162 } 163 164 168 public void iterate() { 169 int removes = ((int) (Math.random() * (1 + MAX_REMOVES - MIN_REMOVES))) + MIN_REMOVES; 170 for (int i = 0; i < removes; ++i) { 171 doRemove(); 172 } 173 174 int adds = ((int) (Math.random() * (1 + MAX_ADDS - MIN_ADDS))) + MIN_ADDS; 175 for (int i = 0; i < adds; ++i) { 176 doAdd(); 177 } 178 System.err.println(components.size()); 179 } 180 181 188 private void recursiveRemoveComponentFromList(Component component) { 189 components.remove(component); 190 Component[] children = component.getComponents(); 191 for (int i = 0; i < children.length; ++i) { 192 recursiveRemoveComponentFromList(children[i]); 193 } 194 } 195 } 196 | Popular Tags |