From 84c12056831d812737d3950ce4d5a7083087d74f Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Thu, 06 Mar 2014 10:42:55 +0000
Subject: [PATCH] Improve behavior of ConfigurationMock class and export it in "tests" jar to allow usage by other modules 

---
 opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMock.java     |   59 +++++++++++++++----
 opendj-config/pom.xml                                                              |   40 +++++++++---
 opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMockTest.java |   29 +++++++++
 3 files changed, 103 insertions(+), 25 deletions(-)

diff --git a/opendj-config/pom.xml b/opendj-config/pom.xml
index d37ffdd..6c830e0 100644
--- a/opendj-config/pom.xml
+++ b/opendj-config/pom.xml
@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- ! CDDL HEADER START ! ! The contents of this file are subject to the 
-  terms of the ! Common Development and Distribution License, Version 1.0 only 
-  ! (the "License"). You may not use this file except in compliance ! with 
-  the License. ! ! You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 
-  ! or http://forgerock.org/license/CDDLv1.0.html. ! See the License for the 
-  specific language governing permissions ! and limitations under the License. 
-  ! ! When distributing Covered Code, include this CDDL HEADER in each ! file 
-  and include the License file at legal-notices/CDDLv1_0.txt. ! If applicable, 
-  add the following below this CDDL HEADER, with the ! fields enclosed by brackets 
-  "[]" replaced with your own identifying ! information: ! Portions Copyright 
-  [yyyy] [name of copyright owner] ! ! CDDL HEADER END ! ! Copyright 2013 ForgeRock 
+<!-- ! CDDL HEADER START ! ! The contents of this file are subject to the
+  terms of the ! Common Development and Distribution License, Version 1.0 only
+  ! (the "License"). You may not use this file except in compliance ! with
+  the License. ! ! You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
+  ! or http://forgerock.org/license/CDDLv1.0.html. ! See the License for the
+  specific language governing permissions ! and limitations under the License.
+  ! ! When distributing Covered Code, include this CDDL HEADER in each ! file
+  and include the License file at legal-notices/CDDLv1_0.txt. ! If applicable,
+  add the following below this CDDL HEADER, with the ! fields enclosed by brackets
+  "[]" replaced with your own identifying ! information: ! Portions Copyright
+  [yyyy] [name of copyright owner] ! ! CDDL HEADER END ! ! Copyright 2013 ForgeRock
   AS ! -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -68,6 +68,24 @@
   <build>
     <plugins>
       <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-jar-plugin</artifactId>
+          <executions>
+            <execution>
+              <goals>
+                <goal>test-jar</goal>
+              </goals>
+              <configuration>
+                 <includes>
+                    <!-- Classes to have mocks with configuration defaults -->
+                    <include>**/ConfigurationMock.class</include>
+                    <include>**/ConfigurationMock$*.class</include>
+                 </includes>
+              </configuration>
+            </execution>
+          </executions>
+        </plugin>
+      <plugin>
         <groupId>org.forgerock.commons</groupId>
         <artifactId>i18n-maven-plugin</artifactId>
         <executions>
diff --git a/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMock.java b/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMock.java
index b0a7cf8..eb497a8 100644
--- a/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMock.java
+++ b/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMock.java
@@ -21,7 +21,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package org.forgerock.opendj.config;
 
@@ -33,6 +33,7 @@
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;
 import org.mockito.invocation.InvocationOnMock;
 
 /**
@@ -76,20 +77,47 @@
      * A stubbed answer for Configuration objects, allowing to return default
      * value for settings when available.
      */
-    private static class ConfigAnswer implements org.mockito.stubbing.Answer<Object> {
+    private static class ConfigAnswer extends ReturnsEmptyValues {
+
+        private static final long serialVersionUID = 1L;
 
         /** {@inheritDoc} */
         @Override
-        public Object answer(InvocationOnMock invocation) throws Throwable {
-            String definitionClassName = toDefinitionClassName(invocation.getMethod().getDeclaringClass()
-                    .getName());
-            Class<?> definitionClass = Class.forName(definitionClassName);
-            ManagedObjectDefinition<?, ?> definition = (ManagedObjectDefinition<?, ?>) definitionClass.getMethod(
-                    "getInstance").invoke(null);
-            Method getPropertyDefMethod =
-                    definitionClass.getMethod(invocation.getMethod().getName() + "PropertyDefinition");
-            Class<?> propertyReturnType = getPropertyReturnType(getPropertyDefMethod);
-            return getDefaultValue(definition, getPropertyDefMethod, propertyReturnType);
+        public Object answer(InvocationOnMock invocation) {
+            try {
+                String definitionClassName =
+                    toDefinitionClassName(invocation.getMethod().getDeclaringClass().getName());
+                Class<?> definitionClass = Class.forName(definitionClassName);
+                ManagedObjectDefinition<?, ?> definition =
+                    (ManagedObjectDefinition<?, ?>) definitionClass.getMethod("getInstance").invoke(null);
+                String invokedMethodName = invocation.getMethod().getName();
+                if (!isGetterMethod(invokedMethodName)) {
+                    return answerFromDefaultMockitoBehavior(invocation);
+                }
+                Method getPropertyDefMethod = getPropertyDefinitionMethod(definitionClass, invokedMethodName);
+                Class<?> propertyReturnType = getPropertyReturnType(getPropertyDefMethod);
+                Object defaultValue = getDefaultValue(definition, getPropertyDefMethod, propertyReturnType);
+                if (defaultValue == null) {
+                    return answerFromDefaultMockitoBehavior(invocation);
+                }
+                return defaultValue;
+            } catch (Exception e) {
+                return answerFromDefaultMockitoBehavior(invocation);
+            }
+        }
+
+        private Object answerFromDefaultMockitoBehavior(InvocationOnMock invocation) {
+            return super.answer(invocation);
+        }
+
+        private boolean isGetterMethod(String invokedMethodName) {
+            return invokedMethodName.startsWith("get") || invokedMethodName.startsWith("is");
+        }
+
+        private Method getPropertyDefinitionMethod(Class<?> definitionClass, String invokedMethodName)
+                throws SecurityException, NoSuchMethodException {
+            // Methods for boolean starts with "is" in Cfg class but with "get" in CfgDefn class.
+            return definitionClass.getMethod(invokedMethodName.replaceAll("^is", "get") + "PropertyDefinition");
         }
 
         /**
@@ -141,10 +169,13 @@
             MockProviderVisitor<T> visitor = new MockProviderVisitor<T>(propertyDefinition);
             Collection<T> values = defaultBehaviorProvider.accept(visitor, null);
 
-            if (propertyDefinition.hasOption(PropertyOption.MULTI_VALUED)) {
+            if (values == null) {
+                // No default behavior defined
+                return null;
+            } else if (propertyDefinition.hasOption(PropertyOption.MULTI_VALUED)) {
                 return values;
             } else {
-                // single value returned
+                // Single value returned
                 return values.iterator().next();
             }
         }
diff --git a/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMockTest.java b/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMockTest.java
index bd0eed2..91ebb96 100644
--- a/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMockTest.java
+++ b/opendj-config/src/test/java/org/forgerock/opendj/config/ConfigurationMockTest.java
@@ -27,11 +27,14 @@
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.forgerock.opendj.config.ConfigurationMock.mockCfg;
+import static org.mockito.Mockito.*;
 
+import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.schema.Schema;
 import org.forgerock.opendj.server.config.meta.PluginCfgDefn.PluginType;
 import org.forgerock.opendj.server.config.server.AttributeCleanupPluginCfg;
 import org.forgerock.opendj.server.config.server.CollectiveAttributeSubentriesVirtualAttributeCfg;
+import org.forgerock.opendj.server.config.server.CoreSchemaCfg;
 import org.forgerock.opendj.server.config.server.GoverningStructureRuleVirtualAttributeCfg;
 import org.forgerock.opendj.server.config.server.LDAPConnectionHandlerCfg;
 import org.testng.annotations.Test;
@@ -68,4 +71,30 @@
         assertThat(mock.getAttributeType()).isEqualTo(
                 Schema.getDefaultSchema().getAttributeType("collectiveAttributeSubentries"));
     }
+
+    @Test
+    public void testPropertyWithNoDefaultBooleanReturnValue() throws Exception {
+        CollectiveAttributeSubentriesVirtualAttributeCfg mock =
+            mockCfg(CollectiveAttributeSubentriesVirtualAttributeCfg.class);
+        // should use default mockito behavior
+        assertThat(mock.isEnabled()).isEqualTo(false);
+    }
+
+    @Test
+    public void testPropertyWithDefaultBooleanReturnValue() throws Exception {
+        CoreSchemaCfg mock = mockCfg(CoreSchemaCfg.class);
+        assertThat(mock.isStrictFormatCountryString()).isEqualTo(true);
+    }
+
+    @Test
+    public void testNonPropertyMethod() throws Exception {
+        CoreSchemaCfg mock = mockCfg(CoreSchemaCfg.class);
+        assertThat(mock.dn()).isNull();
+
+        // Ensure we can add behavior to the mock
+        when(mock.dn()).thenReturn(DN.rootDN());
+        assertThat(mock.dn()).isNotNull();
+        assertThat(mock.dn().toString()).isEqualTo(DN.rootDN().toString());
+    }
+
 }

--
Gitblit v1.10.0