RE: Type conversion on a Map

2012-09-21 Thread Miguel Almeida
On Fri, 2012-09-21 at 09:04 -0400, Martin Gainty wrote:

> a stupid question but i have to ask

If you mean "where is the initialisation of simpleMap e.g. simpleMap=new
java.util.HashMap();" - it's not there. I don't think you need to,
Struts initializes it. (see below)

> 
> public class TypeConversionAction extends ActionSupport 
> {
> private Map simpleMap;   //where is the initialisation of 
> simpleMap e.g. simpleMap=new java.util.HashMap();
> public Map getSimpleMap()  { return simpleMap;  }
> public void setSimpleMap(Map simpleMap) {   
> this.simpleMap = simpleMap; }
> public String execute() {  return SUCCESS; }
> }
> 
> //then the test-harness:
> 
> package test.example;
> import java.util.Map;
> import org.apache.struts2.StrutsTestCase;
> import org.junit.Test;
> import com.opensymphony.xwork2.ActionProxy;
> import com.opensymphony.xwork2.ActionSupport;
> 
> public class TypeConversionTest extends StrutsTestCase 
> {
> private ActionProxy actionProxy;
> private ActionSupport action;
> 
> @Test
> public void test() throws Exception{
> Entity entity = new Entity();
> entity.setId(1);
> super.setUp();
> 
> request.setParameter("simpleMap[1]", "value");
> createAction("/example/Convesion.action");
> executeProxy();
> 
> Map complexMap = ((TypeConversionAction) 
> action).getSimpleMap();
> assertNotNull(complexMap);   //getSimpleMap is NULL in the Action so it 
> would always be NULL here

No. The test fails below, not here. executeProxy executed the action and
initialized the map. If you run the test you'll see it fails on the last
assertEquals, not here.

> 
> assertFalse(complexMap.isEmpty());  //FAIL it is empty
> assertEquals("value", complexMap.get(entity)); //Never initialised so 
> this would fail as well

This is where it is failing. Not because it hasn't been initialized, but
because it doesn't have that key.

Miguel Almeida


RE: Type conversion on a Map

2012-09-21 Thread Martin Gainty

a stupid question but i have to ask

public class TypeConversionAction extends ActionSupport 
{
private Map simpleMap;   //where is the initialisation of 
simpleMap e.g. simpleMap=new java.util.HashMap();
public Map getSimpleMap()  { return simpleMap;  }
public void setSimpleMap(Map simpleMap) {   this.simpleMap 
= simpleMap; }
public String execute() {  return SUCCESS; }
}

//then the test-harness:

package test.example;
import java.util.Map;
import org.apache.struts2.StrutsTestCase;
import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;

public class TypeConversionTest extends StrutsTestCase 
{
private ActionProxy actionProxy;
private ActionSupport action;

@Test
public void test() throws Exception{
Entity entity = new Entity();
entity.setId(1);
super.setUp();

request.setParameter("simpleMap[1]", "value");
createAction("/example/Convesion.action");
executeProxy();

Map complexMap = ((TypeConversionAction) 
action).getSimpleMap();
assertNotNull(complexMap);   //getSimpleMap is NULL in the Action so it 
would always be NULL here

assertFalse(complexMap.isEmpty());  //FAIL it is empty
assertEquals("value", complexMap.get(entity)); //Never initialised so 
this would fail as well
}

/**
 * Creates the action with the given actionPath if it wasn't already created
 * @see isAlreadyCreated
 * @param actionPath
* @throws Exception
*/
public ActionSupport createAction(String actionPath) throws Exception 
{
actionProxy = getActionProxy(actionPath);
action = (ActionSupport) actionProxy.getAction();
return action;   
}

/**
 * Executes the action created and returns the result 
 * @return
 * @throws Exception
 */
public String executeProxy() throws Exception {
actionProxy.setExecuteResult(false);
return actionProxy.execute();
}
}

?

Saludos,
Martin 
__ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.


Subject: Re: Type conversion on a Map
From: mig...@almeida.at
To: user@struts.apache.org
Date: Fri, 21 Sep 2012 10:31:24 +0100




  
  


Sure!



Here's a maven project with a failing test case.



There's actually a map being built, but it's a , and the key 
reads "OGNL no conversion possible".



Cheers,

Miguel Almeida



On Thu, 2012-09-20 at 19:44 +0200, Lukasz Lenart wrote:

Hi,

Could you share the unit test as well ? The whole code would be the best :-)


Regards







-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org