Search in sources :

Example 1 with MockFacesContext

use of com.sun.faces.mock.MockFacesContext in project mojarra by eclipse-ee4j.

the class JUnitFacesTestCaseBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Set up Servlet API Objects
    servletContext = new MockServletContext();
    servletContext.addInitParameter("appParamName", "appParamValue");
    servletContext.setAttribute("appScopeName", "appScopeValue");
    config = new MockServletConfig(servletContext);
    session = new MockHttpSession();
    session.setAttribute("sesScopeName", "sesScopeValue");
    request = new MockHttpServletRequest(session);
    request.setAttribute("reqScopeName", "reqScopeValue");
    response = new MockHttpServletResponse();
    // Set up Faces API Objects
    FactoryFinder.releaseFactories();
    Method reInitializeFactoryManager = FactoryFinder.class.getDeclaredMethod("reInitializeFactoryManager", (Class<?>[]) null);
    reInitializeFactoryManager.setAccessible(true);
    reInitializeFactoryManager.invoke(null, (Object[]) null);
    // Create something to stand-in as the InitFacesContext
    new MockFacesContext(new MockExternalContext(servletContext, request, response), new MockLifecycle());
    FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY, "com.sun.faces.mock.MockFacesContextFactory");
    FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, "com.sun.faces.mock.MockLifecycleFactory");
    FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, "com.sun.faces.mock.MockApplicationFactory");
    FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY, "com.sun.faces.mock.MockRenderKitFactory");
    FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
    LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    lifecycle = (MockLifecycle) lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
    facesContext = (MockFacesContext) fcFactory.getFacesContext(servletContext, request, response, lifecycle);
    externalContext = (MockExternalContext) facesContext.getExternalContext();
    Map map = new HashMap();
    externalContext.setRequestParameterMap(map);
    ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
    application = (MockApplication) applicationFactory.getApplication();
    facesContext.setApplication(application);
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) MockLifecycle(com.sun.faces.mock.MockLifecycle) MockExternalContext(com.sun.faces.mock.MockExternalContext) HashMap(java.util.HashMap) MockHttpServletRequest(com.sun.faces.mock.MockHttpServletRequest) FacesContextFactory(jakarta.faces.context.FacesContextFactory) LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory) MockServletConfig(com.sun.faces.mock.MockServletConfig) Method(java.lang.reflect.Method) MockServletContext(com.sun.faces.mock.MockServletContext) MockHttpSession(com.sun.faces.mock.MockHttpSession) ApplicationFactory(jakarta.faces.application.ApplicationFactory) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(com.sun.faces.mock.MockHttpServletResponse)

Example 2 with MockFacesContext

use of com.sun.faces.mock.MockFacesContext in project mojarra by eclipse-ee4j.

the class UIComponentBaseAttachedStateTestCase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    component = new UIOutput();
    facesContext = new MockFacesContext();
    servletContext = new MockServletContext();
    servletContext.addInitParameter("appParamName", "appParamValue");
    servletContext.setAttribute("appScopeName", "appScopeValue");
    request = new MockHttpServletRequest(null);
    request.setAttribute("reqScopeName", "reqScopeValue");
    response = new MockHttpServletResponse();
    // Create something to stand-in as the InitFacesContext
    new MockFacesContext(new MockExternalContext(servletContext, request, response), new MockLifecycle());
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) MockLifecycle(com.sun.faces.mock.MockLifecycle) MockExternalContext(com.sun.faces.mock.MockExternalContext) MockHttpServletRequest(com.sun.faces.mock.MockHttpServletRequest) MockServletContext(com.sun.faces.mock.MockServletContext) MockHttpServletResponse(com.sun.faces.mock.MockHttpServletResponse)

Example 3 with MockFacesContext

use of com.sun.faces.mock.MockFacesContext in project mojarra by eclipse-ee4j.

the class UIOutputAttachedObjectStateTestCase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    facesContext = new MockFacesContext();
    facesContext = new MockFacesContext();
    servletContext = new MockServletContext();
    servletContext.addInitParameter("appParamName", "appParamValue");
    servletContext.setAttribute("appScopeName", "appScopeValue");
    request = new MockHttpServletRequest(null);
    request.setAttribute("reqScopeName", "reqScopeValue");
    response = new MockHttpServletResponse();
    // Create something to stand-in as the InitFacesContext
    new MockFacesContext(new MockExternalContext(servletContext, request, response), new MockLifecycle());
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) MockLifecycle(com.sun.faces.mock.MockLifecycle) MockExternalContext(com.sun.faces.mock.MockExternalContext) MockHttpServletRequest(com.sun.faces.mock.MockHttpServletRequest) MockServletContext(com.sun.faces.mock.MockServletContext) MockHttpServletResponse(com.sun.faces.mock.MockHttpServletResponse)

Example 4 with MockFacesContext

use of com.sun.faces.mock.MockFacesContext in project newrelic-java-agent by newrelic.

the class JSFTest method testPhase.

@Test
public void testPhase() {
    final List<Tracer> tracers = new ArrayList<>();
    final MockFacesContext mockFacesContext = new MockFacesContext();
    final Phase phase = new Phase() {

        @Override
        public void execute(FacesContext arg0) throws FacesException {
            Tracer lastTracer = Transaction.getTransaction().getTransactionActivity().getLastTracer();
            tracers.add(lastTracer);
        }

        @Override
        protected void handleAfterPhase(FacesContext arg0, ListIterator<PhaseListener> arg1, PhaseEvent arg2) {
        }

        @Override
        protected void handleBeforePhase(FacesContext arg0, ListIterator<PhaseListener> arg1, PhaseEvent arg2) {
        }

        @Override
        protected void queueException(FacesContext ctx, Throwable t, String booleanKey) {
            super.queueException(ctx, t, booleanKey);
        }

        @Override
        protected void queueException(FacesContext ctx, Throwable t) {
            super.queueException(ctx, t);
        }

        @Override
        public PhaseId getId() {
            return PhaseId.ANY_PHASE;
        }
    };
    new Runnable() {

        @Override
        @Trace(dispatcher = true)
        public void run() {
            phase.doPhase(mockFacesContext, new MockLifecycle(), new ArrayList<PhaseListener>().listIterator());
        }
    }.run();
    Assert.assertEquals(1, tracers.size());
    Tracer tracer = tracers.get(0);
    ClassMethodSignature sig = new ClassMethodSignature(phase.getClass().getName(), "doPhase", "(Ljavax/faces/context/FacesContext;Ljavax/faces/lifecycle/Lifecycle;Ljava/util/ListIterator;)V");
    Assert.assertEquals(ClassMethodMetricNameFormat.getMetricName(sig, phase, MetricNames.CUSTOM), tracer.getMetricName());
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) MockLifecycle(com.sun.faces.mock.MockLifecycle) MockFacesContext(com.sun.faces.mock.MockFacesContext) FacesContext(javax.faces.context.FacesContext) Phase(com.sun.faces.lifecycle.Phase) PhaseEvent(javax.faces.event.PhaseEvent) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator) Trace(com.newrelic.api.agent.Trace) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) PhaseListener(javax.faces.event.PhaseListener) Test(org.junit.Test)

Example 5 with MockFacesContext

use of com.sun.faces.mock.MockFacesContext in project mojarra by eclipse-ee4j.

the class FacesLoggerTest method unresolvedNavigationGermany.

@Test
public void unresolvedNavigationGermany() {
    MockFacesContext facesContext = new MockFacesContext();
    facesContext.setApplication(new MockApplication());
    facesContext.setELContext(EasyMock.createMock(ELContext.class));
    UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null);
    root.setLocale(Locale.GERMANY);
    facesContext.setViewRoot(root);
    final Object[] params = { "componentId1", "outcome.xhtml", "viewId.xhtml" };
    Locale.setDefault(new Locale("de", "DE"));
    final String result = FacesLogger.RENDERKIT.interpolateMessage(facesContext, "faces.outcometarget.navigation.case.not.resolved", params);
    Assert.assertEquals("JSF1090: Navigations-Fall wurde für Komponente componentId1 nicht aufgelöst.", result);
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) Locale(java.util.Locale) ELContext(jakarta.el.ELContext) MockApplication(com.sun.faces.mock.MockApplication) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Aggregations

MockFacesContext (com.sun.faces.mock.MockFacesContext)6 MockLifecycle (com.sun.faces.mock.MockLifecycle)4 MockExternalContext (com.sun.faces.mock.MockExternalContext)3 MockHttpServletRequest (com.sun.faces.mock.MockHttpServletRequest)3 MockHttpServletResponse (com.sun.faces.mock.MockHttpServletResponse)3 MockServletContext (com.sun.faces.mock.MockServletContext)3 Test (org.junit.Test)3 MockApplication (com.sun.faces.mock.MockApplication)2 ELContext (jakarta.el.ELContext)2 UIViewRoot (jakarta.faces.component.UIViewRoot)2 Locale (java.util.Locale)2 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)1 Tracer (com.newrelic.agent.tracers.Tracer)1 Trace (com.newrelic.api.agent.Trace)1 Phase (com.sun.faces.lifecycle.Phase)1 MockHttpSession (com.sun.faces.mock.MockHttpSession)1 MockServletConfig (com.sun.faces.mock.MockServletConfig)1 ApplicationFactory (jakarta.faces.application.ApplicationFactory)1 FacesContextFactory (jakarta.faces.context.FacesContextFactory)1 LifecycleFactory (jakarta.faces.lifecycle.LifecycleFactory)1