David Carr avatar David Carr committed 5204324

fixes #6; For now, instead of having a fallback XML unit processor, just don't use the XML unit processor as part of the default configuration.
Add support for unit testing to app component.
Add unit test for default configuration.

Comments (0)

Files changed (5)

-<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">
+<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">
 
     <modelVersion>4.0.0</modelVersion>
 
     <properties>
         <project.version.exe>1.0.0</project.version.exe>
     </properties>
-    
+
     <build>
         <plugins>
             <plugin>
             <artifactId>commons-cli</artifactId>
             <version>1.2</version>
         </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>r09</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <pluginRepositories>
         <pluginRepository>

app/src/main/resources/default.conf.xml

     <unit-processor id="binary" class="deepdiff.unitprocessor.BinaryCompareDiffUnitProcessor"/>
     <unit-processor id="default" class="deepdiff.unitprocessor.DelegatingDiffUnitProcessor">
         <!-- These are in order of precedence, high to low -->
-        <property type="handler" value="xml" name=".*\.xml(#.*)?" />
-        <property type="handler" value="manifest"
-            name=".*[/\\!]META-INF[/\\]MANIFEST\.MF"/>
-        <property type="handler" value="properties"
-            name=".*\.properties" />
-        <property type="handler" value="zip"
-            name=".*\.zip" />
-        <property type="handler" value="zip"
-            name=".*\.jar" />
-        <property type="handler" value="zip"
-            name=".*\.war" />
-        <property type="handler" value="zip"
-            name=".*\.ear" />
-        <!--
-          - TODO: intelligent handling of .rar extension
-          -
-          - For now, always treat .rar files as ZIP files.  This isn't a good
-          - assumption, but it'll work as long as we don't encounter any
-          - RAR-formatted .rar files.
-          -->
-        <property type="handler" value="zip"
-            name=".*\.rar" />
+        <!-- <property type="handler" value="xml" name=".*\.xml(#.*)?" /> -->
+        <property type="handler" value="manifest" name=".*[/\\!]META-INF[/\\]MANIFEST\.MF"/>
+        <property type="handler" value="properties" name=".*\.properties" />
+        <property type="handler" value="zip" name=".*\.zip" />
+        <property type="handler" value="zip" name=".*\.jar" />
+        <property type="handler" value="zip" name=".*\.war" />
+        <property type="handler" value="zip" name=".*\.ear" />
         <property type="handler" value="binary" name=".*" />
     </unit-processor>
 

app/src/test/java/deepdiff/app/DeepDiffTest.java

+/*
+ * Copyright 2011 DeepDiff Contributors
+ *
+ * Licensed 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 deepdiff.app;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+
+import org.junit.After;
+import org.junit.Test;
+import org.xml.sax.InputSource;
+
+import com.google.common.collect.Lists;
+
+import deepdiff.core.DiffPointProcessor;
+import deepdiff.core.DiffPointProcessorFactory;
+import deepdiff.core.DiffUnitProcessor;
+import deepdiff.core.DiffUnitProcessorFactory;
+import deepdiff.pointprocessor.OutputDiffPointProcessor;
+import deepdiff.pointprocessor.RegexFilterDiffPointProcessor;
+import deepdiff.unitprocessor.BinaryCompareDiffUnitProcessor;
+import deepdiff.unitprocessor.DelegatingDiffUnitProcessor;
+import deepdiff.unitprocessor.ManifestCompareDiffUnitProcessor;
+import deepdiff.unitprocessor.PropertiesCompareDiffUnitProcessor;
+import deepdiff.unitprocessor.SortedZipCompareDiffUnitProcessor;
+import deepdiff.unitprocessor.XMLCompareDiffUnitProcessor;
+
+public class DeepDiffTest {
+    @After
+    public void tearDown() {
+        DiffUnitProcessorFactory.clear();
+        DiffPointProcessorFactory.clear();
+    }
+
+    @Test
+    public void testDefaultConfig() throws Exception {
+        DeepDiff app = new DeepDiff();
+        InputSource source = new InputSource(app.getDefaultConfigStream());
+        app.loadConfig(source);
+
+        DiffUnitProcessor manifestUnitProcessor = DiffUnitProcessorFactory.get("manifest");
+        DiffUnitProcessor propertiesUnitProcessor = DiffUnitProcessorFactory.get("properties");
+        DiffUnitProcessor zipUnitProcessor = DiffUnitProcessorFactory.get("zip");
+        DiffUnitProcessor xmlUnitProcessor = DiffUnitProcessorFactory.get("xml");
+        DiffUnitProcessor binaryUnitProcessor = DiffUnitProcessorFactory.get("binary");
+        DiffUnitProcessor defaultUnitProcessor = DiffUnitProcessorFactory.getDefault();
+        assertThat(manifestUnitProcessor, instanceOf(ManifestCompareDiffUnitProcessor.class));
+        assertThat(propertiesUnitProcessor, instanceOf(PropertiesCompareDiffUnitProcessor.class));
+        assertThat(zipUnitProcessor, instanceOf(SortedZipCompareDiffUnitProcessor.class));
+        assertThat(xmlUnitProcessor, instanceOf(XMLCompareDiffUnitProcessor.class));
+        assertThat(binaryUnitProcessor, instanceOf(BinaryCompareDiffUnitProcessor.class));
+        assertThat(defaultUnitProcessor, instanceOf(DelegatingDiffUnitProcessor.class));
+        DelegatingDiffUnitProcessor delegatingUnitProcessor = (DelegatingDiffUnitProcessor) defaultUnitProcessor;
+        assertThat(Lists.newArrayList(delegatingUnitProcessor.getHandlers().keySet()),
+                equalTo(Arrays.asList(".*[/\\\\!]META-INF[/\\\\]MANIFEST\\.MF", ".*\\.properties",
+                        ".*\\.zip", ".*\\.jar", ".*\\.war", ".*\\.ear", ".*")));
+        assertThat(Lists.newArrayList(delegatingUnitProcessor.getHandlers().values()),
+                equalTo(Arrays.asList(manifestUnitProcessor, propertiesUnitProcessor,
+                        zipUnitProcessor, zipUnitProcessor, zipUnitProcessor, zipUnitProcessor,
+                        binaryUnitProcessor)));
+
+        DiffPointProcessor outPointProcessor = DiffPointProcessorFactory.get("out");
+        DiffPointProcessor defaultPointProcessor = DiffPointProcessorFactory.getDefault();
+        assertThat(outPointProcessor, instanceOf(OutputDiffPointProcessor.class));
+        assertThat(defaultPointProcessor, instanceOf(RegexFilterDiffPointProcessor.class));
+        RegexFilterDiffPointProcessor regexPointProcessor = (RegexFilterDiffPointProcessor) defaultPointProcessor;
+        assertThat(regexPointProcessor.getChild(), sameInstance(outPointProcessor));
+    }
+}

engine/src/main/java/deepdiff/pointprocessor/RegexFilterDiffPointProcessor.java

 import deepdiff.core.IllegalConfigException;
 
 /**
- * A {@link DiffPointProcessor} that selectively ignores some DiffPoints based
- * on configurable regular expressions, and then passes the remaining {@link
- * DiffPoint}s on to another {@link DiffPointProcessor} for further processing.
+ * A {@link DiffPointProcessor} that selectively ignores some DiffPoints based on configurable
+ * regular expressions, and then passes the remaining {@link DiffPoint}s on to another
+ * {@link DiffPointProcessor} for further processing.
  */
-public class RegexFilterDiffPointProcessor implements DiffPointProcessor,
-    Configurable {
+public class RegexFilterDiffPointProcessor implements DiffPointProcessor, Configurable {
     private static final String TYPE_CHILD = "child";
     private static final String TYPE_FILTER = "filter";
 
-    private static final Logger log =
-        Logger.getLogger(RegexFilterDiffPointProcessor.class);
+    private static final Logger log = Logger.getLogger(RegexFilterDiffPointProcessor.class);
 
     private final Collection<Pattern[]> filters = new LinkedList<Pattern[]>();
     private DiffPointProcessor child;
 
+    public DiffPointProcessor getChild() {
+        return child;
+    }
+
     /**
-     * Processes a {@link DiffPoint}. First, it checks the {@link DiffPoint}
-     * against the configured regular expressions to see if it should be
-     * ignored. If not, it delegates further processing to the child processor.
-     *
+     * Processes a {@link DiffPoint}. First, it checks the {@link DiffPoint} against the configured
+     * regular expressions to see if it should be ignored. If not, it delegates further processing
+     * to the child processor.
+     * 
      * @param diffPoint the {@link DiffPoint} to process
      */
     public void processDiffPoint(DiffPoint diffPoint) {
     }
 
     /**
-     * Checks whether the specified {@link DiffPoint} matches any of the
-     * configured regular expressions.
-     *
-     * @param diffPoint the {@link DiffPoint} to check against the regular
-     *                  expressions
-     *
-     * @return whether the specified {@link DiffPoint} should be ignored due to
-     *         the configured regular expressions
+     * Checks whether the specified {@link DiffPoint} matches any of the configured regular
+     * expressions.
+     * 
+     * @param diffPoint the {@link DiffPoint} to check against the regular expressions
+     * 
+     * @return whether the specified {@link DiffPoint} should be ignored due to the configured
+     *         regular expressions
      */
     protected boolean isVetoed(DiffPoint diffPoint) {
         String scopedPath = diffPoint.getDiffUnit().getScopedPath();
                 String message = diffPoint.getMessage();
                 if (patternApplies(messagePattern, message)) {
                     log.debug("Diff point (" + scopedPath + ", " + message
-                        + " vetoed due to filter");
+                            + " vetoed due to filter");
                     return true;
                 }
             }
     }
 
     /**
-     * Checks whether a specific Pattern matches a String. If no Pattern is
-     * specified, it matches all content.
-     *
-     * @param pattern the Pattern to check against the content; if null, all
-     *                content is considered a match
+     * Checks whether a specific Pattern matches a String. If no Pattern is specified, it matches
+     * all content.
+     * 
+     * @param pattern the Pattern to check against the content; if null, all content is considered a
+     *            match
      * @param content the content to check against the Pattern
-     *
+     * 
      * @return whether the pattern matches the content
      */
     private boolean patternApplies(Pattern pattern, String content) {
             Matcher matcher = pattern.matcher(content);
             return matcher.matches();
         } else {
-            //Null pattern means match everything
+            // Null pattern means match everything
             return true;
         }
     }
 
     /**
      * Adds a property to the processor configuration
-     *
+     * 
      * @param property the property to add
-     *
+     * 
      * @throws IllegalConfigException if the property isn't valid
      */
-    public void addProperty(ConfigProperty property)
-                     throws IllegalConfigException {
+    public void addProperty(ConfigProperty property) throws IllegalConfigException {
         String type = property.getType();
         if (TYPE_CHILD.equals(type)) {
             if (child != null) {
-                throw new IllegalConfigException("Filter already has a "
-                    + TYPE_CHILD);
+                throw new IllegalConfigException("Filter already has a " + TYPE_CHILD);
             }
             String id = property.getValue();
             child = DiffPointProcessorFactory.get(id);
                 Pattern[] filter = { pathPattern, messagePattern };
                 filters.add(filter);
             } catch (PatternSyntaxException pse) {
-                throw new IllegalConfigException("Invalid pattern specified",
-                    pse);
+                throw new IllegalConfigException("Invalid pattern specified", pse);
             }
         } else {
-            throw new IllegalConfigException("Unsupported property type: "
-                + type);
+            throw new IllegalConfigException("Unsupported property type: " + type);
         }
     }
 
     /**
      * Checks that a valid child processor was specified
-     *
+     * 
      * @throws IllegalConfigException if no valid child processor was specified
      */
     public void validateProperties() throws IllegalConfigException {
         if (child == null) {
-            throw new IllegalConfigException("No property with type "
-                + TYPE_CHILD + " specified");
+            throw new IllegalConfigException("No property with type " + TYPE_CHILD + " specified");
         }
         log.info("Configured " + filters.size() + " filters");
     }

engine/src/main/java/deepdiff/unitprocessor/DelegatingDiffUnitProcessor.java

 
 import java.io.InputStream;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
     public static final String TYPE_HANDLER = "handler";
 
     private static final Logger log = Logger.getLogger(DelegatingDiffUnitProcessor.class);
-    private Map<String, DiffUnitProcessor> fileHandlers = new LinkedHashMap<String, DiffUnitProcessor>();
+    private Map<String, DiffUnitProcessor> handlers = new LinkedHashMap<String, DiffUnitProcessor>();
+
     private Collection<Pattern> excludes = new LinkedList<Pattern>();
 
+    public Map<String, DiffUnitProcessor> getHandlers() {
+        return Collections.unmodifiableMap(handlers);
+    }
+
     /**
      * Adds a property to the processor configuration
      * 
         if (TYPE_HANDLER.equals(type)) {
             String regex = property.getName();
             String handlerId = property.getValue();
-            if (fileHandlers.containsKey(regex)) {
+            if (handlers.containsKey(regex)) {
                 throw new IllegalConfigException("Multiple handlers are configured for pattern: "
                         + regex);
             }
             DiffUnitProcessor fileHandler = DiffUnitProcessorFactory.get(handlerId);
-            fileHandlers.put(regex, fileHandler);
+            handlers.put(regex, fileHandler);
         } else if (TYPE_EXCLUDE.equals(type)) {
             String regex = property.getValue();
             Pattern pattern = Pattern.compile(regex);
      * @throws IllegalConfigException if no file handlers were configured
      */
     public void validateProperties() throws IllegalConfigException {
-        if (fileHandlers.isEmpty()) {
+        if (handlers.isEmpty()) {
             throw new IllegalConfigException("No properties with type " + TYPE_HANDLER
                     + " specified");
         }
             processor.processDiffPoint(diffPoint);
         } else if (!leftIsDir && !rightIsDir) {
             boolean foundHandler = false;
-            for (Iterator<Map.Entry<String, DiffUnitProcessor>> it = fileHandlers.entrySet()
-                    .iterator(); it.hasNext();) {
+            for (Iterator<Map.Entry<String, DiffUnitProcessor>> it = handlers.entrySet().iterator(); it
+                    .hasNext();) {
                 Map.Entry<String, DiffUnitProcessor> entry = it.next();
                 String regex = entry.getKey();
                 if (scopedPath.matches(regex)) {
                 return null;
             }
         }
-        for (String regex : fileHandlers.keySet()) {
+        for (String regex : handlers.keySet()) {
             if (path.matches(regex)) {
-                DiffUnitProcessor unitProcessor = fileHandlers.get(regex);
+                DiffUnitProcessor unitProcessor = handlers.get(regex);
                 if (unitProcessor instanceof ScopedDiffUnitProcessor) {
                     ScopedDiffUnitProcessor sdup = (ScopedDiffUnitProcessor) unitProcessor;
                     return sdup.createScope(path, is1, is2);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.