1 package org.jd.gui.view.component;
2 
3 import junit.framework.TestCase;
4 import org.fife.ui.rsyntaxtextarea.DocumentRange;
5 import org.junit.Assert;
6 
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.TreeMap;
10 
11 public class ClassFilePageTest extends TestCase {
12 
initDeclarations()13     public HashMap<String, TypePage.DeclarationData> initDeclarations() {
14         TypePage.DeclarationData data = new TypePage.DeclarationData(0, 1, "Test", "test", "I");
15         HashMap<String, TypePage.DeclarationData> declarations = new HashMap<>();
16 
17         // Init type declarations
18         declarations.put("Test", data);
19         declarations.put("test/Test", data);
20 
21         // Init field declarations
22         declarations.put("Test-attributeInt-I", data);
23         declarations.put("Test-attributeBoolean-Z", data);
24         declarations.put("Test-attributeArrayBoolean-[[Z", data);
25         declarations.put("Test-attributeString-Ljava/lang/String;", data);
26 
27         declarations.put("test/Test-attributeInt-I", data);
28         declarations.put("test/Test-attributeBoolean-Z", data);
29         declarations.put("test/Test-attributeArrayBoolean-[[Z", data);
30         declarations.put("test/Test-attributeString-Ljava/lang/String;", data);
31 
32         // Init method declarations
33         declarations.put("Test-getInt-()I", data);
34         declarations.put("Test-getString-()Ljava/lang/String;", data);
35         declarations.put("Test-add-(JJ)J", data);
36         declarations.put("Test-createBuffer-(I)[C", data);
37 
38         declarations.put("test/Test-getInt-()I", data);
39         declarations.put("test/Test-getString-()Ljava/lang/String;", data);
40         declarations.put("test/Test-add-(JJ)J", data);
41         declarations.put("test/Test-createBuffer-(I)[C", data);
42 
43         return declarations;
44     }
45 
initHyperlinks()46     public TreeMap<Integer, HyperlinkPage.HyperlinkData> initHyperlinks() {
47         TreeMap<Integer, HyperlinkPage.HyperlinkData> hyperlinks = new TreeMap<>();
48 
49         hyperlinks.put(0, new TypePage.HyperlinkReferenceData(0, 1, new TypePage.ReferenceData("java/lang/Integer", "MAX_VALUE", "I", "Test")));
50         hyperlinks.put(1, new TypePage.HyperlinkReferenceData(0, 1, new TypePage.ReferenceData("java/lang/Integer", "toString", "()Ljava/lang/String;", "Test")));
51 
52         return hyperlinks;
53     }
54 
initStrings()55     public ArrayList<TypePage.StringData> initStrings() {
56         ArrayList<TypePage.StringData> strings = new ArrayList<>();
57 
58         strings.add(new TypePage.StringData(0, 3, "abc", "Test"));
59 
60         return strings;
61     }
62 
testMatchFragmentAndAddDocumentRange()63     public void testMatchFragmentAndAddDocumentRange() {
64         HashMap<String, TypePage.DeclarationData> declarations = initDeclarations();
65         ArrayList<DocumentRange> ranges = new ArrayList<>();
66 
67         ranges.clear();
68         ClassFilePage.matchFragmentAndAddDocumentRange("Test-attributeBoolean-Z", declarations, ranges);
69         Assert.assertTrue(ranges.size() == 1);
70 
71         ranges.clear();
72         ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-attributeBoolean-Z", declarations, ranges);
73         Assert.assertTrue(ranges.size() == 1);
74 
75         ranges.clear();
76         ClassFilePage.matchFragmentAndAddDocumentRange("*/Test-attributeBoolean-Z", declarations, ranges);
77         Assert.assertTrue(ranges.size() == 2);
78 
79         ranges.clear();
80         ClassFilePage.matchFragmentAndAddDocumentRange("Test-createBuffer-(I)[C", declarations, ranges);
81         Assert.assertTrue(ranges.size() == 1);
82 
83         ranges.clear();
84         ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-createBuffer-(I)[C", declarations, ranges);
85         Assert.assertTrue(ranges.size() == 1);
86 
87         ranges.clear();
88         ClassFilePage.matchFragmentAndAddDocumentRange("*/Test-getString-(*)?", declarations, ranges);
89         Assert.assertTrue(ranges.size() == 2);
90 
91         ranges.clear();
92         ClassFilePage.matchFragmentAndAddDocumentRange("test/Test-add-(?J)?", declarations, ranges);
93         Assert.assertTrue(ranges.size() == 1);
94     }
95 
testMatchQueryAndAddDocumentRange()96     public void testMatchQueryAndAddDocumentRange() {
97         HashMap<String, String> parameters = new HashMap<>();
98         HashMap<String, TypePage.DeclarationData> declarations = initDeclarations();
99         TreeMap<Integer, HyperlinkPage.HyperlinkData> hyperlinks = initHyperlinks();
100         ArrayList<TypePage.StringData> strings = initStrings();
101         ArrayList<DocumentRange> ranges = new ArrayList<>();
102 
103         parameters.put("highlightPattern", "ab");
104         parameters.put("highlightFlags", "s");
105 
106         parameters.put("highlightScope", null);
107         ranges.clear();
108         ClassFilePage.matchQueryAndAddDocumentRange(parameters, declarations, hyperlinks, strings, ranges);
109         Assert.assertTrue(ranges.size() == 1);
110 
111         parameters.put("highlightScope", "");
112         ranges.clear();
113         ClassFilePage.matchQueryAndAddDocumentRange(parameters, declarations, hyperlinks, strings, ranges);
114         Assert.assertTrue(ranges.size() == 1);
115 
116         parameters.put("highlightScope", "Test");
117         ranges.clear();
118         ClassFilePage.matchQueryAndAddDocumentRange(parameters, declarations, hyperlinks, strings, ranges);
119         Assert.assertTrue(ranges.size() == 1);
120     }
121 
testMatchScope()122     public void testMatchScope() {
123         Assert.assertTrue(ClassFilePage.matchScope(null, "java/lang/String"));
124         Assert.assertTrue(ClassFilePage.matchScope("", "java/lang/String"));
125 
126         Assert.assertTrue(ClassFilePage.matchScope("java/lang/String", "java/lang/String"));
127         Assert.assertTrue(ClassFilePage.matchScope("*/lang/String", "java/lang/String"));
128         Assert.assertTrue(ClassFilePage.matchScope("*/String", "java/lang/String"));
129 
130         Assert.assertTrue(ClassFilePage.matchScope(null, "Test"));
131         Assert.assertTrue(ClassFilePage.matchScope("", "Test"));
132 
133         Assert.assertTrue(ClassFilePage.matchScope("Test", "Test"));
134         Assert.assertTrue(ClassFilePage.matchScope("*/Test", "Test"));
135     }
136 }
137