myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdebussc...@apache.org
Subject svn commit: r1077841 - in /myfaces/extensions/validator/trunk: core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/ core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/ core/src/main/java/org/apache/myfaces/extens...
Date Fri, 04 Mar 2011 08:03:32 GMT
Author: rdebusscher
Date: Fri Mar  4 08:03:31 2011
New Revision: 1077841

URL: http://svn.apache.org/viewvc?rev=1077841&view=rev
Log:
EXTVAL-128

Added:
    myfaces/extensions/validator/trunk/test-modules/bean-validation-tests/src/test/java/org/apache/myfaces/extensions/validator/test/beanval/DeregisterSubNameMapperTestCase.java
Modified:
    myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/AbstractNameMapperAwareFactory.java
    myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/SubMapperAwareNameMapper.java
    myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/DefaultMetaDataTransformerFactory.java
    myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/mapper/ValidationStrategyToMetaDataTransformerSubMapperAwareNameMapper.java
    myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/MockMetaDataTransformerFactory.java

Modified: myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/AbstractNameMapperAwareFactory.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/AbstractNameMapperAwareFactory.java?rev=1077841&r1=1077840&r2=1077841&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/AbstractNameMapperAwareFactory.java
(original)
+++ myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/factory/AbstractNameMapperAwareFactory.java
Fri Mar  4 08:03:31 2011
@@ -18,6 +18,8 @@
  */
 package org.apache.myfaces.extensions.validator.core.factory;
 
+import org.apache.myfaces.extensions.validator.core.Nested;
+import org.apache.myfaces.extensions.validator.core.mapper.SubMapperAwareNameMapper;
 import org.apache.myfaces.extensions.validator.internal.UsageInformation;
 import org.apache.myfaces.extensions.validator.internal.UsageCategory;
 import org.apache.myfaces.extensions.validator.core.mapper.NameMapper;
@@ -75,14 +77,32 @@ public abstract class AbstractNameMapper
 
     public synchronized void deregister(Class<? extends NameMapper> classToDeregister)
     {
+        boolean subNameMapper = false;
+
+        if (classToDeregister != null && classToDeregister.isAnnotationPresent(Nested.class))
+        {
+            subNameMapper = true;
+        }
+
         Iterator<NameMapper<T>> nameMapperIterator = getNameMapperList().iterator();
-        while(nameMapperIterator.hasNext())
+        while (nameMapperIterator.hasNext())
         {
-            if(nameMapperIterator.next().getClass().getName().equals(classToDeregister.getName()))
+            if (subNameMapper)
+            {
+                NameMapper<T> nameMapper = nameMapperIterator.next();
+                if (nameMapper instanceof SubMapperAwareNameMapper)
+                {
+                    ((SubMapperAwareNameMapper) nameMapper).removeNameMapper(classToDeregister);
+                }
+            }
+            else
             {
-                nameMapperIterator.remove();
-                //don't break - e.g. to deregister all wrappers...
-                //break;
+                if (nameMapperIterator.next().getClass().getName().equals(classToDeregister.getName()))
+                {
+                    nameMapperIterator.remove();
+                    //don't break - e.g. to deregister all wrappers...
+                    //break;
+                }
             }
         }
     }

Modified: myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/SubMapperAwareNameMapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/SubMapperAwareNameMapper.java?rev=1077841&r1=1077840&r2=1077841&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/SubMapperAwareNameMapper.java
(original)
+++ myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/mapper/SubMapperAwareNameMapper.java
Fri Mar  4 08:03:31 2011
@@ -35,4 +35,10 @@ public interface SubMapperAwareNameMappe
      * @param nameMapper sub-nameMapper to add
      */
     void addNameMapper(NameMapper<T> nameMapper);
+
+    /**
+     * Deregisters all (sub-)name mappers of the given type
+     * @param nameMapperClass type of the (sub-)name mappers which have to be deregistered
+     */
+    void removeNameMapper(Class<? extends NameMapper> nameMapperClass);
 }
\ No newline at end of file

Modified: myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/DefaultMetaDataTransformerFactory.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/DefaultMetaDataTransformerFactory.java?rev=1077841&r1=1077840&r2=1077841&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/DefaultMetaDataTransformerFactory.java
(original)
+++ myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/DefaultMetaDataTransformerFactory.java
Fri Mar  4 08:03:31 2011
@@ -38,6 +38,7 @@ import org.apache.myfaces.extensions.val
 import org.apache.myfaces.extensions.validator.internal.UsageInformation;
 import org.apache.myfaces.extensions.validator.internal.UsageCategory;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
@@ -232,6 +233,22 @@ public class DefaultMetaDataTransformerF
         super.register(validationStrategyNameMapper);
     }
 
+    @Override
+    public void deregister(Class<? extends NameMapper> classToDeregister)
+    {
+        super.deregister(classToDeregister);
+        for (NameMapper<ValidationStrategy> nameMapper
+                : new ArrayList<NameMapper<ValidationStrategy>>(subNameMapperList))
+        {
+
+            if (nameMapper.getClass().equals(classToDeregister))
+            {
+                subNameMapperList.remove(nameMapper);
+            }
+        }
+
+    }
+
     /**
      * it's a very special case due to the missing order in the execution of startup-listeners
(phase listeners)
      * packaged in faces-config.xml files of jars
@@ -257,4 +274,11 @@ public class DefaultMetaDataTransformerF
             }
         }
     }
+
+    //just for testing
+    protected List<NameMapper<ValidationStrategy>> getSubNameMapperList()
+    {
+        return this.subNameMapperList;
+    }
 }
+

Modified: myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/mapper/ValidationStrategyToMetaDataTransformerSubMapperAwareNameMapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/mapper/ValidationStrategyToMetaDataTransformerSubMapperAwareNameMapper.java?rev=1077841&r1=1077840&r2=1077841&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/mapper/ValidationStrategyToMetaDataTransformerSubMapperAwareNameMapper.java
(original)
+++ myfaces/extensions/validator/trunk/core/src/main/java/org/apache/myfaces/extensions/validator/core/metadata/transformer/mapper/ValidationStrategyToMetaDataTransformerSubMapperAwareNameMapper.java
Fri Mar  4 08:03:31 2011
@@ -54,6 +54,28 @@ public class ValidationStrategyToMetaDat
         }
     }
 
+    public void removeNameMapper(Class<? extends NameMapper> nameMapperClass)
+    {
+        List<NameMapper<ValidationStrategy>> nameMappersToRemove =
+                new ArrayList<NameMapper<ValidationStrategy>>(this.subNameMappers.size());
+
+        for(NameMapper<ValidationStrategy> currentSubNameMapper : this.subNameMappers)
+        {
+            if(currentSubNameMapper.getClass().getName().equals(nameMapperClass.getName()))
+            {
+                nameMappersToRemove.add(currentSubNameMapper);
+                //don't break - e.g. to deregister all wrappers...
+                //break;
+            }
+        }
+
+        //do it manually due to the CopyOnWriteArrayList
+        for(NameMapper<ValidationStrategy> currentSubNameMapper : nameMappersToRemove)
+        {
+            this.subNameMappers.remove(currentSubNameMapper);
+        }
+    }
+
     private void sortSubNameMappers()
     {
         List<NameMapper<ValidationStrategy>> sortableList =

Modified: myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/MockMetaDataTransformerFactory.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/MockMetaDataTransformerFactory.java?rev=1077841&r1=1077840&r2=1077841&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/MockMetaDataTransformerFactory.java
(original)
+++ myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/MockMetaDataTransformerFactory.java
Fri Mar  4 08:03:31 2011
@@ -33,4 +33,9 @@ public class MockMetaDataTransformerFact
     {
         return super.getNameMapperList();
     }
+
+    public List<NameMapper<ValidationStrategy>> getSubNameMapperList()
+    {
+        return super.getSubNameMapperList();
+    }
 }
\ No newline at end of file

Added: myfaces/extensions/validator/trunk/test-modules/bean-validation-tests/src/test/java/org/apache/myfaces/extensions/validator/test/beanval/DeregisterSubNameMapperTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/test-modules/bean-validation-tests/src/test/java/org/apache/myfaces/extensions/validator/test/beanval/DeregisterSubNameMapperTestCase.java?rev=1077841&view=auto
==============================================================================
--- myfaces/extensions/validator/trunk/test-modules/bean-validation-tests/src/test/java/org/apache/myfaces/extensions/validator/test/beanval/DeregisterSubNameMapperTestCase.java
(added)
+++ myfaces/extensions/validator/trunk/test-modules/bean-validation-tests/src/test/java/org/apache/myfaces/extensions/validator/test/beanval/DeregisterSubNameMapperTestCase.java
Fri Mar  4 08:03:31 2011
@@ -0,0 +1,95 @@
+/*
+ * 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.extensions.validator.test.beanval;
+
+import org.junit.Test;
+import org.junit.Assert;
+import org.apache.myfaces.extensions.validator.util.ExtValUtils;
+import org.apache.myfaces.extensions.validator.beanval.metadata.transformer.mapper.SizeNameMapper;
+import org.apache.myfaces.extensions.validator.core.ExtValContext;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.ValidationStrategy;
+import org.apache.myfaces.extensions.validator.core.mapper.NameMapper;
+import org.apache.myfaces.extensions.validator.core.mapper.SubMapperAwareNameMapper;
+import org.apache.myfaces.extensions.validator.core.factory.FactoryNames;
+import org.apache.myfaces.extensions.validator.test.base.mock.MockMetaDataTransformerFactory;
+
+import java.util.List;
+
+public class DeregisterSubNameMapperTestCase extends AbstractBeanValidationTestCase
+{
+    @Test
+    public void testDenyNameMapper()
+    {
+        int count = getNameMapperAndSubNameMapperCount();
+
+        Assert.assertEquals(7, count);
+
+        ExtValUtils.denyValidationStrategyToMetaDataTransformerNameMapper(SizeNameMapper.class);
+
+        count = getNameMapperAndSubNameMapperCount();
+        Assert.assertEquals(6, count);
+
+        //no effect because it's denied
+        ExtValUtils.registerValidationStrategyToMetaDataTransformerNameMapper(new SizeNameMapper());
+
+        count = getNameMapperAndSubNameMapperCount();
+        Assert.assertEquals(6, count);
+    }
+    @Test
+    public void testDeregisterNameMapper()
+    {
+        int count = getNameMapperAndSubNameMapperCount();
+
+        Assert.assertEquals(7, count);
+
+        ExtValUtils.deregisterValidationStrategyToMetaDataTransformerNameMapper(SizeNameMapper.class);
+
+        count = getNameMapperAndSubNameMapperCount();
+        Assert.assertEquals(6, count);
+
+        ExtValUtils.registerValidationStrategyToMetaDataTransformerNameMapper(new SizeNameMapper());
+
+        count = getNameMapperAndSubNameMapperCount();
+        Assert.assertEquals(7, count);
+    }
+
+    private int getNameMapperAndSubNameMapperCount()
+    {
+        MockMetaDataTransformerFactory nameMapperAwareFactory =
+                (ExtValContext.getContext()
+                .getFactoryFinder()
+                .getFactory(FactoryNames.META_DATA_TRANSFORMER_FACTORY, MockMetaDataTransformerFactory.class));
+
+        List<NameMapper<ValidationStrategy>> nameMapperList = nameMapperAwareFactory.getRegisteredNameMapperList();
+
+        int count = 0;
+
+        for(NameMapper<ValidationStrategy> nameMapper : nameMapperList)
+        {
+            if(!(nameMapper instanceof SubMapperAwareNameMapper))
+            {
+                count++;
+            }
+        }
+
+        List<NameMapper<ValidationStrategy>> subNamMappers = nameMapperAwareFactory.getSubNameMapperList();
+        count += subNamMappers.size();
+        return count;
+    }
+}



Mime
View raw message