[portal-commits] JBoss Portal SVN: r11941 - in branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal: wsrp/consumer and 1 other directory.

portal-commits at lists.jboss.org portal-commits at lists.jboss.org
Fri Sep 19 16:58:42 EDT 2008


Author: chris.laprun at jboss.com
Date: 2008-09-19 16:58:41 -0400 (Fri, 19 Sep 2008)
New Revision: 11941

Modified:
   branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationInfoTestCase.java
   branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationPropertyTestCase.java
   branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationInfo.java
   branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationProperty.java
Log:
- JBPORTAL-2083: started adding support for property value change listening.
- Added more tests.
- More generification.


Modified: branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationInfoTestCase.java
===================================================================
--- branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationInfoTestCase.java	2008-09-19 19:36:43 UTC (rev 11940)
+++ branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationInfoTestCase.java	2008-09-19 20:58:41 UTC (rev 11941)
@@ -106,9 +106,11 @@
       assertFalse(names.isEmpty());
       assertTrue(properties.containsKey(key));
       assertTrue(names.contains(key));
-      assertNotNull(properties.get(key));
-      assertEquals(properties.get(key), info.getRegistrationProperty(key));
-      assertEquals("bar", info.getRegistrationProperty(key).getValue());
+      Object prop = properties.get(key);
+      assertNotNull(prop);
+      RegistrationProperty registrationProperty = info.getRegistrationProperty(key);
+      assertEquals(prop, registrationProperty);
+      assertEquals("bar", registrationProperty.getValue());
    }
 
    public void testRegistrationPropertiesAndRefresh()
@@ -314,16 +316,6 @@
       assertEquals("value1", properties[0].getStringValue());
    }
 
-   private void checkRegistrationData(RegistrationData registrationData, String prop0Value)
-   {
-      assertNotNull(registrationData);
-      Property[] properties = registrationData.getRegistrationProperties();
-      assertNotNull(properties);
-      assertEquals(1, properties.length);
-      assertEquals("prop0", properties[0].getName());
-      assertEquals(prop0Value, properties[0].getStringValue());
-   }
-
    public void testGetRegistrationDataWithInitialWrongValue()
    {
       info.setRegistrationPropertyValue("prop0", "incorrect");
@@ -360,6 +352,32 @@
       assertNull(info.getRegistrationProperty("prop0"));
    }
 
+   public void testRefreshRegisteredWithoutPropsAndProducerNowSendsProps()
+   {
+      // simulate successful registration
+      info.setRegistrationContext(WSRPTypeFactory.createRegistrationContext("handle"));
+      assertTrue(info.isRegistrationRequired());
+      assertTrue(info.isRegistrationValid());
+
+      // producer now requires a registration property
+      info.refresh(createServiceDescription(true, 1), producerId, true, true, true);
+      assertTrue(info.isRegistrationRequired());
+      assertTrue(info.isRegistrationDeterminedRequired());
+      assertFalse(info.isRegistrationValid());
+      assertFalse(info.isConsistentWithProducerExpectations());
+      assertFalse(info.isModified());
+   }
+
+   private void checkRegistrationData(RegistrationData registrationData, String prop0Value)
+   {
+      assertNotNull(registrationData);
+      Property[] properties = registrationData.getRegistrationProperties();
+      assertNotNull(properties);
+      assertEquals(1, properties.length);
+      assertEquals("prop0", properties[0].getName());
+      assertEquals(prop0Value, properties[0].getStringValue());
+   }
+
    private ServiceDescription createServiceDescription(boolean requiresRegistration, int numberOfProperties)
    {
       return ServiceDescriptionBehavior.createServiceDescription(requiresRegistration, numberOfProperties);

Modified: branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationPropertyTestCase.java
===================================================================
--- branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationPropertyTestCase.java	2008-09-19 19:36:43 UTC (rev 11940)
+++ branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/test/wsrp/consumer/RegistrationPropertyTestCase.java	2008-09-19 20:58:41 UTC (rev 11941)
@@ -35,9 +35,24 @@
 {
    private RegistrationProperty prop;
 
+   private Listener listener = new Listener();
+
+   private static final String VALUE = "value";
+   private static final String NEW_VALUE = "newValue";
+
+   private class Listener implements RegistrationProperty.PropertyChangeListener
+   {
+      boolean called;
+
+      public void propertyValueChanged(RegistrationProperty property, Object oldValue, Object newValue)
+      {
+         called = (prop == property) && VALUE.equals(oldValue) && NEW_VALUE.equals(newValue);
+      }
+   }
+
    protected void setUp() throws Exception
    {
-      prop = new RegistrationProperty("name", "value", "en");
+      prop = new RegistrationProperty("name", VALUE, "en", listener);
    }
 
    public void testGetters()
@@ -56,13 +71,13 @@
       prop.setInvalid(Boolean.FALSE, RegistrationProperty.Status.VALID);
 
       // we haven't changed the value, so the status shouldn't have changed
-      prop.setValue("value");
+      prop.setValue(VALUE);
       assertEquals(Boolean.FALSE, prop.isInvalid());
       assertEquals(RegistrationProperty.Status.VALID, prop.getStatus());
 
       // we changed the value, status is now unknown
-      prop.setValue("value2");
-      assertEquals("value2", prop.getValue());
+      prop.setValue(NEW_VALUE);
+      assertEquals(NEW_VALUE, prop.getValue());
       assertNull(prop.isInvalid());
       assertEquals(RegistrationProperty.Status.UNCHECKED_VALUE, prop.getStatus());
    }
@@ -102,4 +117,13 @@
          // expected
       }
    }
+
+   public void testPropertyChangedEvent()
+   {
+      prop.setValue(VALUE);
+      assertFalse(listener.called);
+
+      prop.setValue(NEW_VALUE);
+      assertTrue(listener.called);
+   }
 }

Modified: branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationInfo.java
===================================================================
--- branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationInfo.java	2008-09-19 19:36:43 UTC (rev 11940)
+++ branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationInfo.java	2008-09-19 20:58:41 UTC (rev 11941)
@@ -51,7 +51,7 @@
  * @version $Revision$
  * @since 2.6
  */
-public class RegistrationInfo
+public class RegistrationInfo implements RegistrationProperty.PropertyChangeListener
 {
    private static final Logger log = Logger.getLogger(RegistrationInfo.class);
 
@@ -64,7 +64,7 @@
    private transient Boolean requiresRegistration;
    private transient Boolean consistentWithProducerExpectations;
    private transient RegistrationData registrationData;
-   private transient boolean dirty;
+   private transient boolean modified;
    private transient ProducerInfo parent;
 
    public RegistrationInfo(ProducerInfo producerInfo)
@@ -105,7 +105,7 @@
          for (RegistrationProperty otherProp : other.persistentRegistrationProperties.values())
          {
             String name = otherProp.getName();
-            RegistrationProperty prop = new RegistrationProperty(name, otherProp.getValue(), otherProp.getLang());
+            RegistrationProperty prop = new RegistrationProperty(name, otherProp.getValue(), otherProp.getLang(), this);
             prop.setStatus(otherProp.getStatus());
             this.persistentRegistrationProperties.put(name, prop);
          }
@@ -129,7 +129,7 @@
 
    public void setRegistrationHandle(String registrationHandle)
    {
-      this.persistentRegistrationHandle = modifyIfNeeded(this.persistentRegistrationHandle, registrationHandle);
+      this.persistentRegistrationHandle = registrationHandle;
    }
 
    public byte[] getRegistrationState()
@@ -287,15 +287,13 @@
       RegistrationProperty prop = getOrCreateRegistrationPropertiesMap(true).get(name);
       if (prop != null)
       {
-         value = modifyIfNeeded(prop.getValue(), value);
          prop.setValue(value);
       }
       else
       {
          // todo: deal with language more appropriately
-         prop = new RegistrationProperty(name, value, WSRPUtils.toString(Locale.getDefault()));
+         prop = new RegistrationProperty(name, value, WSRPUtils.toString(Locale.getDefault()), this);
          getOrCreateRegistrationPropertiesMap(false).put(name, prop);
-         dirty = true;
       }
 
       return prop;
@@ -304,12 +302,12 @@
    public void removeRegistrationProperty(String name)
    {
       ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(name, "registration property name", "RegistrationInfo.removeRegistrationProperty");
-      Map propertiesMap = getOrCreateRegistrationPropertiesMap(false);
+      Map<String, RegistrationProperty> propertiesMap = getOrCreateRegistrationPropertiesMap(false);
       if (propertiesMap == null || propertiesMap.remove(name) == null)
       {
          throw new IllegalArgumentException("Cannot remove inexistent registration property '" + name + "'");
       }
-      dirty = true;
+      setModified(true);
    }
 
    private Map<String, RegistrationProperty> getOrCreateRegistrationPropertiesMap(boolean forceCreate)
@@ -404,7 +402,7 @@
          {
             result.setRegistrationProperties(new HashMap<String, RegistrationProperty>(persistentRegistrationProperties));
          }
-         dirty = false;
+         setModified(false);
 
          if (serviceDescription.isRequiresRegistration())
          {
@@ -508,7 +506,7 @@
          if (forceCheckOfExtraProps || !hasRegisteredIfNeeded())
          {
             log.debug("Registration data is available when none is expected by the producer");
-            checkForExtraProperties(producerId, result, Collections.EMPTY_SET, properties, keepExtra);
+            checkForExtraProperties(producerId, result, Collections.<String>emptySet(), properties, keepExtra);
          }
          else
          {
@@ -581,7 +579,7 @@
          {
             String name = description.getName();
             RegistrationPropertyDescription desc = WSRPUtils.convertToRegistrationPropertyDescription(description);
-            RegistrationProperty prop = new RegistrationProperty(name, null, WSRPUtils.toString(desc.getLang()));
+            RegistrationProperty prop = new RegistrationProperty(name, null, WSRPUtils.toString(desc.getLang()), this);
             prop.setDescription(desc);
             prop.setInvalid(Boolean.TRUE, RegistrationProperty.Status.MISSING_VALUE);
             result.put(name, prop);
@@ -629,7 +627,7 @@
 
       consistentWithProducerExpectations = Boolean.TRUE; // since we have a registration context, we're consistent with the Producer
       requiresRegistration = Boolean.TRUE; // we know we require registration
-      dirty = false; // our state is clean :)
+      setModified(false); // our state is clean :)
    }
 
    public RegistrationContext getRegistrationContext()
@@ -648,14 +646,20 @@
 
    public boolean isModified()
    {
-      return dirty;
+      return modified;
    }
 
    public void setModified(boolean modified)
    {
-      this.dirty = modified;
+      this.modified = modified;
    }
 
+   /** todo: Should be package-only, public for tests... */
+   public void propertyValueChanged(RegistrationProperty property, Object oldValue, Object newValue)
+   {
+      setModified(true);
+   }
+
    public class RegistrationRefreshResult extends RefreshResult
    {
       private Map<String, RegistrationProperty> registrationProperties;
@@ -675,16 +679,4 @@
          this.registrationProperties = registrationProperties;
       }
    }
-
-   private String modifyIfNeeded(String oldValue, String newValue)
-   {
-      if ((oldValue != null && !oldValue.equals(newValue)) || (oldValue == null && newValue != null))
-      {
-         oldValue = newValue;
-         dirty = true;
-         consistentWithProducerExpectations = null;
-      }
-
-      return oldValue;
-   }
 }

Modified: branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationProperty.java
===================================================================
--- branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationProperty.java	2008-09-19 19:36:43 UTC (rev 11940)
+++ branches/JBoss_Portal_Branch_2_7/wsrp/src/main/org/jboss/portal/wsrp/consumer/RegistrationProperty.java	2008-09-19 20:58:41 UTC (rev 11941)
@@ -41,13 +41,7 @@
    private String persistentName;
    private String persistentValue;
 
-   /*private transient String status;
-   public static final String INEXISTENT_STATUS = "Inexistent on Producer";
-   public static final String MISSING_STATUS = "Missing";
-   public static final String MISSING_VALUE_STATUS = "Missing value";
-   public static final String UNCHECKED_VALUE_STATUS = "Undetermined status";
-   public static final String INVALID_VALUE_STATUS = "Invalid value";*/
-
+   private transient PropertyChangeListener listener;
    private transient Status status;
 
    public enum Status
@@ -76,12 +70,14 @@
    {
    }
 
-   public RegistrationProperty(String name, String stringValue, String lang)
+   public RegistrationProperty(String name, String stringValue, String lang, PropertyChangeListener listener)
    {
       ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(name, "Name", "RegistrationProperty");
       ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(lang, "Lang", "RegistrationProperty");
+      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(lang, "listener", "RegistrationProperty");
       this.persistentName = name;
       this.persistentLang = lang;
+      this.listener = listener;
       setValue(stringValue);
    }
 
@@ -192,8 +188,10 @@
 
    public void setValue(String stringValue)
    {
+      // only change the value if it's not the same as the old one
       if ((persistentValue != null && !persistentValue.equals(stringValue)) || (persistentValue == null && stringValue != null))
       {
+         String oldValue = persistentValue;
          persistentValue = stringValue;
          persistentInvalid = null;
          if (persistentValue == null)
@@ -204,6 +202,9 @@
          {
             status = UNCHECKED_VALUE;
          }
+
+         // notify listeners
+         notifyListener(oldValue, persistentValue);
       }
    }
 
@@ -227,4 +228,18 @@
    {
       this.status = status;
    }
+
+   private void notifyListener(String oldValue, String newValue)
+   {
+      if (listener != null)
+      {
+         listener.propertyValueChanged(this, oldValue, newValue);
+      }
+   }
+
+   /** todo: Should be package-only, public for tests... */
+   public static interface PropertyChangeListener
+   {
+      void propertyValueChanged(RegistrationProperty property, Object oldValue, Object newValue);
+   }
 }




More information about the portal-commits mailing list