KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > codegen > DelegateMethodGeneratorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.codegen;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.util.ElementFilter;
34 import org.netbeans.api.java.source.CompilationInfo;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.api.java.source.JavaSource.Phase;
37 import org.netbeans.api.java.source.JavaSource.Priority;
38 import org.netbeans.api.java.source.SourceUtilsTestUtil;
39 import org.netbeans.junit.NbTestCase;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42
43 /**
44  *
45  * @author Jan Lahoda
46  */

47 public class DelegateMethodGeneratorTest extends NbTestCase {
48     
49     public DelegateMethodGeneratorTest(String JavaDoc testName) {
50         super(testName);
51     }
52
53     protected void setUp() throws Exception JavaDoc {
54     }
55
56 // public void testFindUsableFields() throws Exception {
57
// prepareTest("FindUsableFieldsTest");
58
//
59
// CompilationInfo info = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
60
// TypeElement clazz = info.getElements().getTypeElement("test.FindUsableFieldsTest");
61
//
62
// Map<? extends TypeElement, ? extends List<? extends VariableElement>> class2Variables = DelegatePanel.findUsableFields(info, clazz);
63
//
64
// assertEquals(class2Variables.toString(), 1, class2Variables.size());
65
//
66
// List<? extends VariableElement> variables = class2Variables.get(clazz);
67
//
68
// assertEquals(2, variables.size());
69
// assertTrue("s".contentEquals(variables.get(0).getSimpleName()));
70
// assertTrue("l".contentEquals(variables.get(1).getSimpleName()));
71
// }
72
//
73
// public void testMethodProposals1() throws Exception {
74
// prepareTest("MethodProposals");
75
//
76
// CompilationInfo info = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
77
// TypeElement clazz = info.getElements().getTypeElement("test.MethodProposals");
78
// VariableElement variable = null;
79
//
80
// for (VariableElement v : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
81
// if ("a".contentEquals(v.getSimpleName())) {
82
// variable = v;
83
// }
84
// }
85
//
86
// assertNotNull(variable);
87
//
88
// compareMethodProposals(DelegatePanel.getMethodProposals(info, clazz, variable));
89
// }
90
//
91
// public void testMethodProposals2() throws Exception {
92
// prepareTest("MethodProposals");
93
//
94
// CompilationInfo info = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
95
// TypeElement clazz = info.getElements().getTypeElement("test.MethodProposals");
96
// VariableElement variable = null;
97
//
98
// for (VariableElement v : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
99
// if ("l".contentEquals(v.getSimpleName())) {
100
// variable = v;
101
// }
102
// }
103
//
104
// assertNotNull(variable);
105
//
106
// compareMethodProposals(DelegatePanel.getMethodProposals(info, clazz, variable));
107
// }
108
//
109
// public void testMethodProposals3() throws Exception {
110
// prepareTest("MethodProposals");
111
//
112
// CompilationInfo info = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
113
// TypeElement clazz = info.getElements().getTypeElement("test.MethodProposals");
114
// VariableElement variable = null;
115
//
116
// for (VariableElement v : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
117
// if ("s".contentEquals(v.getSimpleName())) {
118
// variable = v;
119
// }
120
// }
121
//
122
// assertNotNull(variable);
123
//
124
// compareMethodProposals(DelegatePanel.getMethodProposals(info, clazz, variable));
125
// }
126
//
127
// private void compareMethodProposals(List<TypedExecutableElementWrapper> proposals) {
128
// List<String> result = new ArrayList<String>();
129
//
130
// for (TypedExecutableElementWrapper ee : proposals) {
131
// result.add(ee.toString());
132
// }
133
//
134
// Collections.sort(result);
135
//
136
// for (String s : result) {
137
// ref(s);
138
// }
139
//
140
// compareReferenceFiles();
141
// }
142

143     private FileObject testSourceFO;
144     private JavaSource source;
145     
146     private void copyToWorkDir(File JavaDoc resource, File JavaDoc toFile) throws IOException JavaDoc {
147         //TODO: finally:
148
InputStream JavaDoc is = new FileInputStream JavaDoc(resource);
149         OutputStream JavaDoc outs = new FileOutputStream JavaDoc(toFile);
150         
151         int read;
152         
153         while ((read = is.read()) != (-1)) {
154             outs.write(read);
155         }
156         
157         outs.close();
158         
159         is.close();
160     }
161     
162     private void prepareTest(String JavaDoc fileName) throws Exception JavaDoc {
163         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[0]);
164         
165         FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this);
166         FileObject cache = scratch.createFolder("cache");
167         
168         File JavaDoc wd = getWorkDir();
169         File JavaDoc testSource = new File JavaDoc(wd, "test/" + fileName + ".java");
170         
171         testSource.getParentFile().mkdirs();
172         
173         File JavaDoc dataFolder = new File JavaDoc(getDataDir(), "org/netbeans/modules/java/editor/codegen/data/");
174         
175         for (File JavaDoc f : dataFolder.listFiles()) {
176             copyToWorkDir(f, new File JavaDoc(wd, "test/" + f.getName()));
177         }
178         
179         testSourceFO = FileUtil.toFileObject(testSource);
180         
181         assertNotNull(testSourceFO);
182         
183         File JavaDoc testBuildTo = new File JavaDoc(wd, "test-build");
184         
185         testBuildTo.mkdirs();
186         
187         SourceUtilsTestUtil.prepareTest(FileUtil.toFileObject(dataFolder), FileUtil.toFileObject(testBuildTo), cache);
188         SourceUtilsTestUtil.compileRecursively(FileUtil.toFileObject(dataFolder));
189         
190         source = JavaSource.forFileObject(testSourceFO);
191     }
192 }
193
Popular Tags