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-2006 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: package org.netbeans.modules.php.model;
042:
043: import java.util.List;
044:
045: import org.netbeans.modules.php.model.resources.ResourceMarker;
046:
047: /**
048: * @author ads
049: *
050: */
051: public class StaticReferenceTest extends BaseCase {
052:
053: public void testStaticVar() throws Exception {
054: PhpModel model = getModel(ResourceMarker.STATIC_REF);
055: model.sync();
056: model.readLock();
057: try {
058: List<Statement> list = model.getStatements();
059: assert list.size() > 1 : "Expected to find at least 2 statements,"
060: + " found :" + list.size();
061: Statement statement = list.get(1);
062: assert statement instanceof ExpressionStatement;
063: ExpressionStatement expressionStatement = (ExpressionStatement) statement;
064: Expression expression = expressionStatement.getExpression();
065:
066: assert expression != null;
067: assert expression.getElementType().equals(Variable.class) : "Expected to find variable , but found :"
068: + expression.getElementType();
069:
070: Variable variable = (Variable) expression;
071: Reference<VariableAppearance> varRef = variable
072: .getAppearance();
073: assert varRef != null : "Expected to find not null reference";
074: assert varRef.getIdentifier().equals("Clazz::$var") : "Expected to "
075: + "find string 'Clazz::$var' as reference identifier,"
076: + "but found : " + varRef.getIdentifier();
077:
078: VariableAppearance appearance = varRef.get();
079: assert appearance != null : "Expected to find not null referenced element";
080:
081: assert appearance.getElementType().equals(Attribute.class);
082: Attribute attribute = (Attribute) appearance;
083: assert attribute.getName().equals("$var") : "Expected to find $var"
084: + " as resolved attibute name , but found :"
085: + attribute.getName();
086:
087: assert varRef instanceof ClassMemberReference : "Expect that accessed"
088: + " reference is ClassMemberReference";
089: ClassMemberReference<VariableAppearance> ref = (ClassMemberReference<VariableAppearance>) varRef;
090: assert ref.getObject() != null : "Expected to find not null container for"
091: + " reference";
092:
093: assert ref.getObject().getName().equals("Clazz") : "Expected to "
094: + "find 'Clazz' as name for referenced container , but found :"
095: + ref.getObject().getName();
096:
097: assert ref.getObjectName().equals("Clazz") : "Expected to "
098: + "find 'Clazz' as name for object in reference , but found :"
099: + ref.getObjectName();
100:
101: assert ref.getMemberName().equals("$var") : "Expected to find $var"
102: + " as referenced maember name, but found :"
103: + ref.getMemberName();
104: } finally {
105: model.readUnlock();
106: }
107: }
108:
109: public void testConst() throws Exception {
110: PhpModel model = getModel(ResourceMarker.STATIC_REF);
111: model.sync();
112: model.readLock();
113: try {
114: List<Statement> list = model.getStatements();
115: assert list.size() > 2 : "Expected to find at least 3 statements,"
116: + " found :" + list.size();
117: Statement statement = list.get(2);
118: assert statement instanceof ExpressionStatement;
119: ExpressionStatement expressionStatement = (ExpressionStatement) statement;
120: Expression expression = expressionStatement.getExpression();
121:
122: assert expression != null;
123: assert expression.getElementType().equals(Constant.class) : "Expected to find variable , but found :"
124: + expression.getElementType();
125:
126: Constant constant = (Constant) expression;
127:
128: assert constant.getSourceElement() == null : "Not expected to find "
129: + "reference to defined source element should be null";
130:
131: ClassMemberReference<SourceElement> constRef = constant
132: .getClassConstant();
133: assert constRef != null : "Expected to find not null reference";
134: assert constRef.getIdentifier().equals("Clazz::CONST") : "Expected to "
135: + "find string 'Clazz::CONST' as reference identifier,"
136: + "but found : " + constRef.getIdentifier();
137:
138: SourceElement constDecl = constRef.get();
139: assert constDecl != null : "Expected to find not null referenced element";
140:
141: assert constDecl.getElementType().equals(ClassConst.class);
142: ClassConst classConst = (ClassConst) constDecl;
143: assert classConst.getName().equals("CONST") : "Expected to find $var"
144: + " as resolved attibute name , but found :"
145: + classConst.getName();
146:
147: assert constRef.getObject() != null : "Expected to find not null container for"
148: + " reference";
149:
150: assert constRef.getObject().getName().equals("Clazz") : "Expected to "
151: + "find 'Clazz' as name for referenced container , but found :"
152: + constRef.getObject().getName();
153:
154: assert constRef.getObjectName().equals("Clazz") : "Expected to "
155: + "find 'Clazz' as name for object in reference , but found :"
156: + constRef.getObjectName();
157:
158: assert constRef.getMemberName().equals("CONST") : "Expected to find $var"
159: + " as referenced maember name, but found :"
160: + constRef.getMemberName();
161: } finally {
162: model.readUnlock();
163: }
164: }
165:
166: public void testMethod() throws Exception {
167: PhpModel model = getModel(ResourceMarker.STATIC_REF);
168: model.sync();
169: model.readLock();
170: try {
171: List<Statement> list = model.getStatements();
172: assert list.size() > 3 : "Expected to find at least 4 statements,"
173: + " found :" + list.size();
174: Statement statement = list.get(3);
175: assert statement instanceof ExpressionStatement;
176: ExpressionStatement expressionStatement = (ExpressionStatement) statement;
177: Expression expression = expressionStatement.getExpression();
178:
179: assert expression != null;
180: assert expression.getElementType().equals(
181: CallExpression.class) : "Expected to find call expression m but found :"
182: + expression.getElementType();
183: CallExpression callExpression = (CallExpression) expression;
184:
185: IdentifierExpression id = callExpression.getName();
186: assert id != null;
187: assert id.getElementType().equals(Constant.class) : "Expected to "
188: + "find constant as name identifier for call expression, "
189: + "but found :" + id.getElementType();
190:
191: Constant constant = (Constant) id;
192: Reference<SourceElement> sourceRef = constant
193: .getSourceElement();
194: assert sourceRef == null : "Unexpected reference to non-class member";
195:
196: ClassMemberReference<SourceElement> ref = constant
197: .getClassConstant();
198: assert ref != null : "Reference to class constant is null, but should not be";
199:
200: assert ref.getIdentifier().equals("Clazz::method") : "Expected "
201: + "identifier string in reference is 'Clazz::method', but found :"
202: + ref.getIdentifier();
203:
204: assert ref.getMemberName().equals("method") : "Expected to find "
205: + "'method' as member name in reference , but found :"
206: + ref.getMemberName();
207:
208: assert ref.getObjectName().equals("Clazz") : "Expected to find "
209: + "'Clazz' as owner class name in reference, but found :"
210: + ref.getObjectName();
211:
212: SourceElement resolved = ref.get();
213: assert resolved != null : "Unable to resolve class method name";
214:
215: assert resolved.getElementType().equals(
216: ClassFunctionDefinition.class) : "Expected to find ClassFunctionDefinition, but found :"
217: + resolved.getElementType();
218: ClassFunctionDefinition def = (ClassFunctionDefinition) resolved;
219:
220: assert def.getDeclaration().getName().equals("method") : "Expected "
221: + "'method' name in resolved element via reference, but found :"
222: + def.getDeclaration();
223:
224: ObjectDefinition objectDefinition = ref.getObject();
225: assert objectDefinition != null : "Expected to find not null owner "
226: + "object in reference";
227:
228: assert objectDefinition.getElementType().equals(
229: ClassDefinition.class) : "Expected to find ClassDefinition as element type for owner object,"
230: + "but found :" + objectDefinition.getElementType();
231:
232: ClassDefinition classDef = (ClassDefinition) objectDefinition;
233: assert classDef.getName().equals("Clazz") : "Expected to find 'Clazz'"
234: + " as name for owner object resolved via reference, but found :"
235: + classDef.getName();
236:
237: } finally {
238: model.readUnlock();
239: }
240: }
241:
242: }
|