import com.baidu.rasp.uninstall.windows.TomcatUnInstaller;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Constructor;

import static org.junit.Assert.assertEquals;

/**
 * @author: anyang
 * @create: 2019/03/25 17:29
 */
public class WindowsTomcatUninstallerTest {
    @Test
    public void testGetInstallPath() {
        try {
            Class clazz = TomcatUnInstaller.class;
            Constructor constructor = clazz.getConstructor(String.class, String.class);
            Object installer = constructor.newInstance("Tomcat", "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30/");
            String path = Utils.invokeStringMethod(installer, "getInstallPath", new Class[]{String.class},
                    "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30");
            assertEquals(path, "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30\\rasp");
        } catch (Exception e) {
            //
        }
    }

    @Test
    public void testGetScript() {
        try {
            Class clazz = TomcatUnInstaller.class;
            Constructor constructor = clazz.getConstructor(String.class, String.class);
            Object installer = constructor.newInstance("Tomcat", "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30/");
            String path = Utils.invokeStringMethod(installer, "getScript", new Class[]{String.class},
                    "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30\\rasp");
            assertEquals(path, "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30\\rasp\\..\\bin\\catalina.bat");
        } catch (Exception e) {
            //
        }
    }

    @Test
    public void testRecoverStartScript() {
        try {
            String content = IOUtils.toString(new FileReader(new File("/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30/bin/catalina.bat")));
            Class clazz = TomcatUnInstaller.class;
            Constructor constructor = clazz.getConstructor(String.class, String.class);
            Object installer = constructor.newInstance("Tomcat", "/Users/anyang/Desktop/jacoco/sum/apache-tomcat-8.5.30/");
            Utils.invokeStringMethod(installer, "recoverStartScript", new Class[]{String.class}, content);
        } catch (Exception e) {
            //
        }
    }
}
