package org.jmeterplugins.repository;

import junit.framework.AssertionFailedError;
import net.sf.json.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class RepoTest {
    @Test
    public void testAll() throws IOException {
        String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
        String up = File.separator + "..";
        String repos = path + up + up + up + File.separator + "site" + File.separator + "dat" + File.separator + "repo";
        File dir = new File(repos);
        File[] files = dir.listFiles();
        List<String> problems = new ArrayList<>();
        for (File f : files) {
            System.out.println("Checking repo: " + f.getCanonicalPath());
            String content = new String(Files.readAllBytes(Paths.get(f.getAbsolutePath())), "UTF-8");
            JSON json = JSONSerializer.toJSON(content, new JsonConfig());
            JSONArray list = (JSONArray) json;
            for (Object o : list) {
                JSONObject spec = (JSONObject) o;
                Plugin plugin = Plugin.fromJSON(spec);
                try {
                    System.out.println("Checking plugin: " + plugin);
                    plugin.setCandidateVersion(plugin.getMaxVersion());

                    plugin.download(new GenericCallback<String>() {
                        @Override
                        public void notify(String s) {
                        }
                    });
                } catch (Throwable e) {
                    problems.add(f.getName() + ":" + plugin);
                    System.err.println("Problem with " + plugin);
                    e.printStackTrace(System.err);
                }
            }
        }
        if (problems.size() > 0) {
            throw new AssertionFailedError(problems.toString());
        }
    }

}

@@ -172,16 +172,37 @@
 eption;%0A
+import java.net.URI;%0A
 import j
@@ -270,66 +270,91 @@
 til.
-ArrayList;%0Aimport java.util.List;%0A%0Apublic class RepoTest %7B
+*;%0A%0Apublic class RepoTest %7B%0A    private final Set%3CString%3E cache = new HashSet%3C%3E();%0A
 %0A   
@@ -407,16 +407,67 @@
 ption %7B%0A
+        List%3CString%3E problems = new ArrayList%3C%3E();%0A
         
@@ -754,16 +754,17 @@
 repos);%0A
+%0A
         
@@ -807,49 +807,28 @@
     
-List%3CString%3E prob
+assert fi
 le
-m
 s 
+!
 = n
-ew ArrayList%3C%3E()
+ull
 ;%0A  
@@ -1259,46 +1259,260 @@
     
+check
 Plugin
- plugin = Plugin.fromJSON(
+(problems, f, spec);%0A            %7D%0A        %7D%0A%0A        if (problems.size() %3E 0) %7B%0A            throw new AssertionFailedError(problems.toString());%0A        %7D%0A    %7D%0A%0A    private void checkPlugin(List%3CString%3E problems, File f, JSONObject 
 spec)
-;
+ %7B
 %0A   
@@ -1508,32 +1508,71 @@
 spec) %7B%0A        
+Plugin plugin = Plugin.fromJSON(spec);%0A
         try %7B%0A  
@@ -1569,24 +1569,16 @@
   try %7B%0A
-        
         
@@ -1639,32 +1639,24 @@
             
-        
 plugin.setCa
@@ -1695,22 +1695,16 @@
 ion());%0A
-%0A     
         
@@ -1703,27 +1703,24 @@
             
-   
 plugin.downl
@@ -1727,176 +1727,571 @@
 oad(
-new GenericCallback%3CString%3E() %7B%0A                        @Override%0A                        public void notify(String s) %7B%0A                        %7D%0A                    %7D
+dummy);%0A        %7D catch (Throwable e) %7B%0A            problems.add(f.getName() + %22:%22 + plugin);%0A            System.err.println(%22Problem with %22 + plugin);%0A            e.printStackTrace(System.err);%0A        %7D%0A%0A        Map%3CString, String%3E libs = plugin.getLibs(plugin.getCandidateVersion());%0A        for (String id : libs.keySet()) %7B%0A            if (!cache.contains(libs.get(id))) %7B%0A                try %7B%0A                    Downloader dwn = new Downloader(dummy);%0A                    dwn.download(id, new URI(libs.get(id)));%0A                    cache.add(libs.get(id)
 );%0A 
@@ -2380,32 +2380,43 @@
 ) + %22:%22 + plugin
+ + %22:%22 + id
 );%0A             
@@ -2451,38 +2451,34 @@
 roblem with %22 + 
-plugin
+id
 );%0A             
@@ -2561,107 +2561,148 @@
   %7D%0A
+%0A
     
+%7D%0A%0A
     
-if (problems.size() %3E 0) %7B%0A            throw new AssertionFailedError(problems.to
+private GenericCallback%3CString%3E dummy = new GenericCallback%3CString%3E() %7B%0A        @Override%0A        public void notify(
 String
-());
+ s) %7B
 %0A   
@@ -2713,12 +2713,12 @@
  %7D%0A    %7D
-%0A
+;
 %0A%7D%0A

improve repo test