summaryrefslogtreecommitdiffstats
path: root/test/unit/TestSingleClasses.java
blob: 754fcecb3b28004c5b685fbf86d73f4ac7065199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package unit;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;
import java.util.HashMap;

import org.junit.Test;

import de.fernflower.main.decompiler.ConsoleDecompiler;
import de.fernflower.main.extern.IFernflowerPreferences;

public class TestSingleClasses {

	@Test
	public void test() throws IOException {

		Date start = new Date();
		
		String current_path = new File(".").getCanonicalPath().toString();

		iterateDirectory(new File(current_path + "/bin/unit/classes/"));
		
		System.out.println("\n\nTime elapsed " + (new Date().getTime() - start.getTime())/1000);
	}
	
	private void iterateDirectory(File dir) throws IOException {
		
    	for (File file : dir.listFiles()) {
    		if (file.isDirectory()) {
    			iterateDirectory(file);
    		} else if(file.getName().endsWith(".class")) {
    			decompileAndCheckFile(file);
    		}
    	}
	}
	
	private void decompileAndCheckFile(File file) throws IOException {

		try {
    		
    		ConsoleDecompiler decompiler = new ConsoleDecompiler(new HashMap<String, Object>(){{
    			put("log", "warn");
    			put("ren", "1");
    	        put(IFernflowerPreferences.HIDE_DEFAULT_CONSTRUCTOR, "1");
    	        put(IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES, "0");
    	        put(IFernflowerPreferences.IDEA_NOT_NULL_ANNOTATION, "1");
    	        put(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS, "0");
    	        put(IFernflowerPreferences.USE_DEBUG_VARNAMES, "0");
    	        put(IFernflowerPreferences.NEW_LINE_SEPARATOR, "0");
    	     }});
    		
    		decompiler.addSpace(file, true);

    		// files 
    		String current_path = new File(".").getCanonicalPath().toString();
    		
    		String file_class_name = file.getName();
    		String file_name = file_class_name.substring(0, file_class_name.lastIndexOf(".class")); 
    		String file_java_name = file_name+".java"; 

    		File reference_file = new File(current_path + "/test/unit/results/" + file_name + ".dec");  
    		if(!reference_file.exists()) {
    			return; // no reference file for some reason, not yet created 
    		}
    		
    		File temp_dir = new File(Files.createTempDirectory("tempdec_"+file_name).toString());
    	 	
    		// decompile it
    		decompiler.decompileContext(temp_dir);
    		
    		// get both the decompiled file content and the reference
    		// NOTE: reference files are saved with Windows-style line endings. Convert them if you are decompiling to Unix.
    		File decompiled_file = new File(temp_dir, file_java_name);
    		String decompiled_content = new String(Files.readAllBytes(decompiled_file.toPath()), "UTF-8");
    		String reference_content = new String(Files.readAllBytes(reference_file.toPath()), "UTF-8");
    		
    		// clean up
    		decompiled_file.delete();
    		temp_dir.delete();

    		// compare file content with the reference
    		assertEquals(decompiled_content, reference_content);
    		
		} catch(Exception ex) {
			System.out.println("ERROR: testing file " + file.getCanonicalPath());
			ex.printStackTrace();
		}
		
	}

}