Permalink
Browse files

MYFACES-3659 Conditional include of scripts and stylesheets (add juni…

…t test thanks to dennis hoersch for provide this patch)

git-svn-id: https://svn.apache.org/repos/asf/myfaces/core/trunk@1461890 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 6ee0c62 commit f08ed9b6931a342b3669887e5a1e335b16b75435 Leonardo Uribe committed Mar 27, 2013
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.view.facelets.impl;
+
+import java.io.IOException;
+import java.util.List;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import org.apache.myfaces.view.facelets.FaceletTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ResourceMarkDeleteTest extends FaceletTestCase {
+
+ @Test
+ public void test_only_ajs_is_included() throws IOException
+ {
+ UIViewRoot root = facesContext.getViewRoot();
+
+ // Building the view
+ // Because 'includeResource' is not set the second resource should not be included
+ vdl.buildView(facesContext, root, "test_conditional_include_resources.xhtml");
+
+ List<UIComponent> resources = root.getComponentResources(facesContext, "head");
+
+ Assert.assertTrue("Only one script is included.", resources.size() == 1);
+ Assert.assertTrue("a.js is included.", resources.get(0).getAttributes().get("name").equals("a.js"));
+ }
+
+ @Test
+ public void test_ajs_and_xjs_are_included() throws IOException
+ {
+ facesContext.getAttributes().put("includeResource", Boolean.TRUE);
+ UIViewRoot root = facesContext.getViewRoot();
+
+ // Building the view
+ // Because 'includeResource' is now set the second resource should be included
+ vdl.buildView(facesContext, root, "test_conditional_include_resources.xhtml");
+
+ List<UIComponent> resources = root.getComponentResources(facesContext, "head");
+
+ Assert.assertTrue("Two scripts are included.", resources.size() == 2);
+ }
+
+ @Test
+ public void test_only_ajs_after_refresh_view_is_included() throws IOException
+ {
+ UIViewRoot view = facesContext.getViewRoot();
+
+ // Building the initial view
+ // Because 'includeResource' is not set the second resource should not be included
+ vdl.buildView(facesContext, view, "test_conditional_include_resources.xhtml");
+
+ {
+ List<UIComponent> resources = view.getComponentResources(facesContext, "head");
+
+ Assert.assertTrue("Only one script is included.", resources.size() == 1);
+ }
+
+ // reset 'isFilledView'
+ facesContext.getAttributes().remove(view);
+
+ // Building the view a second time
+ // Because 'includeResource' is now set the second resource should be included
+ facesContext.getAttributes().put("includeResource", Boolean.TRUE);
+ vdl.buildView(facesContext, view);
+
+ {
+ List<UIComponent> resources = view.getComponentResources(facesContext, "head");
+
+ Assert.assertTrue("Two scripts are included.", resources.size() == 2);
+ }
+
+ // reset 'isFilledView'
+ facesContext.getAttributes().remove(view);
+ // Building the view a third time
+ // Because 'includeResource' is now removed the second resource should not be included
+
+ facesContext.getAttributes().remove("includeResource");
+ vdl.buildView(facesContext, view);
+
+ {
+ List<UIComponent> resources = view.getComponentResources(facesContext, "head");
+
+ Assert.assertTrue("Only one script is included.", resources.size() == 1);
+ }
+ }
+}
@@ -0,0 +1,16 @@
+/*
+ * Copyright 2012 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+var a = "A";
@@ -0,0 +1,16 @@
+/*
+ * Copyright 2012 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+var x = "X";
@@ -0,0 +1,36 @@
+<?xml version='1.0' encoding='UTF-8' ?>
+<!--
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:ui="http://java.sun.com/jsf/facelets"
+ xmlns:h="http://java.sun.com/jsf/html"
+ xmlns:f="http://java.sun.com/jsf/core"
+ xmlns:c="http://java.sun.com/jsp/jstl/core">
+<h:head id="head">
+</h:head>
+<h:body id="body">
+
+ <h:outputScript id="ajs" library="js" name="a.js" target="head" />
+ Some text
+ <c:if test="#{facesContext.attributes.includeResource}">
+ BLA
+ <h:outputScript id="xjs" library="js" name="x.js" target="head" />
+ </c:if>
+
+ <h:form id="form">
+ <h:inputText id="input" value="" />
+ </h:form>
+</h:body>
+</html>

0 comments on commit f08ed9b

Please sign in to comment.