VcsIntegrationEnablerTest implemented for mock test vcs

*GitIntegrationEnablerTest changed to common VcsIntegrationEnablerTest;
*test notification methods moved to VcsTestUtil;
*cosmetics
This commit is contained in:
Nadya Zabrodina
2014-03-11 20:01:16 +04:00
parent c6a1054721
commit e5fb46b082
5 changed files with 76 additions and 83 deletions
@@ -29,7 +29,7 @@ import java.util.*;
import static com.intellij.openapi.util.text.StringUtil.pluralize;
public abstract class VcsIntegrationEnabler<VcsT extends AbstractVcs<?>> {
public abstract class VcsIntegrationEnabler<VcsT extends AbstractVcs> {
protected final @NotNull Project myProject;
protected final @NotNull VcsT myVcs;
@@ -1,5 +1,6 @@
package com.intellij.openapi.vcs;
import com.intellij.notification.Notification;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
@@ -20,8 +21,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.fail;
import static org.junit.Assert.*;
/**
* @author Nadya Zabrodina
@@ -218,4 +218,28 @@ public class VcsTestUtil {
}
});
}
public static void assertNotificationShown(@NotNull Project project, @Nullable Notification expected) {
if (expected != null) {
Notification actualNotification =
((TestVcsNotifier)VcsNotifier.getInstance(project)).getLastNotification();
assertNotNull("No notification was shown", actualNotification);
assertEquals("Notification has wrong title", expected.getTitle(), actualNotification.getTitle());
assertEquals("Notification has wrong type", expected.getType(), actualNotification.getType());
assertEquals("Notification has wrong content", adjustTestContent(expected.getContent()), actualNotification.getContent());
}
}
// we allow more spaces and line breaks in tests to make them more readable.
// After all, notifications display html, so all line breaks and extra spaces are ignored.
private static String adjustTestContent(@NotNull String s) {
StringBuilder res = new StringBuilder();
String[] splits = s.split("\n");
for (String split : splits) {
res.append(split.trim());
}
return res.toString();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -13,89 +13,73 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package git4idea.roots;
package com.intellij.openapi.vcs.roots;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsRoot;
import com.intellij.openapi.vcs.VcsTestUtil;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.committed.MockAbstractVcs;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcsUtil.VcsUtil;
import git4idea.GitUtil;
import git4idea.GitVcs;
import git4idea.commands.Git;
import git4idea.test.GitTestUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.picocontainer.MutablePicoContainer;
import java.io.File;
import java.util.Collection;
import java.util.List;
public class VcsIntegrationEnablerTest extends VcsRootPlatformTest {
public class GitIntegrationEnablerTest extends UsefulTestCase {
public Project myProject;
protected VirtualFile myProjectRoot;
protected VirtualFile myTestRoot;
public GitVcs myVcs;
public Git myGit;
private VirtualFile myTestRoot;
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public GitIntegrationEnablerTest() {
public VcsIntegrationEnablerTest() {
PlatformTestCase.initPlatformLangPrefix();
}
public void setUp() throws Exception {
super.setUp();
IdeaProjectTestFixture projectFixture = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getTestName(true)).getFixture();
projectFixture.setUp();
myProject = projectFixture.getProject();
myVcs = GitVcs.getInstance(myProject);
myProjectRoot = myProject.getBaseDir();
MutablePicoContainer picoContainer = (MutablePicoContainer)myProject.getPicoContainer();
String vcsNotifierKey = VcsNotifier.class.getName();
picoContainer.unregisterComponent(vcsNotifierKey);
picoContainer.registerComponentImplementation(vcsNotifierKey, TestVcsNotifier.class);
myTestRoot = myProjectRoot.getParent();
myGit = ServiceManager.getService(myProject, Git.class);
}
public void testOneRootForTheWholeProjectThenJustAddVcsRoot() {
doTest(given("."), null, null);
}
public void testNoGitRootsThenInitAndNotify() {
public void testNoMockRootsThenInitAndNotify() {
doTest(given(),
notification("Created Git repository in " + myProjectRoot.getPresentableUrl()), ".", VcsTestUtil.toAbsolute(".", myProject));
notification("Created mock repository in " + myProjectRoot.getPresentableUrl()), ".", VcsTestUtil.toAbsolute(".", myProject));
}
public void testBelowGitNoInsideThenNotify() {
public void testBelowMockNoInsideThenNotify() {
doTest(given(".."),
notification("Added Git root: " + myTestRoot.getPresentableUrl()));
notification("Added mock root: " + myTestRoot.getPresentableUrl()));
}
public void testGitForProjectSomeInsideThenNotify() {
public void testMockForProjectSomeInsideThenNotify() {
doTest(given(".", "community"),
notification("Added Git roots: " + myProjectRoot.getPresentableUrl() + ", " + getPresentationForRoot("community")));
notification("Added mock roots: " + myProjectRoot.getPresentableUrl() + ", " + getPresentationForRoot("community")));
}
public void testBelowGitSomeInsideThenNotify() {
public void testBelowMockSomeInsideThenNotify() {
doTest(given("..", "community"),
notification("Added Git roots: " + myTestRoot.getPresentableUrl() + ", " + getPresentationForRoot("community")));
notification("Added mock roots: " + myTestRoot.getPresentableUrl() + ", " + getPresentationForRoot("community")));
}
public void testNotUnderGitSomeInsideThenNotify() {
public void testNotUnderMockSomeInsideThenNotify() {
doTest(given("community", "contrib"),
notification(
"Added Git roots: " + getPresentationForRoot("community") + ", " + getPresentationForRoot("contrib"))
"Added mock roots: " + getPresentationForRoot("community") + ", " + getPresentationForRoot("contrib"))
);
}
@@ -108,7 +92,7 @@ public class GitIntegrationEnablerTest extends UsefulTestCase {
private void doTest(@NotNull Collection<VcsRoot> vcsRoots,
@Nullable Notification notification,
@Nullable String git_init,
@Nullable String mock_init,
@NotNull String... vcs_roots) {
List<String> vcsRootsList = ContainerUtil.newArrayList(vcs_roots);
@@ -123,17 +107,17 @@ public class GitIntegrationEnablerTest extends UsefulTestCase {
}
}));
}
new GitIntegrationEnabler(myVcs, myGit).enable(vcsRoots);
new TestIntegrationEnabler(myVcs).enable(vcsRoots);
assertVcsRoots(vcsRootsList);
if (git_init != null) {
assertGitInit(git_init);
if (mock_init != null) {
assertMockInit(mock_init);
}
GitTestUtil.assertNotificationShown(myProject, notification);
VcsTestUtil.assertNotificationShown(myProject, notification);
}
void assertGitInit(@NotNull String root) {
void assertMockInit(@NotNull String root) {
File rootFile = new File(myProjectRoot.getPath(), root);
assertTrue(new File(rootFile.getPath(), GitUtil.DOT_GIT).exists());
assertTrue(new File(rootFile.getPath(), DOT_MOCK).exists());
}
void assertVcsRoots(@NotNull Collection<String> expectedVcsRoots) {
@@ -170,4 +154,19 @@ public class GitIntegrationEnablerTest extends UsefulTestCase {
private String getPresentationForRoot(@NotNull String root) {
return FileUtil.toSystemDependentName(VcsTestUtil.toAbsolute(root, myProject));
}
private static class TestIntegrationEnabler extends VcsIntegrationEnabler<MockAbstractVcs> {
protected TestIntegrationEnabler(@NotNull MockAbstractVcs vcs) {
super(vcs);
}
@Override
protected boolean initOrNotifyError(@NotNull final VirtualFile projectDir) {
File file = new File(projectDir.getPath(), ".mock");
VcsNotifier.getInstance(myVcs.getProject()).notifySuccess("Created mock repository in " + projectDir.getPresentableUrl());
myFilesToDelete.add(file);
return file.mkdir();
}
}
}
@@ -52,12 +52,10 @@ import java.util.HashSet;
import static com.intellij.openapi.vcs.Executor.cd;
import static com.intellij.openapi.vcs.Executor.mkdir;
/**
* @author Nadya Zabrodina
*/
public abstract class VcsRootPlatformTest extends UsefulTestCase {
public static final String DOT_MOCK = ".mock";
private VcsRootChecker myExtension;
@NotNull protected ProjectLevelVcsManagerImpl myVcsManager;
@NotNull protected MockAbstractVcs myVcs;
@@ -100,12 +98,12 @@ public abstract class VcsRootPlatformTest extends UsefulTestCase {
@Override
public boolean isRoot(@NotNull String path) {
return new File(path, ".mock").exists();
return new File(path, DOT_MOCK).exists();
}
@Override
public boolean isVcsDir(@Nullable String path) {
return path != null && path.toLowerCase().endsWith(".mock");
return path != null && path.toLowerCase().endsWith(DOT_MOCK);
}
};
point.registerExtension(myExtension);
@@ -202,7 +200,7 @@ public abstract class VcsRootPlatformTest extends UsefulTestCase {
for (String path : mockRoots) {
File file = new File(projectDir, path);
file.mkdirs();
File mockDir = new File(file, ".mock");
File mockDir = new File(file, DOT_MOCK);
mockDir.mkdirs();
myFilesToDelete.add(mockDir);
mockDir.deleteOnExit();
@@ -15,11 +15,8 @@
*/
package git4idea.test;
import com.intellij.notification.Notification;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.TestVcsNotifier;
import com.intellij.openapi.vcs.VcsNotifier;
import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
@@ -27,7 +24,6 @@ import git4idea.GitUtil;
import git4idea.GitVcs;
import git4idea.repo.GitRepository;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.BuiltInServerManagerImpl;
import java.io.File;
@@ -40,7 +36,6 @@ import static com.intellij.openapi.vcs.VcsTestUtil.createDir;
import static com.intellij.openapi.vcs.VcsTestUtil.createFile;
import static git4idea.test.GitExecutor.git;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.TestCase.assertEquals;
public class GitTestUtil {
@@ -111,29 +106,6 @@ public class GitTestUtil {
return repository;
}
public static void assertNotificationShown(@NotNull Project project, @Nullable Notification expected) {
if (expected != null) {
Notification actualNotification =
((TestVcsNotifier)VcsNotifier.getInstance(project)).getLastNotification();
assertNotNull("No notification was shown", actualNotification);
assertEquals("Notification has wrong title", expected.getTitle(), actualNotification.getTitle());
assertEquals("Notification has wrong type", expected.getType(), actualNotification.getType());
assertEquals("Notification has wrong content", adjustTestContent(expected.getContent()), actualNotification.getContent());
}
}
// we allow more spaces and line breaks in tests to make them more readable.
// After all, notifications display html, so all line breaks and extra spaces are ignored.
private static String adjustTestContent(@NotNull String s) {
StringBuilder res = new StringBuilder();
String[] splits = s.split("\n");
for (String split : splits) {
res.append(split.trim());
}
return res.toString();
}
/**
* Default port will be occupied by main idea instance => define the custom default to avoid searching of free port
*/