Skip to content

Commit

Permalink
Merge 49ab967 into 3583c70
Browse files Browse the repository at this point in the history
  • Loading branch information
psiniemi committed Aug 18, 2015
2 parents 3583c70 + 49ab967 commit 6e7aaae
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 85 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.eluder.coveralls.maven.plugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/*
* #[license]
* coveralls-maven-plugin
Expand Down Expand Up @@ -53,18 +61,9 @@
import org.eluder.coveralls.maven.plugin.service.Travis;
import org.eluder.coveralls.maven.plugin.source.SourceCallback;
import org.eluder.coveralls.maven.plugin.source.SourceLoader;
import org.eluder.coveralls.maven.plugin.source.UniqueSourceCallback;
import org.eluder.coveralls.maven.plugin.util.CoverageParsersFactory;
import org.eluder.coveralls.maven.plugin.util.SourceLoaderFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Mojo(name = "report", threadSafe = false, aggregator = true)
public class CoverallsReportMojo extends AbstractMojo {

Expand Down Expand Up @@ -334,7 +333,6 @@ protected SourceCallback createSourceCallbackChain(final JsonWriter writer, fina
chain = coverageTracingReporter;
reporters.add(coverageTracingReporter);
}
chain = new UniqueSourceCallback(chain);
return chain;
}

Expand Down
49 changes: 31 additions & 18 deletions src/main/java/org/eluder/coveralls/maven/plugin/domain/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,83 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Source implements JsonObject {

private static final Pattern NEWLINE = Pattern.compile("\r\n|\r|\n");
//private static final String CLASSIFIER_SEPARATOR = "#";

private final String name;
private final String source;
private final File source;
private final Charset sourceEncoding;
private final Integer[] coverage;
private String classifier;

public Source(final String name, final String source) {
public Source(final String name, final File source, final Charset sourceEncoding) throws IOException {
this.source = source;
this.sourceEncoding = sourceEncoding;
int lines = 1;
StringBuffer replaced = new StringBuffer(source.length());
Matcher matcher = NEWLINE.matcher(source);
String src = new String(Files.readAllBytes(source.toPath()));
Matcher matcher = NEWLINE.matcher(src);
while (matcher.find()) {
lines++;
matcher.appendReplacement(replaced, "\n");
}
matcher.appendTail(replaced);
this.source = replaced.toString();
this.coverage = new Integer[lines];
this.name = name;
}

@JsonIgnore
public String getName() {
return name;
}

@JsonProperty("name")
public String getFullName() {
return name;

// #45: cannot use identifier due to unfetchable source files
//return (classifier == null ? name : name + CLASSIFIER_SEPARATOR + classifier);
}

@JsonProperty("source")
public String getSource() {
return source;
public String getSource() throws IOException {
String src = new String(Files.readAllBytes(source.toPath()), sourceEncoding);
return src.replaceAll(NEWLINE.pattern(), "\n");
}

@JsonProperty("coverage")
public Integer[] getCoverage() {
return coverage;
}

@JsonIgnore
public String getClassifier() {
return classifier;
}

public void setClassifier(final String classifier) {
this.classifier = classifier;
}

public void addCoverage(final int lineNumber, final Integer coverage) {
int index = lineNumber - 1;
if (index >= this.coverage.length) {
throw new IllegalArgumentException("Line number " + lineNumber + " is greater than the source file " + name + " size");
}
this.coverage[lineNumber - 1] = coverage;
}

public void merge(final Source source) {
for (int i = 0; i < this.coverage.length && i < source.coverage.length; i++) {
if (this.coverage[i] == null && source.coverage[i] != null) {
this.coverage[i] = source.coverage[i];
} else if (this.coverage[i] != null && source.coverage[i] != null) {
this.coverage[i] += source.coverage[i];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

Expand All @@ -52,6 +54,7 @@ public class JsonWriter implements SourceCallback, Closeable {
private final Job job;
private final File coverallsFile;
private final JsonGenerator generator;
private final Map<String, Source> sources = new LinkedHashMap<String, Source>();

public JsonWriter(final Job job, final File coverallsFile) throws IOException {
File directory = coverallsFile.getParentFile();
Expand Down Expand Up @@ -91,6 +94,13 @@ public void writeStart() throws ProcessingException, IOException {
}

public void writeEnd() throws ProcessingException, IOException {
for (Source source : sources.values()) {
try {
generator.writeObject(source);
} catch (JsonProcessingException ex) {
throw new ProcessingException(ex);
}
}
try {
generator.writeEndArray();
generator.writeEndObject();
Expand All @@ -101,10 +111,10 @@ public void writeEnd() throws ProcessingException, IOException {

@Override
public void onSource(final Source source) throws ProcessingException, IOException {
try {
generator.writeObject(source);
} catch (JsonProcessingException ex) {
throw new ProcessingException(ex);
if (sources.get(source.getName()) != null) {
sources.get(source.getName()).merge(source);
} else {
sources.put(source.getName(), source);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
* %[license]
*/

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.Charset;

import org.codehaus.plexus.util.IOUtil;
import org.eluder.coveralls.maven.plugin.domain.Source;

public abstract class AbstractSourceLoader implements SourceLoader {
Expand All @@ -47,15 +45,9 @@ public AbstractSourceLoader(final URI base, final URI sourceBase, final String s

@Override
public Source load(final String sourceFile) throws IOException {
InputStream stream = locate(sourceFile);
if (stream != null) {
InputStreamReader reader = new InputStreamReader(stream, getSourceEncoding());
try {
String source = IOUtil.toString(reader);
return new Source(getFileName(sourceFile), source);
} finally {
IOUtil.close(reader);
}
File source = locate(sourceFile);
if (source != null) {
return new Source(getFileName(sourceFile), source, getSourceEncoding());
} else {
return null;
}
Expand All @@ -69,5 +61,5 @@ protected String getFileName(final String sourceFile) {
return directoryPrefix + sourceFile;
}

protected abstract InputStream locate(String sourceFile) throws IOException;
protected abstract File locate(String sourceFile) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.eluder.coveralls.maven.plugin.source;

/*
* #[license]
* coveralls-maven-plugin
Expand All @@ -26,11 +25,8 @@
* %[license]
*/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DirectorySourceLoader extends AbstractSourceLoader {

Expand All @@ -42,13 +38,13 @@ public DirectorySourceLoader(final File base, final File sourceDirectory, final
}

@Override
protected InputStream locate(final String sourceFile) throws IOException {
protected File locate(final String sourceFile) throws IOException {
File file = new File(sourceDirectory, sourceFile);
if (file.exists()) {
if (!file.isFile()) {
throw new IllegalArgumentException(file.getAbsolutePath() + " is not file");
}
return new BufferedInputStream(new FileInputStream(file));
return file.getAbsoluteFile();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.eluder.coveralls.maven.plugin.source;

/*
* #[license]
* coveralls-maven-plugin
Expand All @@ -26,11 +25,8 @@
* %[license]
*/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -50,14 +46,14 @@ public ScanSourceLoader(final File base, final File sourceDirectory, final Strin
}

@Override
protected InputStream locate(final String sourceFile) throws IOException {
protected File locate(final String sourceFile) throws IOException {
File file = new File(sourceDirectory, getFileName(sourceFile));

if (file.exists()) {
if (!file.isFile()) {
throw new IllegalArgumentException(file.getAbsolutePath() + " is not file");
}
return new BufferedInputStream(new FileInputStream(file));
return file.getAbsoluteFile();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.eluder.coveralls.maven.plugin.source;

import java.io.File;
import java.io.FileOutputStream;

/*
* #[license]
* coveralls-maven-plugin
Expand Down Expand Up @@ -28,25 +31,38 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.eluder.coveralls.maven.plugin.util.UrlUtils;

public class UrlSourceLoader extends AbstractSourceLoader {

private final URL sourceUrl;
private static final int BUFF_LEN = 4 * 1024;

public UrlSourceLoader(final URL base, final URL sourceUrl, final String sourceEncoding) {
super(UrlUtils.toUri(base), UrlUtils.toUri(sourceUrl), sourceEncoding);
this.sourceUrl = sourceUrl;
}

@Override
protected InputStream locate(final String sourceFile) throws IOException {
protected File locate(final String sourceFile) throws IOException {
File file = File.createTempFile(sourceFile, "tmpDownload");
file.deleteOnExit();
URL url = new URL(sourceUrl, sourceFile);
// Checkstyle OFF: EmptyBlock
try {
return url.openStream();
try (InputStream in = url.openStream();
OutputStream out = new FileOutputStream(file)) {
byte[] buffer = new byte[BUFF_LEN];
int read = 0;
while (-1 < (read = in.read(buffer))) {
if (read > 0) {
out.write(buffer, 0, read);
}
}
out.flush();
return file;
} catch (IOException ex) {
// not found from url
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eluder.coveralls.maven.plugin.domain.Git;
import org.eluder.coveralls.maven.plugin.domain.Job;
import org.eluder.coveralls.maven.plugin.domain.Source;
import org.eluder.coveralls.maven.plugin.domain.SourceTest;
import org.eluder.coveralls.maven.plugin.httpclient.CoverallsClient;
import org.eluder.coveralls.maven.plugin.json.JsonWriter;
import org.eluder.coveralls.maven.plugin.parser.CoberturaParser;
Expand All @@ -57,6 +58,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -113,7 +115,7 @@ public void init() throws Exception {
public Source answer(final InvocationOnMock invocation) throws Throwable {
String sourceFile = invocation.getArguments()[0].toString();
String content = readFileContent(sourceFile);
return new Source(sourceFile, content);
return new Source(sourceFile, SourceTest.createTempFile(content), StandardCharsets.UTF_8);
}
});
when(logMock.isInfoEnabled()).thenReturn(true);
Expand Down

0 comments on commit 6e7aaae

Please sign in to comment.