[lsc-changes] r280 - in lsc/trunk/src: main/java/org/lsc/beans main/java/org/lsc/utils test/java/org/lsc/jndi

jclarke at lsc-project.org jclarke at lsc-project.org
Wed Jul 1 10:34:57 CEST 2009


Author: jclarke
Date: 2009-07-01 10:34:57 +0200 (Wed, 01 Jul 2009)
New Revision: 280

Modified:
   lsc/trunk/src/main/java/org/lsc/beans/AbstractBean.java
   lsc/trunk/src/main/java/org/lsc/utils/JScriptEvaluator.java
   lsc/trunk/src/test/java/org/lsc/jndi/JScriptEvaluatorTest.java
Log:
Fixes #78. Thanks to Raphael Ouazana!
Allow Rhino to correctly send multiple values
new getAttributeValuesById function (note the 's' at the end of value)
Deprecates getAttributeValueById, and replaces it with getAttributeFirstValueById
Adds some tests that use of all these functions


Modified: lsc/trunk/src/main/java/org/lsc/beans/AbstractBean.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/beans/AbstractBean.java	2009-06-26 00:43:30 UTC (rev 279)
+++ lsc/trunk/src/main/java/org/lsc/beans/AbstractBean.java	2009-07-01 08:34:57 UTC (rev 280)
@@ -431,21 +431,55 @@
     /**
      * Get the <b>first</b> value of an attribute from its name
      * 
-     * @param id the name
-     * @return the LDAP attribute
+     * @param id The attribute name (case insensitive)
+     * @return String The first value of the attribute, or the empty string ("")
      * @throws NamingException
+     * @deprecated
      */
-    public final String getAttributeValueById(final String id) throws NamingException {
-        // use lower case since attribute names are case-insensitive
-        Attribute attribute = attrs.get(id.toLowerCase());
-        String result = "";
-        if (attribute != null && attribute.size() > 0) {
-            result = attribute.get(0).toString();
-        }
-        return result;
-    }
+    public final String getAttributeValueById(final String id)
+			throws NamingException
+	{
+		return getAttributeFirstValueById(id);
+	}
 
+	/**
+	 * Get the <b>first</b> value of an attribute from its name
+	 * 
+	 * @param id
+	 *            The attribute name (case insensitive)
+	 * @return String The first value of the attribute, or the empty string ("")
+	 * @throws NamingException
+	 */
+	public final String getAttributeFirstValueById(final String id)
+			throws NamingException
+	{
+		List<String> allValues = getAttributeValuesById(id);
+		return allValues.size() >= 1 ? allValues.get(0) : "";
+	}
 
+	/**
+	 * Get all values of an attribute from its name
+	 * 
+	 * @param id
+	 *            The attribute name (case insensitive)
+	 * @return List<String> List of attribute values, or an empty list
+	 * @throws NamingException
+	 */
+	public final List<String> getAttributeValuesById(final String id)
+			throws NamingException
+	{
+		List<String> resultsArray = new ArrayList<String>();
+
+		Attribute attribute = getAttributeById(id);
+		for (int i = 0; attribute != null && i < attribute.size(); i++)
+		{
+			resultsArray.add(attribute.get(i).toString());
+		}
+
+		return resultsArray;
+	}
+
+
     /**
      * Get the attributes name list.
      *

Modified: lsc/trunk/src/main/java/org/lsc/utils/JScriptEvaluator.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/utils/JScriptEvaluator.java	2009-06-26 00:43:30 UTC (rev 279)
+++ lsc/trunk/src/main/java/org/lsc/utils/JScriptEvaluator.java	2009-07-01 08:34:57 UTC (rev 280)
@@ -50,6 +50,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Arrays;
 
 import org.lsc.jndi.JndiServices;
 import org.lsc.jndi.ScriptableJndiServices;
@@ -125,17 +126,35 @@
 
     @SuppressWarnings("unchecked")
     public static List<String> evalToStringList(final String expression,
-            final Map<String, Object> params) {
-        Object result = getInstance().instanceEval(expression, params);
-        if(result != null && result.getClass().isAssignableFrom(List.class)) {
-            return (List<String>)result;
-        } else {
-            List<String> resultsArray = new ArrayList<String>();
-            resultsArray.add(Context.toString(result));
-            return resultsArray;
-        }
-    }
-    
+			final Map<String, Object> params)
+	{
+		Object result = getInstance().instanceEval(expression, params);
+		
+		// First try to convert to Array, else to List, and finally to String
+		try
+		{
+			Object[] resultsRealArray = (Object[]) Context.jsToJava(result, Object[].class);
+			List<String> resultsArray = new ArrayList<String>();
+			for (Object resultValue : resultsRealArray)
+			{
+				resultsArray.add(resultValue.toString());
+			}
+			return resultsArray;
+		}
+		catch (Exception e) {} // try next approach
+		
+		try
+		{
+			Object resultsArray = Context.jsToJava(result, List.class);
+			return (List<String>) resultsArray;
+		}
+		catch (Exception e) {} // try next approach
+
+		List<String> resultsArray = new ArrayList<String>();
+		resultsArray.add(Context.toString(result));
+		return resultsArray;
+	}
+
     public static Boolean evalToBoolean(final String expression, final Map<String, Object> params) {
         return Context.toBoolean(getInstance().instanceEval(expression, params));
     }

Modified: lsc/trunk/src/test/java/org/lsc/jndi/JScriptEvaluatorTest.java
===================================================================
--- lsc/trunk/src/test/java/org/lsc/jndi/JScriptEvaluatorTest.java	2009-06-26 00:43:30 UTC (rev 279)
+++ lsc/trunk/src/test/java/org/lsc/jndi/JScriptEvaluatorTest.java	2009-07-01 08:34:57 UTC (rev 280)
@@ -47,57 +47,106 @@
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import javax.naming.directory.Attribute;
 import javax.naming.directory.BasicAttribute;
 
+import junit.framework.TestCase;
+
+import org.lsc.beans.AbstractBean;
+import org.lsc.beans.personBean;
 import org.lsc.utils.JScriptEvaluator;
 
-import junit.framework.TestCase;
-
 /**
  * Test different use cases of this JScript evaluator
+ * 
  * @author Sebastien Bahloul <seb at lsc-project.org>
+ * @author Jonathan Clarke <jonathan at phillipoux.net>
  */
-public class JScriptEvaluatorTest extends TestCase {
+public class JScriptEvaluatorTest extends TestCase
+{
 
-	public void testOk() throws IllegalAccessException, InstantiationException, InvocationTargetException {
+	public void testOk()
+	{
 		Map<String, Object> table = new HashMap<String, Object>();
 		table.put("srcAttr", new BasicAttribute("a", "b"));
-		assertEquals(JScriptEvaluator.evalToString("srcAttr.get()", table), "b");
+		assertEquals("b", JScriptEvaluator.evalToString("srcAttr.get()", table));
 	}
 
-	public void testNk() throws IllegalAccessException, InstantiationException, InvocationTargetException {
+	public void testNk()
+	{
 		Map<String, Object> table = new HashMap<String, Object>();
 		table.put("srcAttr", new BasicAttribute("a", "b"));
-		try {
+		try
+		{
 			assertEquals(JScriptEvaluator.evalToString("src.get()", table), "b");
 			assertTrue(false);
-		} catch(org.mozilla.javascript.EcmaError e) {
+		}
+		catch (org.mozilla.javascript.EcmaError e)
+		{
 			assertTrue(true);
 		}
+
+		try
+		{
+			assertEquals(JScriptEvaluator.evalToStringList("src.get()", table), "b");
+			assertTrue(false);
+		}
+		catch (org.mozilla.javascript.EcmaError e)
+		{
+			assertTrue(true);
+		}
+
 	}
 
-	public void testOk2() throws IllegalAccessException, InstantiationException, InvocationTargetException {
+	public void testOk2()
+	{
 		Map<String, Object> table = new HashMap<String, Object>();
 		table.put("sn", new BasicAttribute("sn", "Doe"));
 		table.put("givenName", new BasicAttribute("givenName", "John"));
 		assertEquals(JScriptEvaluator.evalToString("givenName.get() + ' ' + sn.get()", table), "John Doe");
 	}
 
-//	public void testOk3() throws IllegalAccessException, InstantiationException, InvocationTargetException {
-//		Map<String, Object> table = new HashMap<String, Object>();
-//		Attribute sn = new BasicAttribute("sn", "Doe");
-//		Attribute givenName = new BasicAttribute("givenName", "John");
-//		inetOrgPersonBean srcIopb = new inetOrgPersonBean();
-//		srcIopb.setAttribute(sn);
-//		srcIopb.setAttribute(givenName);
-//		table.put("srcBean", srcIopb);
-//		assertEquals(JScriptEvaluator.eval("srcBean.getAttributeById('givenName').get() + ' ' + srcBean.getAttributeById('sn').get()", table), "John Doe");
-//	}
+	public void testList()
+	{
+		Map<String, Object> table = new HashMap<String, Object>();
+		Attribute sn = new BasicAttribute("sn", "Doe");
+		Attribute givenName = new BasicAttribute("givenName", "John");
+		Attribute cn = new BasicAttribute("cn");
+		cn.add("John Doe");
+		cn.add("DOE John");
+		AbstractBean bean = (AbstractBean) new personBean();
+		bean.setAttribute(sn);
+		bean.setAttribute(givenName);
+		bean.setAttribute(cn);
 
-//	public void testOkLdap() throws IllegalAccessException, InstantiationException, InvocationTargetException {
-//		Map<String, Object> table = new HashMap<String, Object>();
-//		assertEquals(JScriptEvaluator.eval("ldap.or(ldap.attribute('ou=People,dc=lsc-project,dc=org','ou'), ldap.fsup('ou=People,dc=lsc-project,dc=org','dc=*'))", table), "[People,lsc-project]");
-//	}
+		table.put("srcBean", bean);
+
+		assertEquals("John Doe", JScriptEvaluator.evalToString("srcBean.getAttributeById('givenName').get() + ' ' + srcBean.getAttributeById('sn').get()", table));
+		assertEquals("John Doe", JScriptEvaluator.evalToString("srcBean.getAttributeFirstValueById('givenName') + ' ' + srcBean.getAttributeValueById('sn')", table));
+
+		List<String> res = JScriptEvaluator.evalToStringList("srcBean.getAttributeById('givenName').get() + ' ' + srcBean.getAttributeById('sn').get()", table);
+		assertNotNull(res);
+		assertEquals("John Doe", res.get(0));
+
+		res = JScriptEvaluator.evalToStringList("srcBean.getAttributeValuesById('cn')", table);
+		assertNotNull(res);
+		assertEquals(2, res.size());
+		assertTrue(res.contains("John Doe"));
+		assertTrue(res.contains("DOE John"));
+
+		res = JScriptEvaluator.evalToStringList("srcBean.getAttributeValuesById('nonexistent')", table);
+		assertNotNull(res);
+		assertEquals(0, res.size());
+	}
+
+	public void testOkLdap()
+	{
+		Map<String, Object> table = new HashMap<String, Object>();
+
+		List<String> res = JScriptEvaluator.evalToStringList("ldap.or(ldap.attribute('ou=People,dc=lsc-project,dc=org','ou'), ldap.fsup('ou=People,dc=lsc-project,dc=org','dc=*'))", table);
+		assertEquals("[People, dc=lsc-project,dc=org]", res.toString());
+	}
 }



More information about the lsc-changes mailing list