From 77ff7592617589d9df5479f9be92cad859494f0c Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 1 Jul 2013 18:05:50 +0400 Subject: [PATCH] Github test: * add ShareProject tests * add CreateGits tests * add RequestPaging test --- plugins/github/github-test/github-test.iml | 2 + .../github/GithubCreateGistContentTest.java | 145 ++++++++++++++++++ .../plugins/github/GithubCreateGistTest.java | 132 ++++++++++++++++ .../github/GithubCreateGistTestBase.java | 139 +++++++++++++++++ .../github/GithubRequestPagingTest.java | 42 +++++ .../github/GithubShareProjectTest.java | 89 +++++++++++ .../github/GithubShareProjectTestBase.java | 111 ++++++++++++++ .../plugins/github/GithubShareTest.java | 57 ------- .../plugins/github/test/GithubTest.java | 136 +++++++++++++++- .../github/GithubCreateGistAction.java | 8 +- .../jetbrains/plugins/github/GithubUtil.java | 20 +++ 11 files changed, 815 insertions(+), 66 deletions(-) create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistContentTest.java create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTest.java create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTestBase.java create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubRequestPagingTest.java create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTest.java create mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTestBase.java delete mode 100644 plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareTest.java diff --git a/plugins/github/github-test/github-test.iml b/plugins/github/github-test/github-test.iml index 2924f3eb6f79..123f5e6ac06a 100644 --- a/plugins/github/github-test/github-test.iml +++ b/plugins/github/github-test/github-test.iml @@ -13,6 +13,8 @@ + + diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistContentTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistContentTest.java new file mode 100644 index 000000000000..94667d239345 --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistContentTest.java @@ -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 expected = new ArrayList(); + expected.add(new NamedContent("file.txt", "file.txt content")); + + VirtualFile file = myProjectRoot.findFileByRelativePath("file.txt"); + assertNotNull(file); + + List actual = GithubCreateGistAction.collectContents(myProject, null, file, null); + + checkEquals(expected, actual); + } + + public void testCreateFromDirectory() throws Throwable { + List expected = new ArrayList(); + 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 actual = GithubCreateGistAction.collectContents(myProject, null, file, null); + + checkEquals(expected, actual); + } + + public void testCreateFromEmptyDirectory() throws Throwable { + List expected = new ArrayList(); + + VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_folder"); + assertNotNull(file); + + List actual = GithubCreateGistAction.collectContents(myProject, null, file, null); + + checkEquals(expected, actual); + } + + public void testCreateFromEmptyFile() throws Throwable { + List expected = new ArrayList(); + + VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_file"); + assertNotNull(file); + + List actual = GithubCreateGistAction.collectContents(myProject, null, file, null); + + checkEquals(expected, actual); + } + + public void testCreateFromFiles() throws Throwable { + List expected = new ArrayList(); + 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 actual = GithubCreateGistAction.collectContents(myProject, null, null, files); + + checkEquals(expected, actual); + } + + public void testCreateFromEmptyFiles() throws Throwable { + List expected = new ArrayList(); + + VirtualFile[] files = new VirtualFile[0]; + + List 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 expected = new ArrayList(); + expected.add(new NamedContent("file.txt", "file.txt content")); + + List 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 expected = new ArrayList(); + expected.add(new NamedContent("", "file.txt content")); + + List actual = GithubCreateGistAction.collectContents(myProject, editor, null, null); + + checkEquals(expected, actual); + } +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTest.java new file mode 100644 index 000000000000..bb12e9b510f7 --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTest.java @@ -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 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 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 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 content = Collections.singletonList(new NamedContent("file.txt", "file.txt content")); + List 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 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 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 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 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); + } + + +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTestBase.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTestBase.java new file mode 100644 index 000000000000..3439bd9cef9e --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubCreateGistTestBase.java @@ -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 createContent() { + List content = new ArrayList(); + + 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 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 expected, @NotNull List actual) { + for (NamedContent file : expected) { + assertTrue("Not found: <" + file.getName() + "> " + file.getText(), actual.contains(file)); + } + + assertEquals("Expected less elements", expected.size(), actual.size()); + } +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubRequestPagingTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubRequestPagingTest.java new file mode 100644 index 000000000000..3c217e072db9 --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubRequestPagingTest.java @@ -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 availableRepos = GithubUtil.getAvailableRepos(GithubUtil.getAuthData()); + List realData = new ArrayList(); + for (RepositoryInfo info : availableRepos) { + realData.add(info.getName()); + } + + List expectedData = new ArrayList(); + for (int i = 1; i <= 250; i++) { + expectedData.add(String.valueOf(i)); + } + + assertContainsElements(realData, expectedData); + } +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTest.java new file mode 100644 index 000000000000..488d2cee7036 --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTest.java @@ -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(); + } + +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTestBase.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTestBase.java new file mode 100644 index 000000000000..d708af88804f --- /dev/null +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareProjectTestBase.java @@ -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() { + @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() { + @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")); + } +} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareTest.java deleted file mode 100644 index 14a52112f308..000000000000 --- a/plugins/github/github-test/test/org/jetbrains/plugins/github/GithubShareTest.java +++ /dev/null @@ -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() { - @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 - } - -} diff --git a/plugins/github/github-test/test/org/jetbrains/plugins/github/test/GithubTest.java b/plugins/github/github-test/test/org/jetbrains/plugins/github/test/GithubTest.java index 6cd99aaa4807..f81e13bec11b 100644 --- a/plugins/github/github-test/test/org/jetbrains/plugins/github/test/GithubTest.java +++ b/plugins/github/github-test/test/org/jetbrains/plugins/github/test/GithubTest.java @@ -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 result = new Ref(); + 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 result = new Ref(); + 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); diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java index 14c6ff8263e4..80d2b6c34454 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubCreateGistAction.java @@ -156,7 +156,7 @@ public class GithubCreateGistAction extends DumbAwareAction { } @NotNull - private static List collectContents(@NotNull Project project, + static List collectContents(@NotNull Project project, @Nullable Editor editor, @Nullable VirtualFile file, @Nullable VirtualFile[] files) { @@ -181,7 +181,7 @@ public class GithubCreateGistAction extends DumbAwareAction { } @Nullable - private static String createGist(@NotNull Project project, + static String createGist(@NotNull Project project, @Nullable GithubAuthData auth, @NotNull List contents, boolean isPrivate, @@ -351,11 +351,11 @@ public class GithubCreateGistAction extends DumbAwareAction { return manager.isIgnoredFile(file) || FileTypeManager.getInstance().isFileIgnored(file); } - private static class NamedContent { + static class NamedContent { @NotNull private final String myName; @NotNull private final String myText; - private NamedContent(@NotNull String name, @NotNull String text) { + public NamedContent(@NotNull String name, @NotNull String text) { myName = name; myText = text; } diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java b/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java index 904b7f7d3fab..9cebc9b96016 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubUtil.java @@ -217,6 +217,26 @@ public class GithubUtil { } } + public static void deleteGithubRepository(@NotNull GithubAuthData auth, @NotNull String repo) throws IOException { + String path = "/repos/" + auth.getLogin() + "/" + repo; + GithubApiUtil.deleteRequest(auth, path); + } + + public static void deleteGist(@NotNull GithubAuthData auth, @NotNull String id) throws IOException { + String path = "/gists/" + id; + GithubApiUtil.deleteRequest(auth, path); + } + + @Nullable + public static JsonObject getGist(@NotNull GithubAuthData auth, @NotNull String id) throws IOException { + String path = "/gists/" + id; + JsonElement result = GithubApiUtil.getRequest(auth, path); + if (result == null) { + return null; + } + return result.getAsJsonObject(); + } + @Nullable public static GitRemote findGitHubRemoteBranch(@NotNull GitRepository repository) { // i.e. find origin which points on my github repo