Repository: commons-text Updated Branches: refs/heads/myers-algo a7e88eef1 -> 38f79f914
Fix Javadocs and include initial tests Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/38f79f91 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/38f79f91 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/38f79f91 Branch: refs/heads/myers-algo Commit: 38f79f914c73d902ad720d3bb7e46cad5063b1be Parents: a7e88ee Author: Bruno P. Kinoshita <ki...@apache.org> Authored: Thu Feb 5 21:36:49 2015 -0200 Committer: Bruno P. Kinoshita <ki...@apache.org> Committed: Thu Feb 5 21:36:49 2015 -0200 ---------------------------------------------------------------------- .../apache/commons/text/diff/EditCommand.java | 4 + .../commons/text/diff/StringsComparator.java | 44 +------ .../text/diff/StringsComparatorTest.java | 121 +++++++++++++++++++ 3 files changed, 126 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/38f79f91/src/main/java/org/apache/commons/text/diff/EditCommand.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/diff/EditCommand.java b/src/main/java/org/apache/commons/text/diff/EditCommand.java index 627db4d..972cebb 100644 --- a/src/main/java/org/apache/commons/text/diff/EditCommand.java +++ b/src/main/java/org/apache/commons/text/diff/EditCommand.java @@ -41,6 +41,10 @@ package org.apache.commons.text.diff; * and the <code>equals</code> method is specialized. * </p> * + * <p> + * This code has been adapted from Apache Commons Collections 4.0. + * </p> + * * @see StringsComparator * @see EditScript * http://git-wip-us.apache.org/repos/asf/commons-text/blob/38f79f91/src/main/java/org/apache/commons/text/diff/StringsComparator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/diff/StringsComparator.java b/src/main/java/org/apache/commons/text/diff/StringsComparator.java index c2fbfab..b2940fa 100644 --- a/src/main/java/org/apache/commons/text/diff/StringsComparator.java +++ b/src/main/java/org/apache/commons/text/diff/StringsComparator.java @@ -42,7 +42,7 @@ package org.apache.commons.text.diff; * the second one. * * <p> - * This class has been adapted from the commons-collections implementation. + * This code has been adapted from Apache Commons Collections 4.0. * </p> * * @see EditScript @@ -325,46 +325,4 @@ public class StringsComparator { } } - public static void main(String[] args) { - StringsComparator sc = new StringsComparator("O Bruno eh um bom rapaz. Ele eh do Brasil.", "O Bruno foi um bom rapaz. Ele eh do Brasil ."); - EditScript<Character> es = sc.getScript(); - es.visit(new CommandVisitor<Character>() { - - boolean nlAdd = true; - boolean nlRemove = true; - - @Override - public void visitInsertCommand(Character object) { - if (nlAdd) { - System.out.println(); - System.out.print("> "); - nlAdd = false; - } - System.out.print(object); - } - - @Override - public void visitKeepCommand(Character object) { - if (!nlAdd) { - nlAdd = true; - } - if (!nlRemove) { - nlRemove = true; - System.out.println(); - } - System.out.print(object); - } - - @Override - public void visitDeleteCommand(Character object) { - if (nlRemove) { - System.out.println(); - System.out.print("< "); - nlRemove = false; - } - System.out.print(object); - } - }); - } - } http://git-wip-us.apache.org/repos/asf/commons-text/blob/38f79f91/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java b/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java new file mode 100644 index 0000000..a988581 --- /dev/null +++ b/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java @@ -0,0 +1,121 @@ +/* + * 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.commons.text.diff; + +import java.util.Arrays; +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StringsComparatorTest { + + private List<String> before; + private List<String> after; + private int[] length; + + @Test + public void testLength() { + for (int i = 0; i < before.size(); ++i) { + final StringsComparator comparator = new StringsComparator(before.get(i), after.get(i)); + Assert.assertEquals(length[i], comparator.getScript().getModifications()); + } + } + + @Test + public void testExecution() { + for (int i = 0; i < before.size(); ++i) { + final ExecutionVisitor<Character> ev = new ExecutionVisitor<Character>(); + new StringsComparator(before.get(i), after.get(i)).getScript().visit(ev); + Assert.assertEquals(after.get(i), ev.getString()); + } + } + + private class ExecutionVisitor<T> implements CommandVisitor<T> { + + private StringBuilder v; + + public ExecutionVisitor() { + v = new StringBuilder(); + } + + public void visitInsertCommand(final T object) { + v.append(object); + } + + public void visitKeepCommand(final T object) { + v.append(object); + } + + public void visitDeleteCommand(final T object) { + } + + public String getString() { + return v.toString(); + } + + } + + @Before + public void setUp() { + + before = Arrays.asList( + "bottle", + "nematode knowledge", + "", + "aa", + "prefixed string", + "ABCABBA", + "glop glop", + "coq", + "spider-man"); + + after = Arrays.asList( + "noodle", + "empty bottle", + "", + "C", + "prefix", + "CBABAC", + "pas glop pas glop", + "ane", + "klingon"); + + length = new int[] { + 6, + 16, + 0, + 3, + 9, + 5, + 8, + 6, + 13 + }; + + } + + @After + public void tearDown() { + before = null; + after = null; + length = null; + } + +}