view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/diff/DiffParserTest.java @ 701:ec3a72046099

more code for diffs. Handle commit not reported case
author eugene.petrenko@jetbrains.com
date Tue, 07 Jan 2014 12:51:27 +0100
parents a9adc3daf252
children
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial.command.diff;

import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created 03.01.14 15:55
 *
 * @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
 */
@Test
public class DiffParserTest {
  public static final String TEST_01 = "JJJJ 26952844704d\n" +
          "diff -r 0b4e7f84d45f -r 26952844704d .hgsubstate\n" +
          "--- a/.hgsubstate\tThu Jan 19 17:27:48 2012 +0400\n" +
          "+++ b/.hgsubstate\tThu Jan 19 17:28:16 2012 +0400\n" +
          "@@ -1,2 +1,2 @@\n" +
          "-a4df811123136e817028a44226ff2377bb8bb377 Platform\n" +
          "-5a78af38f4971548792897bcdff778e533068f74 Psi.Features\n" +
          "+0ccb706430e57563ec7caf7a1f3f5212ea661c1a Platform\n" +
          "+627758500a98b3834358aa885549fedccacca26b Psi.Features\n" +
          "\n" +
          "JJJJ 0b4e7f84d45f\n" +
          "diff -r 75a13ad66b06 -r 0b4e7f84d45f .hgsubstate\n" +
          "--- a/.hgsubstate\tThu Jan 19 17:27:01 2012 +0400\n" +
          "+++ b/.hgsubstate\tThu Jan 19 17:27:48 2012 +0400\n" +
          "@@ -1,2 +1,2 @@\n" +
          "-c59974e3c8e7d1758aade9befa4d3419c6a8a6b6 Platform\n" +
          "-5e99e7af8a284e201fe8a659239cd4d05c783f02 Psi.Features\n" +
          "+a4df811123136e817028a44226ff2377bb8bb377 Platform\n" +
          "+5a78af38f4971548792897bcdff778e533068f74 Psi.Features\n" +
          "\n" +
          "JJJJ 75a13ad66b06\n" +
          "diff -r 96eebd936bd2 -r 75a13ad66b06 .hgsubstate\n" +
          "--- /dev/null\tThu Jan 01 00:00:00 1970 +0000\n" +
          "+++ b/.hgsubstate\tThu Jan 19 17:27:01 2012 +0400\n" +
          "@@ -0,0 +1,2 @@\n" +
          "+c59974e3c8e7d1758aade9befa4d3419c6a8a6b6 Platform\n" +
          "+5e99e7af8a284e201fe8a659239cd4d05c783f02 Psi.Features\n" +
          "\n";

  @NotNull
  public static String hugeLog() throws IOException {
    return new String(FileUtil.loadFileText(new File("mercurial-tests/testData/subst/substates.diff.txt"), "utf-8"));
  }

  @Test
  public void should_provide_callbacks_for_simple_diff() {
    do_test(TEST_01,
            "remove 26952844704d, 0b4e7f84d45f, .hgsubstate, a4df811123136e817028a44226ff2377bb8bb377 Platform",
            "remove 26952844704d, 0b4e7f84d45f, .hgsubstate, 5a78af38f4971548792897bcdff778e533068f74 Psi.Features",
            "append 26952844704d, 0b4e7f84d45f, .hgsubstate, 0ccb706430e57563ec7caf7a1f3f5212ea661c1a Platform",
            "append 26952844704d, 0b4e7f84d45f, .hgsubstate, 627758500a98b3834358aa885549fedccacca26b Psi.Features",
            "remove 0b4e7f84d45f, 75a13ad66b06, .hgsubstate, c59974e3c8e7d1758aade9befa4d3419c6a8a6b6 Platform",
            "remove 0b4e7f84d45f, 75a13ad66b06, .hgsubstate, 5e99e7af8a284e201fe8a659239cd4d05c783f02 Psi.Features",
            "append 0b4e7f84d45f, 75a13ad66b06, .hgsubstate, a4df811123136e817028a44226ff2377bb8bb377 Platform",
            "append 0b4e7f84d45f, 75a13ad66b06, .hgsubstate, 5a78af38f4971548792897bcdff778e533068f74 Psi.Features",
            "append 75a13ad66b06, <null>, .hgsubstate, c59974e3c8e7d1758aade9befa4d3419c6a8a6b6 Platform",
            "append 75a13ad66b06, <null>, .hgsubstate, 5e99e7af8a284e201fe8a659239cd4d05c783f02 Psi.Features");
  }


  @Test
  public void huge_should_report_commit_005452865313() throws IOException {
    final String theId = "005452865313";
    final String theFile = ".hgsubstate";

    final AtomicBoolean hasBase = new AtomicBoolean();
    final AtomicBoolean hasIt = new AtomicBoolean();

    processDiff(hugeLog(), new DiffProcessor() {
      private void update(@NotNull final String revision, @Nullable final String baseRevision, @NotNull final String file) {
        if (file.equals(theFile) && theId.equals(revision)) hasIt.set(true);
        if (file.equals(theFile) && theId.equals(baseRevision)) hasBase.set(true);
      }

      public void append(@NotNull String revision, @Nullable String baseRevision, @NotNull String file, @NotNull String line) {
        update(revision, baseRevision, file);
      }

      public void unchanged(@NotNull String revision, @Nullable String baseRevision, @NotNull String file, @NotNull String line) {
      }

      public void remove(@NotNull String revision, @Nullable String baseRevision, @NotNull String file, @NotNull String line) {
        update(revision, baseRevision, file);
      }
    });

    Assert.assertTrue(hasBase.get());
    Assert.assertTrue(hasIt.get());
  }

  public void do_test(@NotNull String input, @NotNull String... gold) {
    final List<String> log = new ArrayList<String>();
    final DiffProcessor proc = (DiffProcessor) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{DiffProcessor.class}, new InvocationHandler() {
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        List<String> argz = new ArrayList<String>();
        for (Object o : args) {
          if (o == null) {
            argz.add("<null>");
          } else {
            argz.add(o.toString());
          }
        }

        final String call = method.getName() + " " + StringUtil.join(", ", argz);
        log.add(call);
        System.out.println(call);
        return null;
      }
    });

    processDiff(input, proc);

    Assert.assertEquals(log, new ArrayList<String>(Arrays.asList(gold)));
  }

  public static void processDiff(@NotNull final String input,
                                 @NotNull final DiffProcessor proc) {
    final String[] lines = StringUtil.splitByLines(input);
    DiffParser.parse(new LinesIterator() {
      int ix = 0;

      @Nullable
      public String nextLine() {
        if (ix >= lines.length) return null;
        return lines[ix++];
      }
    }, proc);
  }
}