Author: jacopoc
Date: Mon Sep 19 13:36:32 2016
New Revision: 1761440

URL: http://svn.apache.org/viewvc?rev=1761440&view=rev
Log:
Implemented: unit tests for some mechanisms to create delegator objects and for 
the initialization of the ControlFilter.

These unit tests are experimental but interesting because they provide test 
coverage in isolation for some aspect of the framework that were not covered 
before or only covered by integration tests.
The tests for the ControlFilter are particularly interesting because they 
leverage Mockito to run the test in isolation by mocking the FilterConfig 
object.
Please review and run these tests in your local environment, provide your 
feedback and report any issues you may find: this will help to define a pattern 
for the definition of unit tests that could be applied more extensively.

Added:
    ofbiz/trunk/framework/entity/src/test/
    ofbiz/trunk/framework/entity/src/test/java/
    ofbiz/trunk/framework/entity/src/test/java/org/
    ofbiz/trunk/framework/entity/src/test/java/org/apache/
    ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/
    ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/
    
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
   (with props)
    ofbiz/trunk/framework/webapp/src/test/
    ofbiz/trunk/framework/webapp/src/test/java/
    ofbiz/trunk/framework/webapp/src/test/java/org/
    ofbiz/trunk/framework/webapp/src/test/java/org/apache/
    ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/
    ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/
    ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/
    
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
   (with props)
Modified:
    ofbiz/trunk/build.gradle
    
ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlFilter.java

Modified: ofbiz/trunk/build.gradle
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/build.gradle?rev=1761440&r1=1761439&r2=1761440&view=diff
==============================================================================
--- ofbiz/trunk/build.gradle (original)
+++ ofbiz/trunk/build.gradle Mon Sep 19 13:36:32 2016
@@ -133,6 +133,7 @@ dependencies {
     compile 'org.zapodot:jackson-databind-java-optional:2.4.2'
     compile 'oro:oro:2.0.8'
     compile 'wsdl4j:wsdl4j:1.6.2'
+    testCompile 'org.mockito:mockito-core:1.+'
 
     // general framework runtime libs
     runtime 'de.odysseus.juel:juel-spi:2.2.7'
@@ -206,6 +207,7 @@ sourceSets {
         resources {
             srcDirs = getDirectoryInActiveComponentsIfExists('src/main/java')
             srcDirs += getDirectoryInActiveComponentsIfExists('config')
+            srcDirs += getDirectoryInActiveComponentsIfExists('dtd')
             exclude excludedJavaSources
         }
     }

Added: 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java?rev=1761440&view=auto
==============================================================================
--- 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
 (added)
+++ 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
 Mon Sep 19 13:36:32 2016
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * 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.ofbiz.entity;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.junit.Assert.*;
+
+public class DelegatorUnitTests {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Before
+    public void initialize() {
+        System.setProperty("ofbiz.home", ".");
+        System.setProperty("derby.system.home", "./runtime/data/derby");
+    }
+
+    @Test
+    public void 
delegatorCreationUsingConstructorFailsIfConfigurationIsMissing() throws 
GenericEntityException {
+        expectedException.expect(GenericEntityException.class);
+        expectedException.expectMessage("No configuration found for 
delegator");
+        new GenericDelegator("delegatorNameWithNoConfiguration");
+    }
+
+    @Test
+    public void delegatorCreationUsingConstructor() throws 
GenericEntityException {
+        Delegator delegator = new GenericDelegator("default");
+        assertNotNull(delegator);
+        assertEquals(delegator.getOriginalDelegatorName(), "default");
+        assertEquals(delegator.getDelegatorBaseName(), "default");
+        assertEquals(delegator.getDelegatorName(), "default");
+    }
+
+    @Test
+    public void delegatorCreationUsingFactoryGetInstance() {
+        DelegatorFactory df = new DelegatorFactoryImpl();
+        assertNotNull(df);
+        Delegator delegator = df.getInstance("default");
+        assertNotNull(delegator);
+        assertTrue(delegator instanceof GenericDelegator);
+        assertEquals(delegator.getOriginalDelegatorName(), "default");
+        assertEquals(delegator.getDelegatorBaseName(), "default");
+        assertEquals(delegator.getDelegatorName(), "default");
+        Delegator delegatorWithSameName = df.getInstance("default");
+        assertNotSame(delegator, delegatorWithSameName);
+    }
+
+    @Test
+    public void delegatorCreationUsingFactoryGetDelegator() {
+        DelegatorFactory df = new DelegatorFactoryImpl();
+        Delegator delegator = df.getDelegator("default");
+        assertNotNull(delegator);
+        assertTrue(delegator instanceof GenericDelegator);
+        assertEquals(delegator.getOriginalDelegatorName(), "default");
+        assertEquals(delegator.getDelegatorBaseName(), "default");
+        assertEquals(delegator.getDelegatorName(), "default");
+        Delegator delegatorWithSameName = df.getDelegator("default");
+        assertSame(delegator, delegatorWithSameName);
+        Delegator delegatorWithNullName = df.getDelegator(null);
+        assertSame(delegator, delegatorWithNullName);
+    }
+
+    @Test
+    public void 
delegatorCreationUsingFactoryReturnsNullIfConfigurationIsMissing() throws 
GenericEntityException {
+        // TODO: the framework code should throw the exception instead of 
returning a null reference
+        DelegatorFactory df = new DelegatorFactoryImpl();
+        Delegator delegator = 
df.getInstance("delegatorNameWithNoConfiguration");
+        assertNull(delegator);
+    }
+
+}

Propchange: 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: 
ofbiz/trunk/framework/entity/src/test/java/org/apache/ofbiz/entity/DelegatorUnitTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlFilter.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlFilter.java?rev=1761440&r1=1761439&r2=1761440&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlFilter.java
 (original)
+++ 
ofbiz/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlFilter.java
 Mon Sep 19 13:36:32 2016
@@ -66,7 +66,7 @@ public class ControlFilter implements Fi
     private boolean redirectAll;
     private boolean redirectPathIsUrl;
     private String redirectPath;
-    private int errorCode;
+    protected int errorCode;
     private Set<String> allowedPaths = new HashSet<>();
 
     @Override

Added: 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java?rev=1761440&view=auto
==============================================================================
--- 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
 (added)
+++ 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
 Mon Sep 19 13:36:32 2016
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * 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.ofbiz.webapp.control;
+
+import org.junit.Test;
+
+import javax.servlet.FilterConfig;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+public class ControlFilterTests {
+    @Test
+    public void initRetrievesAllInitParameters() throws Exception {
+        FilterConfig config = mock(FilterConfig.class);
+        ControlFilter cf = new ControlFilter();
+        cf.init(config);
+        verify(config).getInitParameter("redirectPath");
+        verify(config).getInitParameter("forceRedirectAll");
+        verify(config).getInitParameter("errorCode");
+        verify(config).getInitParameter("allowedPaths");
+        verifyNoMoreInteractions(config);
+    }
+
+    @Test
+    public void initSetsProperErrorCode() throws Exception {
+        FilterConfig config = mock(FilterConfig.class);
+        ControlFilter cf = new ControlFilter();
+        // if no errorCode parameter is specified then the default error code 
is 403
+        cf.init(config);
+        assertEquals(cf.errorCode, 403);
+        // if the errorCode parameter is empty then the default error code is 
403
+        when(config.getInitParameter("errorCode")).thenReturn("");
+        cf.init(config);
+        assertEquals(cf.errorCode, 403); // default error code is 403
+        // if an invalid errorCode parameter is specified then the default 
error code is 403
+        when(config.getInitParameter("errorCode")).thenReturn("NaN");
+        cf.init(config);
+        assertEquals(cf.errorCode, 403);
+        // if the errorCode parameter is specified then it is set in the filter
+        when(config.getInitParameter("errorCode")).thenReturn("404");
+        cf.init(config);
+        assertEquals(cf.errorCode, 404);
+    }
+}

Propchange: 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: 
ofbiz/trunk/framework/webapp/src/test/java/org/apache/ofbiz/webapp/control/ControlFilterTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to