<commit_before>package fitnesse.updates;

import fitnesse.FitNesseContext;
import fitnesse.testutil.FitNesseUtil;
import fitnesse.wiki.FileSystemPage;
import fitnesse.wiki.PageCrawler;
import fitnesse.wiki.WikiPage;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import util.FileUtil;
import static util.RegexTestCase.assertSubString;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class UpdaterImplementationTest {
  private File updateList;
  private File updateDoNotCopyOver;
  public static final String testDir = "testDir";
  public static final String rootName = "RooT";

  protected WikiPage root;
  protected Update update;
  protected UpdaterImplementation updater;
  protected WikiPage pageOne;
  protected WikiPage pageTwo;
  protected FitNesseContext context;
  protected PageCrawler crawler;
  private boolean updateDone = false;

  @Before
  public void setUp() throws Exception {
    setTheContext();
    root = setTheRoot();
    createFakeJarFileResources();
    createFakeUpdateListFiles();
    updater = new UpdaterImplementation(context);
  }

  private WikiPage setTheRoot() throws Exception {
    return root;
  }

  private void setTheContext() {
    FileUtil.makeDir(testDir);
    root = new FileSystemPage(testDir, rootName);
    crawler = root.getPageCrawler();
    context = FitNesseUtil.makeTestContext(root, testDir, rootName, 80);
  }

  private void createFakeUpdateListFiles() {
    updateList = new File("classes/Resources/updateList");
    updateDoNotCopyOver = new File("classes/Resources/updateDoNotCopyOverList");
    FileUtil.createFile(updateList, "files/TestFile\nfiles/BestFile\n");
    FileUtil.createFile(updateDoNotCopyOver, "SpecialFile");
  }

  private void createFakeJarFileResources() throws IOException {
    FileUtil.createFile("classes/Resources/files/TestFile","") ;
    FileUtil.createFile("classes/Resources/files/BestFile","") ;
    FileUtil.createFile("classes/Resources/SpecialFile","");
  }

  @Test
  public void shouldBeAbleToGetUpdateFilesAndMakeAlistFromThem() throws Exception {
    ArrayList<String> updateArrayList = new ArrayList<String>();
    updater.tryToParseTheFileIntoTheList(updateList, updateArrayList);
    assertEquals("files/TestFile", updateArrayList.get(0));
    assertEquals("files/BestFile", updateArrayList.get(1));
    updateArrayList = new ArrayList<String>();
    updater.tryToParseTheFileIntoTheList(updateDoNotCopyOver, updateArrayList);
    assertEquals("SpecialFile", updateArrayList.get(0));
  }

  @Test
  public void shouldBeAbleToGetThePathOfJustTheParent() throws Exception {
    String filePath = updater.getCorrectPathForTheDestination("classes/files/moreFiles/TestFile");
    assertSubString(portablePath("classes/files/moreFiles"), filePath);
  }

  private String portablePath(String path) {
    return FileUtil.buildPath(path.split("/"));
  }

  @Test
  public void shouldCreateTheCorrectPathForGivenPath() throws Exception {
    String filePath = updater.getCorrectPathFromJar("FitNesseRoot/files/moreFiles/TestFile");
    assertEquals("Resources/FitNesseRoot/files/moreFiles/TestFile", filePath);
  }

  @Test
  public void shouldCreateSomeFilesInTheRooTDirectory() throws Exception {
    for (Update update : updater.updates) {
      if (update.getClass() == ReplacingFileUpdate.class || update.getClass() == FileUpdate.class)
        update.doUpdate();
    }
    File testFile = new File(context.rootPath, "files/TestFile");
    File bestFile = new File(context.rootPath, "files/BestFile");
    File specialFile = new File(context.rootPath, "SpecialFile");
    assertTrue(testFile.exists());
    assertTrue(bestFile.exists());
    assertTrue(specialFile.exists());
    assertFalse(testFile.isDirectory());
    assertFalse(bestFile.isDirectory());
    assertFalse(specialFile.isDirectory());
  }

  @Test
  @Ignore
  public void shouldReplaceFitNesseRootWithDirectoryRoot() throws Exception {
    String filePath = "FitNesseRoot/someFolder/someFile";
    //context.rootDirectoryName = "MyNewRoot";
    String updatedPath = updater.getCorrectPathForTheDestination(filePath);
    assertEquals(portablePath("MyNewRoot/someFolder"), updatedPath);
  }

  @Test
  public void updatesShouldBeRunIfCurrentVersionNotAlreadyUpdated() throws Exception {
    String version = "TestVersion";
    updater.setFitNesseVersion(version);
    updater.testing = true;

    File propertiesFile = new File("testDir/RooT/properties");
    FileUtil.deleteFile(propertiesFile);
    assertFalse(propertiesFile.exists());

    updater.updates = new Update[]{
      new UpdateSpy()
    };
    updater.update();
    assertTrue(updateDone);
    assertTrue(propertiesFile.exists());

    Properties properties = updater.loadProperties();
    assertTrue(properties.containsKey("Version"));
    assertEquals(version, properties.get("Version"));
    FileUtil.deleteFile(propertiesFile);
  }

  @Test
  public void updatesShouldNotBeRunIfCurrentVersionAlreadyUpdated() throws Exception {
    String version = "TestVersion";
    updater.setFitNesseVersion(version);
    Properties properties = updater.getProperties();
    properties.put("Version", version);
    updater.updates = new Update[]{
      new UpdateSpy()
    };
    updater.update();
    assertFalse(updateDone);
  }

  @Test(expected = RuntimeException.class)
  public void shouldThrowExceptionInNoUpdateFileExists() throws Exception {
    FileUtil.deleteFile(updateList);
    updater.tryToParseTheFileIntoTheList(updateList,new ArrayList<String>());
  }

  @After
  public void tearDown() {
    FileUtil.deleteFileSystemDirectory("classes/Resources");
    FileUtil.deleteFileSystemDirectory(testDir);
  }

  private class UpdateSpy implements Update {
    public String getName() {
      return "test";
    }

    public String getMessage() {
      return "test";
    }

    public boolean shouldBeApplied() {
      return true;
    }

    public void doUpdate() {
      updateDone = true;
    }
  }
}
<commit_msg>Cleaned up ignored test from UpdaterImplementation.<commit_after>package fitnesse.updates;

import fitnesse.FitNesseContext;
import fitnesse.testutil.FitNesseUtil;
import fitnesse.wiki.FileSystemPage;
import fitnesse.wiki.PageCrawler;
import fitnesse.wiki.WikiPage;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import util.FileUtil;
import static util.RegexTestCase.assertSubString;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class UpdaterImplementationTest {
  private File updateList;
  private File updateDoNotCopyOver;
  public static final String testDir = "testDir";
  public static final String rootName = "RooT";

  protected WikiPage root;
  protected Update update;
  protected UpdaterImplementation updater;
  protected WikiPage pageOne;
  protected WikiPage pageTwo;
  protected FitNesseContext context;
  protected PageCrawler crawler;
  private boolean updateDone = false;

  @Before
  public void setUp() throws Exception {
    setTheContext();
    root = setTheRoot();
    createFakeJarFileResources();
    createFakeUpdateListFiles();
    updater = new UpdaterImplementation(context);
  }

  private WikiPage setTheRoot() throws Exception {
    return root;
  }

  private void setTheContext() {
    FileUtil.makeDir(testDir);
    root = new FileSystemPage(testDir, rootName);
    crawler = root.getPageCrawler();
    context = FitNesseUtil.makeTestContext(root, testDir, rootName, 80);
  }

  private void createFakeUpdateListFiles() {
    updateList = new File("classes/Resources/updateList");
    updateDoNotCopyOver = new File("classes/Resources/updateDoNotCopyOverList");
    FileUtil.createFile(updateList, "files/TestFile\nfiles/BestFile\n");
    FileUtil.createFile(updateDoNotCopyOver, "SpecialFile");
  }

  private void createFakeJarFileResources() throws IOException {
    FileUtil.createFile("classes/Resources/files/TestFile","") ;
    FileUtil.createFile("classes/Resources/files/BestFile","") ;
    FileUtil.createFile("classes/Resources/SpecialFile","");
  }

  @Test
  public void shouldBeAbleToGetUpdateFilesAndMakeAlistFromThem() throws Exception {
    ArrayList<String> updateArrayList = new ArrayList<String>();
    updater.tryToParseTheFileIntoTheList(updateList, updateArrayList);
    assertEquals("files/TestFile", updateArrayList.get(0));
    assertEquals("files/BestFile", updateArrayList.get(1));
    updateArrayList = new ArrayList<String>();
    updater.tryToParseTheFileIntoTheList(updateDoNotCopyOver, updateArrayList);
    assertEquals("SpecialFile", updateArrayList.get(0));
  }

  @Test
  public void shouldBeAbleToGetThePathOfJustTheParent() throws Exception {
    String filePath = updater.getCorrectPathForTheDestination("classes/files/moreFiles/TestFile");
    assertSubString(portablePath("classes/files/moreFiles"), filePath);
  }

  private String portablePath(String path) {
    return FileUtil.buildPath(path.split("/"));
  }

  @Test
  public void shouldCreateTheCorrectPathForGivenPath() throws Exception {
    String filePath = updater.getCorrectPathFromJar("FitNesseRoot/files/moreFiles/TestFile");
    assertEquals("Resources/FitNesseRoot/files/moreFiles/TestFile", filePath);
  }

  @Test
  public void shouldCreateSomeFilesInTheRooTDirectory() throws Exception {
    for (Update update : updater.updates) {
      if (update.getClass() == ReplacingFileUpdate.class || update.getClass() == FileUpdate.class)
        update.doUpdate();
    }
    File testFile = new File(context.rootPath, "files/TestFile");
    File bestFile = new File(context.rootPath, "files/BestFile");
    File specialFile = new File(context.rootPath, "SpecialFile");
    assertTrue(testFile.exists());
    assertTrue(bestFile.exists());
    assertTrue(specialFile.exists());
    assertFalse(testFile.isDirectory());
    assertFalse(bestFile.isDirectory());
    assertFalse(specialFile.isDirectory());
  }

  @Test
  public void updatesShouldBeRunIfCurrentVersionNotAlreadyUpdated() throws Exception {
    String version = "TestVersion";
    updater.setFitNesseVersion(version);
    updater.testing = true;

    File propertiesFile = new File("testDir/RooT/properties");
    FileUtil.deleteFile(propertiesFile);
    assertFalse(propertiesFile.exists());

    updater.updates = new Update[]{
      new UpdateSpy()
    };
    updater.update();
    assertTrue(updateDone);
    assertTrue(propertiesFile.exists());

    Properties properties = updater.loadProperties();
    assertTrue(properties.containsKey("Version"));
    assertEquals(version, properties.get("Version"));
    FileUtil.deleteFile(propertiesFile);
  }

  @Test
  public void updatesShouldNotBeRunIfCurrentVersionAlreadyUpdated() throws Exception {
    String version = "TestVersion";
    updater.setFitNesseVersion(version);
    Properties properties = updater.getProperties();
    properties.put("Version", version);
    updater.updates = new Update[]{
      new UpdateSpy()
    };
    updater.update();
    assertFalse(updateDone);
  }

  @Test(expected = RuntimeException.class)
  public void shouldThrowExceptionInNoUpdateFileExists() throws Exception {
    FileUtil.deleteFile(updateList);
    updater.tryToParseTheFileIntoTheList(updateList,new ArrayList<String>());
  }

  @After
  public void tearDown() {
    FileUtil.deleteFileSystemDirectory("classes/Resources");
    FileUtil.deleteFileSystemDirectory(testDir);
  }

  private class UpdateSpy implements Update {
    public String getName() {
      return "test";
    }

    public String getMessage() {
      return "test";
    }

    public boolean shouldBeApplied() {
      return true;
    }

    public void doUpdate() {
      updateDone = true;
    }
  }
}

{'fraction_non_alphanumeric': 0.10504166315966669, 'fraction_numerical': 0.0008416799932665601, 'mean_word_length': 4.7763733592610595, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7244052', 'n_tokens_mistral': 3407, 'n_tokens_neox': 3364, 'n_words': 668}