Java Code Examples for javax.faces.application.Application

The following are top voted examples for showing how to use javax.faces.application.Application. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: myfaces-trinidad   File: ServletExternalContext.java   View source code 6 votes vote down vote up
/**
 * Cut-and-paste reuse from org/apache/myfaces/context/servlet/ServletExternalContextImpl.
 *
 * Checks the Strings in the List for EL expressions and evaluates them.
 * Note that the returned List will be a copy of the given List, because
 * otherwise it will have unwanted side-effects.
 * @param context faces context
 * @param values from a query parameter that might have EL
 * @return resultant list will have any embedded EL evaluated
 */
private List<String> _evaluateValueExpressions(FacesContext context, List<String> values)
{
  // note that we have to create a new List here, because if we
  // change any value on the given List, it will be changed in the
  // NavigationCase too and the EL expression won't be evaluated again
  List<String> target = new ArrayList<String>(values.size());

  for (String value: values)
  {
    if (_isExpression(value))
    {
      if (context == null)
        throw new UnsupportedOperationException("FacesContext not established yet. Unable to resolve EL bound query" + 
                                                "parameter value: \"" + value + "\"");
      
      Application app = context.getApplication();
      String dynamicValue = app.evaluateExpressionGet(context, value, String.class);
      target.add(dynamicValue);
    }
    else
      target.add(value);
  }
  return target;
}
 
Example 2
Project: myfaces-trinidad   File: XmlHttpConfigurator.java   View source code 6 votes vote down vote up
/**
 * Handle a server-side error by reporting it back to the client.
 */
public static void handleError(ExternalContext ec, 
                               Throwable t) throws IOException
{
  String error = _getErrorString();
  _LOG.severe(error, t);

  ServletResponse response = (ServletResponse)ec.getResponse();
  PrintWriter writer = response.getWriter();
  XmlResponseWriter rw = new XmlResponseWriter(writer, "UTF-8");
  rw.startDocument();
  rw.startElement("partial-response", null);
  rw.startElement("error", null);
  rw.startElement("error-name", null);
  rw.writeText(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
  rw.endElement("error-name");

  String errorMessage = _getErrorMessage(error);

  // Default exception message contains the type of the exception.
  // Do not send this info to client in Production mode
  ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
  Application application = factory.getApplication();
  if (application.getProjectStage() != ProjectStage.Production)
  {
    errorMessage = _getExceptionString(t) + errorMessage;
  }

  rw.startElement("error-message", null);
  rw.writeText(errorMessage, null);
  rw.endElement("error-message");

  rw.endElement("error");
  rw.endElement("partial-response");
  rw.endDocument();
  rw.close();
}
 
Example 3
Project: myfaces-trinidad   File: LazyValueExpression.java   View source code 6 votes vote down vote up
private static ValueExpression _createValueExpressionFromApplication(
  String expression,
  Class<?> expectedType)
{
  ApplicationFactory factory = (ApplicationFactory)
    FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
  if (factory != null)
  {
    Application application = factory.getApplication();
    if (application != null)
    {
      ELContext elContext = _getELContext(application);

      ExpressionFactory expressionFactory = application.getExpressionFactory();
      if (expressionFactory != null)
      {
        return expressionFactory.createValueExpression(elContext, expression, expectedType);
      }
    }
  }

  return null;
}
 
Example 4
Project: myfaces-trinidad   File: UIXEditableValueTemplate.java   View source code 6 votes vote down vote up
/**
 * Executes validation logic.
 */
private void _executeValidate(FacesContext context)
{
  Application application = context.getApplication();
  application.publishEvent(context, PreValidateEvent.class, UIComponent.class, this);
  try
  {
    validate(context);
  }
  catch (RuntimeException e)
  {
    context.renderResponse();
    throw e;
  }
  finally
  {
    application.publishEvent(context, PostValidateEvent.class, UIComponent.class, this);
  }

  if (!isValid())
  {
    context.renderResponse();
  }
}
 
Example 5
Project: hiots   File: LightWidget.java   View source code 6 votes vote down vote up
public LightWidget(Long id, LightSensorVO sensor, Application application, IndexManager manager) {
	this.id = id;
	this.type = "Sensor";
	this.lightSensor = sensor;
	this.application = application;
	this.manager = manager;
	
	gaugeModel = new MeterGaugeChartModel(null, intervals);
	gaugeModel.setSeriesColors("00396B,81e741,f2ee00");
	gaugeModel.setGaugeLabelPosition("bottom");
	gaugeModel.setShowTickLabels(true);
	gaugeModel.setLabelHeightAdjust(0);
	gaugeModel.setMin(0);
   	gaugeModel.setMax(1000);
   	
   	gaugeModel.setTitle(lightSensor.getName());
   	gaugeModel.setValue(lightSensor.getData().getData());
   	gaugeModel.setGaugeLabel(lightSensor.getData().toString());
}
 
Example 6
Project: hiots   File: LightWidget.java   View source code 6 votes vote down vote up
public LightWidget(Long id, DummyLightSensorVO sensor, Application application, IndexManager manager) {
	this.id = id;
	this.type = "Sensor";
	this.lightSensor = sensor;
	this.application = application;
	this.manager = manager;
	
	gaugeModel = new MeterGaugeChartModel(null, intervals);
	gaugeModel.setSeriesColors("00396B,81e741,f2ee00");
	gaugeModel.setGaugeLabelPosition("bottom");
	gaugeModel.setShowTickLabels(true);
	gaugeModel.setLabelHeightAdjust(0);
	gaugeModel.setMin(0);
   	gaugeModel.setMax(1000);
   	
   	gaugeModel.setTitle(lightSensor.getName());
   	gaugeModel.setValue(lightSensor.getData().getData());
   	gaugeModel.setGaugeLabel(lightSensor.getData().toString());
}
 
Example 7
Project: hiots   File: TemperatureWidget.java   View source code 6 votes vote down vote up
public TemperatureWidget(Long id, TemperatureSensorVO tempSensor, Application application, IndexManager manager) {
	this.id = id;
	this.type = "Sensor";
	this.tempSensor = tempSensor;
	this.application = application;
	this.manager = manager;
	
	gaugeModel = new MeterGaugeChartModel(null, intervals);
	gaugeModel.setSeriesColors("2c7bf6,97dafb,88c82a,edec5b,c43030");
	gaugeModel.setGaugeLabelPosition("bottom");
	gaugeModel.setShowTickLabels(true);
	gaugeModel.setLabelHeightAdjust(0);
	gaugeModel.setMin(-10);
   	gaugeModel.setMax(60);
   	
   	gaugeModel.setTitle(tempSensor.getName());
   	gaugeModel.setValue(tempSensor.getData().getData());
   	gaugeModel.setGaugeLabel(tempSensor.getData().toString());
}
 
Example 8
Project: hiots   File: TemperatureWidget.java   View source code 6 votes vote down vote up
public TemperatureWidget(Long id, DummyTemperatureSensorVO tempSensor, Application application, IndexManager manager) {
	this.id  = id;
	this.type = "Sensor";
	this.tempSensor = tempSensor;
	this.application = application;
	this.manager = manager;
	
	gaugeModel = new MeterGaugeChartModel(null, intervals);
	gaugeModel.setSeriesColors("2c7bf6,97dafb,88c82a,f2ee00,c43030");
	gaugeModel.setGaugeLabelPosition("bottom");
	gaugeModel.setShowTickLabels(true);
	gaugeModel.setLabelHeightAdjust(0);
	gaugeModel.setMin(-10);
   	gaugeModel.setMax(60);
   	
   	gaugeModel.setTitle(tempSensor.getName());
   	gaugeModel.setValue(tempSensor.getData().getData());
   	gaugeModel.setGaugeLabel(tempSensor.getData().toString());
}
 
Example 9
Project: myWMS   File: JSFHelper.java   View source code 6 votes vote down vote up
public void logout() {
        FacesContext fc = javax.faces.context.FacesContext.getCurrentInstance();
        ((HttpSession) fc.getExternalContext().getSession(false)).invalidate();
//        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();        
        Application ap = fc.getApplication();
        NavigationHandler nh = ap.getNavigationHandler();
        //Generate Logout Dialog
        List<String> buttonTextList = new ArrayList<String>();
        buttonTextList.add(resolve("Login", new Object[]{}));
        NotifyDescriptorExt n = new NotifyDescriptorExt(Constants.INFORMATION_ICON, resolve("Logouted", new Object[]{}), resolve("LogoutMessage", new Object[]{}), buttonTextList);
        String page = n.setCallbackListener(new ButtonListener() {

            public String buttonClicked(final int buttonId, NotifyDescriptorExtBean notifyDescriptorBean) {
                return NavigationEnum.controller_CenterPanel.toString();
            }
        });
        //Load the NotifyDescriptor because after logout the JSF logic is expired.
        nh.handleNavigation(fc, null, page);
    }
 
Example 10
Project: jk-faces   File: JKJsfUtil.java   View source code 6 votes vote down vote up
/**
 * gets JSF information like JSF version and Faces context.
 *
 * @return the JSF info
 */
public static Map<String, Object> getJSFInfo() {
	final LinkedHashMap<String, Object> details = new LinkedHashMap<String, Object>();
	final FacesContext context = FacesContext.getCurrentInstance();
	final Application application = context.getApplication();
	final ViewHandler viewHandler = application.getViewHandler();
	final ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(context, context.getViewRoot().getViewId());
	final LifecycleFactory LifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);

	details.put("JSF-Version", FacesContext.class.getPackage().getImplementationVersion());
	details.put("JSF-Version-Package", FacesContext.class.getPackage().getName());
	details.put("FacesContext", context.getClass());
	details.put("Application", application.getClass());
	details.put("ViewHandler", viewHandler.getClass());
	details.put("ViewDeclarationLanguage", vdl.getClass());
	details.put("LifecycleFactory", LifecycleFactory.getClass());

	return details;
}
 
Example 11
Project: sample.mono-to-ms.pbw-monolith   File: Util.java   View source code 6 votes vote down vote up
/**
 * Output RAS message.
 * @param msg Message to be output.
 */
static final public void debug(String msg) {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
    	Application app = context.getApplication();
    	if (app != null) {
    		ProjectStage stage = app.getProjectStage();
    		if (stage == ProjectStage.Development || stage == ProjectStage.UnitTest) {
    			setDebug(true);
    		}
    	}
    	if (debug) {
    		System.out.println(msg);
    	}
    }
}
 
Example 12
Project: iclos   File: Navigator.java   View source code 6 votes vote down vote up
public void goToRightPage() {
    String viewId = this.originalViewId;
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    ViewHandler viewHandler = app.getViewHandler();
    UIViewRoot root = viewHandler.createView(context, viewId);
    context.setViewRoot(root);
    if ("/datacenter/dataCenterSimulationRealTime.xhtml".equals(viewId)) {
        this.dataCenterControllerSimulator.initHostMachineSimulatorList();
    }
    if ("/datacenter/dataCenterVirtualMachinePlacement.xhtml".equals(viewId)) {
        this.dataCenterController.getDataCenterVirtualMachinePlacementSimulator().setDataCenter(dataCenterController.getDataCenter());
    }
    if ("/datacenter/vmPlacementTestRunner.xhtml".equals(viewId)) {
        this.dataCenterController.getPlacementSimulatorTestRunner().setDataCenter(dataCenterController.getDataCenter());            
    }
}
 
Example 13
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static void setAction(UIComponent component,
  String attributeValue)
{
  if (attributeValue == null)
  {
    return;
  }
  if (UIComponentTag.isValueReference(attributeValue))
  {
    setMethodBinding(component, "action", attributeValue,
      new Class[]
      {});
  }
  else
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    MethodBinding mb = new ActionMethodBinding(attributeValue);
    component.getAttributes().put("action", mb);
  }
}
 
Example 14
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static void setMethodBinding(UIComponent component,
  String attributeName,
  String attributeValue, Class[] paramTypes)
{
  if (attributeValue == null)
  {
    return;
  }
  if (UIComponentTag.isValueReference(attributeValue))
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    MethodBinding mb = app.createMethodBinding(attributeValue,
                       paramTypes);
    component.getAttributes().put(attributeName, mb);
  }
}
 
Example 15
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static String eval(String expression)
{
  if (expression == null)
  {
    return null;
  }
  if (UIComponentTag.isValueReference(expression))
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    return "" + app.createValueBinding(expression).getValue(context);
  }
  else
  {
    return expression;
  }
}
 
Example 16
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
/**
 * Set an action on a component - used by tags setProperties() method.
 * Handles method bindings.
 */
public static void setAction(UIComponent component, String value)
{
    if (value == null)
    {
        return;
    }
    if (UIComponentTag.isValueReference(value))
    {
        setMethodBinding(component, "action", value, new Class[] {});
    } else
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        MethodBinding mb = new ActionMethodBinding(value);
        component.getAttributes().put("action", mb);
    }
}
 
Example 17
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
/**
 * Set a MethodBinding on a component - used by tags setProperties() method.
 */
public static void setMethodBinding(UIComponent component, String name, String value,
        Class[] paramTypes)
{
    if (value == null)
    {
        return;
    }
    if (UIComponentTag.isValueReference(value))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        MethodBinding mb = app.createMethodBinding(value, paramTypes);
        component.getAttributes().put(name, mb);
    }
}
 
Example 18
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static String eval(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        return "" + app.createValueBinding(expression).getValue(context);
    } else
    {
        return expression;
    }
}
 
Example 19
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Integer evalInteger(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Integer)
        {
            return (Integer) r;
        } else
        {
            return Integer.valueOf(r.toString());
        }
    } else
    {
        return Integer.valueOf(expression);
    }
}
 
Example 20
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Double evalDouble(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Double)
        {
            return (Double) r;
        } else
        {
            return Double.valueOf(r.toString());
        }
    } else
    {
        return Double.valueOf(expression);
    }
}
 
Example 21
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Boolean evalBoolean(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Boolean)
        {
            return (Boolean) r;
        } else
        {
            return Boolean.valueOf(r.toString());
        }
    } else
    {
        return Boolean.valueOf(expression);
    }
}
 
Example 22
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static void setMethodBinding(UIComponent component,
  String attributeName,
  String attributeValue, Class[] paramTypes)
{
  if (attributeValue == null)
  {
    return;
  }
  if (UIComponentTag.isValueReference(attributeValue))
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    MethodBinding mb = app.createMethodBinding(attributeValue,
                       paramTypes);
    component.getAttributes().put(attributeName, mb);
  }
}
 
Example 23
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static String eval(String expression)
{
  if (expression == null)
  {
    return null;
  }
  if (UIComponentTag.isValueReference(expression))
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Application app = context.getApplication();
    return "" + app.createValueBinding(expression).getValue(context);
  }
  else
  {
    return expression;
  }
}
 
Example 24
Project: sakai   File: RichTextEditArea.java   View source code 6 votes vote down vote up
public static void setValueBinding(UIComponent component, String attributeName,
        String attributeValue)
{
  FacesContext context = FacesContext.getCurrentInstance();
  Application app = context.getApplication();
  ValueBinding vb = app.createValueBinding(attributeValue);
  component.setValueBinding(attributeName, vb);
}
 
Example 25
Project: sakai   File: Tags.java   View source code 6 votes vote down vote up
public static void setMethodBinding(UIComponent component,
		String attributeName, String attributeValue, Class[] paramTypes) {
	if (attributeValue == null)
		return;
	if (UIComponentTag.isValueReference(attributeValue)) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		MethodBinding mb = app.createMethodBinding(attributeValue, paramTypes);
		component.getAttributes().put(attributeName, mb);
	}
}
 
Example 26
Project: sakai   File: Tags.java   View source code 6 votes vote down vote up
public static Integer evalInteger(String expression) {
	if (expression == null)
		return null;
	if (UIComponentTag.isValueReference(expression)) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		Object r = app.createValueBinding(expression).getValue(context);
		if (r == null)
			return null;
		else if (r instanceof Integer)
			return (Integer) r;
		else
			return new Integer(r.toString());
	} else
		return new Integer(expression);
}
 
Example 27
Project: sakai   File: Tags.java   View source code 6 votes vote down vote up
public static Double evalDouble(String expression) {
	if (expression == null)
		return null;
	if (UIComponentTag.isValueReference(expression)) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		Object r = app.createValueBinding(expression).getValue(context);
		if (r == null)
			return null;
		else if (r instanceof Double)
			return (Double) r;
		else
			return new Double(r.toString());
	} else
		return new Double(expression);
}
 
Example 28
Project: sakai   File: Tags.java   View source code 6 votes vote down vote up
public static Boolean evalBoolean(String expression) {
	if (expression == null)
		return null;
	if (UIComponentTag.isValueReference(expression)) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		Object r = app.createValueBinding(expression).getValue(context);
		if (r == null)
			return null;
		else if (r instanceof Boolean)
			return (Boolean) r;
		else
			return new Boolean(r.toString());
	} else
		return new Boolean(expression);
}
 
Example 29
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
/**
 * Set a MethodBinding on a component - used by tags setProperties() method.
 */
public static void setMethodBinding(UIComponent component, String name, String value,
        Class[] paramTypes)
{
    if (value == null)
    {
        return;
    }
    if (UIComponentTag.isValueReference(value))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        MethodBinding mb = app.createMethodBinding(value, paramTypes);
        component.getAttributes().put(name, mb);
    }
}
 
Example 30
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static String eval(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        return "" + app.createValueBinding(expression).getValue(context);
    } else
    {
        return expression;
    }
}
 
Example 31
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Integer evalInteger(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Integer)
        {
            return (Integer) r;
        } else
        {
            return Integer.valueOf(r.toString());
        }
    } else
    {
        return Integer.valueOf(expression);
    }
}
 
Example 32
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Double evalDouble(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Double)
        {
            return (Double) r;
        } else
        {
            return Double.valueOf(r.toString());
        }
    } else
    {
        return Double.valueOf(expression);
    }
}
 
Example 33
Project: sakai   File: TagUtil.java   View source code 6 votes vote down vote up
public static Boolean evalBoolean(String expression)
{
    if (expression == null)
    {
        return null;
    }
    if (UIComponentTag.isValueReference(expression))
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        Object r = app.createValueBinding(expression).getValue(context);
        if (r == null)
        {
            return null;
        } else if (r instanceof Boolean)
        {
            return (Boolean) r;
        } else
        {
            return Boolean.valueOf(r.toString());
        }
    } else
    {
        return Boolean.valueOf(expression);
    }
}
 
Example 34
Project: yawl   File: SessionBean.java   View source code 6 votes vote down vote up
/**
 * redirects to the specified page
 * @param page the name of the page to go to
 */
public void gotoPage(String page) {
    Application app = getApplication() ;
    if (app != null) {
        NavigationHandler navigator = app.getNavigationHandler();
        navigator.handleNavigation(getFacesContext(), null, page);
    }

    // if app is null, session has been destroyed
    else {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("Login.jsp");
        }
        catch (IOException ioe) {
            // message about destroyed app
        }
    }
}
 
Example 35
Project: yawl   File: SessionBean.java   View source code 6 votes vote down vote up
/**
 * redirects to the specified page
 * @param page the name of the page to go to
 */
public void gotoPage(String page) {
    Application app = getApplication() ;
    if (app != null) {
        NavigationHandler navigator = app.getNavigationHandler();
        navigator.handleNavigation(getFacesContext(), null, page);
    }

    // if app is null, session has been destroyed
    else {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("msLogin.jsp");
        }
        catch (IOException ioe) {
            // message about destroyed app
        }
    }
}
 
Example 36
Project: ft-components   File: MockFacesContext.java   View source code 6 votes vote down vote up
public static FacesContext mockFacesContext() {
    FacesContext context = Mockito.mock(FacesContext.class);
    Application application = Mockito.mock(Application.class);
    ExternalContext external = Mockito.mock(ExternalContext.class);
    ResourceBundle bundle = Mockito.mock(ResourceBundle.class);
    RenderKit renderKit = Mockito.mock(RenderKit.class);
    UIViewRoot viewRoot = Mockito.mock(UIViewRoot.class);
    ELContext elContext = Mockito.mock(ELContext.class);
    when(context.getApplication()).thenReturn(application);
    when(context.getExternalContext()).thenReturn(external);
    when(application.getResourceBundle(context, MessageProvider.BUNDLE_KEY)).thenReturn(bundle);
    when(context.getRenderKit()).thenReturn(renderKit);
    when(context.getViewRoot()).thenReturn(viewRoot);
    when(context.getELContext()).thenReturn(elContext);
    when(renderKit.getRenderer(Matchers.anyString(), Matchers.anyString())).thenReturn(Mockito.mock(Renderer.class));

    setCurrentInstance(context);
    Mockito.doAnswer(RELEASE).when(context).release();
    return context;
}
 
Example 37
Project: music-albums-obermueller   File: GuiUtilsTest.java   View source code 6 votes vote down vote up
/**
 * method under test: getFacesMessage
 */

@Test
public void thatGetFacesMessageReturnTranslatedMessage() throws Exception {
    when(mockedUIViewRoot.getLocale()).thenReturn(new Locale("de"));
    mockedApplication = mock(Application.class);
    when(mockedFacesContext.getApplication()).thenReturn(mockedApplication);
    when(mockedApplication.getMessageBundle()).thenReturn("test_messages");
    guiUtils = new GuiUtils();
    FacesMessage expectedMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Testwert", null);

    FacesMessage message = guiUtils.getFacesMessage(mockedFacesContext,
            FacesMessage.SEVERITY_INFO, "test", null);

    assertNotNull(message);
    assertEquals(expectedMessage.getSummary(), message.getSummary());
    assertEquals(expectedMessage.getSeverity(), message.getSeverity());
}
 
Example 38
Project: music-albums-obermueller   File: GuiUtilsTest.java   View source code 6 votes vote down vote up
@Test
public void thatGetFacesMessageReturnTranslatedMessageWithArguments() throws Exception {
    when(mockedUIViewRoot.getLocale()).thenReturn(new Locale("de"));
    mockedApplication = mock(Application.class);
    when(mockedFacesContext.getApplication()).thenReturn(mockedApplication);
    when(mockedApplication.getMessageBundle()).thenReturn("test_messages");
    guiUtils = new GuiUtils();
    FacesMessage expectedMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Testwert: value", null);

    FacesMessage message = guiUtils.getFacesMessage(mockedFacesContext,
            FacesMessage.SEVERITY_INFO, "test.value", new Object[] {"value"});

    assertNotNull(message);
    assertEquals(expectedMessage.getSummary(), message.getSummary());
    assertEquals(expectedMessage.getSeverity(), message.getSeverity());
}
 
Example 39
Project: converge-1.x   File: Common.java   View source code 6 votes vote down vote up
/**
 * Gets a {@link Map} of available languages supported by the application.
 * The languages are fetched from the JavaServer Faces configuration.
 *
 * @return {@link Map} of supported languages
 */
public Map<String, String> getAvailableLanguages() {
    Map<String, String> languages = new LinkedHashMap<String, String>();

    Application app = FacesContext.getCurrentInstance().getApplication();

    Locale loc = app.getDefaultLocale();
    languages.put(loc.getDisplayLanguage(loc), loc.getLanguage());

    for (Iterator i = app.getSupportedLocales(); i.hasNext();) {
        loc = (Locale) i.next();
        languages.put(loc.getDisplayLanguage(loc), loc.getLanguage());
    }

    return languages;
}
 
Example 40
Project: converge-1.x   File: StringFunctions.java   View source code 6 votes vote down vote up
/**
 * Returns a message from a {@link ResourceBundle} based on a given key. If the key could not be found in the bundle an empty {@link String} is returned.
 *
 * @param bundleId
 *          ID of the bundle in FacesConfig.xml
 * @param key
 *          Key of the message to extract
 * @param param
 *          Parameters to merge into the message
 * @return {@link String} containing the message
 */
public static String message(String bundleId, String key, Object param1, Object param2, Object param3, Object param4, Object param5) {
    FacesContext ctx = FacesContext.getCurrentInstance();
    Application application = ctx.getApplication();
    String msg = key;
    try {
        ResourceBundle bundle = application.getResourceBundle(ctx, bundleId);
        String msgPattern = bundle.getString(key);
        msg = msgPattern;

        Object[] params = new Object[]{param1, param2, param3, param4, param5};
        msg = MessageFormat.format(msg, params);
        return msg;
    } catch (Exception ex) {
        return "";
    }
}