SubmoduleSubscription: Move common code to AbstractSubmoduleSubscription
This makes it easy to introduce another class for testing the submodule subscriptions. No changes in behavior and functionality intended. Change-Id: I45068b8cf8312bceef935048fb7f9babd8d72201
This commit is contained in:
		@@ -0,0 +1,82 @@
 | 
				
			|||||||
 | 
					// Copyright (C) 2015 The Android Open Source Project
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// 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 com.google.gerrit.acceptance.git;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static com.google.common.truth.Truth.assertThat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.google.gerrit.acceptance.AbstractDaemonTest;
 | 
				
			||||||
 | 
					import com.google.gerrit.common.data.Permission;
 | 
				
			||||||
 | 
					import com.google.gerrit.reviewdb.client.Project;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.eclipse.jgit.junit.TestRepository;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.lib.Config;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.lib.ObjectId;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.revwalk.RevCommit;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.revwalk.RevObject;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.revwalk.RevTree;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.revwalk.RevWalk;
 | 
				
			||||||
 | 
					import org.eclipse.jgit.transport.RefSpec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public abstract class AbstractSubmoduleSubscription extends AbstractDaemonTest {
 | 
				
			||||||
 | 
					  protected TestRepository<?> createProjectWithPush(String name)
 | 
				
			||||||
 | 
					      throws Exception {
 | 
				
			||||||
 | 
					    Project.NameKey project = createProject(name);
 | 
				
			||||||
 | 
					    grant(Permission.PUSH, project, "refs/heads/*");
 | 
				
			||||||
 | 
					    grant(Permission.SUBMIT, project, "refs/for/refs/heads/*");
 | 
				
			||||||
 | 
					    return cloneProject(project);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  protected void createSubscription(
 | 
				
			||||||
 | 
					      TestRepository<?> repo, String branch, String subscribeToRepo,
 | 
				
			||||||
 | 
					      String subscribeToBranch) throws Exception {
 | 
				
			||||||
 | 
					    subscribeToRepo = name(subscribeToRepo);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // The submodule subscription module checks for gerrit.canonicalWebUrl to
 | 
				
			||||||
 | 
					    // detect if it's configured for automatic updates. It doesn't matter if
 | 
				
			||||||
 | 
					    // it serves from that URL.
 | 
				
			||||||
 | 
					    String url = cfg.getString("gerrit", null, "canonicalWebUrl") + "/"
 | 
				
			||||||
 | 
					        + subscribeToRepo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Config cfg = new Config();
 | 
				
			||||||
 | 
					    cfg.setString("submodule", subscribeToRepo, "path", subscribeToRepo);
 | 
				
			||||||
 | 
					    cfg.setString("submodule", subscribeToRepo, "url", url);
 | 
				
			||||||
 | 
					    cfg.setString("submodule", subscribeToRepo, "branch", subscribeToBranch);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    repo.branch("HEAD").commit().insertChangeId()
 | 
				
			||||||
 | 
					      .message("subject: adding new subscription")
 | 
				
			||||||
 | 
					      .add(".gitmodules", cfg.toText().toString())
 | 
				
			||||||
 | 
					      .create();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    repo.git().push().setRemote("origin").setRefSpecs(
 | 
				
			||||||
 | 
					        new RefSpec("HEAD:refs/heads/" + branch)).call();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  protected void expectToHaveSubmoduleState(TestRepository<?> repo,
 | 
				
			||||||
 | 
					      String branch, String submodule, ObjectId expectedId) throws Exception {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    submodule = name(submodule);
 | 
				
			||||||
 | 
					    ObjectId commitId = repo.git().fetch().setRemote("origin").call()
 | 
				
			||||||
 | 
					        .getAdvertisedRef("refs/heads/" + branch).getObjectId();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    RevWalk rw = repo.getRevWalk();
 | 
				
			||||||
 | 
					    RevCommit c = rw.parseCommit(commitId);
 | 
				
			||||||
 | 
					    rw.parseBody(c.getTree());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    RevTree tree = c.getTree();
 | 
				
			||||||
 | 
					    RevObject actualId = repo.get(tree, submodule);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assertThat(actualId).isEqualTo(expectedId);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -8,6 +8,7 @@ acceptance_tests(
 | 
				
			|||||||
    'SubmoduleSubscriptionsIT.java',
 | 
					    'SubmoduleSubscriptionsIT.java',
 | 
				
			||||||
    'VisibleRefFilterIT.java',
 | 
					    'VisibleRefFilterIT.java',
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
 | 
					  deps = [':submodule_util'],
 | 
				
			||||||
  labels = ['git'],
 | 
					  labels = ['git'],
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -22,3 +23,9 @@ java_library(
 | 
				
			|||||||
  srcs = ['AbstractPushForReview.java'],
 | 
					  srcs = ['AbstractPushForReview.java'],
 | 
				
			||||||
  deps = ['//gerrit-acceptance-tests:lib'],
 | 
					  deps = ['//gerrit-acceptance-tests:lib'],
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					java_library(
 | 
				
			||||||
 | 
					  name = 'submodule_util',
 | 
				
			||||||
 | 
					  srcs = ['AbstractSubmoduleSubscription.java',],
 | 
				
			||||||
 | 
					  deps = ['//gerrit-acceptance-tests:lib',]
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,15 +16,9 @@ package com.google.gerrit.acceptance.git;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import static com.google.common.truth.Truth.assertThat;
 | 
					import static com.google.common.truth.Truth.assertThat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
 | 
					 | 
				
			||||||
import com.google.gerrit.common.data.Permission;
 | 
					 | 
				
			||||||
import com.google.gerrit.reviewdb.client.Project;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.eclipse.jgit.junit.TestRepository;
 | 
					import org.eclipse.jgit.junit.TestRepository;
 | 
				
			||||||
import org.eclipse.jgit.lib.Config;
 | 
					 | 
				
			||||||
import org.eclipse.jgit.lib.ObjectId;
 | 
					import org.eclipse.jgit.lib.ObjectId;
 | 
				
			||||||
import org.eclipse.jgit.revwalk.RevCommit;
 | 
					import org.eclipse.jgit.revwalk.RevCommit;
 | 
				
			||||||
import org.eclipse.jgit.revwalk.RevObject;
 | 
					 | 
				
			||||||
import org.eclipse.jgit.revwalk.RevTree;
 | 
					import org.eclipse.jgit.revwalk.RevTree;
 | 
				
			||||||
import org.eclipse.jgit.revwalk.RevWalk;
 | 
					import org.eclipse.jgit.revwalk.RevWalk;
 | 
				
			||||||
import org.eclipse.jgit.transport.RefSpec;
 | 
					import org.eclipse.jgit.transport.RefSpec;
 | 
				
			||||||
@@ -32,7 +26,7 @@ import org.junit.Test;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.concurrent.atomic.AtomicInteger;
 | 
					import java.util.concurrent.atomic.AtomicInteger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class SubmoduleSubscriptionsIT extends AbstractDaemonTest {
 | 
					public class SubmoduleSubscriptionsIT extends AbstractSubmoduleSubscription {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Test
 | 
					  @Test
 | 
				
			||||||
  public void testSubscriptionToEmptyRepo() throws Exception {
 | 
					  public void testSubscriptionToEmptyRepo() throws Exception {
 | 
				
			||||||
@@ -109,14 +103,6 @@ public class SubmoduleSubscriptionsIT extends AbstractDaemonTest {
 | 
				
			|||||||
    assertThat(hasSubmodule(subRepo, "master", "super-project")).isFalse();
 | 
					    assertThat(hasSubmodule(subRepo, "master", "super-project")).isFalse();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private TestRepository<?> createProjectWithPush(String name)
 | 
					 | 
				
			||||||
      throws Exception {
 | 
					 | 
				
			||||||
    Project.NameKey project = createProject(name);
 | 
					 | 
				
			||||||
    grant(Permission.PUSH, project, "refs/heads/*");
 | 
					 | 
				
			||||||
    grant(Permission.SUBMIT, project, "refs/for/refs/heads/*");
 | 
					 | 
				
			||||||
    return cloneProject(project);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private static AtomicInteger contentCounter = new AtomicInteger(0);
 | 
					  private static AtomicInteger contentCounter = new AtomicInteger(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private ObjectId pushChangeTo(TestRepository<?> repo, String branch, String message)
 | 
					  private ObjectId pushChangeTo(TestRepository<?> repo, String branch, String message)
 | 
				
			||||||
@@ -138,27 +124,6 @@ public class SubmoduleSubscriptionsIT extends AbstractDaemonTest {
 | 
				
			|||||||
    return pushChangeTo(repo, branch, "some change");
 | 
					    return pushChangeTo(repo, branch, "some change");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private void createSubscription(
 | 
					 | 
				
			||||||
      TestRepository<?> repo, String branch, String subscribeToRepo,
 | 
					 | 
				
			||||||
      String subscribeToBranch) throws Exception {
 | 
					 | 
				
			||||||
    subscribeToRepo = name(subscribeToRepo);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // The submodule subscription module checks for gerrit.canonicalWebUrl to
 | 
					 | 
				
			||||||
    // detect if it's configured for automatic updates. It doesn't matter if
 | 
					 | 
				
			||||||
    // it serves from that URL.
 | 
					 | 
				
			||||||
    String url = cfg.getString("gerrit", null, "canonicalWebUrl") + "/"
 | 
					 | 
				
			||||||
        + subscribeToRepo;
 | 
					 | 
				
			||||||
    String content = buildSubmoduleSection(subscribeToRepo, subscribeToRepo,
 | 
					 | 
				
			||||||
        url, subscribeToBranch);
 | 
					 | 
				
			||||||
    repo.branch("HEAD").commit().insertChangeId()
 | 
					 | 
				
			||||||
      .message("subject: adding new subscription")
 | 
					 | 
				
			||||||
      .add(".gitmodules", content)
 | 
					 | 
				
			||||||
      .create();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    repo.git().push().setRemote("origin").setRefSpecs(
 | 
					 | 
				
			||||||
        new RefSpec("HEAD:refs/heads/" + branch)).call();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private void deleteAllSubscriptions(TestRepository<?> repo, String branch)
 | 
					  private void deleteAllSubscriptions(TestRepository<?> repo, String branch)
 | 
				
			||||||
      throws Exception {
 | 
					      throws Exception {
 | 
				
			||||||
    repo.git().fetch().setRemote("origin").call();
 | 
					    repo.git().fetch().setRemote("origin").call();
 | 
				
			||||||
@@ -176,23 +141,6 @@ public class SubmoduleSubscriptionsIT extends AbstractDaemonTest {
 | 
				
			|||||||
    assertThat(actualId).isEqualTo(expectedId);
 | 
					    assertThat(actualId).isEqualTo(expectedId);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private void expectToHaveSubmoduleState(TestRepository<?> repo, String branch,
 | 
					 | 
				
			||||||
      String submodule, ObjectId expectedId) throws Exception {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    submodule = name(submodule);
 | 
					 | 
				
			||||||
    ObjectId commitId = repo.git().fetch().setRemote("origin").call()
 | 
					 | 
				
			||||||
        .getAdvertisedRef("refs/heads/" + branch).getObjectId();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    RevWalk rw = repo.getRevWalk();
 | 
					 | 
				
			||||||
    RevCommit c = rw.parseCommit(commitId);
 | 
					 | 
				
			||||||
    rw.parseBody(c.getTree());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    RevTree tree = c.getTree();
 | 
					 | 
				
			||||||
    RevObject actualId = repo.get(tree, submodule);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assertThat(actualId).isEqualTo(expectedId);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private boolean hasSubmodule(TestRepository<?> repo, String branch,
 | 
					  private boolean hasSubmodule(TestRepository<?> repo, String branch,
 | 
				
			||||||
      String submodule) throws Exception {
 | 
					      String submodule) throws Exception {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -212,12 +160,4 @@ public class SubmoduleSubscriptionsIT extends AbstractDaemonTest {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private static String buildSubmoduleSection(String name,
 | 
					 | 
				
			||||||
      String path, String url, String branch) {
 | 
					 | 
				
			||||||
    Config cfg = new Config();
 | 
					 | 
				
			||||||
    cfg.setString("submodule", name, "path", path);
 | 
					 | 
				
			||||||
    cfg.setString("submodule", name, "url", url);
 | 
					 | 
				
			||||||
    cfg.setString("submodule", name, "branch", branch);
 | 
					 | 
				
			||||||
    return cfg.toText().toString();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user