Changeset 116

Show
Ignore:
Timestamp:
02/23/07 15:17:18 (6 years ago)
Author:
tobi
Message:

Moved getAlias method from Utilities.js to getAccessName in HopObject?.js (including unit test)

Location:
jala/trunk
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • jala/trunk/code/Utilities.js

    r99 r116  
    7272 */ 
    7373jala.util = new jala.Utilities(); 
    74  
    75 /** 
    76  * Constructs a name from an object which 
    77  * is unique in the underlying HopObject collection.  
    78  * @param {Object} obj The object representing or containing the alias name. 
    79  * Possible object types include: 
    80  * <ul> 
    81  * <li>File</li> 
    82  * <li>helma.File</li> 
    83  * <li>java.io.File</li> 
    84  * <li>String</li> 
    85  * <li>java.lang.String</li> 
    86  * </ul> 
    87  * @param {HopObject} collection The collection the alias  
    88  * potentially should be added to 
    89  * @param {Number} maxLength The maximum length of the alias 
    90  * @returns The resulting alias 
    91  * @type String 
    92  */ 
    93 jala.Utilities.prototype.getAlias = function(obj, collection, maxLength) { 
    94    var name; 
    95    var clazz = obj.constructor || obj.getClass(); 
    96    switch (clazz) { 
    97       case File: 
    98       case helma.File: 
    99       case java.io.File: 
    100       case Packages.helma.util.MimePart: 
    101       // first fix bloody ie/win file paths containing backslashes 
    102       var rawName = obj.getName().replace(/\\/g, "/"); 
    103       rawName = rawName.split("/"); 
    104       name = rawName.pop(); 
    105       if (name.contains(".")) 
    106          name = name.substring(0, name.lastIndexOf(".")); 
    107       break; 
    108        
    109       case String: 
    110       case java.lang.String: 
    111       name = obj; 
    112       break; 
    113  
    114       default: 
    115       name = obj.toString(); 
    116    } 
    117  
    118    // convert alias to lowercase and clean it from any invalid characters 
    119    var alias = name.toLowerCase().toFileName(); 
    120    var len = alias.length; 
    121    var counter = 0; 
    122  
    123    var getAlias = function() { 
    124       var overflow; 
    125       if (maxLength) { 
    126          if (counter) { 
    127             len += counter.toString().length; 
    128          } 
    129          if ((overflow = len - maxLength) > 0) { 
    130             alias = alias.substring(0, alias.length - overflow); 
    131             len = alias.length; 
    132          } 
    133       } 
    134       return (counter ? alias + counter.toString() : alias); 
    135    } 
    136  
    137    if (collection) { 
    138       var alias; 
    139       do { 
    140          alias = getAlias(); 
    141          counter += 1; 
    142       } while (collection[alias] || collection.get(alias)); 
    143       return alias; 
    144    } 
    145    return getAlias(); 
    146 }; 
    14774 
    14875/** 
  • jala/trunk/code/all.js

    r115 r116  
    4242      "History", 
    4343      "HtmlDocument", 
     44      "HopObject", 
    4445      "i18n", 
    4546      "ImageFilter", 
  • jala/trunk/tests/Utilities.js

    r114 r116  
    3030var tests = [ 
    3131   "testCreatePassword", 
    32    "testGetAlias", 
    3332   "testDiffObjects", 
    3433   "testPatchObject" 
     
    4746   assertEqual(jala.util.createPassword(null, 2).length, 8); 
    4847   assertEqual(jala.util.createPassword(100, 2).length, 100); 
    49    return; 
    50 }; 
    51  
    52 /** 
    53  * Unit test for #jala.util.getAlias. 
    54  */ 
    55 var testGetAlias = function() { 
    56    var name = "foobar"; 
    57    assertEqual(jala.util.getAlias(name), name); 
    58    assertEqual(jala.util.getAlias(name, null, 3), name.substr(0, 3)); 
    59  
    60    var collection = new HopObject; 
    61    assertEqual(jala.util.getAlias(name, collection), name); 
    62    // Test alias with the same name as a default HopObject method 
    63    assertNotEqual(jala.util.getAlias("get", collection), "get"); 
    64    assertEqual(jala.util.getAlias("get", collection), "get1"); 
    65    // Set a custom property of the collection and test it 
    66    collection[name] = true; 
    67    assertNotEqual(jala.util.getAlias(name, collection), name); 
    68    assertEqual(jala.util.getAlias(name, collection), name + "1"); 
    69     
    70    // Set custom properties equally to the method's numbering 
    71    collection[name + "1"] = true; 
    72    collection[name + "12"] = true; 
    73    assertNotEqual(jala.util.getAlias(name, collection), name + "1"); 
    74    assertNotEqual(jala.util.getAlias(name, collection), name + "12"); 
    75    assertEqual(jala.util.getAlias(name, collection), name + "123"); 
    76  
    77    assertNotEqual(jala.util.getAlias(name, collection, name.length), name); 
    78    assertEqual(jala.util.getAlias(name, collection, name.length), "fooba1"); 
    7948   return; 
    8049};