Thursday, September 27, 2012

No RequestContext for operation GWT





Problema

Ao tentar executar uma aplicação Spring Roo que utiliza o GWT apresenta a seguinte mensagem de erro no console: No RequestContext for operation.



Observação:

Este erro também acontece ao se utilizar RequestFactory e os métodos no lado cliente e no lado servidor não estejam compatíveis. Antes de tentar o explicado aqui certifique-se que os métodos foram criados corretamente. Maiores informações de como os métodos devem ser criados, ver a seguinte documentação do GWT: https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory?hl=pt-BR


Motivo

Modificações feita em classe filha de RequestContext. Aparentemente o projeto ficou inconsistente após modificações em classe filha de RequestContext. Está relacionado a problemas de build/compilação do projeto

Solução

Reconstruir o projeto. Uma simples rebuild não funcionou. Segue os passos que provavelmente efetuei para solucionar o problema. Não simulei o erro novamente. Na linha de comando execute:


 mvn clean
 mvn compile
 mvn gwt:run

Não tenho certeza se o passo seguinte também é necessário. No eclipse:

plugin do google->GWT Compile Projet

Se não funcionar tente recompilar/reconstruir de com outras sequências/formas.




Thursday, September 15, 2011

Spring Roo - Error: One or more exceptions caught, see full set in UmbrellaException#getCauses

Problem

When trying to save a entity in spring roo (using in the web tier gwt) it showed the following error message:

Error: One or more exceptions caught, see full set in UmbrellaException#getCauses

Reason

The declaration of an empty constructor was causing the problem. The roo cause was not identified.

Solution

Remove the constructor. In my case the code was changed from:

public class A {
transient B b;

A(){
this.b = getStub();
}
...
}

to:

public class A {
transient B b= getStub();
...
}



Spring Roo - Error: One or more exceptions caught, see full set in UmbrellaException#getCauses

Problema

Ao tentar salvar uma entidade no spring roo (usando na camada de visão o gwt) apresentou o seguinte problema.

Error: One or more exceptions caught, see full set in UmbrellaException#getCauses

Motivo

A declaração de um construtor vazio estava ocasionando o problema. Mas a causa raiz não foi identificada.

Solução

Remover o construtor vazio. No meu caso foi modificado o código de:

public class A {
transient B b;

A(){
this.b = getStub();
}
...
}

para:

public class A {
transient B b= getStub();
...
}

Wednesday, September 14, 2011

Mock/Stub EntityManager in Spring Roo

Problem

Trying to create unit test to the method persist from a class created in Spring Roo using a stub do the EntityManger that is passed in the constructor as follows:


public class ClassATest {
    @Test
    public void whenPersistShoulCallClassB() throws Exception {
        
        Mockery contexto = new Mockery();
        final ClassB classB = contexto.mock(ClassB.class);
        
        final ClassA classA = 
            new ClassA(getEntityManagerStub(), classB);

        contexto.checking(new Expectations() {{
            one(classB).doSomething(classA);

        }
    });
        classA.persist();
        
        contexto.assertIsSatisfied();
    }

    private EntityManager getEntityManagerStub() {
        return new EntityManager() {
            ...
        }
    }
}
Source from ClassA

public class ClassA {
...

    public ClassA(EntityManager entityManager, ClassB classB) {
        this.entityManager = entityManager;
        this.classB = classB;
    }



    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
     
        this.entityManager.persist(this);
     
        classB.doSomething(this);
     
    }

...
 }


This test works inside Eclipse, but doesn´t when called by maven, as a result of  mvn jetty:run-exploded.

Reason

Probably Spring Roo is injecting the EntityManager just before the call to the constructor.

Solution

Use of setter injection, as follows:

public class ClassATest {
    @Test
    public void whenPersistShoulCallClassB() throws Exception {
        
        Mockery contexto = new Mockery();
        final ClassB classB = contexto.mock(ClassB.class);
        
        final ClassA classA =
            new ClassA(classB);
        classeA.setEntityManager(getEntityManagerStub());
        
        contexto.checking(new Expectations() {{
            one(classB).doSomething(classeA);

        }
    });
        classeA.persist();
        
        contexto.assertIsSatisfied();
    }

    private EntityManager getEntityManagerStub() {

        return new EntityManager() {
            ...
        }

    }
}


Código da ClasseA


public class ClasseA {
...

    public ClasseA(ClasseB classeB) {
        this.classeB = classeB;
    }


    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }




    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
      
        this.entityManager.persist(this);
      
        classeB.doSomething(this);
      
    }

...
 }


There must be a more elegant solution that enables the utilization of construction injection. When I have time to research, I will post it.






Mock/Stub do EntityManager no Spring Roo

Problema

Ao tentar criar testes de unidade para o método persist de uma classe criada pelo Spring Roo, tentei utilizar um Stub para o EntityManager da seguinte forma:



public class ClasseATest {
    @Test
    public void aoPersistirDeveriaChamarClasseB() throws Exception {
        
        Mockery contexto = new Mockery();
        final ClasseB classeB = contexto.mock(ClasseB.class);
        
        final ClasseA classeA = 
            new ClasseA(getEntityManagerStub(), classeB);

        contexto.checking(new Expectations() {{
            one(classeB).fazerAlgo(classeA);

        }
    });
        classeA.persist();
        
        contexto.assertIsSatisfied();
    }

    private EntityManager getEntityManagerStub() {
        return new EntityManager() {
            ...
        }
    }
}
Código da ClasseA

public class ClasseA {
...

    public ClasseA(EntityManager entityManager, ClasseB classeB) {
        this.entityManager = entityManager;
        this.classeB = classeB;
    }



    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
     
        this.entityManager.persist(this);
     
        classeB.fazerAlgo(this);
     
    }

...
 }


O teste funcionava quando era executado dentro de Eclipse, mas falhava quando era executado via maven, no momento da execução do mvn jetty:run-exploded.


Motivo

Provavelmente o spring roo está injetando o EntityManager logo após a chamada ao constructor.

Solução

Utilizar setter injection, como mostrado abaixo:


public class ClasseATest {
    @Test
    public void aoPersistirDeveriaChamarClasseB() throws Exception {
        
        Mockery contexto = new Mockery();
        final ClasseB classeB = contexto.mock(ClasseB.class);
        
        final ClasseA classeA =
            new ClasseA(classeB);
        classeA.setEntityManager(getEntityManagerStub());
        
        contexto.checking(new Expectations() {{
            one(classeB).fazerAlgo(classeA);

        }
    });
        classeA.persist();
        
        contexto.assertIsSatisfied();
    }

    private EntityManager getEntityManagerStub() {

        return new EntityManager() {
            ...
        }

    }
}


Código da ClasseA


public class ClasseA {
...

    public ClasseA(ClasseB classeB) {
        this.classeB = classeB;
    }


    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }




    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
      
        this.entityManager.persist(this);
      
        classeB.fazerAlgo(this);
      
    }

...
 }


Provavelmente deve existir uma solução mais elegante, que permita a utilização da injeção da dependência no construtor. Quando tiver tempo vou pesquisar e bloggar a solução.






Wednesday, June 15, 2011

Keep CMD Open

Problem

The cmd window close after finish the execution of a bat file. The desired behavior is that the cmd stay opened to the input of new comands.

Reason

It is the default behavior.

Solution

  • Create a bat file that will call another bat. Ex: keppCmdOpen.bat
  • In the file put the line: start "" fileWithCommands.bat /k
  • Create the file fileWithCommands.bat and add the desired commands.

Others

If the need is just to see the window, and not to input commands, there is no need to two bats. Just put the pause command at the end of the file.

More info:

http://answers.yahoo.com/question/index?qid=20090306121738AAkg8Ij
http://pt.w3support.net/index.php?db=so&id=905352

Manter cmd aberto

Problema

A janela do cmd se fecha ao final da execução das instruções em um arquivo bat. O comportamento desejado é que o cmd permaneça aberto para a entrada de novos comandos.

Motivo

Este é o comportamento padrão.

Solução

  • Crie um arquivo bat que irá chamar outro. Por exemplo: manterCmdAberto.bat
  • No arquivo criado adicione a seguinte linha: start "" arquivoComComandos.bat /k
  • Crie o arquivo arquivoComComandos.bat e adicione os comando desejados.
Outros

Se o que se deseja é apenas visualizar o conteúdo da janela cmd, e não entrar com comandos, não precisa dos dois arquivos. Simplesmente no segundo arquivo (arquivoComComandos.bat) adicione a linha: pause

Mais informações nos seguinte links: