2 * Copyright 2000-2018 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package jetbrains.buildServer.buildTriggers.vcs.git.tests;
19 import jetbrains.buildServer.TempFiles;
20 import jetbrains.buildServer.buildTriggers.vcs.git.*;
21 import jetbrains.buildServer.serverSide.ServerPaths;
22 import org.eclipse.jgit.transport.URIish;
23 import org.jetbrains.annotations.NotNull;
24 import org.testng.annotations.AfterMethod;
25 import org.testng.annotations.BeforeMethod;
26 import org.testng.annotations.Test;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
34 import static jetbrains.buildServer.buildTriggers.vcs.git.GitServerUtil.getRepository;
35 import static org.testng.AssertJUnit.*;
38 * @author dmitry.neverov
41 public class MirrorManagerTest {
43 private TempFiles myTempFiles = new TempFiles();
44 private ServerPluginConfig myConfig;
48 public void setUp() throws Exception {
49 ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
50 myConfig = new PluginConfigBuilder(paths).build();
55 public void tearDown() {
56 myTempFiles.cleanup();
60 public void should_handle_clashing_urls() {
61 String clashingUrl1 = "git://some.org/first-clashing-url.git";
62 String clashingUrl2 = "git://some.org/second-clashing-url.git";
63 HashCalculator clashingHash = new ClashingHashCalculator(Arrays.asList(clashingUrl1, clashingUrl2));
64 MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, clashingHash);
65 File dir1 = mirrorManager.getMirrorDir(clashingUrl1);
66 File dir2 = mirrorManager.getMirrorDir(clashingUrl2);
67 assertFalse(dir1.equals(dir2));
71 public void should_restore_mapping_from_existing_repositories() throws Exception {
72 //There are 3 existing repositories and no map file (this is the first start after
73 //mirror manager is introduced). After the start map file should be created
74 //and mirrorManager should respect old repositories locations.
76 File baseMirrorsDir = myConfig.getCachesDir();
77 Map<String, String> existingRepositories = new HashMap<String, String>() {{
78 put("git://some.org/repository1.git", "git-11111111.git");
79 put("git://some.org/repository2.git", "git-22222222.git");
80 put("git://some.org/repository3.git", "git-33333333.git");
83 File map = new File(baseMirrorsDir, "map");
84 createRepositories(baseMirrorsDir, existingRepositories);
85 assertFalse(map.exists());
87 MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, new HashCalculatorImpl());
88 assertTrue(map.exists());
89 for (Map.Entry<String, String> entry : existingRepositories.entrySet()) {
90 String url = entry.getKey();
91 String dir = entry.getValue();
92 assertEquals(new File(baseMirrorsDir, dir), mirrorManager.getMirrorDir(url));
97 public void should_give_different_dirs_for_same_url_if_dir_was_invalidated() {
98 MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, new HashCalculatorImpl());
99 String url = "git://some.org/repository.git";
100 File dir1 = mirrorManager.getMirrorDir(url);
101 mirrorManager.invalidate(dir1);
102 File dir2 = mirrorManager.getMirrorDir(url);
103 assertFalse(dir1.equals(dir2));
107 public void should_remember_invalidated_dirs_after_restart() {
108 String url1 = "git://some.org/repository1.git";
109 String url2 = "git://some.org/repository2.git";
110 HashCalculator clashingHash = new ClashingHashCalculator(Arrays.asList(url1, url2));
111 MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, clashingHash);
112 File dir1 = mirrorManager.getMirrorDir(url1);
113 mirrorManager.invalidate(dir1);
114 mirrorManager = new MirrorManagerImpl(myConfig, clashingHash); //restart
115 File dir2 = mirrorManager.getMirrorDir(url2);
116 assertFalse(dir1.equals(dir2));
120 private void createRepositories(File baseDir, Map<String, String> url2dir) throws Exception {
121 for (Map.Entry<String, String> entry : url2dir.entrySet()) {
122 String url = entry.getKey();
123 String dir = entry.getValue();
124 getRepository(new File(baseDir, dir), new URIish(url));
129 private static class ClashingHashCalculator implements HashCalculator {
130 private final List<String> myValuesWhenShouldClash;
132 ClashingHashCalculator(List<String> valuesWhenShouldClash) {
133 myValuesWhenShouldClash = valuesWhenShouldClash;
136 public long getHash(@NotNull String value) {
137 if (myValuesWhenShouldClash.contains(value))
140 return value.hashCode();