Search in sources :

Example 46 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class CalendarUtilsTest method testGetLocale_ifRuLocale.

@Test
public void testGetLocale_ifRuLocale() {
    FacesContext facesContext = createMock(FacesContext.class);
    ExternalContext externalContext = createMock(ExternalContext.class);
    PowerMock.mockStatic(FacesContext.class);
    expect(FacesContext.getCurrentInstance()).andReturn(facesContext);
    expect(facesContext.getExternalContext()).andReturn(externalContext);
    expect(externalContext.getRequestLocale()).andReturn(new Locale("ru"));
    PowerMock.replayAll(FacesContext.class, facesContext, externalContext);
    Locale actual = CalendarUtils.getLocale();
    PowerMock.verifyAll();
    assertEquals(new Locale("ru", "RU"), actual);
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 47 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class CalendarUtilsTest method testGetLocale_ifFacesContextNotNull.

@Test
public void testGetLocale_ifFacesContextNotNull() {
    FacesContext facesContext = createMock(FacesContext.class);
    ExternalContext externalContext = createMock(ExternalContext.class);
    PowerMock.mockStatic(FacesContext.class);
    expect(FacesContext.getCurrentInstance()).andReturn(facesContext);
    expect(facesContext.getExternalContext()).andReturn(externalContext);
    expect(externalContext.getRequestLocale()).andReturn(Locale.CANADA);
    PowerMock.replayAll(FacesContext.class, facesContext, externalContext);
    Locale actual = CalendarUtils.getLocale();
    PowerMock.verifyAll();
    assertEquals(Locale.CANADA, actual);
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 48 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class WebCachedInterceptorTest method testProcess_resetCache.

@Test
public void testProcess_resetCache() throws Exception {
    InvocationContext context = createMock(InvocationContext.class);
    Method method = this.getClass().getMethod("getTestValue1");
    Map<String, Object> contextProperties = new HashMap<String, Object>();
    Object[] parameters = new Object[2];
    String targetInstance = "targetInstance";
    ExternalContext externalContext = createMock(ExternalContext.class);
    expect(context.getMethod()).andReturn(method);
    expect(context.getTarget()).andReturn(targetInstance);
    expect(facesContextMock.getExternalContext()).andReturn(externalContext);
    expect(externalContext.getRequestMap()).andReturn(contextProperties);
    expect(context.getParameters()).andReturn(parameters);
    expect(context.proceed()).andReturn(getTestValue());
    replay(context, facesContextMock, externalContext);
    interceptor.process(context);
    verify(context, facesContextMock, externalContext);
    String cacheId = "_webCache:" + targetInstance.getClass().getName();
    @SuppressWarnings("unchecked") Map<String, Object> cache = (Map<String, Object>) contextProperties.get(cacheId);
    assertNull(cache);
}
Also used : HashMap(java.util.HashMap) ExternalContext(javax.faces.context.ExternalContext) Method(java.lang.reflect.Method) InvocationContext(javax.interceptor.InvocationContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 49 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class WebCachedInterceptorTest method testProcess_getFromCache.

@SuppressWarnings("unchecked")
@Test
public void testProcess_getFromCache() throws Exception {
    InvocationContext context = createMock(InvocationContext.class);
    Method method = this.getClass().getMethod("getTestValue");
    Map<String, Object> contextProperties = new HashMap<String, Object>();
    Object[] parameters = new Object[2];
    String targetInstance = "targetInstance";
    ExternalContext externalContext = createMock(ExternalContext.class);
    expect(context.getMethod()).andReturn(method).times(2);
    expect(context.getTarget()).andReturn(targetInstance).times(2);
    expect(facesContextMock.getExternalContext()).andReturn(externalContext).times(2);
    expect(externalContext.getRequestMap()).andReturn(contextProperties).times(2);
    expect(context.getParameters()).andReturn(parameters).times(2);
    expect(context.proceed()).andReturn(getTestValue()).once();
    replay(context, facesContextMock, externalContext);
    Object actual = interceptor.process(context);
    actual = interceptor.process(context);
    verify(context, facesContextMock, externalContext);
    String cacheId = "_webCache:" + targetInstance.getClass().getName();
    Map<String, Object> cache = (Map<String, Object>) contextProperties.get(cacheId);
    WebCachedInterceptor.CacheKey cacheKey = interceptor.new CacheKey(targetInstance.getClass(), method, parameters);
    assertEquals(cache.get(cacheKey.toString()), actual);
}
Also used : HashMap(java.util.HashMap) ExternalContext(javax.faces.context.ExternalContext) Method(java.lang.reflect.Method) InvocationContext(javax.interceptor.InvocationContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 50 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class WebCachedInterceptorTest method testGetContextProperties_forSessionScope.

@Test
public void testGetContextProperties_forSessionScope() {
    Map<String, Object> contextProperties = new HashMap<String, Object>();
    ExternalContext externalContext = createMock(ExternalContext.class);
    expect(facesContextMock.getExternalContext()).andReturn(externalContext);
    expect(externalContext.getSessionMap()).andReturn(contextProperties);
    replay(facesContextMock, externalContext);
    Map<String, Object> actual = interceptor.getContextProperties(Scope.SESSION_SCOPED);
    verify(facesContextMock, externalContext);
    assertEquals(contextProperties, actual);
}
Also used : HashMap(java.util.HashMap) ExternalContext(javax.faces.context.ExternalContext) Test(org.junit.Test)

Aggregations

ExternalContext (javax.faces.context.ExternalContext)58 FacesContext (javax.faces.context.FacesContext)28 Test (org.junit.Test)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Map (java.util.Map)7 IOException (java.io.IOException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 Locale (java.util.Locale)4 FacesException (javax.faces.FacesException)4 Flash (javax.faces.context.Flash)4 InvocationContext (javax.interceptor.InvocationContext)4 ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)3 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2