Github test:

* add ShareProject tests
* add CreateGits tests
* add RequestPaging test
This commit is contained in:
Aleksey Pivovarov
2013-07-02 19:03:27 +04:00
parent 7bd7d69f48
commit 77ff759261
11 changed files with 815 additions and 66 deletions
@@ -13,6 +13,8 @@
<orderEntry type="module" module-name="github" scope="TEST" />
<orderEntry type="module" module-name="dvcs" scope="TEST" />
<orderEntry type="module" module-name="jira-connector" scope="TEST" />
<orderEntry type="module" module-name="vcs-impl" scope="TEST" />
<orderEntry type="library" name="gson" level="project" />
</component>
</module>
@@ -0,0 +1,145 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed 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 org.jetbrains.plugins.github;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.vfs.VirtualFile;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.plugins.github.GithubCreateGistAction.NamedContent;
/**
* @author Aleksey Pivovarov
*/
public class GithubCreateGistContentTest extends GithubCreateGistTestBase {
@Override
public void setUp() throws Exception {
super.setUp();
createProjectFiles();
}
public void testCreateFromFile() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
expected.add(new NamedContent("file.txt", "file.txt content"));
VirtualFile file = myProjectRoot.findFileByRelativePath("file.txt");
assertNotNull(file);
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
checkEquals(expected, actual);
}
public void testCreateFromDirectory() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
expected.add(new NamedContent("folder_file1", "file1 content"));
expected.add(new NamedContent("folder_file2", "file2 content"));
expected.add(new NamedContent("folder_dir_file3", "file3 content"));
VirtualFile file = myProjectRoot.findFileByRelativePath("folder");
assertNotNull(file);
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
checkEquals(expected, actual);
}
public void testCreateFromEmptyDirectory() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_folder");
assertNotNull(file);
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
checkEquals(expected, actual);
}
public void testCreateFromEmptyFile() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_file");
assertNotNull(file);
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
checkEquals(expected, actual);
}
public void testCreateFromFiles() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
expected.add(new NamedContent("file.txt", "file.txt content"));
expected.add(new NamedContent("file2", "file2 content"));
expected.add(new NamedContent("file3", "file3 content"));
VirtualFile[] files = new VirtualFile[3];
files[0] = myProjectRoot.findFileByRelativePath("file.txt");
files[1] = myProjectRoot.findFileByRelativePath("folder/file2");
files[2] = myProjectRoot.findFileByRelativePath("folder/dir/file3");
assertNotNull(files[0]);
assertNotNull(files[1]);
assertNotNull(files[2]);
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, null, files);
checkEquals(expected, actual);
}
public void testCreateFromEmptyFiles() throws Throwable {
List<NamedContent> expected = new ArrayList<NamedContent>();
VirtualFile[] files = new VirtualFile[0];
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, null, null, files);
checkEquals(expected, actual);
}
public void testCreateFromEditor() throws Throwable {
VirtualFile file = myProjectRoot.findFileByRelativePath("file.txt");
assertNotNull(file);
Editor editor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, file, 0), false);
assertNotNull(editor);
((EditorImpl)editor).setCaretActive();
List<NamedContent> expected = new ArrayList<NamedContent>();
expected.add(new NamedContent("file.txt", "file.txt content"));
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, editor, file, null);
checkEquals(expected, actual);
}
public void testCreateFromEditorWithoutFile() throws Throwable {
VirtualFile file = myProjectRoot.findFileByRelativePath("file.txt");
assertNotNull(file);
Editor editor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, file, 0), false);
assertNotNull(editor);
((EditorImpl)editor).setCaretActive();
List<NamedContent> expected = new ArrayList<NamedContent>();
expected.add(new NamedContent("", "file.txt content"));
List<NamedContent> actual = GithubCreateGistAction.collectContents(myProject, editor, null, null);
checkEquals(expected, actual);
}
}
@@ -0,0 +1,132 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed 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 org.jetbrains.plugins.github;
import com.intellij.notification.NotificationType;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.plugins.github.GithubCreateGistAction.NamedContent;
/**
* @author Aleksey Pivovarov
*/
public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testSimple() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", null);
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
checkGistExists();
checkGistNotAnonymous();
checkGistPrivate();
checkGistDescription("description");
checkGistContent(expected);
}
public void testAnonymous() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, null, expected, true, "description", null);
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
checkGistExists();
checkGistAnonymous();
checkGistPrivate();
checkGistDescription("description");
checkGistContent(expected);
}
public void testUnusedFilenameField() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", "filename");
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
checkGistExists();
checkGistNotAnonymous();
checkGistPrivate();
checkGistDescription("description");
checkGistContent(expected);
}
public void testUsedFilenameField() throws Throwable {
List<NamedContent> content = Collections.singletonList(new NamedContent("file.txt", "file.txt content"));
List<NamedContent> expected = Collections.singletonList(new NamedContent("filename", "file.txt content"));
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), content, true, "description", "filename");
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
checkGistExists();
checkGistNotAnonymous();
checkGistPrivate();
checkGistDescription("description");
checkGistContent(expected);
}
public void testPublic() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, false, "description", null);
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
checkGistExists();
checkGistNotAnonymous();
checkGistPublic();
checkGistDescription("description");
checkGistContent(expected);
}
public void testEmpty() throws Throwable {
List<NamedContent> expected = Collections.emptyList();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", null);
assertNull("Gist was created", url);
checkNotification(NotificationType.WARNING, "Can't create Gist", "Can't create empty gist");
}
public void testWrongLogin() throws Throwable {
List<NamedContent> expected = createContent();
GithubAuthData auth = GithubUtil.getAuthData();
GithubAuthData myAuth = new GithubAuthData(auth.getHost(), auth.getLogin() + "some_suffix", auth.getPassword());
String url = GithubCreateGistAction.createGist(myProject, myAuth, expected, true, "description", null);
assertNull("Gist was created", url);
checkNotification(NotificationType.ERROR, "Can't create Gist", null);
}
public void testWrongPassword() throws Throwable {
List<NamedContent> expected = createContent();
GithubAuthData auth = GithubUtil.getAuthData();
GithubAuthData myAuth = new GithubAuthData(auth.getHost(), auth.getLogin(), auth.getPassword() + "some_suffix");
String url = GithubCreateGistAction.createGist(myProject, myAuth, expected, true, "description", null);
assertNull("Gist was created", url);
checkNotification(NotificationType.ERROR, "Can't create Gist", null);
}
}
@@ -0,0 +1,139 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed 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 org.jetbrains.plugins.github;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.github.test.GithubTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.plugins.github.GithubCreateGistAction.NamedContent;
/**
* @author Aleksey Pivovarov
*/
public abstract class GithubCreateGistTestBase extends GithubTest {
protected static String GIST_ID = null;
protected static JsonObject GIST = null;
@Override
public void setUp() throws Exception {
super.setUp();
}
@Override
public void tearDown() throws Exception {
deleteGist();
super.tearDown();
}
protected void deleteGist() {
if (GIST_ID != null) {
try {
GithubUtil.deleteGist(GithubUtil.getAuthData(), GIST_ID);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
GIST = null;
GIST_ID = null;
}
}
@NotNull
protected static List<NamedContent> createContent() {
List<NamedContent> content = new ArrayList<NamedContent>();
content.add(new NamedContent("file1", "file1 content"));
content.add(new NamedContent("file2", "file2 content"));
content.add(new NamedContent("dir_file3", "file3 content"));
return content;
}
@NotNull
protected static JsonObject getGist() {
assertNotNull(GIST_ID);
if (GIST == null) {
try {
GIST = GithubUtil.getGist(GithubUtil.getAuthData(), GIST_ID);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
assertNotNull("Gist does not exist", GIST);
return GIST;
}
protected static void checkGistExists() {
getGist();
}
protected static void checkGistPublic() {
JsonObject result = getGist();
assertTrue("Gist does not public", result.get("public").getAsBoolean());
}
protected static void checkGistPrivate() {
JsonObject result = getGist();
assertFalse("Gist does not private", result.get("public").getAsBoolean());
}
protected static void checkGistAnonymous() {
JsonObject result = getGist();
assertTrue("Gist does not anonymous", result.get("user").isJsonNull());
}
protected static void checkGistNotAnonymous() {
JsonObject result = getGist();
assertFalse("Gist does not anonymous", result.get("user").isJsonNull());
}
protected static void checkGistDescription(@NotNull String expected) {
JsonObject result = getGist();
assertEquals("Gist content differs from sample", expected, result.get("description").getAsString());
}
protected static void checkGistContent(@NotNull List<NamedContent> expected) {
JsonObject result = getGist();
JsonObject files = result.get("files").getAsJsonObject();
for (NamedContent file : expected) {
String content = files.get(file.getName()).getAsJsonObject().get("content").getAsString();
assertEquals("Gist content differs from sample", file.getText(), content);
}
}
protected static void checkEquals(@NotNull List<NamedContent> expected, @NotNull List<NamedContent> actual) {
for (NamedContent file : expected) {
assertTrue("Not found: <" + file.getName() + "> " + file.getText(), actual.contains(file));
}
assertEquals("Expected less elements", expected.size(), actual.size());
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed 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 org.jetbrains.plugins.github;
import org.jetbrains.plugins.github.test.GithubTest;
import java.util.ArrayList;
import java.util.List;
/**
* @author Aleksey Pivovarov
*/
public class GithubRequestPagingTest extends GithubTest {
public void testCase() throws Throwable {
List<RepositoryInfo> availableRepos = GithubUtil.getAvailableRepos(GithubUtil.getAuthData());
List<String> realData = new ArrayList<String>();
for (RepositoryInfo info : availableRepos) {
realData.add(info.getName());
}
List<String> expectedData = new ArrayList<String>();
for (int i = 1; i <= 250; i++) {
expectedData.add(String.valueOf(i));
}
assertContainsElements(realData, expectedData);
}
}
@@ -0,0 +1,89 @@
package org.jetbrains.plugins.github;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import git4idea.commands.Git;
import git4idea.test.GitExecutor;
/**
* @author Aleksey Pivovarov
*/
public class GithubShareProjectTest extends GithubShareProjectTestBase {
public void testSimple() throws Throwable {
registerDefaultShareDialogHandler();
registerDefaultUntrackedFilesDialogHandler();
createProjectFiles();
GithubShareAction.shareProjectOnGithub(myProject);
checkNotification(NotificationType.INFORMATION, "Successfully created project on GitHub", null);
checkGitExists();
checkGithubExists();
checkRemoteConfigured();
checkLastCommitPushed();
}
public void testGithubAlreadyExists() throws Throwable {
registerDefaultShareDialogHandler();
registerDefaultUntrackedFilesDialogHandler();
createProjectFiles();
GithubShareAction.shareProjectOnGithub(myProject);
GithubShareAction.shareProjectOnGithub(myProject);
checkNotification(NotificationType.INFORMATION, "Project is already on GitHub", null);
}
public void testExistingGit() throws Throwable {
registerDefaultShareDialogHandler();
registerDefaultUntrackedFilesDialogHandler();
createProjectFiles();
GitExecutor.cd(myProjectRoot.getPath());
GitExecutor.git(myProjectRoot.getPath(), "init");
GitExecutor.git(myProjectRoot.getPath(), "add file.txt");
GitExecutor.git(myProjectRoot.getPath(), "commit -m init");
GithubShareAction.shareProjectOnGithub(myProject);
checkNotification(NotificationType.INFORMATION, "Successfully created project on GitHub", null);
checkGitExists();
checkGithubExists();
checkRemoteConfigured();
checkLastCommitPushed();
}
public void testExistingFreshGit() throws Throwable {
registerDefaultShareDialogHandler();
registerDefaultUntrackedFilesDialogHandler();
createProjectFiles();
Git git = ServiceManager.getService(Git.class);
git.init(myProject, myProjectRoot);
GithubShareAction.shareProjectOnGithub(myProject);
checkNotification(NotificationType.INFORMATION, "Successfully created project on GitHub", null);
checkGitExists();
checkGithubExists();
checkRemoteConfigured();
checkLastCommitPushed();
}
public void testEmptyProject() throws Throwable {
registerDefaultUntrackedFilesDialogHandler();
registerDefaultShareDialogHandler();
GithubShareAction.shareProjectOnGithub(myProject);
checkNotification(NotificationType.WARNING, "Failed to commit file during post activities", "No files to commit");
checkGitExists();
checkGithubExists();
checkRemoteConfigured();
}
}
@@ -0,0 +1,111 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed 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 org.jetbrains.plugins.github;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.ui.DialogWrapper;
import git4idea.GitUtil;
import git4idea.Notificator;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import git4idea.test.GitExecutor;
import git4idea.test.TestDialogHandler;
import git4idea.test.TestNotificator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.test.GithubTest;
import org.jetbrains.plugins.github.ui.GithubShareDialog;
import java.io.IOException;
/**
* @author Aleksey Pivovarov
*/
public abstract class GithubShareProjectTestBase extends GithubTest {
protected static final String PROJECT_NAME = "new_project_from_test";
@Override
public void setUp() throws Exception {
super.setUp();
registerHttpAuthService();
}
@Override
public void tearDown() throws Exception {
deleteGithubRepo();
super.tearDown();
}
protected void deleteGithubRepo() {
try {
GithubUtil.deleteGithubRepository(GithubUtil.getAuthData(), PROJECT_NAME);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
protected void registerDefaultShareDialogHandler() {
myDialogManager.registerDialogHandler(GithubShareDialog.class, new TestDialogHandler<GithubShareDialog>() {
@Override
public int handleDialog(GithubShareDialog dialog) {
dialog.setRepositoryName(PROJECT_NAME);
return DialogWrapper.OK_EXIT_CODE;
}
});
}
protected void registerDefaultUntrackedFilesDialogHandler() {
myDialogManager.registerDialogHandler(GithubShareAction.GithubUntrackedFilesDialog.class,
new TestDialogHandler<GithubShareAction.GithubUntrackedFilesDialog>() {
@Override
public int handleDialog(GithubShareAction.GithubUntrackedFilesDialog dialog) {
return DialogWrapper.OK_EXIT_CODE;
}
});
}
protected void checkGithubExists() {
RepositoryInfo githubInfo = GithubUtil.getDetailedRepoInfo(GithubUtil.getAuthData(), GithubUtil.getAuthData().getLogin(), PROJECT_NAME);
assertNotNull("Github repository does not exist", githubInfo);
}
protected void checkGitExists() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
assertNotNull("Git repository does not exist", gitRepository);
}
protected void checkRemoteConfigured() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
assertNotNull(gitRepository);
assertNotNull("Github remote does not configured", GithubUtil.findGithubRemoteUrl(gitRepository));
}
protected void checkLastCommitPushed() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
assertNotNull(gitRepository);
String hash = GitExecutor.git(gitRepository, "log -1 --pretty=%h");
String ans = GitExecutor.git(gitRepository, "branch --contains " + hash + " -a");
assertTrue(ans.contains("remotes/origin/master"));
}
}
@@ -1,57 +0,0 @@
package org.jetbrains.plugins.github;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.testFramework.TestActionEvent;
import git4idea.test.TestDialogHandler;
import git4idea.test.TestNotificator;
import org.jetbrains.plugins.github.test.GithubTest;
import org.jetbrains.plugins.github.ui.GithubShareDialog;
import static git4idea.test.GitTestUtil.assertNotification;
/**
* @author Kirill Likhodedov
*/
public class GithubShareTest extends GithubTest {
private static final String PROJECT_NAME = "new_project_from_test";
@Override
public void setUp() throws Exception {
super.setUp();
}
@Override
public void tearDown() throws Exception {
removeCreatedProject();
super.tearDown();
}
private void removeCreatedProject() {
// TODO: send a request to GitHub to remove the created project
}
public void testSimpleCase() throws Throwable {
myDialogManager.registerDialogHandler(GithubShareDialog.class, new TestDialogHandler<GithubShareDialog>() {
@Override
public int handleDialog(GithubShareDialog dialog) {
dialog.setRepositoryName(PROJECT_NAME);
return DialogWrapper.OK_EXIT_CODE;
}
});
// TODO: refactor the production code to avoid programmatically invoking the action with a fake data context
AnAction shareAction = ActionManager.getInstance().getAction("Github.Share");
shareAction.actionPerformed(new TestActionEvent(shareAction));
assertNotification(myProject, new Notification(TestNotificator.TEST_NOTIFICATION_GROUP, "Success",
"Successfully created project '" + PROJECT_NAME + "' on GitHub",
NotificationType.INFORMATION));
// TODO check that the project was created on github, and the repository was created locally and pushed there
}
}
@@ -15,8 +15,13 @@
*/
package org.jetbrains.plugins.github.test;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.UsefulTestCase;
@@ -24,14 +29,20 @@ import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
import git4idea.DialogManager;
import git4idea.Notificator;
import git4idea.commands.GitHttpAuthService;
import git4idea.commands.GitHttpAuthenticator;
import git4idea.config.GitVcsSettings;
import git4idea.remote.GitHttpAuthTestService;
import git4idea.test.GitExecutor;
import git4idea.test.GitTestUtil;
import git4idea.test.TestDialogManager;
import git4idea.test.TestNotificator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.GithubSettings;
import java.io.IOException;
import static org.junit.Assume.assumeNotNull;
/**
@@ -51,8 +62,6 @@ import static org.junit.Assume.assumeNotNull;
*/
public abstract class GithubTest extends UsefulTestCase {
@NotNull protected static final String HOST = "github.com";
@NotNull protected Project myProject;
@NotNull protected VirtualFile myProjectRoot;
@NotNull protected GitVcsSettings myGitSettings;
@@ -69,6 +78,121 @@ public abstract class GithubTest extends UsefulTestCase {
GitTestUtil.setDefaultBuiltInServerPort();
}
protected void createProjectFiles() {
createFile("file.txt");
createFile("file");
createFile("folder/file1");
createFile("folder/file2");
createFile("folder/empty_file", "");
createFile("folder/empty_folder/");
createFile("folder/dir/file3");
}
protected void createFile(@NotNull String path) {
createFile(path, path.substring(path.lastIndexOf('/') + 1) + " content");
}
protected void createFile(@NotNull String path, @NotNull String content) {
String[] pathElements = path.split("/");
boolean lastIsDir = path.endsWith("/");
VirtualFile currentParent = myProjectRoot;
for (int i = 0; i < pathElements.length - 1; i++) {
currentParent = createDir(myProject, currentParent, pathElements[i]);
}
String lastElement = pathElements[pathElements.length - 1];
if (lastIsDir) {
createDir(myProject, currentParent, lastElement);
}
else {
createFile(myProject, currentParent, lastElement, content);
}
}
protected static VirtualFile createFile(@NotNull Project project,
@NotNull final VirtualFile parent,
@NotNull final String name,
@Nullable final String content) {
final Ref<VirtualFile> result = new Ref<VirtualFile>();
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
try {
VirtualFile file = parent.createChildData(this, name);
if (content != null) {
file.setBinaryContent(CharsetToolkit.getUtf8Bytes(content));
}
result.set(file);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}.execute();
return result.get();
}
protected static VirtualFile createDir(@NotNull Project project, @NotNull final VirtualFile parent, @NotNull final String name) {
final Ref<VirtualFile> result = new Ref<VirtualFile>();
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
try {
VirtualFile dir = parent.findChild(name);
if (dir == null) {
dir = parent.createChildDirectory(this, name);
}
result.set(dir);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}.execute();
return result.get();
}
protected void checkNotification(@NotNull NotificationType type, @Nullable String title, @Nullable String content) {
Notification actualNotification = ((TestNotificator)ServiceManager.getService(myProject, Notificator.class)).getLastNotification();
assertNotNull("No notification was shown", actualNotification);
System.out.println("Notification title: " + actualNotification.getTitle());
System.out.println("Notification content: " + actualNotification.getContent());
assertEquals("Notification has wrong type", type, actualNotification.getType());
if (title != null) {
assertEquals("Notification has wrong title", title, actualNotification.getTitle());
}
if (content != null) {
assertEquals("Notification has wrong content", content, actualNotification.getContent());
}
}
protected void registerHttpAuthService() {
GitHttpAuthTestService myHttpAuthService = (GitHttpAuthTestService)ServiceManager.getService(GitHttpAuthService.class);
myHttpAuthService.register(new GitHttpAuthenticator() {
@NotNull
@Override
public String askPassword(@NotNull String url) {
return myGitHubSettings.getPassword();
}
@NotNull
@Override
public String askUsername(@NotNull String url) {
return myGitHubSettings.getLogin();
}
@Override
public void saveAuthData() {
}
@Override
public void forgetPassword() {
}
});
}
@Override
protected void setUp() throws Exception {
super.setUp();
@@ -78,10 +202,12 @@ public abstract class GithubTest extends UsefulTestCase {
myProject = myProjectFixture.getProject();
myProjectRoot = myProject.getBaseDir();
String login = System.getProperty("test.github.login");
String password = System.getProperty("test.github.password");
final String host = System.getProperty("test.github.host");
final String login = System.getProperty("test.github.login");
final String password = System.getProperty("test.github.password");
// TODO change to assert when a stable Github testing server is ready
assumeNotNull(host);
assumeNotNull(login);
assumeNotNull(password);
@@ -89,7 +215,7 @@ public abstract class GithubTest extends UsefulTestCase {
myGitSettings.getAppSettings().setPathToGit(GitExecutor.GIT_EXECUTABLE);
myGitHubSettings = GithubSettings.getInstance();
myGitHubSettings.setHost(HOST);
myGitHubSettings.setHost(host);
myGitHubSettings.setLogin(login);
myGitHubSettings.setPassword(password);