package com.elab.my.maven;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

//@SpringBootTest(classes = {TestService.class})
public class TestServiceTest extends TestCase {

    @Test
    public void test() {
        String files =
            "[ERROR] [FATAL] Non-parseable POM D:\\lib\\maven\\io\\netty\\netty-bom\\4.1.31.Final\\netty-bom-4.1.31.Final.pom: unexpected markup <!d (position: START_DOCUMENT seen \\r\\n<!d... @2:4)  @ D:\\lib\\maven\\io\\netty\\netty-bom\\4.1.31.Final\\netty-bom-4.1.31.Final.pom, line 2, column 4\n"
                + "[ERROR] [FATAL] Non-parseable POM D:\\lib\\maven\\io\\projectreactor\\reactor-bom\\Californium-SR3\\reactor-bom-Californium-SR3.pom: unexpected markup <!d (position: START_DOCUMENT seen \\r\\n<!d... @2:4)  @ D:\\lib\\maven\\io\\projectreactor\\reactor-bom\\Californium-SR3\\reactor-bom-Californium-SR3.pom, line 2, column 4\n";
        String[] split = files.split("\n");
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            int start = s.indexOf("@ ") + 2;
            int end = s.indexOf(",", start);
            String file = s.substring(start, end);

            if (file.endsWith("pom")) {
                System.out.println(file);
                File f = new File(file);
                f.delete();
            }
        }
        System.out.println(split);
    }

    @Test
    public void testDPom() throws Exception {
        String pom =
            "[WARNING] The POM for org.springframework.cloud:spring-cloud-starter-config:jar:2.0.0.RELEASE is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details\n"
                + "[WARNING] The POM for org.springframework.boot:spring-boot-starter-web:jar:2.0.0.RELEASE is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details\n"
                + "[WARNING] The POM for org.springframework.boot:spring-boot-starter-test:jar:2.0.0.RELEASE is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details\n"
                + "[WARNING] The POM for org.springframework.cloud:spring-cloud-config-server:jar:2.0.0.RELEASE is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details\n";
        String[] split = pom.split("\n");
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            int start = s.indexOf("for ") + 4;
            int end = s.indexOf(" is ", start);
            String file = s.substring(start, end);
            StringBuilder sb = new StringBuilder("D:\\lib\\maven\\");
            String[] fileList = file.split(":");
            sb.append(fileList[0].replaceAll("\\.", "\\\\"));
            sb.append("\\" + fileList[1]);
            sb.append("\\" + fileList[3]);

            File f = new File(sb.toString());
            deleteFile(f);
            f.delete();
            System.out.println(sb.toString() + " : \t " + f.exists());
        }
    }

    private static void deleteFile(File file) throws Exception {
        /**
         * File[] listFiles()
         *  返回一个抽象路径名数组，这些路径名表示此抽象路径名表示的目录中的文件。
         */
        File[] files = file.listFiles();
        if (files != null) {//如果包含文件进行删除操作
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    //删除子文件
                    files[i].delete();
                } else if (files[i].isDirectory()) {
                    //通过递归的方法找到子目录的文件
                    deleteFile(files[i]);
                }
                files[i].delete();//删除子目录
            }
        }
    }
}