You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2013/12/26 17:31:34 UTC

git commit: DELTASPIKE-480 ViewConfigPathValidatorTest

Updated Branches:
  refs/heads/master 015ce76ed -> 78ef204fd


DELTASPIKE-480 ViewConfigPathValidatorTest

 (and minor improvements)


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/78ef204f
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/78ef204f
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/78ef204f

Branch: refs/heads/master
Commit: 78ef204fde8d67a91e1806a2bceafbd11cce80c2
Parents: 015ce76
Author: gpetracek <gp...@apache.org>
Authored: Thu Dec 26 16:18:02 2013 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Thu Dec 26 16:29:36 2013 +0100

----------------------------------------------------------------------
 .../config/view/ViewConfigPathValidator.java    | 113 +++++++++++++++
 .../impl/config/view/ViewConfigValidator.java   |  70 ----------
 .../main/resources/META-INF/web-fragment.xml    |   2 +-
 .../jsf/impl/config/view/validation/Pages.java  |  29 ++++
 .../validation/ViewConfigPathValidatorTest.java | 138 +++++++++++++++++++
 5 files changed, 281 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/78ef204f/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigPathValidator.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigPathValidator.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigPathValidator.java
new file mode 100644
index 0000000..ed8f08c
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigPathValidator.java
@@ -0,0 +1,113 @@
+/*
+ * 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.deltaspike.jsf.impl.config.view;
+
+import org.apache.deltaspike.core.api.config.view.metadata.ConfigDescriptor;
+import org.apache.deltaspike.core.api.config.view.metadata.ViewConfigDescriptor;
+import org.apache.deltaspike.core.api.config.view.metadata.ViewConfigResolver;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+import org.apache.deltaspike.core.util.ClassDeactivationUtils;
+import org.apache.deltaspike.core.util.ExceptionUtils;
+import org.apache.deltaspike.jsf.api.config.view.View;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class ViewConfigPathValidator implements ServletContextListener, Deactivatable
+{
+    @Override
+    public void contextInitialized(ServletContextEvent sce)
+    {
+        if (ClassDeactivationUtils.isActivated(getClass()))
+        {
+            ViewConfigResolver viewConfigResolver = BeanProvider.getContextualReference(ViewConfigResolver.class);
+            List<String> supportedExtensions = new ArrayList<String>();
+            supportedExtensions.add(View.Extension.XHTML);
+            supportedExtensions.add(View.Extension.JSP);
+            validateViewConfigPaths(sce, viewConfigResolver, supportedExtensions);
+        }
+    }
+
+    //allows to test and re-use it in a custom listener
+    // (if a custom listener is needed for supporting custom extensions or
+    // this listener is deactivated e.g. to change the order)
+    protected void validateViewConfigPaths(ServletContextEvent sce,
+                                           ViewConfigResolver viewConfigResolver,
+                                           List<String> supportedExtensions)
+    {
+        for (ConfigDescriptor configDescriptor : viewConfigResolver.getConfigDescriptors())
+        {
+            try
+            {
+                if (configDescriptor instanceof ViewConfigDescriptor)
+                {
+                    //currently other extensions aren't supported
+                    String viewId = ((ViewConfigDescriptor) configDescriptor).getViewId();
+                    String extension = viewId.substring(viewId.lastIndexOf('.') + 1);
+
+                    if (!supportedExtensions.contains(extension))
+                    {
+                        continue;
+                    }
+                }
+
+                if (!isValidPath(sce, configDescriptor))
+                {
+                    throw new IllegalStateException("path '" + configDescriptor.getPath() +
+                            "' is missing, but mapped by: " + configDescriptor.getConfigClass().getName());
+                }
+            }
+            catch (Exception e)
+            {
+                printException(e);
+                throw ExceptionUtils.throwAsRuntimeException(e);
+            }
+        }
+    }
+
+    protected boolean isValidPath(ServletContextEvent sce, ConfigDescriptor configDescriptor)
+    {
+        try
+        {
+            return sce.getServletContext().getResource(configDescriptor.getPath()) != null;
+        }
+        catch (MalformedURLException e)
+        {
+            throw ExceptionUtils.throwAsRuntimeException(e);
+        }
+    }
+
+    protected void printException(Exception e)
+    {
+        //for easier analysis (in combination with several servers)
+        Logger.getLogger(ViewConfigPathValidator.class.getName()).log(Level.SEVERE, "invalid view-config found", e);
+    }
+
+    @Override
+    public void contextDestroyed(ServletContextEvent sce)
+    {
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/78ef204f/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigValidator.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigValidator.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigValidator.java
deleted file mode 100644
index be91261..0000000
--- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/config/view/ViewConfigValidator.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.deltaspike.jsf.impl.config.view;
-
-import org.apache.deltaspike.core.api.config.view.metadata.ConfigDescriptor;
-import org.apache.deltaspike.core.api.config.view.metadata.ViewConfigResolver;
-import org.apache.deltaspike.core.api.provider.BeanProvider;
-import org.apache.deltaspike.core.spi.activation.Deactivatable;
-import org.apache.deltaspike.core.util.ClassDeactivationUtils;
-import org.apache.deltaspike.core.util.ExceptionUtils;
-
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-
-public class ViewConfigValidator implements ServletContextListener, Deactivatable
-{
-    @Override
-    public void contextInitialized(ServletContextEvent sce)
-    {
-        if (ClassDeactivationUtils.isActivated(getClass()))
-        {
-            checkViewConfig(sce);
-        }
-    }
-
-    //allows to re-use it in a custom listener (if this one is deactivated e.g. to change the order)
-    protected void checkViewConfig(ServletContextEvent sce)
-    {
-        ViewConfigResolver viewConfigResolver = BeanProvider.getContextualReference(ViewConfigResolver.class);
-
-        for (ConfigDescriptor configDescriptor : viewConfigResolver.getConfigDescriptors())
-        {
-            try
-            {
-                if (sce.getServletContext().getResource(configDescriptor.getPath()) == null)
-                {
-                    throw new IllegalStateException("path '" + configDescriptor.getPath() +
-                        "' is missing, but mapped by: " + configDescriptor.getConfigClass().getName());
-                }
-            }
-            catch (Exception e)
-            {
-                e.printStackTrace(); //for easier analysis (in combination with several servers)
-                throw ExceptionUtils.throwAsRuntimeException(e);
-            }
-        }
-    }
-
-    @Override
-    public void contextDestroyed(ServletContextEvent sce)
-    {
-    }
-}
-

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/78ef204f/deltaspike/modules/jsf/impl/src/main/resources/META-INF/web-fragment.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/web-fragment.xml b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/web-fragment.xml
index c9e9907..5f9fc21 100644
--- a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/web-fragment.xml
+++ b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/web-fragment.xml
@@ -25,7 +25,7 @@
     <distributable/>
 
     <listener>
-        <listener-class>org.apache.deltaspike.jsf.impl.config.view.ViewConfigValidator</listener-class>
+        <listener-class>org.apache.deltaspike.jsf.impl.config.view.ViewConfigPathValidator</listener-class>
     </listener>
 
     <ordering>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/78ef204f/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/Pages.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/Pages.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/Pages.java
new file mode 100644
index 0000000..0e65d52
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/Pages.java
@@ -0,0 +1,29 @@
+/*
+ * 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.deltaspike.test.jsf.impl.config.view.validation;
+
+import org.apache.deltaspike.core.api.config.view.ViewConfig;
+import org.apache.deltaspike.jsf.api.config.view.View;
+
+interface Pages extends ViewConfig
+{
+    class Index implements Pages
+    {
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/78ef204f/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/ViewConfigPathValidatorTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/ViewConfigPathValidatorTest.java b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/ViewConfigPathValidatorTest.java
new file mode 100644
index 0000000..533af66
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/validation/ViewConfigPathValidatorTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.deltaspike.test.jsf.impl.config.view.validation;
+
+import junit.framework.Assert;
+import org.apache.deltaspike.core.api.config.view.metadata.ConfigDescriptor;
+import org.apache.deltaspike.core.api.config.view.metadata.ViewConfigResolver;
+import org.apache.deltaspike.jsf.api.config.view.Folder;
+import org.apache.deltaspike.jsf.api.config.view.View;
+import org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension;
+import org.apache.deltaspike.jsf.impl.config.view.ViewConfigPathValidator;
+import org.apache.deltaspike.jsf.impl.config.view.ViewConfigResolverProducer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.ServletContextEvent;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ViewConfigPathValidatorTest
+{
+    private ViewConfigExtension viewConfigExtension;
+
+    private ViewConfigResolverProducer viewConfigResolverProducer;
+
+    @Before
+    public void before()
+    {
+        this.viewConfigExtension = new ViewConfigExtension();
+        this.viewConfigResolverProducer = new ViewConfigResolverProducer(this.viewConfigExtension);
+    }
+
+    @After
+    public void after()
+    {
+        this.viewConfigExtension.freeViewConfigCache(null);
+    }
+
+    @Test
+    public void testValidViewConfig()
+    {
+        this.viewConfigExtension.addPageDefinition(Pages.Index.class);
+
+        ViewConfigResolver viewConfigResolver = this.viewConfigResolverProducer.createViewConfigResolver();
+
+        List<String> supportedExtensions = new ArrayList<String>();
+        supportedExtensions.add(View.Extension.XHTML);
+
+        try
+        {
+            new MockedViewConfigPathValidator(true).validateViewConfigPaths(null, viewConfigResolver, supportedExtensions);
+        }
+        catch (Exception e)
+        {
+            Assert.fail("valid view-config was reported as invalid");
+        }
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testMissingPath()
+    {
+        this.viewConfigExtension.addPageDefinition(Pages.Index.class);
+
+        ViewConfigResolver viewConfigResolver = this.viewConfigResolverProducer.createViewConfigResolver();
+
+        List<String> supportedExtensions = new ArrayList<String>();
+        supportedExtensions.add(View.Extension.XHTML);
+        new MockedViewConfigPathValidator(false).validateViewConfigPaths(null, viewConfigResolver, supportedExtensions);
+    }
+
+    @Test
+    public void testMissingPathButUnsupportedExtension()
+    {
+        this.viewConfigExtension.addPageDefinition(Pages.Index.class);
+
+        ViewConfigResolver viewConfigResolver = this.viewConfigResolverProducer.createViewConfigResolver();
+
+        List<String> supportedExtensions = new ArrayList<String>();
+        supportedExtensions.add(View.Extension.JSF);
+
+        try
+        {
+            new MockedViewConfigPathValidator(false).validateViewConfigPaths(null, viewConfigResolver, supportedExtensions);
+        }
+        catch (Exception e)
+        {
+            Assert.fail("unsupported extension wasn't ignored");
+        }
+    }
+
+    private class MockedViewConfigPathValidator extends ViewConfigPathValidator
+    {
+        private final boolean validatePathAsValid;
+
+        private MockedViewConfigPathValidator(boolean validatePathAsValid)
+        {
+            this.validatePathAsValid = validatePathAsValid;
+        }
+
+        @Override
+        public void validateViewConfigPaths(ServletContextEvent sce,
+                                            ViewConfigResolver viewConfigResolver,
+                                            List<String> supportedExtensions)
+        {
+            super.validateViewConfigPaths(sce, viewConfigResolver, supportedExtensions);
+        }
+
+        @Override
+        protected boolean isValidPath(ServletContextEvent sce, ConfigDescriptor configDescriptor)
+        {
+            //in our tests we just validate views -> skip folders
+            return !configDescriptor.getMetaData(Folder.class).isEmpty() || this.validatePathAsValid;
+        }
+
+        @Override
+        protected void printException(Exception e)
+        {
+            //do nothing
+        }
+    }
+}