// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.updater;

import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.testFramework.rules.TempDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

import java.io.File;

public abstract class UpdaterTestCase {
  protected static class TestUpdaterUI extends ConsoleUpdaterUI {
    public boolean cancelled = false;

    @Override public void setDescription(String oldBuildDesc, String newBuildDesc) { }
    @Override public void startProcess(String title) { }
    @Override public void checkCancelled() throws OperationCancelledException { if (cancelled) throw new OperationCancelledException(); }
    @Override public void showError(String message) { }
  }

  @Rule public TempDirectory tempDir = new TempDirectory();

  protected File dataDir;
  protected TestUpdaterUI TEST_UI;
  protected CheckSums CHECKSUMS;

  @Before
  public void setUp() throws Exception {
    dataDir = PathManagerEx.findFileUnderCommunityHome("updater/testData");

    Runner.checkCaseSensitivity(dataDir.getPath());
    Runner.initTestLogger();

    TEST_UI = new TestUpdaterUI();

    boolean windowsLineEnds = new File(dataDir, "Readme.txt").length() == 7132;
    CHECKSUMS = new CheckSums(windowsLineEnds);
  }

  @After
  public void tearDown() throws Exception {
    Utils.cleanup();
  }

  public File getTempFile(String fileName) {
    return new File(tempDir.getRoot(), fileName);
  }

  @SuppressWarnings("FieldMayBeStatic")
  protected static class CheckSums {
    public final long README_TXT;
    public final long IDEA_BAT;
    public final long ANNOTATIONS_JAR = 2119442657L;
    public final long ANNOTATIONS_JAR_BIN = 2525796836L;
    public final long ANNOTATIONS_CHANGED_JAR = 4088078858L;
    public final long ANNOTATIONS_CHANGED_JAR_BIN = 2587736223L;
    public final long BOOT_JAR = 3018038682L;
    public final long BOOT_WITH_DIRECTORY_BECOMES_FILE_JAR = 1972168924;
    public final long BOOT2_JAR = 2406818996L;
    public final long BOOT2_CHANGED_WITH_UNCHANGED_CONTENT_JAR = 2406818996L;
    public final long BOOTSTRAP_JAR = 2082851308L;
    public final long BOOTSTRAP_JAR_BIN = 2745721972L;
    public final long BOOTSTRAP_DELETED_JAR = 544883981L;
    public final long LINK_TO_README_TXT = 2305843011042707672L;
    public final long LINK_TO_DOT_README_TXT;

    public CheckSums(boolean windowsSeparators) {
      README_TXT = windowsSeparators ? 1272723667L : 7256327L;
      IDEA_BAT = windowsSeparators ? 3088608749L : 1493936069L;
      LINK_TO_DOT_README_TXT = windowsSeparators ? 2305843009503057206L : 2305843011210142148L;
    }
  }
}
@@ -1271,33 +1271,40 @@
     
-boolean windowsLineEnds =
+CHECKSUMS = new CheckSums(%0A     
  new
@@ -1352,55 +1352,42 @@
 7132
-;
+,
 %0A    
-CHECKSUMS = new CheckSums(windowsLineEnds
+  File.separatorChar == '%5C%5C'
 );%0A 
@@ -2497,32 +2497,45 @@
 boolean 
-windowsSeparator
+crLfs, boolean backwardSlashe
 s) %7B%0A   
@@ -2550,32 +2550,20 @@
 E_TXT = 
-windowsSeparator
+crLf
 s ? 1272
@@ -2599,32 +2599,20 @@
 A_BAT = 
-windowsSeparator
+crLf
 s ? 3088
@@ -2669,24 +2669,22 @@
 T = 
-windowsSeparator
+backwardSlashe
 s ? 
@@ -2695,19 +2695,19 @@
 8430
-09503057206
+11210142148
 L : 
@@ -2714,27 +2714,27 @@
 23058430
-11210142148
+09503057206
 L;%0A    %7D

correct Unix/Windows test data selection in updater tests