Skip to content

Commit

Permalink
#53 - updated tests and some clean up
Browse files Browse the repository at this point in the history
Note: looks like we need to think twice about the current code structure
as it's moving to bad place;)
  • Loading branch information
sawickil committed Feb 7, 2016
1 parent 62a743e commit 7e2fd9b
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 136 deletions.
Binary file modified docs/screenshots/yagga-detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
@Component
public class AnnotateService {

private final AnnotateHandler gitAnnotateHandler;
private final AnnotateHandler annotateHandler;

@Autowired
public AnnotateService(AnnotateHandler gitAnnotateHandler) {
this.gitAnnotateHandler = gitAnnotateHandler;
public AnnotateService(AnnotateHandler annotateHandler) {
this.annotateHandler = annotateHandler;
}

public AnnotateResponse annotate(AnnotateRequest annotateRequest) {
return gitAnnotateHandler.annotate(annotateRequest);
return annotateHandler.annotate(annotateRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public class AnnotateResponse {

private List<AnnotateResponseLine> annotationResponseLines;

private String annotations;

private String fileContent;

public AnnotateResponse(String annotations, String fileContent) {
this.annotations = annotations;
this.fileContent = fileContent;
}

public AnnotateResponse(List<AnnotateResponseLine> annotateResponseLines) {
this.annotationResponseLines = annotateResponseLines;
}
Expand All @@ -34,12 +25,4 @@ public void addAnnotationResponseLine(AnnotateResponseLine line) {
public List<AnnotateResponseLine> getAnnotationResponseLines() {
return annotationResponseLines;
}

public String getAnnotations() {
return annotations;
}

public String getFileContent() {
return fileContent;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.yu55.yagga.core.annotate.model;

/**
* This class represents a single line of the code displayed on the view page. Each line consists of a few parts like
* commit id, author, date, line number and line.
* This class represents a single line of code displayed on the view page.
*/
public class AnnotateResponseLine {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.yu55.yagga.handler.generic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yu55.yagga.handler.api.DvcsRepository;

import java.util.List;
import java.util.Optional;

public class Repositories {
import org.yu55.yagga.handler.api.DvcsRepository;

private static final Logger logger = LoggerFactory.getLogger(Repositories.class);
public class Repositories {

private List<DvcsRepository> repositories;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.yu55.yagga.handler.generic.command;

import java.io.File;

import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.springframework.stereotype.Component;
Expand All @@ -8,18 +10,18 @@
@Component
public class CommandExecutorFactory {

public CommandExecutor factorize(Command command) {
return factorize(new File("."), null, command);
}

public CommandExecutor factorize(DvcsRepository repository, Command command) {
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(repository.getDirectory().toFile());
CommandExecutorStreamHandler executorStreamHandler =
new CommandExecutorStreamHandler(repository.getDirectoryName());
executor.setStreamHandler(new PumpStreamHandler(executorStreamHandler));
return new CommandExecutor(command, executor, executorStreamHandler::getOutput);
return factorize(repository.getDirectory().toFile(), repository.getDirectoryName(), command);
}

public CommandExecutor factorize(Command command) {
private CommandExecutor factorize(File dir, String name, Command command) {
DefaultExecutor executor = new DefaultExecutor();
CommandExecutorStreamHandler executorStreamHandler = new CommandExecutorStreamHandler();
executor.setWorkingDirectory(dir);
CommandExecutorStreamHandler executorStreamHandler = new CommandExecutorStreamHandler(name);
executor.setStreamHandler(new PumpStreamHandler(executorStreamHandler));
return new CommandExecutor(command, executor, executorStreamHandler::getOutput);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@ public class GitAnnotateResponseFactory {
"(\\w+)\\s+\\(\\s?(.+)\\s+(\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}\\s+[\\+\\-]?\\d{4})\\s+(\\d+)\\)(" +
".*)";

/*
* Don't touch it for now as I think we will have to re-think of the entire code structure, which does not seem
* pretty well right now;)
*/
public static AnnotateResponse factorizeAnnotateResponse(CommandOutput output) {
List<CommandOutputLine> outputLines = output.getOutputLines();
AnnotateResponse response = new AnnotateResponse();

for (CommandOutputLine outputLine : outputLines) {
String annotatedLine = outputLine.getLine();
if (output.getExitValue() == 0) {
Matcher matcher = Pattern.compile(LINE_PATTERN).matcher(annotatedLine);
if (matcher.matches()) {
String author = matcher.group(2).trim();
AnnotateResponseLine annotationResponseLine = new AnnotateResponseLine(
!author.equals("Not Committed Yet") ? matcher.group(1).trim() : "",
author, matcher.group(3).trim(),
Integer.parseInt(matcher.group(4).trim()),
matcher.group(5));
response.addAnnotationResponseLine(annotationResponseLine);
}
generateSuccessfulContent(response, annotatedLine);
} else {
response.addAnnotationResponseLine(new AnnotateResponseLine(annotatedLine));
generateUnsuccessfulContent(response, annotatedLine);
}
}

return response;
}

private static class SuccessfulGitAnnotateResponseProducer {
private static void generateSuccessfulContent(AnnotateResponse response, String annotatedLine) {
Matcher matcher = Pattern.compile(LINE_PATTERN).matcher(annotatedLine);
if (matcher.matches()) {
String author = matcher.group(2).trim();
AnnotateResponseLine annotationResponseLine = new AnnotateResponseLine(
!author.equals("Not Committed Yet") ? matcher.group(1).trim() : "",
author, matcher.group(3).trim(),
Integer.parseInt(matcher.group(4).trim()),
matcher.group(5));
response.addAnnotationResponseLine(annotationResponseLine);
}
}

private static void generateUnsuccessfulContent(AnnotateResponse response, String line) {
response.addAnnotationResponseLine(new AnnotateResponseLine(line));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.exec.CommandLine;
import org.yu55.yagga.handler.api.command.annotate.AnnotateParameters;
import org.yu55.yagga.handler.generic.command.Command;
import org.yu55.yagga.handler.mercurial.command.common.MercurialCommand;

public class MercurialAnnotateCommand implements MercurialCommand {
Expand All @@ -18,7 +17,7 @@ public CommandLine getCommandLine() {
CommandLine commandLine = CommandLine.parse(COMMAND);
commandLine.addArgument("annotate", false);
commandLine.addArgument("-u", false);
commandLine.addArgument("-n", false);
commandLine.addArgument("-c", false);
commandLine.addArgument("-l", false);
commandLine.addArgument("-d", false);
commandLine.addArgument(annotateParameters.getFile(), false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
package org.yu55.yagga.handler.mercurial.command.annotate;

import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.yu55.yagga.core.annotate.model.AnnotateResponse;
import org.yu55.yagga.core.annotate.model.AnnotateResponseLine;
import org.yu55.yagga.handler.generic.command.CommandOutput;
import org.yu55.yagga.handler.generic.command.CommandOutputLine;

import java.util.List;

public class MercurialAnnotateResponseFactory {

private static final String LINE_PATTERN =
"(\\w+)\\s+(\\w{12})\\s+(\\w{3}\\s+\\w{3}\\s+\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}\\s+\\d{4}\\s+[\\+\\-]?\\d{4})" +
":(\\d+):\\s+(.*)";

public static AnnotateResponse factorizeAnnotateResponse(CommandOutput output) {
List<CommandOutputLine> outputLines = output.getOutputLines();

StringBuilder annotationsStringBuilder = new StringBuilder();
StringBuilder fileContentStringBuilder = new StringBuilder();
AnnotateResponse response = new AnnotateResponse();

for (CommandOutputLine outputLine : outputLines) {
String annotatedLine = outputLine.getLine();
if (output.getExitValue() == 0) {
generateSuccessfulContent(annotationsStringBuilder, fileContentStringBuilder, annotatedLine);
generateSuccessfulContent(response, annotatedLine);
} else {
generateFailureContent(annotationsStringBuilder, annotatedLine);
generateUnsuccessfulContent(response, annotatedLine);
}
}

return new AnnotateResponse(annotationsStringBuilder.toString(), fileContentStringBuilder.toString());
return response;
}

private static void generateSuccessfulContent(StringBuilder annotationsStringBuilder,
StringBuilder fileContentStringBuilder, String annotatedLine) {
int splitIndex = StringUtils.ordinalIndexOf(annotatedLine,":", 4);
annotationsStringBuilder.append(annotatedLine.substring(0, splitIndex + 1)).append("\n");
fileContentStringBuilder.append(annotatedLine.substring(splitIndex + 1)).append("\n");
private static void generateSuccessfulContent(AnnotateResponse response, String annotatedLine) {
Matcher matcher = Pattern.compile(LINE_PATTERN).matcher(annotatedLine);
if (matcher.matches()) {
AnnotateResponseLine annotationResponseLine = new AnnotateResponseLine(
matcher.group(2).trim(),
matcher.group(1).trim(), matcher.group(3).trim(),
Integer.parseInt(matcher.group(4).trim()),
matcher.group(5));
response.addAnnotationResponseLine(annotationResponseLine);
}
}

private static void generateFailureContent(StringBuilder annotationsStringBuilder, String annotatedLine) {
annotationsStringBuilder.append(annotatedLine).append("\n");
private static void generateUnsuccessfulContent(AnnotateResponse response, String line) {
response.addAnnotationResponseLine(new AnnotateResponseLine(line));
}

}
17 changes: 0 additions & 17 deletions src/main/java/org/yu55/yagga/utils/StringUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.yu55.yagga.testutils.mockito.DvcsRepositoryResolverMockBehavior.should;
import static org.yu55.yagga.utils.mockito.DvcsRepositoryResolverMockBehavior.should;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.yu55.yagga.core.grep.model.GrepResponseAssert.assertThat;
import static org.yu55.yagga.testutils.mockito.DvcsRepositoryMockBehavior.should;
import static org.yu55.yagga.utils.mockito.DvcsRepositoryMockBehavior.should;

import org.junit.Test;
import org.yu55.yagga.core.grep.model.GrepRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.yu55.yagga.handler.git.GitRepositoryResolver.GIT_REPOSITORY_DISCRIMINATOR;
import static org.yu55.yagga.testutils.RepositoryFolderStub.stubGitRepository;
import static org.yu55.yagga.testutils.RepositoryFolderStub.stubUndefinedRepository;
import static org.yu55.yagga.utils.RepositoryFolderStub.stubGitRepository;
import static org.yu55.yagga.utils.RepositoryFolderStub.stubUndefinedRepository;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -21,7 +21,7 @@
import org.yu55.yagga.handler.generic.command.CommandOutput;
import org.yu55.yagga.handler.generic.command.CommandOutputLine;
import org.yu55.yagga.handler.git.command.common.GitCommandExecutorFactory;
import org.yu55.yagga.testutils.RepositoryFolderStub;
import org.yu55.yagga.utils.RepositoryFolderStub;

@RunWith(MockitoJUnitRunner.class)
public class GitRepositoryResolverTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package org.yu55.yagga.handler.git.command.annotate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.yu55.yagga.handler.git.command.annotate.GitAnnotateResponseFactory.factorizeAnnotateResponse;

import org.junit.Test;
import org.yu55.yagga.core.annotate.model.AnnotateResponse;
import org.yu55.yagga.core.annotate.model.AnnotateResponseLine;
import org.yu55.yagga.core.annotate.model.AnnotateResponseLineAssert;
import org.yu55.yagga.handler.generic.command.CommandOutput;
import org.yu55.yagga.handler.generic.command.CommandOutputLine;

public class GitAnnotateResponseFactoryTest {

@Test
public void testFactorizeAnnotateResponse2() throws Exception {
public void testFactorizeAnnotateResponse() throws Exception {
// given
CommandOutput annotateCommandOutput = new CommandOutput();
annotateCommandOutput.addOutputLine(new CommandOutputLine(
"af7545c8\t( Mikołaj Fejzer 2015-11-21 20:07:59 +0100 1)package org.yu55.yagga.handler.api.command.grep;"
"af7545c8\t( Mikołaj 2015-11-21 20:07:59 +0100 1)package org.yu55.yagga.handler.api.command.grep;"
));

// when
AnnotateResponse annotateResponse = factorizeAnnotateResponse(annotateCommandOutput);

// then
// TODO: change assertions to custom ones (generated)!
AnnotateResponseLine annotateResponseLine = annotateResponse.getAnnotationResponseLines().get(0);
assertThat(annotateResponseLine.getCommitId()).isEqualTo("af7545c8");
assertThat(annotateResponseLine.getAuthor()).isEqualTo("Mikołaj Fejzer");
assertThat(annotateResponseLine.getCommitDate()).isEqualTo("2015-11-21 20:07:59 +0100");
assertThat(annotateResponseLine.getLineNumber()).isEqualTo(1);
assertThat(annotateResponseLine.getLine()).isEqualTo("package org.yu55.yagga.handler.api.command.grep;");
AnnotateResponseLineAssert.assertThat(annotateResponse.getAnnotationResponseLines().get(0))
.hasCommitId("af7545c8")
.hasAuthor("Mikołaj")
.hasCommitDate("2015-11-21 20:07:59 +0100")
.hasLineNumber(1)
.hasLine("package org.yu55.yagga.handler.api.command.grep;");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.yu55.yagga.handler.git.command.grep;

import static org.yu55.yagga.testutils.assertion.CustomAssertions.assertThat;
import static org.yu55.yagga.utils.assertion.CustomAssertions.assertThat;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.yu55.yagga.handler.mercurial.MercurialRepositoryResolver.MERCURIAL_REPOSITORY_DISCRIMINATOR;
import static org.yu55.yagga.testutils.RepositoryFolderStub.stubMercurialRepository;
import static org.yu55.yagga.testutils.RepositoryFolderStub.stubUndefinedRepository;
import static org.yu55.yagga.utils.RepositoryFolderStub.stubMercurialRepository;
import static org.yu55.yagga.utils.RepositoryFolderStub.stubUndefinedRepository;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -16,7 +16,7 @@
import org.mockito.runners.MockitoJUnitRunner;
import org.yu55.yagga.handler.api.DvcsRepository;
import org.yu55.yagga.handler.mercurial.command.common.MercurialCommandExecutorFactory;
import org.yu55.yagga.testutils.RepositoryFolderStub;
import org.yu55.yagga.utils.RepositoryFolderStub;

@RunWith(MockitoJUnitRunner.class)
public class MercurialRepositoryResolverTest {
Expand Down
Loading

0 comments on commit 7e2fd9b

Please sign in to comment.