Author: nbelaevski
Date: 2010-05-25 19:49:09 -0400 (Tue, 25 May 2010)
New Revision: 17242
Added:
root/ui/misc/trunk/ui/src/test/
root/ui/misc/trunk/ui/src/test/java/
root/ui/misc/trunk/ui/src/test/java/org/
root/ui/misc/trunk/ui/src/test/java/org/richfaces/
root/ui/misc/trunk/ui/src/test/java/org/richfaces/function/
root/ui/misc/trunk/ui/src/test/java/org/richfaces/function/RichFunctionTest.java
Modified:
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/RichFunction.java
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/package-info.java
Log:
https://jira.jboss.org/browse/RF-7983
Modified: root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/RichFunction.java
===================================================================
---
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/RichFunction.java 2010-05-25
22:58:22 UTC (rev 17241)
+++
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/RichFunction.java 2010-05-25
23:49:09 UTC (rev 17242)
@@ -39,64 +39,81 @@
public final class RichFunction {
+ //EasyMock requires at least protected access for the interface for calls to be
delegated to
+ protected static interface ComponentLocator {
+
+ UIComponent findComponent(FacesContext facesContext, UIComponent
contextComponent, String id);
+
+ }
+
+ private static ComponentLocator locator = new ComponentLocator() {
+
+ private final RendererUtils utils = RendererUtils.getInstance();
+
+ public UIComponent findComponent(FacesContext context, UIComponent
contextComponent, String id) {
+ return utils.findComponentFor(context, contextComponent, id);
+ }
+ };
+
private RichFunction() {
//utility class constructor
}
-
- private static UIComponent findComponent(FacesContext context, String id) {
- if (id != null) {
- if (context != null) {
- UIComponent contextComponent = UIComponent.getCurrentComponent(context);
- if (contextComponent == null) {
- contextComponent = context.getViewRoot();
- }
- if (contextComponent != null) {
- UIComponent component =
RendererUtils.getInstance().findComponentFor(contextComponent, id);
+ //used by unit tests
+ static void setComponentLocator(ComponentLocator mockLocator) {
+ locator = mockLocator;
+ }
- if (component != null) {
- return component;
- }
- }
- }
- }
+ private static UIComponent findComponent(FacesContext context, String id) {
+ if (id != null) {
+ UIComponent contextComponent = UIComponent.getCurrentComponent(context);
+ if (contextComponent == null) {
+ contextComponent = context.getViewRoot();
+ }
- return null;
- }
-
+ UIComponent component = locator.findComponent(context, contextComponent,
id);
+
+ if (component != null) {
+ return component;
+ }
+ }
+
+ return null;
+ }
+
@Function
public static String clientId(String id) {
- FacesContext context = FacesContext.getCurrentInstance();
- UIComponent component = findComponent(context, id);
- return component != null ? component.getClientId(context) : null;
+ FacesContext context = FacesContext.getCurrentInstance();
+ UIComponent component = findComponent(context, id);
+ return component != null ? component.getClientId(context) : null;
}
-
+
@Function
public static String component(String id) {
- String element = element(id);
- if (element != null) {
+ String clientId = clientId(id);
+ if (clientId != null) {
//TODO nick - what if jQuery.RichFaces doesn't exist?
- return "RichFaces.$(" + clientId(id) + ")";
- }
+ return "RichFaces.$('" + clientId + "')";
+ }
- return null;
+ return null;
}
@Function
public static String element(String id) {
- String clientId = clientId(id);
- if (clientId != null) {
- return "document.getElementById('" + clientId + "')";
- }
+ String clientId = clientId(id);
+ if (clientId != null) {
+ return "document.getElementById('" + clientId +
"')";
+ }
- return null;
+ return null;
}
@Function
public static UIComponent findComponent(String id) {
- return findComponent(FacesContext.getCurrentInstance(), id);
+ return findComponent(FacesContext.getCurrentInstance(), id);
}
-
+
/**
* @since 3.3.1
* @param rolesObject
@@ -105,18 +122,18 @@
@Function
public static boolean isUserInRole(Object rolesObject) {
//TODO nick - AjaxRendererUtils split text by commas and whitespace, what is the
right variant?
- Set<String> rolesSet = AjaxRendererUtils.asSet(rolesObject);
- if (rolesSet != null) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- ExternalContext externalContext = facesContext.getExternalContext();
+ Set<String> rolesSet = AjaxRendererUtils.asSet(rolesObject);
+ if (rolesSet != null) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
- for (String role : rolesSet) {
- if (externalContext.isUserInRole(role)) {
- return true;
- }
- }
- }
-
- return false;
+ for (String role : rolesSet) {
+ if (externalContext.isUserInRole(role)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
}
}
Modified: root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/package-info.java
===================================================================
---
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/package-info.java 2010-05-25
22:58:22 UTC (rev 17241)
+++
root/ui/misc/trunk/ui/src/main/java/org/richfaces/function/package-info.java 2010-05-25
23:49:09 UTC (rev 17242)
@@ -1,4 +1,6 @@
/**
* Implementation of RichFaces functions library
*/
+@TagLibrary(uri="http://richfaces.org/misc", shortName="misc")
package org.richfaces.function;
+import org.richfaces.cdk.annotations.TagLibrary;
Added: root/ui/misc/trunk/ui/src/test/java/org/richfaces/function/RichFunctionTest.java
===================================================================
--- root/ui/misc/trunk/ui/src/test/java/org/richfaces/function/RichFunctionTest.java
(rev 0)
+++
root/ui/misc/trunk/ui/src/test/java/org/richfaces/function/RichFunctionTest.java 2010-05-25
23:49:09 UTC (rev 17242)
@@ -0,0 +1,225 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.function;
+
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.same;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+import org.easymock.EasyMock;
+import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.richfaces.function.RichFunction.ComponentLocator;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class RichFunctionTest {
+
+ private static final class StubComponentLocator implements ComponentLocator {
+
+ private UIComponent locatedComponent;
+
+ public StubComponentLocator(UIComponent locatedComponent) {
+ super();
+ this.locatedComponent = locatedComponent;
+ }
+
+ public UIComponent findComponent(FacesContext facesContext, UIComponent
contextComponent, String id) {
+ if (facesContext == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (contextComponent == null) {
+ throw new NullPointerException("contextComponent");
+ }
+
+ if (EXISTING_TEST_ID.equals(id)) {
+ return locatedComponent;
+ } else if (NONEXISTING_TEST_ID.equals(id)) {
+ return null;
+ } else {
+ fail(id);
+ return null;
+ }
+ }
+ }
+
+ private static final String TEST_CLIENT_ID = "table:0:testId";
+
+ private static final String EXISTING_TEST_ID = "testId";
+
+ private static final String NONEXISTING_TEST_ID = "nonExistent";
+
+ private MockFacesEnvironment environment;
+
+ private ComponentLocator stubComponentLocator;
+
+ private ComponentLocator mockComponentLocator;
+
+ private FacesContext facesContext;
+
+ private ExternalContext externalContext;
+
+ private UIComponent currentComponent;
+
+ private UIComponent locatedComponent;
+
+ private UIViewRoot viewRoot;
+
+ private <T extends UIComponent> T createMockComponent(Class<T>
componentClass) {
+ T component = environment.createMock(componentClass);
+ expect(component.getAttributes()).andStubReturn(new HashMap<String,
Object>());
+ return component;
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ environment = MockFacesEnvironment.createEnvironment();
+
+ facesContext = environment.getFacesContext();
+ expect(facesContext.getAttributes()).andStubReturn(new HashMap<Object,
Object>());
+
+ externalContext = environment.createMock(ExternalContext.class);
+ expect(facesContext.getExternalContext()).andStubReturn(externalContext);
+
+ viewRoot = createMockComponent(UIViewRoot.class);
+ expect(facesContext.getViewRoot()).andStubReturn(viewRoot);
+
+ currentComponent = createMockComponent(UIComponent.class);
+
+ locatedComponent = createMockComponent(UIComponent.class);
+
expect(locatedComponent.getClientId(same(facesContext))).andStubReturn(TEST_CLIENT_ID);
+
+ stubComponentLocator = new StubComponentLocator(locatedComponent);
+ mockComponentLocator = environment.createMock(ComponentLocator.class);
+ RichFunction.setComponentLocator(mockComponentLocator);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ facesContext = null;
+ externalContext = null;
+ currentComponent = null;
+ locatedComponent = null;
+ viewRoot = null;
+
+ environment.verify();
+ environment.release();
+ environment = null;
+ }
+
+ @Test
+ public void testFunctionsInCurrentComponentContext() throws Exception {
+ expect(mockComponentLocator.findComponent(same(facesContext),
eq(currentComponent),
+ EasyMock.<String>notNull())).andStubDelegateTo(stubComponentLocator);
+
+ environment.replay();
+
+ currentComponent.pushComponentToEL(environment.getFacesContext(), null);
+
+ assertEquals(TEST_CLIENT_ID, RichFunction.clientId(EXISTING_TEST_ID));
+ assertEquals("RichFaces.$('" + TEST_CLIENT_ID + "')",
RichFunction.component(EXISTING_TEST_ID));
+ assertEquals("document.getElementById('" + TEST_CLIENT_ID +
"')", RichFunction.element(EXISTING_TEST_ID));
+ assertEquals(locatedComponent, RichFunction.findComponent(EXISTING_TEST_ID));
+
+ assertNull(RichFunction.clientId(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.component(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.element(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.findComponent(NONEXISTING_TEST_ID));
+
+ assertNull(RichFunction.clientId(null));
+ assertNull(RichFunction.component(null));
+ assertNull(RichFunction.element(null));
+ assertNull(RichFunction.findComponent(null));
+
+ currentComponent.popComponentFromEL(environment.getFacesContext());
+ }
+
+ @Test
+ public void testFunctionsInViewRootContext() throws Exception {
+ expect(mockComponentLocator.findComponent(same(facesContext), eq(viewRoot),
+ EasyMock.<String>notNull())).andStubDelegateTo(stubComponentLocator);
+
+ environment.replay();
+
+ assertEquals(TEST_CLIENT_ID, RichFunction.clientId(EXISTING_TEST_ID));
+ assertEquals("RichFaces.$('" + TEST_CLIENT_ID + "')",
RichFunction.component(EXISTING_TEST_ID));
+ assertEquals("document.getElementById('" + TEST_CLIENT_ID +
"')", RichFunction.element(EXISTING_TEST_ID));
+ assertEquals(locatedComponent, RichFunction.findComponent(EXISTING_TEST_ID));
+
+ assertNull(RichFunction.clientId(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.component(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.element(NONEXISTING_TEST_ID));
+ assertNull(RichFunction.findComponent(NONEXISTING_TEST_ID));
+
+ assertNull(RichFunction.clientId(null));
+ assertNull(RichFunction.component(null));
+ assertNull(RichFunction.element(null));
+ assertNull(RichFunction.findComponent(null));
+ }
+
+ @Test
+ public void testIsUserInRole() throws Exception {
+ expect(externalContext.isUserInRole(eq("admin"))).andReturn(false);
+ expect(externalContext.isUserInRole(eq("user"))).andReturn(true);
+
+ expect(externalContext.isUserInRole(eq("manager"))).andReturn(false);
+ expect(externalContext.isUserInRole(eq("guest"))).andReturn(false);
+
expect(externalContext.isUserInRole(eq("supervisor"))).andReturn(true);
+
+ expect(externalContext.isUserInRole(eq("auditor"))).andReturn(false);
+
+ environment.replay();
+
+ assertTrue(RichFunction.isUserInRole("admin, user, root"));
+
+ Set<String> set = new LinkedHashSet<String>();
+ set.add("manager");
+ set.add("guest");
+ set.add("supervisor");
+
+ assertTrue(RichFunction.isUserInRole(set));
+
+ assertFalse(RichFunction.isUserInRole("auditor"));
+
+ assertFalse(RichFunction.isUserInRole(null));
+}
+}