[jboss-cvs] JBossAS SVN: r95688 - projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Oct 28 12:20:18 EDT 2009
Author: kabir.khan at jboss.com
Date: 2009-10-28 12:20:16 -0400 (Wed, 28 Oct 2009)
New Revision: 95688
Added:
projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerStateModelTestCase.java
Log:
Add ControllerStateModelTestCase so I don't lose it
Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerStateModelTestCase.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerStateModelTestCase.java (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerStateModelTestCase.java 2009-10-28 16:20:16 UTC (rev 95688)
@@ -0,0 +1,619 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This 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 software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.dependency.controller.test;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import junit.framework.Test;
+
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.dependency.spi.ControllerStateModel;
+
+/**
+ * A DisabledControllerTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ControllerStateModelTestCase extends AbstractDependencyTest
+{
+ final static int ITERATIONS = 1000000;
+
+ final static int NUM_STATES = 40;
+
+ final static StateFactory STATE_FACTORY = StateFactory.NEW_STATE;
+
+ public static Test suite()
+ {
+ return suite(ControllerStateModelTestCase.class);
+ }
+
+ public ControllerStateModelTestCase(String name)
+ {
+ super(name);
+ }
+
+
+
+ public void testExistingControllerStateModel() throws Exception
+ {
+ runControllerStateModelTest(ControllerStateModelFactory.EXISITING);
+ }
+
+ public void testMapControllerStateModel() throws Exception
+ {
+ runControllerStateModelTest(ControllerStateModelFactory.MAP);
+ }
+
+ private void runControllerStateModelTest(ControllerStateModelFactory factory)
+ {
+ ControllerStateModelAdapter model = factory.createControllerStateModel();
+
+ model.addState(ControllerState.DESCRIBED, null);
+ model.addState(ControllerState.INSTANTIATED, null);
+ model.addState(ControllerState.CONFIGURED, null);
+ model.addState(ControllerState.INSTALLED, null);
+ try
+ {
+ model.addState(ControllerState.START, ControllerState.NOT_INSTALLED);
+ fail("NOT_INSTALLED should not have been recognized");
+ }
+ catch(Exception expected)
+ {
+ }
+ model.addState(ControllerState.PRE_INSTALL, ControllerState.DESCRIBED);
+
+
+ assertEquals(ControllerState.DESCRIBED, model.getNextState(ControllerState.PRE_INSTALL));
+ assertEquals(ControllerState.INSTANTIATED, model.getNextState(ControllerState.DESCRIBED));
+ assertEquals(ControllerState.CONFIGURED, model.getNextState(ControllerState.INSTANTIATED));
+ assertEquals(ControllerState.INSTALLED, model.getNextState(ControllerState.CONFIGURED));
+ assertNull(model.getNextState(ControllerState.INSTALLED));
+ try
+ {
+ model.getNextState(ControllerState.NOT_INSTALLED);
+ fail("NOT_INSTALLED should not have been recognized");
+ }
+ catch(Exception expected)
+ {
+ }
+
+ assertEquals(ControllerState.CONFIGURED, model.getPreviousState(ControllerState.INSTALLED));
+ assertEquals(ControllerState.INSTANTIATED, model.getPreviousState(ControllerState.CONFIGURED));
+ assertEquals(ControllerState.DESCRIBED, model.getPreviousState(ControllerState.INSTANTIATED));
+ assertEquals(ControllerState.PRE_INSTALL, model.getPreviousState(ControllerState.DESCRIBED));
+ assertNull(model.getPreviousState(ControllerState.PRE_INSTALL));
+ try
+ {
+ model.getPreviousState(ControllerState.NOT_INSTALLED);
+ fail("NOT_INSTALLED should not have been recognized");
+ }
+ catch(Exception expected)
+ {
+ }
+
+ assertFalse(model.isBeforeState(ControllerState.PRE_INSTALL, ControllerState.PRE_INSTALL));
+ assertTrue(model.isBeforeState(ControllerState.PRE_INSTALL, ControllerState.DESCRIBED));
+ assertTrue(model.isBeforeState(ControllerState.PRE_INSTALL, ControllerState.INSTANTIATED));
+ assertTrue(model.isBeforeState(ControllerState.PRE_INSTALL, ControllerState.CONFIGURED));
+ assertTrue(model.isBeforeState(ControllerState.PRE_INSTALL, ControllerState.INSTALLED));
+ assertFalse(model.isAfterState(ControllerState.PRE_INSTALL, ControllerState.PRE_INSTALL));
+ assertFalse(model.isAfterState(ControllerState.PRE_INSTALL, ControllerState.DESCRIBED));
+ assertFalse(model.isAfterState(ControllerState.PRE_INSTALL, ControllerState.INSTANTIATED));
+ assertFalse(model.isAfterState(ControllerState.PRE_INSTALL, ControllerState.CONFIGURED));
+ assertFalse(model.isAfterState(ControllerState.PRE_INSTALL, ControllerState.INSTALLED));
+
+ assertFalse(model.isBeforeState(ControllerState.DESCRIBED, ControllerState.PRE_INSTALL));
+ assertFalse(model.isBeforeState(ControllerState.DESCRIBED, ControllerState.DESCRIBED));
+ assertTrue(model.isBeforeState(ControllerState.DESCRIBED, ControllerState.INSTANTIATED));
+ assertTrue(model.isBeforeState(ControllerState.DESCRIBED, ControllerState.CONFIGURED));
+ assertTrue(model.isBeforeState(ControllerState.DESCRIBED, ControllerState.INSTALLED));
+ assertTrue(model.isAfterState(ControllerState.DESCRIBED, ControllerState.PRE_INSTALL));
+ assertFalse(model.isAfterState(ControllerState.DESCRIBED, ControllerState.DESCRIBED));
+ assertFalse(model.isAfterState(ControllerState.DESCRIBED, ControllerState.INSTANTIATED));
+ assertFalse(model.isAfterState(ControllerState.DESCRIBED, ControllerState.CONFIGURED));
+ assertFalse(model.isAfterState(ControllerState.DESCRIBED, ControllerState.INSTALLED));
+
+ assertFalse(model.isBeforeState(ControllerState.INSTANTIATED, ControllerState.PRE_INSTALL));
+ assertFalse(model.isBeforeState(ControllerState.INSTANTIATED, ControllerState.DESCRIBED));
+ assertFalse(model.isBeforeState(ControllerState.INSTANTIATED, ControllerState.INSTANTIATED));
+ assertTrue(model.isBeforeState(ControllerState.INSTANTIATED, ControllerState.CONFIGURED));
+ assertTrue(model.isBeforeState(ControllerState.INSTANTIATED, ControllerState.INSTALLED));
+ assertTrue(model.isAfterState(ControllerState.INSTANTIATED, ControllerState.PRE_INSTALL));
+ assertTrue(model.isAfterState(ControllerState.INSTANTIATED, ControllerState.DESCRIBED));
+ assertFalse(model.isAfterState(ControllerState.INSTANTIATED, ControllerState.INSTANTIATED));
+ assertFalse(model.isAfterState(ControllerState.INSTANTIATED, ControllerState.CONFIGURED));
+ assertFalse(model.isAfterState(ControllerState.INSTANTIATED, ControllerState.INSTALLED));
+
+ assertFalse(model.isBeforeState(ControllerState.CONFIGURED, ControllerState.PRE_INSTALL));
+ assertFalse(model.isBeforeState(ControllerState.CONFIGURED, ControllerState.DESCRIBED));
+ assertFalse(model.isBeforeState(ControllerState.CONFIGURED, ControllerState.INSTANTIATED));
+ assertFalse(model.isBeforeState(ControllerState.CONFIGURED, ControllerState.CONFIGURED));
+ assertTrue(model.isBeforeState(ControllerState.CONFIGURED, ControllerState.INSTALLED));
+ assertTrue(model.isAfterState(ControllerState.CONFIGURED, ControllerState.PRE_INSTALL));
+ assertTrue(model.isAfterState(ControllerState.CONFIGURED, ControllerState.DESCRIBED));
+ assertTrue(model.isAfterState(ControllerState.CONFIGURED, ControllerState.INSTANTIATED));
+ assertFalse(model.isAfterState(ControllerState.CONFIGURED, ControllerState.CONFIGURED));
+ assertFalse(model.isAfterState(ControllerState.CONFIGURED, ControllerState.INSTALLED));
+
+
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.PRE_INSTALL));
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.DESCRIBED));
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.INSTANTIATED));
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.CONFIGURED));
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.INSTALLED));
+ assertTrue(model.isAfterState(ControllerState.INSTALLED, ControllerState.PRE_INSTALL));
+ assertTrue(model.isAfterState(ControllerState.INSTALLED, ControllerState.DESCRIBED));
+ assertTrue(model.isAfterState(ControllerState.INSTALLED, ControllerState.INSTANTIATED));
+ assertTrue(model.isAfterState(ControllerState.INSTALLED, ControllerState.CONFIGURED));
+ assertFalse(model.isAfterState(ControllerState.INSTALLED, ControllerState.INSTALLED));
+
+ //Check before/after with one non-existant state
+ assertTrue(model.isAfterState(ControllerState.INSTALLED, ControllerState.NOT_INSTALLED));
+ assertFalse(model.isAfterState(ControllerState.NOT_INSTALLED, ControllerState.INSTALLED));
+ assertFalse(model.isAfterState(ControllerState.NOT_INSTALLED, ControllerState.NOT_INSTALLED));
+ assertFalse(model.isBeforeState(ControllerState.INSTALLED, ControllerState.NOT_INSTALLED));
+ assertFalse(model.isBeforeState(ControllerState.NOT_INSTALLED, ControllerState.NOT_INSTALLED));
+ assertTrue(model.isBeforeState(ControllerState.NOT_INSTALLED, ControllerState.INSTALLED));
+
+ ControllerState[] states = new ControllerState[] {
+ ControllerState.PRE_INSTALL, ControllerState.DESCRIBED,
+ ControllerState.INSTANTIATED, ControllerState.CONFIGURED, ControllerState.INSTALLED};
+
+ //Test the iterators
+ int i = 0;
+ for (ControllerState state : model)
+ {
+ assertEquals(states[i++], state);
+ }
+
+ //TODO test list iterator
+ }
+
+
+ public void testBenchmarkExistingControllerStateModel() throws Throwable
+ {
+ benchmarkControllerStateModel(ControllerStateModelFactory.EXISITING);
+ }
+
+ public void testBenchmarkMapControllerStateModel() throws Throwable
+ {
+ benchmarkControllerStateModel(ControllerStateModelFactory.MAP);
+ }
+
+ public void benchmarkControllerStateModel(ControllerStateModelFactory factory) throws Throwable
+ {
+ int counter = 0;
+
+ ControllerStateModelAdapter model = factory.createControllerStateModel();
+
+ ControllerState[] states = new ControllerState[NUM_STATES];
+ for (int i = 0 ; i < NUM_STATES ; i++)
+ {
+ states[i] = new ControllerState("State" + i);
+ model.addState(states[i], null);
+ }
+
+ //Just make sure things were added
+ assertEquals(new ControllerState("State" + (NUM_STATES - 2)), model.getPreviousState(new ControllerState("State" + (NUM_STATES -1))));
+
+ ControllerState[] input = new ControllerState[ITERATIONS];
+ for (int i = 0 ; i < ITERATIONS ; i++)
+ {
+ int stateIndex = (int)Math.round(Math.random() * (NUM_STATES - 1));
+ input[i] = STATE_FACTORY.getState(states[stateIndex].getStateString());
+ }
+
+ System.out.println("========= Starting benchmark =========================");
+ System.out.println("Iterations: " + ITERATIONS);
+ System.out.println("States: " + NUM_STATES);
+ System.out.println("State Model Factory: " + factory);
+ System.out.println("State Factory: " + STATE_FACTORY);
+ System.out.println("=====================================================");
+
+ long start = System.currentTimeMillis();
+
+ for (int i = 1 ; i < ITERATIONS - 2 ; i++)
+ {
+ ControllerState state1 = model.isAfterState(input[i], input[i-1]) ? input[i] : input[i-1];
+ ControllerState state2 = model.isBeforeState(input[i], input[i+1]) ? input[i] : input[i+1];
+ state1 = model.getNextState(state1);
+ state2 = model.getPreviousState(state2);
+ if (state1 != null && state2 != null)
+ counter++;
+ else
+ counter--;
+ }
+ System.out.println("========= Test took " + (System.currentTimeMillis() - start) + " ms");
+ System.out.println(counter);
+ }
+
+
+ private interface StateFactory
+ {
+ ControllerState getState(String state);
+
+ StateFactory NEW_STATE = new StateFactory()
+ {
+ public ControllerState getState(String state)
+ {
+ return new ControllerState(state);
+ }
+
+ @Override
+ public String toString()
+ {
+ return "New States";
+ }
+ };
+
+
+ StateFactory CACHED_STATE = new StateFactory()
+ {
+ private Map<String, ControllerState> cachedStates = new HashMap<String, ControllerState>();
+
+ public ControllerState getState(String state)
+ {
+ ControllerState cached = cachedStates.get(state);
+ if (cached == null)
+ {
+ cached = new ControllerState(state);
+ cachedStates.put(state, cached);
+ }
+
+ return cached;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Cached states";
+ }
+ };
+ }
+
+ private interface ControllerStateModelFactory
+ {
+ ControllerStateModelAdapter createControllerStateModel();
+
+ ControllerStateModelFactory EXISITING = new ControllerStateModelFactory()
+ {
+ public ControllerStateModelAdapter createControllerStateModel()
+ {
+ return new ExistingControllerStateModel();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Abstract Controller (List)";
+ }
+ };
+
+ ControllerStateModelFactory MAP = new ControllerStateModelFactory()
+ {
+ public ControllerStateModelAdapter createControllerStateModel()
+ {
+ return new MapControllerStateModel();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Map ControllerStateModel";
+ }
+
+ };
+ }
+
+ private abstract static class ControllerStateModelAdapter implements ControllerStateModel
+ {
+ public abstract void addState(ControllerState state, ControllerState before);
+ }
+
+ private static class ExistingControllerStateModel extends ControllerStateModelAdapter
+ {
+ /** The states in order List<ControllerState> */
+ private List<ControllerState> states = new CopyOnWriteArrayList<ControllerState>();
+
+ public void addState(ControllerState state, ControllerState before)
+ {
+ if (states.contains(state))
+ return;
+
+ if (before == null)
+ {
+ states.add(state);
+ }
+ else
+ {
+ states.add(getStateIndex(before), state);
+ }
+ }
+
+ protected int getStateIndex(ControllerState state)
+ {
+ return getStateIndex(state, false);
+ }
+
+ protected int getStateIndex(ControllerState state, boolean allowNotFound)
+ {
+ if (state == null)
+ throw new IllegalArgumentException("Null state");
+
+ int stateIndex = states.indexOf(state);
+ if (stateIndex < 0 && allowNotFound == false)
+ throw new IllegalArgumentException("No such state " + state + " in states " + states);
+
+ return stateIndex;
+ }
+
+ protected ControllerState getState(int index)
+ {
+ if (index < 0 || index >= states.size())
+ return null;
+ else
+ return states.get(index);
+ }
+
+ public ControllerState getPreviousState(ControllerState state)
+ {
+ return getState(getStateIndex(state) - 1);
+ }
+
+ public ControllerState getNextState(ControllerState state)
+ {
+ return getState(getStateIndex(state) + 1);
+ }
+
+ public boolean isBeforeState(ControllerState state, ControllerState reference)
+ {
+ int stateIndex = getStateIndex(state, true);
+ int referenceIndex = getStateIndex(reference, true);
+ return stateIndex < referenceIndex;
+ }
+
+ public boolean isAfterState(ControllerState state, ControllerState reference)
+ {
+ int stateIndex = getStateIndex(state, true);
+ int referenceIndex = getStateIndex(reference, true);
+ return stateIndex > referenceIndex;
+ }
+
+ public Iterator<ControllerState> iterator()
+ {
+ return states.iterator();
+ }
+
+ public ListIterator<ControllerState> listIteraror()
+ {
+ return states.listIterator(states.size() - 1);
+ }
+ }
+
+
+ private static class MapControllerStateModel extends ControllerStateModelAdapter
+ {
+ ControllerStateWrapper first;
+ ControllerStateWrapper last;
+ private Map<ControllerState, ControllerStateWrapper> states = new HashMap<ControllerState, ControllerStateWrapper>();
+
+ public void addState(ControllerState state, ControllerState before)
+ {
+ if (states.containsKey(state))
+ return;
+
+ if (before == null)
+ {
+ ControllerStateWrapper newState = new ControllerStateWrapper(state);
+ ControllerStateWrapper previous = last;
+ if (previous != null)
+ {
+ newState.setIndex(previous.getIndex() + 1);
+ previous.setAfter(newState);
+ newState.setBefore(previous);
+ }
+ else
+ {
+ newState.setIndex(0);
+ }
+ last = newState;
+ states.put(state, newState);
+ }
+ else
+ {
+ ControllerStateWrapper next = getState(before);
+ if (next == null)
+ throw new IllegalArgumentException("No such state " + state + " in states " + states);
+
+ ControllerStateWrapper newState = new ControllerStateWrapper(state);
+ newState.setIndex(next.getIndex());
+ newState.setAfter(next);
+ newState.setBefore(next.getBefore());
+ next.setBefore(newState);
+ if (newState.getBefore() == null)
+ first = newState;
+
+ while (next != null)
+ {
+ next.incrementIndex();
+ next = next.getAfter();
+ }
+
+ states.put(state, newState);
+ }
+ }
+
+
+ protected ControllerStateWrapper getState(ControllerState state)
+ {
+ return getState(state, false);
+ }
+
+ protected ControllerStateWrapper getState(ControllerState state, boolean allowNotFound)
+ {
+ if (state == null)
+ throw new IllegalArgumentException("Null state");
+
+ ControllerStateWrapper found = states.get(state);
+ if (found == null && !allowNotFound)
+ throw new IllegalArgumentException("No such state " + state + " in states " + states);
+
+ return found;
+ }
+
+ protected int getStateIndex(ControllerState state)
+ {
+ return getStateIndex(state, false);
+ }
+
+ protected int getStateIndex(ControllerState state, boolean allowNotFound)
+ {
+ ControllerStateWrapper stateWrapper = getState(state, allowNotFound);
+ return stateWrapper == null ? -1 : stateWrapper.getIndex();
+ }
+
+ public ControllerState getPreviousState(ControllerState state)
+ {
+ ControllerStateWrapper previous = getState(state).getBefore();
+ return previous == null ? null : previous.getState();
+ }
+
+ public ControllerState getNextState(ControllerState state)
+ {
+ ControllerStateWrapper next = getState(state).getAfter();
+ return next == null ? null : next.getState();
+ }
+
+ public boolean isBeforeState(ControllerState state, ControllerState reference)
+ {
+ int stateIndex = getStateIndex(state, true);
+ int referenceIndex = getStateIndex(reference, true);
+ return stateIndex < referenceIndex;
+ }
+
+ public boolean isAfterState(ControllerState state, ControllerState reference)
+ {
+ int stateIndex = getStateIndex(state, true);
+ int referenceIndex = getStateIndex(reference, true);
+ return stateIndex > referenceIndex;
+ }
+
+ public Iterator<ControllerState> iterator()
+ {
+ return new StateIterator(first);
+ }
+
+ public ListIterator<ControllerState> listIteraror()
+ {
+ return null;//states.listIterator(states.size() - 1);
+ }
+
+ private static class StateIterator implements Iterator<ControllerState>
+ {
+ ControllerStateWrapper current;
+
+ public StateIterator(ControllerStateWrapper current)
+ {
+ this.current = current;
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException("Remove not allowed on ControllerStateModel");
+ }
+
+ public ControllerState next()
+ {
+ if (current == null)
+ throw new NoSuchElementException();
+ ControllerState state = current.getState();
+ current = current.getAfter();
+ return state;
+ }
+
+ public boolean hasNext()
+ {
+ return current != null;
+ }
+ }
+
+ private static class ControllerStateWrapper
+ {
+ final ControllerState state;
+ int index;
+ ControllerStateWrapper before;
+ ControllerStateWrapper after;
+
+ public ControllerStateWrapper(ControllerState state)
+ {
+ this.state = state;
+ }
+
+ public int getIndex()
+ {
+ return index;
+ }
+
+ public void setIndex(int index)
+ {
+ this.index = index;
+ }
+
+ public void incrementIndex()
+ {
+ this.index++;
+ }
+
+ public ControllerStateWrapper getBefore()
+ {
+ return before;
+ }
+
+ public void setBefore(ControllerStateWrapper before)
+ {
+ this.before = before;
+ }
+
+ public ControllerStateWrapper getAfter()
+ {
+ return after;
+ }
+
+ public void setAfter(ControllerStateWrapper after)
+ {
+ this.after = after;
+ }
+
+ public ControllerState getState()
+ {
+ return state;
+ }
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list