Looks like I broke the NetUI BVTs....Funny...There isn't any AJAX tests in the DRTs obviously...
I'll have it fixed in a bit. On 2/21/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > Author: dolander > Date: Tue Feb 21 13:14:05 2006 > New Revision: 379589 > > URL: http://svn.apache.org/viewcvs?rev=379589&view=rev > Log: > This change adds an event interface to the NameService. The event is > called when an object is > added to the NameService for tracking. The NameService supports adding > and removing NamingObjectListeners. > > In addition, the NameService also supports an attribute map that can be > obtained for a named object > that is being tracked by the NameService. This allows a Listener to > associate data with the object > being tracked. > > I also added a full set of jUnit tests which test the name service. > > > > Added: > > > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/LazyMap.java > > > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NamingObjectListener.java > > > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/ > > > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceListenerTest.java > > > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java > > > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameableObject.java > Modified: > > > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java > beehive/trunk/netui/test/ant/junitCore.xml > > Added: > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/LazyMap.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/LazyMap.java?rev=379589&view=auto > > ============================================================================== > --- > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/LazyMap.java > (added) > +++ > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/LazyMap.java > Tue Feb 21 13:14:05 2006 > @@ -0,0 +1,115 @@ > +/* > + * Copyright 2006 The Apache Software Foundation. > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + * > + * $Header:$ > + */ > +package org.apache.beehive.netui.pageflow.requeststate; > + > +import java.util.Map; > +import java.util.Set; > +import java.util.Collection; > +import java.util.HashMap; > + > +/** > + * This class implements the <code>java.util.Map</code> interface in a > lazy way. It will delegate to the > + * <code>java.util.HashMap</code> for the real implementation. The > HashMap will only be created when it > + * certain methods are called such as <code>put</code>, <code>get</code>, > etc. > + */ > +class LazyMap implements Map > +{ > + private HashMap _map; > + > + final boolean isMapCreated() { > + return (_map != null); > + } > + > + ///************************* This is the MAP interface > implementation ************************* > + > + final public int size() { > + return (_map == null) ? 0 : _map.size(); > + } > + > + final public boolean isEmpty() { > + return (_map == null) || _map.isEmpty(); > + } > + > + final public boolean containsKey(Object key) { > + return (_map != null) && _map.containsKey(key); > + } > + > + final public boolean containsValue(Object value) { > + return (_map != null) && _map.containsValue(value); > + } > + > + final public Object get(Object key) { > + Map map = getMap(); > + return map.get(key); > + } > + > + final public Object put(Object key, Object value) { > + Map map = getMap(); > + return map.put(key,value); > + } > + > + final public Object remove(Object key) { > + Map map = getMap(); > + return map.remove(key); > + } > + > + final public void putAll(Map t) { > + Map map = getMap(); > + map.putAll(t); > + } > + > + final public void clear() { > + if (_map != null) > + _map.clear(); > + } > + > + final public Set keySet() { > + Map map = getMap(); > + return map.keySet(); > + } > + > + final public Collection values() { > + Map map = getMap(); > + return map.values(); > + } > + > + final public Set entrySet() { > + Map map = getMap(); > + return map.entrySet(); > + } > + > + /****************************************** End Map Interface > ********************************************** > + > + /** > + * This method will return the map. If the map hasn't been created > + * it will be created. This is done in a thread safe way. > + * @return the map implementation. > + */ > + private Map getMap() > + { > + if (_map != null) > + return _map; > + > + synchronized (this) { > + if (_map != null) > + return _map; > + _map = new HashMap(); > + } > + return _map; > + } > +} > > Modified: > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java?rev=379589&r1=379588&r2=379589&view=diff > > ============================================================================== > --- > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java > (original) > +++ > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java > Tue Feb 21 13:14:05 2006 > @@ -20,22 +20,29 @@ > import javax.servlet.http.HttpSession; > import java.lang.ref.WeakReference; > import java.util.HashMap; > +import java.util.ArrayList; > +import java.util.Map; > > /** > - * > + * This class implements a service that will name and track objects which > implement the > + * <code>INameable</code> interface. The typical use of this class is in > the XmlHttpRequest > + * request processing to lookup the object that should handle the > request. > */ > -public class NameService > +final public class NameService > { > private static final String NAME_SERVICE = "netui.nameService"; > > - private HashMap/*<String,WeakReference>*/ _nameMap; > + // static value for situation where this is not stored in the > session. > + private static NameService _nameService; > + > + final private HashMap/*<String,WeakReference>*/ _nameMap = new > HashMap(); > private int _nextValue; > + private ArrayList _listeners; > > /** > * private constructor allowing for a factory method to access > NameService objects. > */ > private NameService() { > - _nameMap = new HashMap/*<String,WeakReference>*/(); > _nextValue = 0; > } > > @@ -47,6 +54,9 @@ > */ > public static NameService instance(HttpSession session) > { > + if (session == null) > + throw new IllegalArgumentException("Session must not be > null"); > + > // synchronize on the session so we only create a single > NameService > // within the session. > synchronized (session) { > @@ -55,12 +65,59 @@ > nameService = new NameService(); > session.setAttribute(NAME_SERVICE,nameService); > } > - assert(nameService != null) : "Found invalid null name > service"; > return nameService; > } > } > > /** > + * This will return a create a static name service. This is used > mainly to test the Name service > + * class. > + * @return The statically scoped <code>NameService</code> > + */ > + public static synchronized NameService staticInstance() > + { > + if (_nameService == null) > + _nameService = new NameService(); > + return _nameService; > + } > + > + /** > + * This method will add a <code>NamingObjectListener</code> to the > set of listeners for the NamingObject event. > + * @param nol The <code>NamingObjectListener</code> to add as a > listener. This must not be null. > + * @throws IllegalArgumentException when nol is null. > + */ > + public void addNamingObjectListener(NamingObjectListener nol) { > + if (nol == null) > + throw new IllegalArgumentException("The NameingObjectListener > must not be null"); > + > + ArrayList listener = getListener(); > + > + synchronized(listener) { > + if (listener.contains(nol)) > + return; > + listener.add(nol); > + } > + } > + > + /** > + * This method will remove a <code>NamingObjectListener</code> from > the set of listeners. If the > + * It is safe to call this if the NamingObjectListener hasn't been > added to the listener list. > + * @param nol The <code>NamingObjectListener</code> to remove as a > listener. This must not be null. > + * @throws IllegalArgumentException when nol is null. > + */ > + public void removeNamingObjectListener(NamingObjectListener nol) { > + if (nol == null) > + throw new IllegalArgumentException("The NameingObjectListener > must not be null"); > + > + ArrayList listener = getListener(); > + synchronized(listener) { > + if (!listener.contains(nol)) > + return; > + listener.remove(nol); > + } > + } > + > + /** > * This method will create a unique name for an INameable > object. The name > * will be unque within the session. This will throw an > IllegalStateException > * if INameable.setObjectName has previously been called on object. > @@ -77,15 +134,17 @@ > /** > * This is a debug method that will set the next integer value. This > is used > * so tests can force the name. > - * @param val > + * @param val the integer value that will be forced to be the next > value. > */ > public void debugSetNameIntValue(int val) { > _nextValue = val; > } > > /** > - * > - * @param object > + * This method will store an INameable object into the > <code>NameService</code>. The name > + * is obtained from the INameable. The object will be stored in the > <code>NameService</code> > + * with a <code>WeakReference</code> so the <code>NameService</code> > will not keep an object alive. > + * @param object The <code>INameable</code> to be stored in the name > service. > */ > public void put(INameable object) { > if (object == null) > @@ -93,25 +152,128 @@ > String name = object.getObjectName(); > if (name == null) > throw new IllegalStateException("object has not been named"); > + TrackingObject to = new TrackingObject(); > + to.setINameable(object); > > - _nameMap.put(name,new WeakReference(object)); > + _nameMap.put(name,new WeakReference(to)); > + > + // fire the fact that we just added a nameable to be tracked > + if (_listeners != null) > + fireNamingObjectEvent(to); > } > > /** > - * > - * @param name > - * @return INameable > + * Given the name, return the <code>INameable</code> object stored by > the <code>NameService</code>. Objects > + * are stored in the <code>NameService</code> using > <code>WeakReference</code>s so this will not keep an object > + * alive. If the object is not found or has been reclaimed, this > method will return null. > + * @param name The name of the object to get from the > <code>NameService</code> > + * @return INameable If the named object is stored by the name > service, it will be returned otherwise > + * <code>null</code> is returned. > */ > public INameable get(String name) { > if (name == null) > throw new IllegalStateException("name must not be null"); > + > + WeakReference wr = (WeakReference) _nameMap.get(name); > + > + // The object wasn't found > + if (wr == null) > + return null; > + > + // If the object has been reclaimed, then we remove the named > object from the map. > + TrackingObject to = (TrackingObject) wr.get(); > + if (to == null) { > + _nameMap.remove(name); > + return null; > + } > + return to.getINameable(); > + } > + > + /** > + * This method will return the state map associated with the Nameable > object if the > + * object has been stored in the <code>NameService</code> and > something has been stored > + * into the <code>Map</code>. Otherwise this will return null > indicating that the map > + * is empty. If the <code>create</code> parameter is true, we will > always return the > + * <code>Map</code> object. > + * @param name The name of the object to return the named > object. This must not be null. > + * @param create This will create the map if necessary. > + * @return A Map Object for the named object. This will return null > if nothing has been stored in > + * the map and <code>create</code> is false. > + */ > + public Map getMap(String name, boolean create) { > + if (name == null) > + throw new IllegalStateException("name must not be null"); > + > WeakReference wr = (WeakReference) _nameMap.get(name); > + > + // The object wasn't found > if (wr == null) > return null; > - INameable object = (INameable) wr.get(); > - if (object == null) { > + > + // If the object has been reclaimed, then we remove the named > object from the map. > + TrackingObject to = (TrackingObject) wr.get(); > + if (to == null) { > _nameMap.remove(name); > + return null; > + } > + if (create) > + return to; > + > + return to.isMapCreated() ? to : null; > + } > + > + /** > + * This method will fire the NamingObject event. This event is > triggered when > + * we have added an element to be tracked. > + * @param to The <code>TrackingObject</code> that acts as the Map and > also contains the INameable. > + */ > + private void fireNamingObjectEvent(TrackingObject to) > + { > + Object[] copy; > + if (_listeners == null) > + return; > + > + // create a copy of the listeners so that there isn't any > modifications while we > + // fire the events. > + synchronized (_listeners) { copy = _listeners.toArray(); } > + > + for (int i = 0; i < copy.length; i++) { > + ((NamingObjectListener)copy[i]).namingObject(to.getINameable(), > to); > + } > + } > + > + /** > + * This method will return the event listener <code>ArrayList</code> > when called. The event listener is > + * lazily created. > + * @return The listener array list. > + */ > + private ArrayList getListener() > + { > + if (_listeners != null) > + return _listeners; > + synchronized (this) { > + if (_listeners != null) > + return _listeners; > + _listeners = new ArrayList(); > + } > + return _listeners; > + } > + > + final private class TrackingObject extends LazyMap > + { > + private INameable _nameable; > + > + public Map getMap() { > + return (isMapCreated() ? this : null); > + } > + > + public void setINameable(INameable nameable) > + { > + _nameable = nameable; > + } > + > + public INameable getINameable() { > + return _nameable; > } > - return object; > } > } > > Added: > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NamingObjectListener.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NamingObjectListener.java?rev=379589&view=auto > > ============================================================================== > --- > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NamingObjectListener.java > (added) > +++ > beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NamingObjectListener.java > Tue Feb 21 13:14:05 2006 > @@ -0,0 +1,40 @@ > +/* > + * Copyright 2006 The Apache Software Foundation. > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + * > + * $Header:$ > + */ > +package org.apache.beehive.netui.pageflow.requeststate; > + > +import java.util.EventListener; > +import java.util.Map; > + > +/** > + * The listener interface for receiving naming events from the > NameService. The class that is interested in > + * processing a <code>NamingObject</code> event must implement this > interface. The object is registered with the <code>NameService</code> > + * using the <code>addNamingObjectListener</code> method. When an object > is named, the <code>namingObject</code> method is > + * called. > + */ > +public interface NamingObjectListener extends EventListener > +{ > + /** > + * This method is called when an object is registered with the > <code>NameService</code> to listen to > + * <code>NamingObject</code> events. The > <code>objectAttributes</code> map is passed allowing the object > + * to store information in the object. The Map is held by a weak > reference by the NameService so it > + * will be released when the <code>namedObject</code> is released. > + * @param namedObject The object that will be named. > + * @param objectAttributes A map that associates data with the data. > + */ > + void namingObject(INameable namedObject, Map objectAttributes); > +} > > Modified: beehive/trunk/netui/test/ant/junitCore.xml > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/test/ant/junitCore.xml?rev=379589&r1=379588&r2=379589&view=diff > > ============================================================================== > --- beehive/trunk/netui/test/ant/junitCore.xml (original) > +++ beehive/trunk/netui/test/ant/junitCore.xml Tue Feb 21 13:14:05 2006 > @@ -60,6 +60,7 @@ > <include > name="org/apache/beehive/netui/test/script/el/**/*Test.class"/> > <include > name="org/apache/beehive/netui/test/pageflow/**/*Test.class"/> > <include > name="org/apache/beehive/netui/test/datagrid/**Test.class"/> > + <include > name="org/apache/beehive/netui/test/nameservice/**Test.class"/> > </fileset> > </batchtest> > <test name=" > org.apache.beehive.netui.test.databinding.expression.IndexedNameTest" > todir="${testout.dir}"/> > > Added: > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceListenerTest.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceListenerTest.java?rev=379589&view=auto > > ============================================================================== > --- > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceListenerTest.java > (added) > +++ > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceListenerTest.java > Tue Feb 21 13:14:05 2006 > @@ -0,0 +1,212 @@ > +/* > + * Copyright 2006 The Apache Software Foundation. > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + * > + * $Header:$ > + */ > +package org.apache.beehive.netui.test.nameservice; > + > +import junit.framework.TestCase; > +import junit.framework.Test; > +import junit.framework.TestSuite; > +import org.apache.beehive.netui.pageflow.requeststate.NameService; > +import > org.apache.beehive.netui.pageflow.requeststate.NamingObjectListener; > +import org.apache.beehive.netui.pageflow.requeststate.INameable; > + > +import java.util.Map; > + > +/** > + */ > +public class NameServiceListenerTest extends TestCase > +{ > + public NameServiceListenerTest(String name) { > + super(name); > + } > + > + public static Test suite() { > + return new TestSuite(NameServiceListenerTest.class); > + } > + > + public void testNamingListeners() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + // add the listener > + ListenerObject lo = new ListenerObject(); > + ns.addNamingObjectListener(lo); > + > + // remove the listener > + ns.removeNamingObjectListener(lo); > + > + // the listener may be removed twice > + ns.removeNamingObjectListener(lo); > + } > + > + public void testNamingListenersEvents() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + ListenerObject lo1 = new ListenerObject(); > + ns.addNamingObjectListener(lo1); > + > + // now add a nameable object and verify that the listener is > called > + NameableObject no1 = new NameableObject(); > + ns.nameObject("Nameable",no1); > + ns.put(no1); > + > + assertTrue("NameListener [1] = 1 (" + lo1.getEventCount() + ")", > lo1.getEventCount() == 1); > + > + // now add a nameable object and verify that the listener is > called > + NameableObject no2 = new NameableObject(); > + ns.nameObject("Nameable",no2); > + ns.put(no2); > + > + assertTrue("NameListener [1] = 2 (" + lo1.getEventCount() + ")", > lo1.getEventCount() == 2); > + > + // add another listener and verify that both are called > + ListenerObject lo2 = new ListenerObject(); > + ns.addNamingObjectListener(lo2); > + > + // now add a nameable object and verify that the listener is > called > + NameableObject no3 = new NameableObject(); > + ns.nameObject("Nameable",no3); > + ns.put(no3); > + > + assertTrue("NameListener [1] = 3 (" + lo1.getEventCount() + ")", > lo1.getEventCount() == 3); > + assertTrue("NameListener [2] = 1 (" + lo1.getEventCount() + ")", > lo2.getEventCount() == 1); > + > + // remove the first listender and verify that things work > correctly > + ns.removeNamingObjectListener(lo1); > + > + // now add a nameable object and verify that the listener is > called > + NameableObject no4 = new NameableObject(); > + ns.nameObject("Nameable",no4); > + ns.put(no4); > + > + assertTrue("NameListener [1] = 3 (" + lo1.getEventCount() + ")", > lo1.getEventCount() == 3); > + assertTrue("NameListener [2] = 2 (" + lo1.getEventCount() + ")", > lo2.getEventCount() == 2); > + > + // remove the second listender and verify that things work > correctly > + ns.removeNamingObjectListener(lo2); > + > + // now add a nameable object and verify that the listener is > called > + NameableObject no5 = new NameableObject(); > + ns.nameObject("Nameable",no5); > + ns.put(no5); > + > + assertTrue("NameListener [1] = 3 (" + lo1.getEventCount() + ")", > lo1.getEventCount() == 3); > + assertTrue("NameListener [2] = 2 (" + lo1.getEventCount() + ")", > lo2.getEventCount() == 2); > + } > + > + public void testApplingStateToNamedObject() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + StatefulListenerObject listner = new StatefulListenerObject(); > + ns.addNamingObjectListener(listner); > + > + try { > + NameableObject no = new NameableObject(); > + ns.nameObject("Nameable",no); > + ns.put(no); > + > + Map map = ns.getMap(no.getObjectName(), false); > + assertNotNull("The returned Map was null", map); > + > + // verify that there was state stored in the map by the > listener > + Object o = map.get(StatefulListenerObject.STATE_NAME); > + assertNotNull("The state was not found", o); > + assertTrue("The state was not the nameable's name", > no.getObjectName().equals(o)); > + } > + finally { > + ns.removeNamingObjectListener(listner); > + } > + } > + > + public void testApplingStateToNamedObject2() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + NameableObject no = new NameableObject(); > + ns.nameObject("Nameable",no); > + ns.put(no); > + > + Map map = ns.getMap(no.getObjectName(), false); > + assertNull("The returned Map was not null", map); > + > + map = ns.getMap(no.getObjectName(), true); > + assertNotNull("The returned Map was null when create is true", > map); > + > + // place something into the map > + map.put("x","x"); > + > + // now that the map has something in it, it should return > + map = ns.getMap(no.getObjectName(), false); > + assertNotNull("The returned Map was null", map); > + > + Object o = map.get("x"); > + assertNotNull("The state was not found", o); > + assertTrue("The state was not the set state", "x".equals(o)); > + } > + > + public void testNameServiceListenerErrors() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + boolean error = true; > + > + // cannot add a null listener object > + try { > + ns.addNamingObjectListener(null); > + } > + catch (IllegalArgumentException e) { > + error = false; > + } > + assertFalse("Able to add a null listener object",error); > + > + // cannot remove a null listener object > + try { > + ns.removeNamingObjectListener(null); > + } > + catch (IllegalArgumentException e) { > + error = false; > + } > + assertFalse("Able to remove a null listener object",error); > + } > + > + static class ListenerObject implements NamingObjectListener > + { > + private int _cnt = 0; > + public void namingObject(INameable iNameable, Map map) { > + _cnt++; > + } > + > + public int getEventCount() { > + return _cnt; > + } > + } > + > + static class StatefulListenerObject implements NamingObjectListener > + { > + public static String STATE_NAME = "StatefulListenerObject.state"; > + public void namingObject(INameable iNameable, Map map) { > + map.put(STATE_NAME, iNameable.getObjectName()); > + } > + } > +} > > Added: > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java?rev=379589&view=auto > > ============================================================================== > --- > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java > (added) > +++ > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java > Tue Feb 21 13:14:05 2006 > @@ -0,0 +1,108 @@ > +/* > + * Copyright 2006 The Apache Software Foundation. > + * > + * Licensed under the Apache License, Version 2.0 (the "License"); > + * you may not use this file except in compliance with the License. > + * You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + * > + * $Header:$ > + */ > +package org.apache.beehive.netui.test.nameservice; > + > +import junit.framework.Test; > +import junit.framework.TestCase; > +import junit.framework.TestSuite; > +import org.apache.beehive.netui.pageflow.requeststate.INameable; > +import org.apache.beehive.netui.pageflow.requeststate.NameService; > + > +public class NameServiceTest extends TestCase > +{ > + public NameServiceTest(String name) { > + super(name); > + } > + > + public static Test suite() { > + return new TestSuite(NameServiceTest.class); > + } > + > + public void testNameServiceNaming() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + NameableObject no1 = new NameableObject(); > + ns.nameObject("NameObject",no1); > + assertNotNull("The Name was returned as null", no1.getObjectName > ()); > + > + > + NameableObject no2 = new NameableObject(); > + ns.nameObject("NameObject",no2); > + assertNotNull("The Name was returned as null", no2.getObjectName > ()); > + > + assertFalse("The name of the named objects cannot be the same", > + no1.getObjectName().equals(no2.getObjectName())); > + } > + > + public void testNameServicePutAndGet() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + // create a nameable object and the name it. > + NameableObject no1 = new NameableObject(); > + ns.nameObject("Nameable",no1); > + ns.put(no1); > + > + // get the object back and verify that it was the same object > + INameable nameable = ns.get(no1.getObjectName()); > + assertEquals(no1,nameable); > + } > + > + > + public void testNameServiceErrors() > + { > + NameService ns = NameService.staticInstance(); > + assertNotNull("The NameService was returned as null", ns); > + > + boolean error = true; > + // you cannot add an object without it having a name > + try { > + NameableObject no1 = new NameableObject(); > + ns.put(no1); > + } > + catch (IllegalStateException e) { > + error = false; > + } > + assertFalse("Able to add INameable without naming it",error); > + > + // you cannot put an object without providing the object > + error = true; > + try { > + ns.put(null); > + } > + catch (IllegalStateException e) { > + error = false; > + } > + assertFalse("Didn't fail the put when we passed a null > object",error); > + > + > + // you cannot get an object without providing a name > + error = true; > + try { > + ns.get(null); > + } > + catch (IllegalStateException e) { > + error = false; > + } > + assertFalse("Didn't fail the get when we passed a null > string",error); > + > + } > +} > > Added: > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameableObject.java > URL: > http://svn.apache.org/viewcvs/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameableObject.java?rev=379589&view=auto > > ============================================================================== > --- > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameableObject.java > (added) > +++ > beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameableObject.java > Tue Feb 21 13:14:05 2006 > @@ -0,0 +1,18 @@ > +package org.apache.beehive.netui.test.nameservice; > + > +import org.apache.beehive.netui.pageflow.requeststate.INameable; > + > +public class NameableObject implements INameable { > + private String _name; > + > + public void setObjectName(String name) { > + if (_name != null) { > + throw new IllegalStateException("Attempt to set the > ObjectName twice"); > + } > + _name = name; > + } > + > + public String getObjectName() { > + return _name; > + } > + } > > >
