deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-480 improved view-config validation
Date Fri, 07 Feb 2014 12:29:14 GMT
Updated Branches:
  refs/heads/master bd6796897 -> d3d8cd081


DELTASPIKE-480 improved view-config validation


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

Branch: refs/heads/master
Commit: d3d8cd0817ace97f7b198f5a0f50f30b4f605627
Parents: bd67968
Author: gpetracek <gpetracek@apache.org>
Authored: Fri Feb 7 13:11:09 2014 +0100
Committer: gpetracek <gpetracek@apache.org>
Committed: Fri Feb 7 13:28:54 2014 +0100

----------------------------------------------------------------------
 .../deltaspike/jsf/api/config/view/View.java    | 13 ++++
 .../config/view/ViewConfigPathValidator.java    | 14 ++++
 .../destination/uc005/ViewConfigPathTest.java   | 59 +++++++++++++++
 ...ViewConfigPreProcessorWithoutValidation.java | 13 ++++
 .../navigation/destination/uc006/Pages.java     | 44 +++++++++++
 .../uc006/ViewConfigBasePathValidationTest.java | 77 ++++++++++++++++++++
 6 files changed, 220 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/config/view/View.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/config/view/View.java
b/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/config/view/View.java
index 802ed56..9af192b 100644
--- a/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/config/view/View.java
+++ b/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/config/view/View.java
@@ -143,6 +143,8 @@ public @interface View
         @Override
         public View beforeAddToConfig(View view, ViewConfigNode viewConfigNode)
         {
+            validateViewMetaData(view, viewConfigNode);
+
             boolean defaultValueReplaced = false;
 
             View.NavigationMode navigation = view.navigation();
@@ -206,6 +208,17 @@ public @interface View
             return view;
         }
 
+        protected void validateViewMetaData(View view, ViewConfigNode viewConfigNode)
+        {
+            String basePath = view.basePath();
+            if (viewConfigNode.getSource().isInterface() && !"".equals(basePath)
&& basePath != null)
+            {
+                throw new IllegalStateException("Using @" + View.class.getName() + "#basePath
isn't allowed on" +
+                    " folder-nodes (= interfaces). Use @" + Folder.class.getName() + " for
intended folder-nodes" +
+                    " or a class instead of the interface for page-nodes.");
+            }
+        }
+
         private void updateNodeMetaData(ViewConfigNode viewConfigNode, View view)
         {
             Set<Annotation> metaData = viewConfigNode.getMetaData();

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/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
index ed8f08c..0e8351c 100644
--- 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
@@ -25,6 +25,7 @@ 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.Folder;
 import org.apache.deltaspike.jsf.api.config.view.View;
 
 import javax.servlet.ServletContextEvent;
@@ -37,6 +38,8 @@ import java.util.logging.Logger;
 
 public class ViewConfigPathValidator implements ServletContextListener, Deactivatable
 {
+    private static final Logger LOGGER = Logger.getLogger(ViewConfigPathValidator.class.getName());
+
     @Override
     public void contextInitialized(ServletContextEvent sce)
     {
@@ -75,6 +78,17 @@ public class ViewConfigPathValidator implements ServletContextListener,
Deactiva
 
                 if (!isValidPath(sce, configDescriptor))
                 {
+                    if (configDescriptor instanceof DefaultFolderConfigDescriptor &&
+                        !configDescriptor.getConfigClass().isAnnotationPresent(Folder.class))
+                    {
+
+                        LOGGER.fine(configDescriptor.getConfigClass().getName() + " looks
like a marker interface" +
+                            " only used for providing meta-data, because the path " + configDescriptor.getPath()
+
+                            " doesn't exist and the config-class isn't annotated with " +
Folder.class.getName());
+
+                        continue;
+                    }
+
                     throw new IllegalStateException("path '" + configDescriptor.getPath()
+
                             "' is missing, but mapped by: " + configDescriptor.getConfigClass().getName());
                 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPathTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPathTest.java
b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPathTest.java
index a39e27a..7404596 100644
--- a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPathTest.java
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPathTest.java
@@ -18,21 +18,80 @@
  */
 package org.apache.deltaspike.test.jsf.impl.config.view.navigation.destination.uc005;
 
+import org.apache.deltaspike.core.api.config.ConfigResolver;
 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.spi.config.ConfigSource;
+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.ViewConfigResolverProducer;
 import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+
 public class ViewConfigPathTest
 {
+    private static boolean active;
+
     private ViewConfigExtension viewConfigExtension;
 
     private ViewConfigResolverProducer viewConfigResolverProducer;
 
+    @BeforeClass
+    public static void init()
+    {
+        active = true;
+
+        ConfigResolver.addConfigSources(new ArrayList<ConfigSource>() {
+            {
+                add(new ConfigSource() {
+                    @Override
+                    public int getOrdinal() {
+                        return Integer.MAX_VALUE;
+                    }
+
+                    @Override
+                    public Map<String, String> getProperties() {
+                        return Collections.emptyMap();
+                    }
+
+                    @Override
+                    public String getPropertyValue(String key) {
+                        if (active && View.ViewConfigPreProcessor.class.getName().equals(key))
{
+                            return ViewConfigPreProcessorWithoutValidation.class.getName();
+                        }
+                        return null;
+                    }
+
+                    @Override
+                    public String getConfigName() {
+                        return "test-view-config";
+                    }
+
+                    @Override
+                    public boolean isScannable() {
+                        return false;
+                    }
+                });
+            }
+
+            private static final long serialVersionUID = 3247551986947387154L;
+        });
+    }
+
+    @AfterClass
+    public static void cleanup()
+    {
+        active = false;
+    }
+
     @Before
     public void before()
     {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPreProcessorWithoutValidation.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPreProcessorWithoutValidation.java
b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPreProcessorWithoutValidation.java
new file mode 100644
index 0000000..2110684
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc005/ViewConfigPreProcessorWithoutValidation.java
@@ -0,0 +1,13 @@
+package org.apache.deltaspike.test.jsf.impl.config.view.navigation.destination.uc005;
+
+import org.apache.deltaspike.core.spi.config.view.ViewConfigNode;
+import org.apache.deltaspike.jsf.api.config.view.View;
+
+public class ViewConfigPreProcessorWithoutValidation extends View.ViewConfigPreProcessor
+{
+    @Override
+    protected void validateViewMetaData(View view, ViewConfigNode viewConfigNode)
+    {
+        //do nothing to check intentionally ignored basePath usages at folder-nodes
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/Pages.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/Pages.java
b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/Pages.java
new file mode 100644
index 0000000..62dcd10
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/Pages.java
@@ -0,0 +1,44 @@
+/*
+ * 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.navigation.destination.uc006;
+
+import org.apache.deltaspike.core.api.config.view.ViewConfig;
+import org.apache.deltaspike.jsf.api.config.view.Folder;
+import org.apache.deltaspike.jsf.api.config.view.View;
+
+interface Pages
+{
+    @View(basePath = "w1") //causes a violation because @Folder should be used instead
+    interface Wizard1
+    {
+        @View(basePath = "")
+        class Start implements ViewConfig
+        {
+        }
+    }
+
+    @Folder(name = "w2") //valid usage
+    interface Wizard2
+    {
+        @View(basePath = "")
+        class Start implements ViewConfig
+        {
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d3d8cd08/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/ViewConfigBasePathValidationTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/ViewConfigBasePathValidationTest.java
b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/ViewConfigBasePathValidationTest.java
new file mode 100644
index 0000000..149daf3
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/test/java/org/apache/deltaspike/test/jsf/impl/config/view/navigation/destination/uc006/ViewConfigBasePathValidationTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.navigation.destination.uc006;
+
+import junit.framework.Assert;
+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.ViewConfigResolverProducer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ViewConfigBasePathValidationTest
+{
+    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 testInvalidBasePathUsage()
+    {
+        this.viewConfigExtension.addPageDefinition(Pages.Wizard1.Start.class);
+
+        try
+        {
+            this.viewConfigResolverProducer.createViewConfigResolver();
+        }
+        catch (IllegalStateException e)
+        {
+            if (!(e.getMessage().contains(View.class.getName()) &&
+                e.getMessage().contains(Folder.class.getName())&&
+                e.getMessage().contains("#basePath")))
+            {
+                Assert.fail("unexpected violation message found");
+            }
+            return;
+        }
+        Assert.fail("violation not found");
+    }
+
+    @Test
+    public void testValidBasePathUsage()
+    {
+        this.viewConfigExtension.addPageDefinition(Pages.Wizard2.Start.class);
+        Assert.assertNotNull(this.viewConfigResolverProducer.createViewConfigResolver());
//won't cause an exception
+    }
+}


Mime
View raw message