[JBoss Seam] - Seam and JUnit
by dahm
Hi,
I tried to get Seam tests running with JUnit. As a first approach I took one
of the NG-Tests from the examples and tried to adapt it:
|
| public class SeamTest extends TestCase {
|
| private MockExternalContext externalContext;
| private MockServletContext servletContext;
| private MockApplication application;
| private SeamPhaseListener phases;
| private MockFacesContext facesContext;
| private MockHttpSession session;
| private Map<String, ConversationState> conversationStates;
|
| protected void setUp() throws Exception {
| init();
| begin();
| }
|
| protected void tearDown() throws Exception {
| end();
| cleanup();
| }
|
| public void testDefault() throws Exception {
| new Script() {
|
| @Override
| protected void updateModelValues() throws Exception
| {
| User user = (User) Component.getInstance("user", true);
| assert user!=null;
| user.setUserName("1ovthafew");
| user.setPassword("secret");
| user.setRealName("Gavin King");
| }
|
| @Override
| protected void invokeApplication()
| {
| LoginAction register = (LoginAction) Component.getInstance("login", true);
| String outcome = register.login();
| assert "success".equals( outcome );
| }
|
| @Override
| protected void renderResponse()
| {
| User user = (User) Component.getInstance("user", false);
| assert user!=null;
| assert user.getRealName().equals("Gavin King");
| assert user.getUserName().equals("1ovthafew");
| assert user.getPassword().equals(null);
| }
|
| }.run();
| }
|
| protected HttpSession getSession()
| {
| return (HttpSession) externalContext.getSession(true);
| }
|
| protected boolean isSessionInvalid()
| {
| return ( (MockHttpSession) getSession() ).isInvalid();
| }
|
| protected FacesContext getFacesContext()
| {
| return facesContext;
| }
|
| /**
| * Script is an abstract superclass for usually anonymous
| * inner classes that test JSF interactions.
| *
| * @author Gavin King
| */
| public abstract class Script
| {
| private String conversationId;
| private String outcome;
| private boolean validationFailed;
|
| /**
| * A script for a JSF interaction with
| * no existing long-running conversation.
| */
| protected Script() {}
|
| /**
| * A script for a JSF interaction in the
| * scope of an existing long-running
| * conversation.
| */
| protected Script(String id)
| {
| conversationId = id;
| }
|
| protected boolean isGetRequest()
| {
| return false;
| }
|
| protected String getViewId()
| {
| return null;
| }
|
| /**
| * Helper method for resolving components in
| * the test script.
| */
| protected Object getInstance(Class clazz)
| {
| return Component.getInstance(clazz, true);
| }
|
| /**
| * Helper method for resolving components in
| * the test script.
| */
| protected Object getInstance(String name)
| {
| return Component.getInstance(name, true);
| }
|
| /**
| * Override to implement the interactions between
| * the JSF page and your components that occurs
| * during the apply request values phase.
| */
| protected void applyRequestValues() throws Exception {}
| /**
| * Override to implement the interactions between
| * the JSF page and your components that occurs
| * during the process validations phase.
| */
| protected void processValidations() throws Exception {}
| /**
| * Override to implement the interactions between
| * the JSF page and your components that occurs
| * during the update model values phase.
| */
| protected void updateModelValues() throws Exception {}
| /**
| * Override to implement the interactions between
| * the JSF page and your components that occurs
| * during the invoke application phase.
| */
| protected void invokeApplication() throws Exception {}
| protected void setOutcome(String outcome) { this.outcome = outcome; }
| protected String getInvokeApplicationOutcome() { return outcome; }
| /**
| * Override to implement the interactions between
| * the JSF page and your components that occurs
| * during the render response phase.
| */
| protected void renderResponse() throws Exception {}
| /**
| * Override to set up any request parameters for
| * the request.
| */
| protected void setup() {}
|
| public Map<String, String[]> getParameters()
| {
| return ( (MockHttpServletRequest) externalContext.getRequest() ).getParameters();
| }
|
| public Map<String, String[]> getHeaders()
| {
| return ( (MockHttpServletRequest) externalContext.getRequest() ).getHeaders();
| }
|
| protected void validate(Class modelClass, String property, Object value)
| {
| ClassValidator validator = Validation.getValidator(modelClass);
| InvalidValue[] ivs = validator.getPotentialInvalidValues(property, value);
| if (ivs.length>0)
| {
| validationFailed = true;
| FacesMessage message = FacesMessages.createFacesMessage( FacesMessage.SEVERITY_WARN, ivs[0].getMessage() );
| FacesContext.getCurrentInstance().addMessage( property, /*TODO*/ message );
| FacesContext.getCurrentInstance().renderResponse();
| }
| }
|
| public boolean isValidationFailure()
| {
| return validationFailed;
| }
|
| @SuppressWarnings("unchecked")
| public String run() throws Exception
| {
| externalContext = new MockExternalContext(servletContext, session);
| facesContext = new MockFacesContext( externalContext, application );
| facesContext.setCurrent();
| Map attributes = facesContext.getViewRoot().getAttributes();
|
| if ( !isGetRequest() && conversationId!=null )
| {
| if ( conversationStates.containsKey( conversationId ) )
| {
| attributes.putAll(
| conversationStates.get( conversationId ).state
| );
| }
| }
|
| setup();
|
| facesContext.getViewRoot().setViewId( getViewId() );
|
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, MockLifecycle.INSTANCE) );
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, MockLifecycle.INSTANCE) );
|
| if ( !isGetRequest() )
| {
|
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES, MockLifecycle.INSTANCE) );
|
| applyRequestValues();
|
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.APPLY_REQUEST_VALUES, MockLifecycle.INSTANCE) );
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS, MockLifecycle.INSTANCE) );
|
| processValidations();
|
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.PROCESS_VALIDATIONS, MockLifecycle.INSTANCE) );
|
| if ( !FacesContext.getCurrentInstance().getRenderResponse() )
| {
|
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES, MockLifecycle.INSTANCE) );
|
| updateModelValues();
|
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.UPDATE_MODEL_VALUES, MockLifecycle.INSTANCE) );
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION, MockLifecycle.INSTANCE) );
|
| invokeApplication();
|
| String outcome = getInvokeApplicationOutcome();
| facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
|
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION, MockLifecycle.INSTANCE) );
|
| }
|
| }
|
| if ( !FacesContext.getCurrentInstance().getResponseComplete() )
| {
|
| phases.beforePhase( new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, MockLifecycle.INSTANCE) );
|
| //TODO: hackish workaround for the fact that page actions don't get called!
| if ( isGetRequest() )
| {
| Lifecycle.setPhaseId(PhaseId.INVOKE_APPLICATION);
| invokeApplication();
| Lifecycle.setPhaseId(PhaseId.RENDER_RESPONSE);
| }
|
| renderResponse();
|
| facesContext.getApplication().getStateManager().saveSerializedView(facesContext);
|
| phases.afterPhase( new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, MockLifecycle.INSTANCE ) );
|
| }
|
| Map pageContextMap = (Map) attributes.get( ScopeType.PAGE.getPrefix() );
| if (pageContextMap!=null)
| {
| conversationId = (String) pageContextMap.get(Manager.CONVERSATION_ID);
| ConversationState conversationState = new ConversationState();
| conversationState.state.putAll( attributes );
| conversationStates.put( conversationId, conversationState );
| }
|
| return conversationId;
| }
|
| }
|
| public void begin()
| {
| session = new MockHttpSession(servletContext);
| }
|
| public void end()
| {
| Lifecycle.endSession( servletContext, new ServletSessionImpl(session) );
| session = null;
| }
|
| /**
| * Create a SeamPhaseListener by default. Override to use
| * one of the other standard Seam phase listeners.
| */
| protected SeamPhaseListener createPhaseListener()
| {
| return new SeamPhaseListener();
| }
|
| public void init() throws Exception
| {
| application = new MockApplication();
| application.setStateManager( new SeamStateManager( application.getStateManager() ) );
| application.setNavigationHandler( new SeamNavigationHandler( application.getNavigationHandler() ) );
| //don't need a SeamVariableResolver, because we don't test the view
| phases = createPhaseListener();
|
| servletContext = new MockServletContext();
| initServletContext( servletContext.getInitParameters() );
| new Initialization(servletContext).init();
| Lifecycle.setServletContext(servletContext);
|
| conversationStates = new HashMap<String, ConversationState>();
| }
|
| public void cleanup() throws Exception
| {
| Lifecycle.endApplication(servletContext);
| externalContext = null;
| conversationStates.clear();
| conversationStates = null;
| }
|
| /**
| * Override to set up any servlet context attributes.
| */
| public void initServletContext(Map initParams) {}
|
| private class ConversationState
| {
| Map state = new HashMap();
| }
|
| protected InitialContext getInitialContext() throws NamingException {
| return Naming.getInitialContext();
| }
|
| protected UserTransaction getUserTransaction() throws NamingException
| {
| return Transactions.getUserTransaction();
| }
|
| protected Object getField(Object object, String fieldName)
| {
| try
| {
| Field declaredField = object.getClass().getDeclaredField(fieldName);
| if ( !declaredField.isAccessible() ) declaredField.setAccessible(true);
| return declaredField.get(object);
| }
| catch (Exception e)
| {
| throw new IllegalArgumentException("could not get field value: " + fieldName, e);
| }
| }
|
| protected void setField(Object object, String fieldName, Object value)
| {
| try
| {
| Field declaredField = object.getClass().getDeclaredField(fieldName);
| if ( !declaredField.isAccessible() ) declaredField.setAccessible(true);
| declaredField.set(object, value);
| }
| catch (Exception e)
| {
| throw new IllegalArgumentException("could not set field value: " + fieldName, e);
| }
| }
| }
|
I'm not sure what's wrong with it, all I get is:
| java.lang.AssertionError
| at de.kvbb.ingver.test.SeamTest$1.updateModelValues(SeamTest.java:72)
| at de.kvbb.ingver.test.SeamTest$Script.run(SeamTest.java:280)
| at de.kvbb.ingver.test.SeamTest.testDefault(SeamTest.java:96)
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
| at java.lang.reflect.Method.invoke(Method.java:585)
| at junit.framework.TestCase.runTest(TestCase.java:154)
| at junit.framework.TestCase.runBare(TestCase.java:127)
| at junit.framework.TestResult$1.protect(TestResult.java:106)
| at junit.framework.TestResult.runProtected(TestResult.java:124)
| at junit.framework.TestResult.run(TestResult.java:109)
| at junit.framework.TestCase.run(TestCase.java:118)
| at junit.framework.TestSuite.runTest(TestSuite.java:208)
| at junit.framework.TestSuite.run(TestSuite.java:203)
| at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
| at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
| at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
|
Thanks in advance
Markus
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3978994#3978994
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3978994
19 years, 8 months
[JBoss Seam] - Re: Remoting and java map to js map error conversion
by fguerzoni
I thank you very much!
Now I get an error using the latest cvs version.
In fact I get a ClassNotFoundException on org.jboss.seam.core.Hibernate.
Looking for that on Fisheye shows that this class was deleted 3 days ago.
Could you suggest how to modify my components.xml in order to work with latest cvs version?
My actually configuration is
<components>
|
| <component name="org.jboss.seam.core.init">
| <property name="myFacesLifecycleBug">false</property>
| <property name="debug">true</property>
| </component>
|
| <!-- 120 second conversation timeout -->
| <component name="org.jboss.seam.core.manager">
| <property name="conversationTimeout">120000</property>
| </component>
|
| <!-- Bootstrap Hibernate -->
| <component name="cbiFeDatabase"
| class="org.jboss.seam.core.ManagedHibernateSession"/>
| <component class="org.jboss.seam.core.Hibernate" installed="true"/>
| <component class="org.jboss.seam.core.Microcontainer"
| installed="true"/>
|
| <component class="org.jboss.seam.core.Ejb" installed="false"/>
|
| </components>
the stack trace is
C:\jetty-6.0.1>java -jar start.jar
2006-10-18 08:47:53.500::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2006-10-18 08:47:53.718::INFO: jetty-6.0.1
2006-10-18 08:47:54.312::INFO: Extract jar:file:/C:/jetty-6.0.1/webapps/CBI_FE.war!/ to C:\DOCUME~1\CR00570\IMPOST~1\Te
mp\Jetty_0_0_0_0_8080__CBI_FE_\webapp
08:48:03,312 INFO [ServletContextListener] Welcome to Seam 1.1.0.BETA
08:48:03,390 INFO [Initialization] reading components.xml
2006-10-18 08:48:03.484::WARN: failed ContextHandler@18f1d7e{/CBI_FE,file:/C:/Documents%20and%20Settings/CR00570/Impost
azioni%20locali/Temp/Jetty_0_0_0_0_8080__CBI_FE_/webapp/}
2006-10-18 08:48:03.484::WARN: failed ContextHandlerCollection@d9660d
2006-10-18 08:48:03.531::WARN: failed HandlerCollection@55e55f
2006-10-18 08:48:04.250::INFO: Started SelectChannelConnector @ 0.0.0.0:8080
2006-10-18 08:48:04.250::WARN: failed Server@f47396
2006-10-18 08:48:04.250::WARN: EXCEPTION
java.lang.RuntimeException: error while reading components.xml
at org.jboss.seam.init.Initialization.initPropertiesFromXml(Initialization.java:192)
at org.jboss.seam.init.Initialization.(Initialization.java:126)
at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:32)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:450)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1129)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:420)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:457)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:38)
at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:156)
at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:120)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:38)
at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:156)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:38)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:119)
at org.mortbay.jetty.Server.doStart(Server.java:210)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:38)
at org.mortbay.xml.XmlConfiguration.main(XmlConfiguration.java:905)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mortbay.start.Main.invokeMain(Main.java:183)
at org.mortbay.start.Main.start(Main.java:497)
at org.mortbay.start.Main.main(Main.java:115)
Caused by: java.lang.ClassNotFoundException: org.jboss.seam.core.Hibernate
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:358)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:320)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.jboss.seam.util.Reflections.classForName(Reflections.java:141)
at org.jboss.seam.init.Initialization.installComponent(Initialization.java:217)
at org.jboss.seam.init.Initialization.initPropertiesFromXml(Initialization.java:164)
... 23 more
2006-10-18 08:50:56.140::INFO: Shutdown hook executing
2006-10-18 08:50:56.140::INFO: Shutdown hook complete
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3978988#3978988
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3978988
19 years, 8 months
[JBoss Seam] - Re: Conversor error in examples/secutiry
by rengar
-In example/security/resources/import.sql (CVS) :
insert into Users (username, password) values ('shane', 'password')
| insert into Users (username, password) values ('demo', 'demo')
| insert into Role (roleid, username, role) values (1, 'shane', 'user');
| insert into Role (roleid, username, role) values (2, 'shane', 'admin');
| insert into Role (roleid, username, role) values (3, 'demo', 'user');
| insert into Role (roleid, username, role) values (4, 'demo', 'admin');
-Don't have column "user_username"
-In Role.java :
| package org.jboss.seam.example.security;
|
| import javax.persistence.Entity;
| import org.jboss.seam.annotations.Name;
| import javax.persistence.Id;
| import javax.persistence.ManyToOne;
| import javax.persistence.JoinColumn;
|
| /**
| * A user role.
| *
| * @author Shane Bryzak
| * @version 1.0
| */
| @Entity
| @Name("userrole")
| public class Role
| {
| private Integer roleId;
| private User user;
| private String role;
|
| @Id
| public Integer getRoleId()
| {
| return roleId;
| }
|
| @ManyToOne
| public User getUser()
| {
| return user;
| }
|
| public String getRole()
| {
| return role;
| }
|
| public void setRoleId(Integer roleId)
| {
| this.roleId = roleId;
| }
|
| public void setUser(User user)
| {
| this.user = user;
| }
|
| public void setRole(String role)
| {
| this.role = role;
| }
| }
|
-In User.java :
| package org.jboss.seam.example.security;
|
| import java.util.Set;
| import javax.persistence.Entity;
| import javax.persistence.Id;
| import javax.persistence.OneToMany;
| import javax.persistence.Table;
|
| import org.jboss.seam.annotations.Name;
| import javax.persistence.JoinColumn;
|
| /**
| *
| * @author Shane Bryzak
| */
| @Entity
| @Name("user")
| @Table(name="Users")
| public class User
| {
| private String username;
| private String password;
| private Set<Role> roles;
|
| @Id
| public String getUsername()
| {
| return username;
| }
|
| public String getPassword()
| {
| return password;
| }
|
| @OneToMany
| @JoinColumn(name = "USERNAME")
| public Set<Role> getRoles()
| {
| return roles;
| }
|
| public void setPassword(String password)
| {
| this.password = password;
| }
|
| public void setUsername(String username)
| {
| this.username = username;
| }
|
| public void setRoles(Set<Role> roles)
| {
| this.roles = roles;
| }
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3978981#3978981
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3978981
19 years, 8 months