You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2009/02/08 01:58:12 UTC

svn commit: r741985 - in /incubator/shindig/trunk/features: core/config-test.js core/config.js pom.xml

Author: etnu
Date: Sun Feb  8 00:58:11 2009
New Revision: 741985

URL: http://svn.apache.org/viewvc?rev=741985&view=rev
Log:
Applied patch for SHINDIG-889. This is not the same as Henning's original patch, which would not apply for some reason. The behavior is essentially the same, but I also added exhaustive test cases for gadgets.config and ensured that all tests are being run without having to manually specify them.


Added:
    incubator/shindig/trunk/features/core/config-test.js
Modified:
    incubator/shindig/trunk/features/core/config.js
    incubator/shindig/trunk/features/pom.xml

Added: incubator/shindig/trunk/features/core/config-test.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/core/config-test.js?rev=741985&view=auto
==============================================================================
--- incubator/shindig/trunk/features/core/config-test.js (added)
+++ incubator/shindig/trunk/features/core/config-test.js Sun Feb  8 00:58:11 2009
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+function ConfigTest(name) {
+  TestCase.call(this, name);
+}
+
+ConfigTest.inherits(TestCase);
+
+ConfigTest.prototype.testBasic = function() {
+  var testBasicConfig;
+  gadgets.config.register("testBasic", null, function(config) {
+    testBasicConfig = config.testBasic;
+  });
+
+  gadgets.config.init({testBasic: {data: "Hello, World!"}});
+
+  this.assertEquals("Hello, World!", testBasicConfig.data);
+};
+
+ConfigTest.prototype.testMultiple = function() {
+  var testMultiple0;
+  gadgets.config.register("testMultiple", null, function(config) {
+    testMultiple0 = config.testMultiple;
+  });
+
+  var testMultiple1;
+  gadgets.config.register("testMultiple", null, function(config) {
+    testMultiple1 = config.testMultiple;
+  });
+
+  gadgets.config.init({testMultiple: {data: "Hello, World!"}});
+
+  this.assertEquals("Hello, World!", testMultiple0.data);
+  this.assertEquals("Hello, World!", testMultiple1.data);
+};
+
+ConfigTest.prototype.testValidator = function() {
+  var validatorValue;
+  gadgets.config.register("testValidator", {data: function(value) {
+    validatorValue = value;
+    return true;
+  }});
+
+  gadgets.config.init({testValidator: {data: "Hello, World!"}});
+
+  this.assertEquals("Hello, World!", validatorValue);
+};
+
+ConfigTest.prototype.testValidatorMultiple = function() {
+  var validatorValue0;
+  gadgets.config.register("testValidator", {key0: function(value) {
+    validatorValue0 = value;
+    return true;
+  }});
+
+  var validatorValue1;
+  gadgets.config.register("testValidator", {key1: function(value) {
+    validatorValue1 = value;
+    return true;
+  }});
+
+  gadgets.config.init({testValidator: {key0: "Hello, World!", key1: "Goodbye, World!"}});
+
+  this.assertEquals("Hello, World!", validatorValue0);
+  this.assertEquals("Goodbye, World!", validatorValue1);
+};
+
+ConfigTest.prototype.testValidatorRejection = function() {
+  gadgets.config.register("testValidatorRejection", {data: function(value) {
+    return false;
+  }});
+
+  try {
+    gadgets.config.init({testValidatorRejection: {data: "Hello, World!"}});
+    this.fail("Did not throw an exception when validation failed.");
+  } catch (e) {
+    // Expected.
+  }
+};
+
+ConfigTest.prototype.testValidatorDisabled = function() {
+  var testValidatorDisabledConfig;
+  gadgets.config.register("testValidatorDisabled", {data: function(value) {
+    return false;
+  }},
+  function(config) {
+    testValidatorDisabledConfig = config.testValidatorDisabled;
+  });
+
+  gadgets.config.init({testValidatorDisabled: {data: "Hello, World!"}}, true);
+
+  this.assertEquals("Hello, World!", testValidatorDisabledConfig.data);
+};
+
+ConfigTest.prototype.testEnumValidator = function() {
+  var validator = gadgets.config.EnumValidator("foo", "bar", "baz");
+
+  this.assertTrue(validator("foo"));
+  this.assertTrue(validator("bar"));
+  this.assertTrue(validator("baz"));
+  this.assertFalse(validator("junk"));
+};
+
+ConfigTest.prototype.testRegExValidator = function() {
+  var validator = gadgets.config.RegExValidator(/^hello.*$/);
+
+  this.assertTrue(validator("hello"));
+  this.assertTrue(validator("hello, world"));
+  this.assertTrue(validator("hellothere"));
+  this.assertFalse(validator("not hello"));
+};
+
+ConfigTest.prototype.testExistsValidator = function() {
+  var validator = gadgets.config.ExistsValidator;
+
+  this.assertTrue(validator("hello"));
+  this.assertTrue(validator(0));
+  this.assertTrue(validator(false));
+  this.assertTrue(validator(null));
+  this.assertTrue(validator(""));
+
+  this.assertFalse(validator({}.foo));
+};
+
+ConfigTest.prototype.testNonEmptyStringValidator = function() {
+  var validator = gadgets.config.NonEmptyStringValidator;
+
+  this.assertTrue(validator("hello"));
+
+  this.assertFalse(validator(0));
+  this.assertFalse(validator(false));
+  this.assertFalse(validator(null));
+  this.assertFalse(validator(""));
+  this.assertFalse(validator(undefined));
+};
+
+ConfigTest.prototype.testBooleanValidator = function() {
+  var validator = gadgets.config.BooleanValidator;
+
+  this.assertTrue(validator(true));
+  this.assertTrue(validator(false));
+
+  this.assertFalse(validator("hello"));
+  this.assertFalse(validator(0));
+  this.assertFalse(validator(null));
+  this.assertFalse(validator(undefined));
+};
+
+ConfigTest.prototype.testLikeValidator = function() {
+  var key0value, key1value;
+
+  var validator = gadgets.config.LikeValidator({
+    key0: function(data) {
+      key0value = data;
+      return true;
+    },
+    key1: function(data) {
+      key1value = data;
+      return true;
+    },
+  });
+
+  this.assertTrue(validator({key0:"Key0", key1: "Key1"}));
+  this.assertEquals("Key0", key0value);
+  this.assertEquals("Key1", key1value);
+};
+
+ConfigTest.prototype.testLikeValidatorWithFailure = function() {
+  var key0value, key1value;
+
+  var validator = gadgets.config.LikeValidator({
+    key0: function(data) {
+      key0value = data;
+      return false;
+    },
+    key1: function(data) {
+      key1value = data;
+      return true;
+    },
+  });
+
+  this.assertFalse(validator({key0:"Key0", key1: "Key1"}));
+  this.assertEquals("Key0", key0value);
+  this.assertEquals(null, key1value);
+};

Modified: incubator/shindig/trunk/features/core/config.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/core/config.js?rev=741985&r1=741984&r2=741985&view=diff
==============================================================================
--- incubator/shindig/trunk/features/core/config.js (original)
+++ incubator/shindig/trunk/features/core/config.js Sun Feb  8 00:58:11 2009
@@ -59,7 +59,7 @@
 var gadgets = gadgets || {};
 
 gadgets.config = function() {
-  var components = {};
+  var components = [];
 
   return {
     /**
@@ -82,13 +82,16 @@
      * @throws {Error} If the component has already been registered.
      */
     register: function(component, opt_validators, opt_callback) {
-      if (components[component]) {
-        throw new Error('Component "' + component + '" is already registered.');
+      var registered = components[component];
+      if (!registered) {
+        registered = [];
+        components[component] = registered;
       }
-      components[component] = {
+
+      registered.push({
         validators: opt_validators || {},
         callback: opt_callback
-      };
+      });
     },
 
     /**
@@ -101,9 +104,6 @@
      */
     get: function(opt_component) {
       if (opt_component) {
-        if (!components[opt_component]) {
-          throw new Error('Component "' + opt_component + '" not registered.');
-        }
         return configuration[opt_component] || {};
       }
       return configuration;
@@ -119,20 +119,25 @@
     init: function(config, opt_noValidation) {
       configuration = config;
       for (var name in components) if (components.hasOwnProperty(name)) {
-        var component = components[name],
-            conf = config[name],
-            validators = component.validators;
-        if (!opt_noValidation) {
-          for (var v in validators) if (validators.hasOwnProperty(v)) {
-            if (!validators[v](conf[v])) {
-              throw new Error('Invalid config value "' + conf[v] +
-                  '" for parameter "' + v + '" in component "' +
-                  name + '"');
+        var componentList = components[name],
+            conf = config[name];
+
+        for (var i = 0, j = componentList.length; i < j; ++i) {
+          var component = componentList[i];
+          if (conf && !opt_noValidation) {
+            var validators = component.validators;
+            for (var v in validators) if (validators.hasOwnProperty(v)) {
+              if (!validators[v](conf[v])) {
+                throw new Error('Invalid config value "' + conf[v] +
+                    '" for parameter "' + v + '" in component "' +
+                    name + '"');
+              }
             }
           }
-        }
-        if (component.callback) {
-          component.callback(config);
+
+          if (component.callback) {
+            component.callback(config);
+          }
         }
       }
     },

Modified: incubator/shindig/trunk/features/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/pom.xml?rev=741985&r1=741984&r2=741985&view=diff
==============================================================================
--- incubator/shindig/trunk/features/pom.xml (original)
+++ incubator/shindig/trunk/features/pom.xml Sun Feb  8 00:58:11 2009
@@ -107,20 +107,10 @@
               <testSourceDirectory>${basedir}</testSourceDirectory>
               <testSuites>
                 <testSuite>
-                  <name>GadgetsJSAPITests</name>
+                  <name>FeatureTests</name>
                   <type>TESTCASES</type>
                   <includes>
-                    <include>core/*test.js</include>
-                    <include>core.io/*test.js</include>
-                    <include>views/*test.js</include>
-                  </includes>
-                </testSuite>
-                <testSuite>
-                  <name>OpenSocialJsTests</name>
-                  <type>TESTCASES</type>
-                  <includes>
-                    <include>opensocial-reference/*test.js</include>
-                    <include>opensocial-base/*test.js</include>
+                    <include>*/*test.js</include>
                   </includes>
                 </testSuite>
               </testSuites>