From c404b1ec107f7d59b0399074d243e19a252f36fa Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 21 Feb 2011 21:13:57 +0100 Subject: [PATCH] added repository client api for tests --- scm-test/pom.xml | 12 + .../client/GitRepositoryClient.java | 294 ++++++++++++++++++ .../repository/client/RepositoryClient.java | 92 ++++++ .../client/RepositoryClientException.java | 87 ++++++ .../client/RepositoryClientFactory.java | 102 ++++++ 5 files changed, 587 insertions(+) create mode 100644 scm-test/src/main/java/sonia/scm/repository/client/GitRepositoryClient.java create mode 100644 scm-test/src/main/java/sonia/scm/repository/client/RepositoryClient.java create mode 100644 scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientException.java create mode 100644 scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientFactory.java diff --git a/scm-test/pom.xml b/scm-test/pom.xml index 12e562c1a2..8890c0c252 100644 --- a/scm-test/pom.xml +++ b/scm-test/pom.xml @@ -47,6 +47,18 @@ ${slf4j.version} + + org.eclipse.jgit + org.eclipse.jgit + 0.10.1 + + + + com.jcraft + jsch + 0.1.42 + + diff --git a/scm-test/src/main/java/sonia/scm/repository/client/GitRepositoryClient.java b/scm-test/src/main/java/sonia/scm/repository/client/GitRepositoryClient.java new file mode 100644 index 0000000000..66e0b2d851 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/repository/client/GitRepositoryClient.java @@ -0,0 +1,294 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.client; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.GitCommand; +import org.eclipse.jgit.api.RmCommand; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.TextProgressMonitor; +import org.eclipse.jgit.storage.file.FileBasedConfig; +import org.eclipse.jgit.storage.file.FileRepository; +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.RemoteRefUpdate; +import org.eclipse.jgit.transport.Transport; +import org.eclipse.jgit.transport.URIish; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.IOException; + +import java.net.URISyntaxException; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class GitRepositoryClient implements RepositoryClient +{ + + /** + * Constructs ... + * + * + * @param localRepository + * @param remoteRepository + * @param username + * @param password + * + * + * @throws IOException + * @throws URISyntaxException + */ + GitRepositoryClient(File localRepository, String remoteRepository, + String username, String password) + throws URISyntaxException, IOException + { + uri = new URIish(remoteRepository); + + if ((username != null) && (password != null)) + { + uri.setUser(username); + uri.setPass(password); + credentialsProvider = new UsernamePasswordCredentialsProvider(username, + password); + } + + this.repository = new FileRepository(new File(localRepository, + Constants.DOT_GIT)); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param file + * @param others + * + * @throws RepositoryClientException + */ + @Override + public void add(String file, String... others) + throws RepositoryClientException + { + AddCommand cmd = new Git(repository).add(); + + cmd.addFilepattern(file); + + if (others != null) + { + for (String f : others) + { + cmd.addFilepattern(f); + } + } + + callCommand(cmd); + } + + /** + * Method description + * + * + * @param message + * + * @throws RepositoryClientException + */ + @Override + public void commit(String message) throws RepositoryClientException + { + List refSpecs = new ArrayList(); + + refSpecs.add(Transport.REFSPEC_PUSH_ALL); + + try + { + CommitCommand cmd = new Git(repository).commit(); + + cmd.setMessage(message); + callCommand(cmd); + + List transports = Transport.openAll(repository, remoteConfig, + Transport.Operation.PUSH); + + for (Transport transport : transports) + { + if (credentialsProvider != null) + { + transport.setCredentialsProvider(credentialsProvider); + } + + transport.setPushThin(Transport.DEFAULT_PUSH_THIN); + + Collection toPush = + transport.findRemoteRefUpdatesFor(refSpecs); + + try + { + transport.push(new TextProgressMonitor(), toPush); + } + finally + { + transport.close(); + } + } + } + catch (Exception ex) + { + throw new RepositoryClientException(ex); + } + } + + /** + * Method description + * + * + * @param file + * @param others + * + * @throws RepositoryClientException + */ + @Override + public void delete(String file, String... others) + throws RepositoryClientException + { + RmCommand cmd = new Git(repository).rm(); + + cmd = cmd.addFilepattern(file); + + if (others != null) + { + for (String f : others) + { + cmd = cmd.addFilepattern(f); + } + } + + callCommand(cmd); + } + + /** + * Method description + * + * + * @throws RepositoryClientException + */ + @Override + public void init() throws RepositoryClientException + { + try + { + repository.create(false); + + FileBasedConfig fc = repository.getConfig(); + + remoteConfig = new RemoteConfig(fc, Constants.HEAD); + remoteConfig.addURI(uri); + remoteConfig.addFetchRefSpec( + new RefSpec().setForceUpdate(true).setSourceDestination( + Constants.R_HEADS + "*", + Constants.R_REMOTES + Constants.HEAD + "/*")); + remoteConfig.update(fc); + fc.save(); + } + catch (Exception ex) + { + throw new RepositoryClientException(ex); + } + } + + /** + * Method description + * + * + * @throws RepositoryClientException + */ + @Override + public void update() throws RepositoryClientException + { + + // todo pull + } + + /** + * Method description + * + * + * @param cmd + * + * @throws RepositoryClientException + */ + private void callCommand(GitCommand cmd) throws RepositoryClientException + { + try + { + cmd.call(); + } + catch (Exception ex) + { + throw new RepositoryClientException(ex); + } + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private CredentialsProvider credentialsProvider; + + /** Field description */ + private RemoteConfig remoteConfig; + + /** Field description */ + private FileRepository repository; + + /** Field description */ + private URIish uri; + + /** Field description */ + private String username; +} diff --git a/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClient.java b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClient.java new file mode 100644 index 0000000000..9fa5552e68 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClient.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.client; + +/** + * + * @author Sebastian Sdorra + */ +public interface RepositoryClient +{ + + /** + * Method description + * + * + * @param file + * @param others + * + * @throws RepositoryClientException + */ + public void add(String file, String... others) + throws RepositoryClientException; + + /** + * Method description + * + * + * @param message + * + * @throws RepositoryClientException + */ + public void commit(String message) throws RepositoryClientException; + + /** + * Method description + * + * + * @param file + * @param others + * + * @throws RepositoryClientException + */ + public void delete(String file, String... others) + throws RepositoryClientException; + + /** + * Method description + * + * + * @throws RepositoryClientException + */ + public void init() throws RepositoryClientException; + + /** + * Method description + * + * + * @throws RepositoryClientException + */ + public void update() throws RepositoryClientException; +} diff --git a/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientException.java b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientException.java new file mode 100644 index 0000000000..c812b737db --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientException.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.client; + +/** + * + * @author Sebastian Sdorra + */ +public class RepositoryClientException extends Exception +{ + + /** Field description */ + private static final long serialVersionUID = -6017272751596608450L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public RepositoryClientException() {} + + /** + * Constructs ... + * + * + * @param message + */ + public RepositoryClientException(String message) + { + super(message); + } + + /** + * Constructs ... + * + * + * @param throwable + */ + public RepositoryClientException(Throwable throwable) + { + super(throwable); + } + + /** + * Constructs ... + * + * + * @param message + * @param throwable + */ + public RepositoryClientException(String message, Throwable throwable) + { + super(message, throwable); + } +} diff --git a/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientFactory.java b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientFactory.java new file mode 100644 index 0000000000..c75910fbd1 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/repository/client/RepositoryClientFactory.java @@ -0,0 +1,102 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.client; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +/** + * + * @author Sebastian Sdorra + */ +public class RepositoryClientFactory +{ + + /** + * Method description + * + * + * @param type + * @param localRepository + * @param remoteRepository + * + * @return + * + * @throws RepositoryClientException + */ + public static RepositoryClient createClient(String type, + File localRepository, String remoteRepository) + throws RepositoryClientException + { + return createClient(type, localRepository, remoteRepository, null, null); + } + + /** + * Method description + * + * + * @param type + * @param localRepository + * @param remoteRepository + * @param username + * @param password + * + * @return + * + * @throws RepositoryClientException + */ + public static RepositoryClient createClient(String type, + File localRepository, String remoteRepository, String username, + String password) + throws RepositoryClientException + { + RepositoryClient client = null; + + try + { + if ("git".equals(type)) + { + client = new GitRepositoryClient(localRepository, remoteRepository, + username, password); + } + } + catch (Exception ex) + { + throw new RepositoryClientException(ex); + } + + return client; + } +}