deepdiff.unitprocessor.PropertiesCompareDiffUnitProcessor Maven / Gradle / Ivy
/*
* 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.unitprocessor;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import org.apache.log4j.Logger;
import deepdiff.core.DiffPoint;
import deepdiff.core.DiffPointProcessor;
import deepdiff.core.DiffScope;
import deepdiff.core.DiffUnit;
import deepdiff.core.DiffUnitProcessor;
/**
* A DiffUnitProcessor that performs Properties comparisons on DiffUnits.
*/
public class PropertiesCompareDiffUnitProcessor implements DiffUnitProcessor {
private static final Logger log = Logger.getLogger(PropertiesCompareDiffUnitProcessor.class);
/**
* Performs a Properties comparison on diffUnit
, and passes any {@link DiffPoint}s
* found to processor
. The properties are iterated through and their values
* compared.
*
* @param diffUnit the unit to process
* @param processor the processor to pass differences to
*
* @return null (no child scopes needed)
*/
public DiffScope processDiffUnit(DiffUnit diffUnit, DiffPointProcessor processor) {
Properties p1 = new Properties();
Properties p2 = new Properties();
InputStream is1 = null;
InputStream is2 = null;
try {
is1 = diffUnit.getLeftInputStream();
is2 = diffUnit.getRightInputStream();
p1.load(is1);
p2.load(is2);
Set keys = new TreeSet();
keys.addAll(p1.stringPropertyNames());
keys.addAll(p2.stringPropertyNames());
for (Iterator it = keys.iterator(); it.hasNext();) {
String key = it.next();
String v1 = p1.getProperty(key);
String v2 = p2.getProperty(key);
if (v1 == null) {
DiffPoint diffPoint = new DiffPoint(diffUnit, "Property in right only: " + key);
processor.processDiffPoint(diffPoint);
} else if (v2 == null) {
DiffPoint diffPoint = new DiffPoint(diffUnit, "Property in left only: " + key);
processor.processDiffPoint(diffPoint);
} else if (!v1.equals(v2)) {
DiffPoint diffPoint = new DiffPoint(diffUnit,
"Property value mismatch for key " + key + ": " + v1 + ", " + v2);
processor.processDiffPoint(diffPoint);
}
}
} catch (IOException ioe) {
log.error("Failure comparing properties file: " + diffUnit.getScopedPath(), ioe);
} finally {
if (is1 != null) {
try {
is1.close();
} catch (Exception ex) {
// Ignore exception on close
}
}
if (is2 != null) {
try {
is2.close();
} catch (Exception ex) {
// Ignore exception on close
}
}
}
return null; // No child scopes needed
}
}