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 InterfaceDefTest extends BaseCase {
052:
053: public void testInterface() throws Exception {
054: PhpModel model = getModel(ResourceMarker.INTERFACE);
055: model.sync();
056: model.readLock();
057: try {
058: List<InterfaceDefinition> list = model
059: .getStatements(InterfaceDefinition.class);
060:
061: assert list.size() > 0 : "Expected at least one interface defenition";
062: InterfaceDefinition def = list.get(0);
063:
064: assert def.getSuperInterfaces().size() == 0 : "Unexpected "
065: + def.getSuperInterfaces().size()
066: + " super interfaces found";
067:
068: InterfaceBody body = def.getBody();
069: assert body != null : "Expected not null interface body";
070:
071: List<InterfaceStatement> interfaceStats = body
072: .getStatements();
073: assert interfaceStats.size() > 0 : "Expected not empty interface"
074: + " statements list";
075:
076: InterfaceStatement statement = interfaceStats.get(0);
077: assert statement != null;
078: assert statement.getElementType().equals(
079: ConstDeclaration.class) : "Expected to find first interface statement as const declaration,"
080: + "but found : " + statement.getElementType();
081:
082: statement = interfaceStats.get(1);
083: assert statement != null;
084: assert statement.getElementType().equals(
085: ClassFunctionDeclaration.class) : "Expected to find second interface statemant as function "
086: + "declaration, but found :"
087: + statement.getElementType();
088: } finally {
089: model.readUnlock();
090: }
091: }
092:
093: public void testFunctionDecl() throws Exception {
094: PhpModel model = getModel(ResourceMarker.INTERFACE);
095: model.sync();
096: model.readLock();
097: try {
098: List<InterfaceDefinition> list = model
099: .getStatements(InterfaceDefinition.class);
100:
101: InterfaceDefinition def = list.get(0);
102: List<InterfaceStatement> interfaceStats = def.getBody()
103: .getStatements();
104:
105: ClassFunctionDeclaration decl = (ClassFunctionDeclaration) interfaceStats
106: .get(1);
107:
108: assert decl.getName().equals("op") : "Expected to find method"
109: + " with name 'op' , but found :" + decl.getName();
110:
111: List<Modifier> modifiers = decl.getModifiers();
112: assert modifiers.size() == 1 : "Expected to find exactly one modifier,"
113: + "but found :" + modifiers.size();
114: Modifier modifier = modifiers.get(0);
115: assert modifier.equals(Modifier.PUBLIC) : "Expected to find"
116: + " 'public' modifier, but found :" + modifier;
117:
118: FormalParameterList parameters = decl.getParamaterList();
119: assert parameters != null;
120: assert parameters.getElementType().equals(
121: FormalParameterList.class) : "Expected to find formal parameter list type for method"
122: + decl.getName()
123: + " , but found :"
124: + parameters.getElementType();
125:
126: List<FormalParameter> params = parameters.getParameters();
127: assert params.size() > 0 : "Expected to find at least one paramenter in "
128: + "parameter list ";
129: assert params.get(0).getElementType().equals(
130: FormalParameter.class) : "Expected to find formal parameter type for method parameter,"
131: + "but found : " + params.get(0).getElementType();
132:
133: assert params.get(0).getName().equals("$arg") : "Expected to "
134: + "find parameter with name '$arg' , but found : "
135: + params.get(0).getName();
136:
137: assert params.get(0).getDefaultValue() == null;
138: } finally {
139: model.readUnlock();
140: }
141: }
142:
143: public void testConstDecl() throws Exception {
144: PhpModel model = getModel(ResourceMarker.INTERFACE);
145: model.sync();
146: model.readLock();
147: try {
148: List<InterfaceDefinition> list = model
149: .getStatements(InterfaceDefinition.class);
150:
151: InterfaceDefinition def = list.get(0);
152: List<InterfaceStatement> interfaceStats = def.getBody()
153: .getStatements();
154: ConstDeclaration decl = (ConstDeclaration) interfaceStats
155: .get(0);
156:
157: List<ClassConst> cnst = decl.getDeclaredConstants();
158:
159: assert cnst.size() > 0 : "Expected to find at least one declared constants";
160: ClassConst conzt = cnst.get(0);
161: assert conzt != null;
162:
163: assert conzt.getElementType().equals(ClassConst.class) : "Expected to find class const in constant declaration , "
164: + "but found: " + conzt.getElementType();
165:
166: Expression expr = conzt.getValue();
167: assert expr != null : "Expected to find not null expression as const value";
168: assert expr.getElementType().equals(Literal.class) : "Expected"
169: + " to find literal as expression in constant , but found :"
170: + expr.getElementType();
171:
172: assert conzt.getName().equals("CONST") : "Expected name CONST "
173: + "as constant name in interface constant declaration,"
174: + "but found : " + conzt.getName();
175: } finally {
176: model.readUnlock();
177: }
178: }
179:
180: public void testExtendedInterface() throws Exception {
181: PhpModel model = getModel(ResourceMarker.INTERFACE);
182: model.sync();
183: model.readLock();
184: try {
185: List<InterfaceDefinition> list = model
186: .getStatements(InterfaceDefinition.class);
187:
188: assert list.size() > 1 : "Expected at least two interfaces";
189:
190: InterfaceDefinition def = list.get(1);
191:
192: List<Reference<InterfaceDefinition>> interfaces = def
193: .getSuperInterfaces();
194: assert interfaces.size() == 2 : "Expected to find two super interfaces,"
195: + " but found :" + interfaces.size();
196: Reference<InterfaceDefinition> reference = interfaces
197: .get(0);
198:
199: assert reference.getIdentifier().equals("InterfaceName") : "Expected to find 'InterfaceName' identifier as first "
200: + "super interface identifier, but found :"
201: + reference.getIdentifier();
202:
203: reference = interfaces.get(1);
204: assert reference.getIdentifier().equals("One") : "Expected to find 'One' identifier as first "
205: + "super interface identifier, but found :"
206: + reference.getIdentifier();
207:
208: assert def != null;
209: InterfaceBody body = def.getBody();
210:
211: assert body != null : "Expected not null interface body";
212:
213: List<InterfaceStatement> interfaceStats = body
214: .getStatements();
215: assert interfaceStats.size() > 0 : "Expected not empty interface"
216: + " statements list";
217:
218: InterfaceStatement statement = interfaceStats.get(0);
219: assert statement != null;
220: assert statement.getElementType().equals(
221: ClassFunctionDeclaration.class) : "Expected to find function declaration , but found :"
222: + statement.getElementType();
223:
224: ClassFunctionDeclaration decl = (ClassFunctionDeclaration) statement;
225: assert decl.getModifiers().size() == 0 : "Expected no modifiers for"
226: + " function declaration, but found "
227: + decl.getModifiers().size() + " modifiers";
228:
229: statement = interfaceStats.get(1);
230: assert statement != null;
231: assert statement.getElementType().equals(
232: ConstDeclaration.class) : "Expected to find constant declaration as second statement , but"
233: + " found :" + statement.getElementType();
234:
235: statement = interfaceStats.get(2);
236: assert statement != null;
237: assert statement.getElementType().equals(
238: ConstDeclaration.class) : "Expected to find constant declaration as third statement , but"
239: + " found :" + statement.getElementType();
240:
241: statement = interfaceStats.get(3);
242: assert statement != null;
243: assert statement.getElementType().equals(
244: ClassFunctionDeclaration.class) : "Expected to find function declaration as fourth interface statement,"
245: + " but found :" + statement.getElementType();
246:
247: decl = (ClassFunctionDeclaration) statement;
248: assert decl.getModifiers().size() == 1 : "Expected exactly one modifier for"
249: + " function declaration, but found "
250: + decl.getModifiers().size();
251: assert decl.getModifiers().get(0).equals(Modifier.PUBLIC) : "Expected to find 'public' modifier for method, but found :"
252: + decl.getModifiers().get(0);
253: } finally {
254: model.readUnlock();
255: }
256: }
257: }
|