Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package plumber.core.git.test; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.MergeResult; import org.eclipse.jgit.api.PullResult; import org.eclipse.jgit.api.ResetCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Test; import plumber.core.config.PlumberConfig; import plumber.core.git.GitWorker; import java.io.File; import java.io.IOException; import java.util.Iterator; import static org.junit.Assert.*; import static plumber.core.config.PlumberConfigEntry.*; public class BasicGitTest { @Test public void testGitInit() throws IOException, GitAPIException { String targetPath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile() + "/.."; System.setProperty(PLUMBER_GIT_URL.configKey(), "git@github.com:github/testrepo.git"); System.setProperty(PLUMBER_GIT_PATH.configKey(), targetPath + "/junit-git-iaac"); PlumberConfig.reload(); System.clearProperty(PLUMBER_GIT_URL.configKey()); System.clearProperty(PLUMBER_GIT_PATH.configKey()); File gitFolder = new File(PlumberConfig.get(PLUMBER_GIT_PATH)); if (gitFolder.exists() && gitFolder.isDirectory()) { FileUtils.deleteDirectory(gitFolder); } assertTrue(gitFolder.mkdirs()); GitWorker gitWorker = new GitWorker(PlumberConfig.get(PLUMBER_GIT_URL), PlumberConfig.get(PLUMBER_GIT_PATH)); gitWorker.init(); gitWorker = new GitWorker(PlumberConfig.get(PLUMBER_GIT_URL), PlumberConfig.get(PLUMBER_GIT_PATH)); gitWorker.init(); Git git = gitWorker.getGit(); assertNotNull(git); Iterator<RevCommit> log = git.log().call().iterator(); assertTrue(log.hasNext()); String latestRef = log.next().getName(); assertTrue(log.hasNext()); String previousRef = log.next().getName(); assertNotEquals(latestRef, previousRef); ResetCommand resetCommand = git.reset(); resetCommand.setMode(ResetCommand.ResetType.HARD); resetCommand.setRef("HEAD~1"); resetCommand.call(); assertTrue(git.log().call().iterator().hasNext()); log = git.log().call().iterator(); assertTrue(log.hasNext()); String latestRefAfterRebase = log.next().getName(); assertEquals(previousRef, latestRefAfterRebase); PullResult pullResult = gitWorker.pull(); assertTrue(pullResult.isSuccessful()); assertEquals(MergeResult.MergeStatus.FAST_FORWARD, pullResult.getMergeResult().getMergeStatus()); log = git.log().call().iterator(); assertTrue(log.hasNext()); String latestRefAfterRePull = log.next().getName(); assertEquals(latestRef, latestRefAfterRePull); if (gitFolder.exists() && gitFolder.isDirectory()) { FileUtils.deleteDirectory(gitFolder); } } }