view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialResetCacheHandlerTest.java @ 908:b39494a5ff96

TW-39095 handle commit messages with non-ascii symbols
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 05 Dec 2014 14:59:15 +0100
parents 31a1aca3305c
children 7bf4d943d5bb
line wrap: on
line source
/*
 * 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.
 * 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 jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.TempFiles;
import jetbrains.buildServer.util.cache.ResetCacheHandler;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.States;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static org.testng.AssertJUnit.*;

/**
 * @author dmitry.neverov
 */
@Test
public class MercurialResetCacheHandlerTest {

  private Mockery myContext;
  private TempFiles myTempFiles;
  private File myCachesDir;
  private MirrorManager myMirrorManager;
  private ResetCacheHandler myCacheHandler;

  @BeforeMethod
  public void setUp() throws IOException {
    myContext = new Mockery();
    myTempFiles = new TempFiles();
    myCachesDir = myTempFiles.createTempDir();
    myMirrorManager = myContext.mock(MirrorManager.class);
    myCacheHandler = new MercurialResetCacheHandler(myMirrorManager);
  }

  @AfterMethod
  public void tearDown() {
    myTempFiles.cleanup();
  }


  public void cache_list_should_contains_entry_for_mercurial_in_general() {
    assertEquals(asList("mercurial"), myCacheHandler.listCaches());
  }


  public void mercurial_cache_is_empty_when_there_are_no_mirrors() {
    myContext.checking(new Expectations() {{
      atLeast(1).of(myMirrorManager).getMappings(); will(returnValue(emptyMap()));
    }});

    assertTrue(myCacheHandler.isEmpty("mercurial"));
    myContext.assertIsSatisfied();
  }


  public void mercurial_cache_is_not_empty_when_mirrors_exist() {
    final Map<String, File> mapping = new HashMap<String, File>() {{
      put("http://some.org/repository1", new File(myCachesDir, "a"));
      put("http://some.org/repository2", new File(myCachesDir, "b"));
    }};
    myContext.checking(new Expectations() {{
      atLeast(1).of(myMirrorManager).getMappings(); will(returnValue(mapping));
    }});

    assertFalse(myCacheHandler.isEmpty("mercurial"));
    myContext.assertIsSatisfied();
  }


  public void reset_cache_should_reset_caches_for_all_mirrors() {
    final String url1 = "http://some.org/repository1";
    final String url2 = "http://some.org/repository2";
    final File mirror1 = new File(myCachesDir, "a");
    final File mirror2 = new File(myCachesDir, "b");
    mirror1.mkdirs();
    mirror2.mkdirs();
    final Map<String, File> mapping = new HashMap<String, File>() {{
      put(url1, mirror1);
      put(url2, mirror2);
    }};
    final States stateOfMirror1 = myContext.states("mirror1").startsAs("initial");
    final States stateOfMirror2 = myContext.states("mirror2").startsAs("initial");
    myContext.checking(new Expectations() {{
      atLeast(1).of(myMirrorManager).getMappings(); will(returnValue(mapping));

      one(myMirrorManager).lockDir(mirror1); when(stateOfMirror1.is("initial")); then(stateOfMirror1.is("locked"));
      one(myMirrorManager).lockDir(mirror2); when(stateOfMirror2.is("initial")); then(stateOfMirror2.is("locked"));

      one(myMirrorManager).forgetDir(mirror1); when(stateOfMirror1.is("locked")); then(stateOfMirror1.is("reset"));
      one(myMirrorManager).forgetDir(mirror2); when(stateOfMirror2.is("locked")); then(stateOfMirror2.is("reset"));

      one(myMirrorManager).unlockDir(mirror1); when(stateOfMirror1.is("reset"));
      one(myMirrorManager).unlockDir(mirror2); when(stateOfMirror2.is("reset"));
    }});

    myCacheHandler.resetCache("mercurial");
    myContext.assertIsSatisfied();
    assertTrue(myCachesDir.listFiles().length == 0);
  }
}