KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > TypeMirrorHandleTest


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
20 package org.netbeans.api.java.source;
21
22 import java.io.OutputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.lang.model.element.TypeElement;
27 import javax.lang.model.type.TypeMirror;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.source.CancellableTask;
30 import org.netbeans.api.java.source.ClasspathInfo;
31 import org.netbeans.api.java.source.CompilationController;
32 import org.netbeans.api.java.source.CompilationInfo;
33 import org.netbeans.api.java.source.JavaSource;
34 import org.netbeans.api.java.source.JavaSource.Phase;
35 import org.netbeans.api.java.source.SourceUtilsTestUtil;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
38 import org.openide.filesystems.FileLock;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.filesystems.FileUtil;
42
43 /**
44  *
45  * @author Jan Lahoda
46  */

47 public class TypeMirrorHandleTest extends NbTestCase {
48     
49     private FileObject testSource;
50     
51     public TypeMirrorHandleTest(String JavaDoc testName) {
52         super(testName);
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[0]);
57     }
58
59     private TypeMirror parse(CompilationInfo info, String JavaDoc name) {
60         TypeElement string = info.getElements().getTypeElement("test.Test");
61         
62         assertNotNull(string);
63         
64         return info.getTreeUtilities().parseType(name, string);
65     }
66     
67     //TODO: cannot handle wildcards, as Types.isSameType returns false for wildcards:
68
private void testCase(CompilationInfo info, String JavaDoc name) {
69         TypeMirror tm = parse(info, name);
70         TypeMirrorHandle th = TypeMirrorHandle.create(tm);
71         
72         assertTrue(info.getTypes().isSameType(th.resolve(info), tm));
73         assertTrue(info.getTypes().isSameType(tm, th.resolve(info)));
74     }
75     
76     private void writeIntoFile(FileObject file, String JavaDoc what) throws Exception JavaDoc {
77         FileLock lock = file.lock();
78         OutputStream JavaDoc out = file.getOutputStream(lock);
79         
80         try {
81             out.write(what.getBytes());
82         } finally {
83             out.close();
84             lock.releaseLock();
85         }
86     }
87     
88     private void prepareTest() throws Exception JavaDoc {
89         FileSystem fs = FileUtil.createMemoryFileSystem();
90         
91         testSource = fs.getRoot().createData("Test.java");
92         assertNotNull(testSource);
93     }
94     
95     public void testTypeMirrorHandle() throws Exception JavaDoc {
96         prepareTest();
97         writeIntoFile(testSource, "package test; public class Test<T> {}");
98         ClassPath empty = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
99         JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL JavaDoc[0])), empty, empty), testSource);
100         
101         js.runUserActionTask(new CancellableTask<CompilationController>() {
102             public void cancel() {
103             }
104             public void run(CompilationController info) throws Exception JavaDoc {
105                 info.toPhase(Phase.RESOLVED);
106                 testCase(info, "java.util.Map");
107                 testCase(info, "java.util.Map<java.lang.Object, java.util.List>");
108                 testCase(info, "java.util.Map<java.lang.Object, java.util.List<java.lang.String>>");
109                 testCase(info, "int[]");
110             }
111         }, true);
112     }
113
114     public void testTypeMirrorHandleCannotResolve() throws Exception JavaDoc {
115         prepareTest();
116         writeIntoFile(testSource, "package test; public class Test {} class Test1{}");
117         ClassPath empty = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
118         JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL JavaDoc[0])), empty, empty), testSource);
119         final List JavaDoc<TypeMirrorHandle> handles = new ArrayList JavaDoc<TypeMirrorHandle>();
120         
121         js.runUserActionTask(new CancellableTask<CompilationController>() {
122             public void cancel() {
123             }
124             public void run(CompilationController info) throws Exception JavaDoc {
125                 info.toPhase(Phase.RESOLVED);
126                 handles.add(TypeMirrorHandle.create(parse(info, "test.Test1")));
127                 handles.add(TypeMirrorHandle.create(parse(info, "java.util.List<test.Test1>")));
128                 handles.add(TypeMirrorHandle.create(parse(info, "test.Test1[]")));
129             }
130         }, true);
131         writeIntoFile(testSource, "package test; public class Test {}");
132         js.runUserActionTask(new CancellableTask<CompilationController>() {
133             public void cancel() {
134             }
135             public void run(CompilationController info) throws Exception JavaDoc {
136                 info.toPhase(Phase.RESOLVED);
137                 
138                 int count = 0;
139                 
140                 for (TypeMirrorHandle h : handles) {
141                     assertNull(String.valueOf(count++), h.resolve(info));
142                 }
143             }
144         }, true);
145     }
146     
147 }
148
Popular Tags