Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f1605440f20d8f9a2d262fba6635f9c9dff642f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.httpauth;

import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.op.CloneOperation;
import org.eclipse.egit.ui.common.EGitTestCase;
import org.eclipse.egit.ui.common.LoginDialogTester;
import org.eclipse.egit.ui.common.PushResultDialogTester;
import org.eclipse.egit.ui.common.PushWizardTester;
import org.eclipse.egit.ui.common.RefSpecPageTester;
import org.eclipse.egit.ui.common.RepoPropertiesPage;
import org.eclipse.egit.ui.test.TestUtil;
import org.eclipse.egit.ui.wizards.clone.SampleTestRepository;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class PushTest extends EGitTestCase {

	private static final int NUMBER_RANDOM_COMMITS = 5;

	private SampleTestRepository remoteRepository;

	private Repository localRepository;

	private File file;

	private File localRepoPath;

	@Before
	public void setup() throws Exception {
		TestUtil.disableProxy();
		remoteRepository = new SampleTestRepository(NUMBER_RANDOM_COMMITS, true);
		localRepoPath = new File(ResourcesPlugin.getWorkspace().getRoot()
				.getLocation().toFile(), "test" + System.nanoTime());
		String branch = Constants.R_HEADS + SampleTestRepository.FIX;
		CloneOperation cloneOperation = new CloneOperation(new URIish(
				remoteRepository.getUri()), true, null, localRepoPath, branch,
				"origin", 30);
		cloneOperation
				.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
						"agitter", "letmein"));
		cloneOperation.run(new NullProgressMonitor());
		file = new File(localRepoPath, SampleTestRepository.A_txt_name);
		assertTrue(file.exists());
		localRepository = Activator.getDefault().getRepositoryCache()
				.lookupRepository(new File(localRepoPath, ".git"));
		assertNotNull(localRepository);
	}

	@Test
	public void testPush() throws Exception {
		// change file
		TestUtil.appendFileContent(file, "additional content", true);
		// commit change
		try (Git git = new Git(localRepository)) {
			git.add().addFilepattern(SampleTestRepository.A_txt_name).call();
			git.commit().setMessage("Change").call();
		}
		configurePush();
		// push change
		PushWizardTester wizardTester = new PushWizardTester();
		RepoPropertiesPage repoPropertiesPage = wizardTester.openPushWizard(localRepository);
		repoPropertiesPage.setPushDestination("push");
		wizardTester.nextPage();
		// now login dialog appears
		LoginDialogTester loginDialogTester = new LoginDialogTester();
		loginDialogTester.login("agitter", "letmein");
		RefSpecPageTester refSpecPageTester = new RefSpecPageTester();
		refSpecPageTester.waitUntilPageIsReady(1);
		wizardTester.finish();
		loginDialogTester.login("agitter", "letmein");
		PushResultDialogTester pushResultDialogTester = new PushResultDialogTester();
		bot.waitUntil(shellIsActive("Push Results: push"));
		String expectedMessage = "Repository " + remoteRepository.getUri();
		pushResultDialogTester.assertResultMessage(expectedMessage);
		pushResultDialogTester.closeDialog();
	}

	private void configurePush() throws Exception {
		StoredConfig config = localRepository.getConfig();
		config.setString("remote", "push", "pushurl", remoteRepository.getUri());
		config.setString("remote", "push", "push", "+refs/heads/*:refs/heads/*");
		config.save();
	}

	@After
	public void tearDown() throws Exception {
		if (remoteRepository != null)
			remoteRepository.shutDown();
		Activator.getDefault().getRepositoryCache().clear();
		if (localRepository != null)
			localRepository.close();
		if (localRepoPath != null)
			FileUtils.delete(localRepoPath, FileUtils.RECURSIVE
					| FileUtils.RETRY);
	}

}

Back to the top