target
.DS_Store
META-INF
+.fbExcludeFilterFile
<scope>compile</scope>
<version>1.2.0</version>
</dependency>
+ <dependency>
+ <groupId>com.googlecode.java-diff-utils</groupId>
+ <artifactId>diffutils</artifactId>
+ <version>1.3.0</version>
+ </dependency>
<dependency>
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>mdsal-binding-dom-adapter</artifactId>
<artifactId>mdsal-binding-test-model</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.google.truth</groupId>
+ <artifactId>truth</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<propertyExpansion>checkstyle.violationSeverity=error</propertyExpansion>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ <configuration>
+ <failOnError>true</failOnError>
+ </configuration>
+ </plugin>
</plugins>
</build>
import ch.vorburger.xtendbeans.AssertBeans;
import org.junit.ComparisonFailure;
import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Assertion utilities for YANG {@link DataObject}s.
*/
public final class AssertDataObjects {
+ private static final Logger LOG = LoggerFactory.getLogger(AssertDataObjects.class);
+
private static final XtendYangBeanGenerator GENERATOR = new XtendYangBeanGenerator();
private AssertDataObjects() {
static void assertEqualByText(String expectedText, Object actual) throws ComparisonFailure {
String actualText = GENERATOR.getExpression(actual);
if (!expectedText.equals(actualText)) {
+ String diff = DiffUtil.diff(expectedText, actualText);
+ LOG.warn("diff for ComparisonFailure about to be thrown:\n{}", diff);
throw new ComparisonFailure("Expected and actual beans do not match", expectedText, actualText);
}
}
*
* @author Michael Vorburger
*/
-//package-local: no need to expose this, consider it an implementation detail; public API is the AssertDataObjects
+// package-local: no need to expose this, consider it an implementation detail; public API is the AssertDataObjects
class AugmentableExtension {
private static final ReflectExtensions REFLECT_EXTENSIONS = new ReflectExtensions();
--- /dev/null
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.testutils;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+import difflib.DiffUtils;
+import difflib.Patch;
+import java.util.List;
+
+/**
+ * Utility to create diff of text.
+ *
+ * @author Michael Vorburger
+ */
+// package-local: no need to expose this, consider it an implementation detail; public API is the AssertDataObjects
+final class DiffUtil {
+
+ // Configuration which we could tune as we use this more
+ private static final int MAX_DIFFS = 1;
+ private static final int CONTEXT_LINES = 3; // number of lines of context output around each difference
+
+ private static final Splitter SPLITTER = Splitter.on(System.getProperty("line.separator"));
+ private static final Joiner JOINER = Joiner.on(System.getProperty("line.separator"));
+
+ public static String diff(String expectedText, String actualText) {
+ List<String> originalLines = SPLITTER.splitToList(expectedText);
+ List<String> revisedLines = SPLITTER.splitToList(actualText);
+ Patch<String> patch = DiffUtils.diff(originalLines, revisedLines);
+ List<String> diff = DiffUtils.generateUnifiedDiff("expected", "actual", originalLines, patch, CONTEXT_LINES);
+
+ String header = "";
+ int deltas = patch.getDeltas().size();
+ if (deltas > MAX_DIFFS) {
+ header = String.format("(Too many differences (%d); only showing first %d)%n", deltas, MAX_DIFFS);
+ diff = diff.subList(0, MAX_DIFFS);
+ }
+ return header + JOINER.join(diff);
+ }
+
+ private DiffUtil() { }
+
+}
*/
package org.opendaylight.mdsal.binding.testutils;
+import static com.google.common.truth.Truth.assertThat;
+
import ch.vorburger.xtendbeans.AssertBeans;
+import org.junit.ComparisonFailure;
import org.junit.Test;
import org.opendaylight.yangtools.yang.binding.DataObject;
AssertDataObjects.assertEqualBeans(first, second);
}
+ @Test
+ public void testSomeBeanMismatch() {
+ SomeBean expected = new SomeBean();
+ expected.setName("hello, world 1");
+
+ SomeBean actual = new SomeBean();
+ actual.setName("hello, world 2");
+
+ try {
+ AssertDataObjects.assertEqualBeans(expected, actual);
+ } catch (ComparisonFailure e) {
+ assertThat(e.getExpected()).contains("hello, world 1");
+ assertThat(e.getActual()).contains("hello, world 2");
+ }
+ }
}