Skip to content
This repository has been archived by the owner on Sep 20, 2020. It is now read-only.

Commit

Permalink
Issue 20: Factor out ClientBuilder from IndexBuilder
Browse files Browse the repository at this point in the history
It holds the basic utility methods and log management. Add test for
logging.

Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
  • Loading branch information
jonas committed Sep 11, 2009
1 parent f6de036 commit 713ba4b
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/org/nbgit/OutputLogger.java
Expand Up @@ -78,7 +78,7 @@ private OutputLogger(String repositoryRoot) {
log = IOProvider.getDefault().getIO(repositoryRootString, false);
}

private OutputLogger() {
protected OutputLogger() {
}

public void closeLog() {
Expand Down
76 changes: 76 additions & 0 deletions src/org/nbgit/client/ClientBuilder.java
@@ -0,0 +1,76 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file.
*
* This particular file is subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.nbgit.client;

import java.io.File;
import org.nbgit.Git;
import org.nbgit.OutputLogger;
import org.spearce.jgit.lib.Repository;

public class ClientBuilder {

protected final Repository repository;
protected OutputLogger logger;
private int loggedLines;

protected ClientBuilder(Repository repository) {
this.repository = repository;
}

protected <T extends ClientBuilder> T log(Class<T> clazz, OutputLogger logger) {
this.logger = logger;
return (T) this;
}

protected void log(String format, Object... args) {
if (logger != null && loggedLines < OutputLogger.MAX_LINES_TO_PRINT) {
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof File)
args[i] = toPath((File) args[i]);
}
logger.output(String.format(format, args));
loggedLines++;
}
}

protected String toPath(File file) {
return Repository.stripWorkDir(repository.getWorkDir(), file);
}

protected static Repository toRepository(File workDir) {
return Git.getInstance().getRepository(workDir);
}
}
35 changes: 7 additions & 28 deletions src/org/nbgit/client/IndexBuilder.java
Expand Up @@ -39,35 +39,31 @@
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import org.nbgit.Git;
import org.nbgit.OutputLogger;
import org.spearce.jgit.lib.GitIndex;
import org.spearce.jgit.lib.Repository;

/**
* Wrapper for JGit's index API.
*/
public class IndexBuilder {
public class IndexBuilder extends ClientBuilder {

private static String ADDING = "A"; // NOI18N
private static String DELETING = "D"; // NOI18N
private static String MOVING = "R"; // NOI18N
private final Repository repository;
private static String ADDING = "A %s"; // NOI18N
private static String DELETING = "D %s"; // NOI18N
private static String MOVING = "R %s -> %s"; // NOI18N
private final HashSet<File> delete = new HashSet<File>();
private final HashSet<File> add = new HashSet<File>();
private OutputLogger logger;

private IndexBuilder(Repository repository) {
this.repository = repository;
super(repository);
}

public static IndexBuilder create(Repository repository) throws IOException {
return new IndexBuilder(repository);
}

public static IndexBuilder create(File workDir) throws IOException {
Repository repository = Git.getInstance().getRepository(workDir);
return create(repository);
return create(toRepository(workDir));
}

public IndexBuilder add(File file) {
Expand Down Expand Up @@ -110,27 +106,10 @@ public void write() throws IOException {
for (File file : delete)
index.remove(repository.getWorkDir(), file);
index.write();
add.clear();
delete.clear();
}

public IndexBuilder log(OutputLogger logger) {
this.logger = logger;
return this;
}

private void log(String string, File... files) {
if (logger != null && files.length > 0 &&
(add.size() + delete.size()) < OutputLogger.MAX_LINES_TO_PRINT) {
string += " " + toPath(files[0]); //NOI18N
if (files.length > 1)
string += " -> " + toPath(files[1]); //NOI18N
logger.output(string);
}
}

private String toPath(File file) {
return Repository.stripWorkDir(repository.getWorkDir(), file);
return log(IndexBuilder.class, logger);
}

}
67 changes: 67 additions & 0 deletions test/unit/src/org/nbgit/client/ClientBuilderTest.java
@@ -0,0 +1,67 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file.
*
* This particular file is subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.nbgit.client;

import org.nbgit.OutputLogger;
import org.nbgit.junit.RepositoryTestCase;

public class ClientBuilderTest extends RepositoryTestCase {

public ClientBuilderTest(String name) {
super(name);
}

public void testLogLimit() throws Exception {
Builder builder = new Builder().log(logger);

for (int i = 0; i < OutputLogger.MAX_LINES_TO_PRINT + 10; i++)
builder.log("line %d", i);

assertEquals(OutputLogger.MAX_LINES_TO_PRINT, loggerMessages.size());
}

private class Builder extends ClientBuilder {

Builder() {
super(ClientBuilderTest.this.repository);
}

Builder log(OutputLogger logger) {
return log(Builder.class, logger);
}

}
}
21 changes: 18 additions & 3 deletions test/unit/src/org/nbgit/client/IndexBuilderTest.java
Expand Up @@ -35,7 +35,6 @@
*/
package org.nbgit.client;

import java.io.IOException;
import org.nbgit.junit.RepositoryTestCase;
import org.spearce.jgit.dircache.DirCache;
import org.spearce.jgit.dircache.DirCacheEntry;
Expand Down Expand Up @@ -107,7 +106,23 @@ public void testMove() throws Exception {
compareIndexFiles();
}

private void compareIndexFiles() throws IOException {
public void testLog() throws Exception {
String[] expectedMessages = {
"A add",
"D delete",
"R from -> to"
};
IndexBuilder.create(repository).
log(logger).
add(toWorkDirFile("add")).
delete(toWorkDirFile("delete")).
move(toWorkDirFile("from"), toWorkDirFile("to"));
assertEquals(expectedMessages.length, loggerMessages.size());
for (int i = 0; i < expectedMessages.length; i++)
assertEquals(expectedMessages[i], loggerMessages.get(i));
}

private void compareIndexFiles() throws Exception {
refDirCache(DirCache.read(repository));
compareReferenceFiles();
}
Expand All @@ -126,4 +141,4 @@ private void refDirCacheEntry(DirCacheEntry entry) {
ref(builder.toString());
}

}
}
85 changes: 85 additions & 0 deletions test/unit/src/org/nbgit/junit/MockOutputLogger.java
@@ -0,0 +1,85 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file.
*
* This particular file is subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.nbgit.junit;

import java.util.List;
import org.nbgit.OutputLogger;

public class MockOutputLogger extends OutputLogger {

private final List<String> logMessages;

public static MockOutputLogger create(List<String> logMessages) {
return new MockOutputLogger(logMessages);
}

private MockOutputLogger(List<String> logMessages) {
this.logMessages = logMessages;
}

@Override
public void closeLog() {
}

@Override
public void flushLog() {
}

@Override
public void output(List<String> list) {
logMessages.addAll(list);
}

@Override
public void output(String msg) {
logMessages.add(msg);
}

@Override
public void outputInRed(String msg) {
logMessages.add(msg);
}

@Override
public void outputLink(final String sURL) {
logMessages.add(sURL);
}

@Override
public void clearOutput() {
logMessages.clear();
}
}
5 changes: 5 additions & 0 deletions test/unit/src/org/nbgit/junit/RepositoryTestCase.java
Expand Up @@ -42,6 +42,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import org.nbgit.OutputLogger;
import org.netbeans.junit.NbTestCase;
import org.spearce.jgit.lib.FileBasedConfig;
import org.spearce.jgit.lib.Repository;
Expand All @@ -54,6 +55,8 @@ public class RepositoryTestCase extends NbTestCase {

private final File dataRoot = new File(getDataDir(), getClass().getCanonicalName());
protected Repository repository;
protected OutputLogger logger;
protected ArrayList<String> loggerMessages;
protected File dataDir;
protected File gitDir;
protected File workDir;
Expand All @@ -72,6 +75,8 @@ protected void setUp() throws Exception {
gitDir = new File(workDir, ".git");
repository = new Repository(gitDir);
repository.create();
loggerMessages = new ArrayList<String>();
logger = MockOutputLogger.create(loggerMessages);

copyRepositoryFiles("default", repository);
}
Expand Down

0 comments on commit 713ba4b

Please sign in to comment.