001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.container;
043:
044: import java.io.File;
045: import java.util.Collection;
046: import java.util.HashSet;
047: import java.util.Set;
048: import org.netbeans.modules.cnd.api.model.CsmClass;
049: import org.netbeans.modules.cnd.api.model.CsmFriend;
050: import org.netbeans.modules.cnd.api.model.CsmFriendClass;
051: import org.netbeans.modules.cnd.api.model.CsmFriendFunction;
052: import org.netbeans.modules.cnd.api.model.CsmFunction;
053: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
054: import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
055: import org.netbeans.modules.cnd.modelimpl.impl.services.FriendResolverImpl;
056: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
057: import org.netbeans.modules.cnd.modelimpl.trace.TraceModelTestBase;
058:
059: /**
060: * base class for friends tests
061: *
062: * @author Alexander Simon
063: */
064: public class FriendTestCase extends TraceModelTestBase {
065:
066: public FriendTestCase(String testName) {
067: super (testName);
068: }
069:
070: @Override
071: protected void setUp() throws Exception {
072: super .setUp();
073: //log("CndFriendTestCase.setUp started.");
074: //log("Test "+getName()+ " started");
075: }
076:
077: @Override
078: protected void tearDown() throws Exception {
079: super .tearDown();
080: }
081:
082: public void testFriend() throws Exception {
083: performTest("friend.cc"); // NOI18N
084: }
085:
086: @Override
087: protected void performTest(String source) throws Exception {
088: File testFile = getDataFile(source);
089: assertTrue("File not found " + testFile.getAbsolutePath(),
090: testFile.exists()); // NOI18N
091: performModelTest(testFile, System.out, System.err);
092: checkFriend();
093: for (FileImpl file : getProject().getAllFileImpls()) {
094: file.stateChanged(true);
095: file.scheduleParsing(true);
096: }
097: checkFriend();
098: getProject().onFileRemoved(testFile);
099: checkEmpty();
100: }
101:
102: private void checkEmpty() {
103: ProjectBase project = getProject();
104: assertNotNull("Project must be valid", project); // NOI18N
105: assertTrue("Should be 0 declarations in project", project
106: .findDeclarationsByPrefix("").size() == 0); // NOI18N
107: assertTrue(
108: "Should be 0 declarations in global namespace",
109: project.getGlobalNamespace().getDeclarations().size() == 0); // NOI18N
110: assertTrue(
111: "Should be 0 definitions in global namespace",
112: project.getGlobalNamespace().getDefinitions().size() == 0); // NOI18N
113: assertTrue("Should be 0 namespaces in global namespace",
114: project.getGlobalNamespace().getNestedNamespaces()
115: .size() == 0); // NOI18N
116: }
117:
118: private String getClassName(Class cls) {
119: String s = cls.getName();
120: return s.substring(s.lastIndexOf('.') + 1);
121: }
122:
123: private void checkFriend() {
124: ProjectBase project = getProject();
125: assertNotNull("Project must be valid", project); // NOI18N
126: CsmClass clsB = (CsmClass) project.findClassifier("B"); // NOI18N
127: assertNotNull("Class B not found", clsB); // NOI18N
128: Collection<CsmFriend> friends = clsB.getFriends();
129: assertTrue("Should be 5 friends in class B",
130: friends.size() == 5); // NOI18N
131: CsmFriendClass friendA2 = null;
132: CsmFriendFunction friendMoo2 = null;
133: CsmFriendFunction friendMoo = null;
134: CsmFriendFunction friendSoo = null;
135: CsmFriendFunction friendSoo2 = null;
136: for (CsmFriend friend : friends) {
137: if ("A2".equals(friend.getName().toString())) { // NOI18N
138: friendA2 = (CsmFriendClass) friend;
139: } else if ("moo2".equals(friend.getName().toString())) { // NOI18N
140: friendMoo2 = (CsmFriendFunction) friend;
141: } else if ("moo".equals(friend.getName().toString())) { // NOI18N
142: friendMoo = (CsmFriendFunction) friend;
143: } else if ("soo".equals(friend.getName().toString())) { // NOI18N
144: friendSoo = (CsmFriendFunction) friend;
145: } else if ("soo2".equals(friend.getName().toString())) { // NOI18N
146: friendSoo2 = (CsmFriendFunction) friend;
147: }
148: }
149: assertNotNull("Friend class declaration A2 not found", friendA2); // NOI18N
150: assertNotNull("Friend method declaration moo2 not found",
151: friendMoo2); // NOI18N
152: assertNotNull("Friend method declaration moo not found",
153: friendMoo); // NOI18N
154: assertNotNull("Friend method declaration soo not found",
155: friendSoo); // NOI18N
156: assertNotNull("Friend method declaration soo2 not found",
157: friendSoo2); // NOI18N
158:
159: CsmClass clsA2 = friendA2.getReferencedClass();
160: assertNotNull("Referenced class A2 for friend not found", clsA2); // NOI18N
161: CsmFunction funMoo2 = friendMoo2.getReferencedFunction();
162: assertNotNull("Referenced function moo2 for friend not found",
163: funMoo2); // NOI18N
164: CsmFunction funMoo = friendMoo.getReferencedFunction();
165: assertNotNull("Referenced function moo for friend not found",
166: funMoo); // NOI18N
167: CsmFunction funSoo = friendSoo.getReferencedFunction();
168: assertNotNull("Referenced function soo for friend not found",
169: funSoo); // NOI18N
170: CsmFunction funSoo2 = friendSoo2.getReferencedFunction();
171: assertNotNull("Referenced function soo2 for friend not found",
172: funSoo); // NOI18N
173:
174: Collection<CsmFriend> list = FriendResolverImpl.getDefault()
175: .findFriends(clsA2);
176: assertTrue("Should be 1 friend declaration for class A2", list
177: .size() == 1); // NOI18N
178: assertTrue(
179: "Friend declaration for class A2 has wrong instance",
180: list.iterator().next() == friendA2); // NOI18N
181: list = FriendResolverImpl.getDefault().findFriends(funMoo2);
182: assertTrue("Should be 1 friend declaration for function moo2",
183: list.size() == 1); // NOI18N
184: assertTrue(
185: "Friend declaration for function moo2 has wrong instance",
186: list.iterator().next() == friendMoo2); // NOI18N
187: list = FriendResolverImpl.getDefault().findFriends(funMoo);
188: assertTrue("Should be 1 friend declaration for function moo",
189: list.size() == 1); // NOI18N
190: assertTrue(
191: "Friend declaration for function moo has wrong instance",
192: list.iterator().next() == friendMoo); // NOI18N
193: list = FriendResolverImpl.getDefault().findFriends(funSoo);
194: assertTrue("Should be 1 friend declaration for function moo",
195: list.size() == 1); // NOI18N
196: assertTrue(
197: "Friend declaration for function soo has wrong instance",
198: list.iterator().next() == friendSoo);
199:
200: Collection<CsmOffsetableDeclaration> declarations = project
201: .findDeclarationsByPrefix("");
202: Set<String> set = new HashSet<String>();
203: for (CsmOffsetableDeclaration decl : declarations) {
204: String uName = decl.getUniqueName().toString();
205: System.out.println(uName + " \t"
206: + getClassName(decl.getClass())); // NOI18N
207: if ("F:moo2(int)".equals(uName)) {
208: assertTrue("moo2(int) is not friend B",
209: FriendResolverImpl.getDefault().isFriend(decl,
210: clsB)); // NOI18N
211: } else if ("f:moo(int)".equals(uName)) { // NOI18N
212: assertTrue("moo(int) is not friend B",
213: FriendResolverImpl.getDefault().isFriend(decl,
214: clsB)); // NOI18N
215: } else if ("S:S2".equals(uName)) { // NOI18N
216: assertFalse("S2 is friend B", FriendResolverImpl
217: .getDefault().isFriend(decl, clsB)); // NOI18N
218: } else if ("F:S2::soo()".equals(uName)) { // NOI18N
219: assertTrue("S2::soo() is not friend B",
220: FriendResolverImpl.getDefault().isFriend(decl,
221: clsB)); // NOI18N
222: } else if ("f:S2::soo2()".equals(uName)) { // NOI18N
223: assertTrue("S2::soo2() is not friend B",
224: FriendResolverImpl.getDefault().isFriend(decl,
225: clsB)); // NOI18N
226: } else if ("F:S2::soo3()".equals(uName)) { // NOI18N
227: assertTrue("S2::soo3() is not friend B",
228: FriendResolverImpl.getDefault().isFriend(decl,
229: clsB)); // NOI18N
230: } else if ("C:A2".equals(uName)) { // NOI18N
231: assertTrue("A2 is not friend B", FriendResolverImpl
232: .getDefault().isFriend(decl, clsB)); // NOI18N
233: } else if ("F:A2::foo()".equals(uName)) { // NOI18N
234: assertTrue("A2::foo() is not friend B",
235: FriendResolverImpl.getDefault().isFriend(decl,
236: clsB)); // NOI18N
237: } else if ("f:A2::foo()".equals(uName)) { // NOI18N
238: assertTrue("A2::foo() is not friend B",
239: FriendResolverImpl.getDefault().isFriend(decl,
240: clsB)); // NOI18N
241: } else if ("F:ccStyle()".equals(uName)) { // NOI18N
242: assertFalse("ccStyle() is friend B", FriendResolverImpl
243: .getDefault().isFriend(decl, clsB)); // NOI18N
244: } else if ("C:B".equals(uName)) { // NOI18N
245: assertFalse("B is friend B", FriendResolverImpl
246: .getDefault().isFriend(decl, clsB)); // NOI18N
247: } else if ("t:B::xxx".equals(uName)) { // NOI18N
248: assertFalse("B::xxx is friend B", FriendResolverImpl
249: .getDefault().isFriend(decl, clsB)); // NOI18N
250: } else if ("r:B::A2".equals(uName)) { // NOI18N
251: assertFalse("B::A2 is friend B", FriendResolverImpl
252: .getDefault().isFriend(decl, clsB)); // NOI18N
253: } else if ("F:B::boo()".equals(uName)) { // NOI18N
254: assertFalse("B::boo() is friend B", FriendResolverImpl
255: .getDefault().isFriend(decl, clsB)); // NOI18N
256: } else if ("f:moo2(int)".equals(uName)) { // NOI18N
257: assertTrue("moo2(int) is not friend B",
258: FriendResolverImpl.getDefault().isFriend(decl,
259: clsB)); // NOI18N
260: } else if ("F:moo(int)".equals(uName)) { // NOI18N
261: assertTrue("moo(int) is friend B", FriendResolverImpl
262: .getDefault().isFriend(decl, clsB)); // NOI18N
263: } else if ("f:S2::soo()".equals(uName)) { // NOI18N
264: assertTrue("S2::soo() is not friend B",
265: FriendResolverImpl.getDefault().isFriend(decl,
266: clsB)); // NOI18N
267: } else if ("F:S2::soo2()".equals(uName)) { // NOI18N
268: assertTrue("S2::soo2() is not friend B",
269: FriendResolverImpl.getDefault().isFriend(decl,
270: clsB)); // NOI18N
271: } else if ("f:ccStyle(int)".equals(uName)) { // NOI18N
272: assertFalse("ccStyle(int) is friend B",
273: FriendResolverImpl.getDefault().isFriend(decl,
274: clsB)); // NOI18N
275: } else {
276: assertTrue("Inexpected declaration " + uName, false); // NOI18N
277: }
278: assertFalse("Duplicated declaration ", set.contains(uName)); // NOI18N
279: set.add(uName);
280: }
281: assertTrue("Not all declaration found in project",
282: set.size() == 18); // NOI18N
283: }
284: /*
285: int moo2(int); //F:moo2(int) FunctionImpl
286: int moo(int){return 0;} //f:moo(int) FunctionDDImpl
287: struct S2 { //S:S2 ClassImpl
288: int soo(); //F:S2::soo() MethodImpl
289: int soo2(){return 0;} }; //f:S2::soo2() MethodDDImpl
290: class A2{ //C:A2 ClassImpl
291: int foo(); }; //F:A2::foo() MethodImpl
292: int A2::foo(){ return 0; } //f:A2::foo() FunctionDefinitionImpl
293: int ccStyle(); //F:ccStyle() FunctionImpl
294: class B{ //C:B ClassImpl
295: typedef int xxx; //t:B::xxx ClassImpl$MemberTypedef
296: friend class A2; //r:B::A2 FriendClassImpl
297: int boo(); //F:B::boo() MethodImpl
298: friend int moo2(int) { return 0; }; //f:moo2(int) FriendFunctionDDImpl
299: friend int moo(int); //F:moo(int) FriendFunctionImpl
300: friend int S2::soo(){ return 0; } //f:S2::soo() FriendFunctionDefinitionImpl
301: friend int S2::soo2(); }; //F:S2::soo2() FriendFunctionImplEx
302: int ccStyle(int){ return 0; } //f:ccStyle(int) FunctionDDImpl
303: */
304: }
|