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.uml.core.roundtripframework.requestprocessors.javarpcomponent;
043:
044: import org.netbeans.modules.uml.core.AbstractUMLTestCase;
045: import org.netbeans.modules.uml.core.metamodel.core.constructs.IClass;
046: import org.netbeans.modules.uml.core.metamodel.core.constructs.IDataType;
047: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPackage;
048: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IAttribute;
049: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
050: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IOperation;
051: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IParameter;
052: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
053: import org.netbeans.modules.uml.core.roundtripframework.ChangeRequest;
054: import org.netbeans.modules.uml.core.roundtripframework.IChangeRequest;
055: import org.netbeans.modules.uml.core.roundtripframework.RequestDetailKind;
056: import org.netbeans.modules.uml.core.roundtripframework.requestprocessors.javarpcomponent.JavaRequestProcessor;
057:
058: public class JavaRequestProcessorTestCase extends AbstractUMLTestCase {
059:
060: /**
061: * EnsureUniqueAttributeTestCase
062: */
063: public void testEnsureUniqueAttribute() {
064: // Create attribute, class and add attribute to the class
065: IClass cls = createClass("TestClass");
066: IAttribute attr = cls.createAttribute("int", "TstAttr");
067: cls.addAttribute(attr);
068:
069: // Create another attribute and add it to the class
070: IAttribute newAttr = cls.createAttribute("int", "TstAttr2");
071: cls.addAttribute(newAttr);
072:
073: // Check whether the attribute names are unique in the class
074: JavaRequestProcessor javaReqProc = new JavaRequestProcessor();
075: boolean uniqAttr = javaReqProc.ensureUniqueAttribute(cls,
076: "TstAttr");
077: assertFalse(uniqAttr);
078: uniqAttr = javaReqProc.ensureUniqueAttribute(cls, "TstAttr1");
079: assertTrue(uniqAttr);
080: uniqAttr = javaReqProc.ensureUniqueAttribute(cls, "TstAttr2");
081: assertFalse(uniqAttr);
082: }
083:
084: /**
085: * PreValidationCheckTestCase
086: * This tests that no roundtrip processing should happen for data type request objects.
087: */
088: public void testPreValidationCheck() {
089: // Create a data type
090: IDataType dataType = createType("DataType");
091: dataType.setName("TestDataType");
092:
093: // Create change request and request validator for the data type.
094: IChangeRequest chngReq = new ChangeRequest();
095: chngReq.setState(0);
096: chngReq.setAfter(dataType);
097: IRequestValidator pRequest = new RequestValidator(chngReq);
098:
099: // Do the pre validation check for the data type.
100: JavaRequestProcessor javaReqProc = new JavaRequestProcessor();
101: boolean validateChk = javaReqProc.preValidationCheck(pRequest);
102: assertFalse(validateChk);
103:
104: // Create attribute, class and add the attribute to the class.
105: IClass newCls = createClass("TestClass");
106: IAttribute attr = newCls.createAttribute("int", "NewAttr");
107: newCls.addAttribute(attr);
108:
109: // Create change request and request validator for the data type.
110: chngReq.setState(0);
111: chngReq.setAfter(attr);
112: pRequest = new RequestValidator(chngReq);
113:
114: // Do the pre validation check for the data type.
115: validateChk = javaReqProc.preValidationCheck(pRequest);
116: assertTrue(validateChk);
117: }
118:
119: /**
120: * GetClassOfRequestTestCase
121: * This method tests whether the request object is a data type or not.
122: */
123: public void testGetClassOfRequest() {
124: // Create a data type
125: IDataType dataType = createType("DataType");
126: dataType.setName("TestDataType");
127:
128: // Create change request and request validator for the data type.
129: IChangeRequest chngReq = new ChangeRequest();
130: chngReq.setState(0);
131: chngReq.setAfter(dataType);
132: IRequestValidator requestVal = new RequestValidator(chngReq);
133:
134: // Do the pre validation check for the data type.
135: JavaRequestProcessor javaReqProc = new JavaRequestProcessor();
136: IClassifier reqCls = javaReqProc.getClassOfRequest(requestVal
137: .getRequest(), false);
138: assertTrue(reqCls instanceof IDataType);
139:
140: // Create a attribute, operation, classifier and add the attribute and operation to the class
141: IClass tstCls = createClass("TestClass");
142: IAttribute tstAttr = tstCls.createAttribute("int", "TestAttr");
143: tstCls.addAttribute(tstAttr);
144: IOperation tstOper = tstCls.createOperation("int", "TestOper");
145: tstCls.addOperation(tstOper);
146:
147: // Create change request and request validator for the the classifier.
148: chngReq.setState(0);
149: chngReq.setAfter(tstAttr);
150: requestVal = new RequestValidator(chngReq);
151: reqCls = javaReqProc.getClassOfRequest(requestVal.getRequest(),
152: false);
153: assertTrue(reqCls instanceof IClass);
154: }
155:
156: /**
157: * CheckForInvalidNameTestCase
158: */
159: public void testCheckForInvalidName() {
160:
161: // Create a class and check invalid name for class
162: IClass tstCls = createClass("TestClass");
163: JavaRequestProcessor javaReqProc = new JavaRequestProcessor();
164: assertFalse(javaReqProc
165: .checkForInvalidName(tstCls, "TestClass"));
166: assertTrue(javaReqProc.checkForInvalidName(tstCls, "public"));
167: assertTrue(javaReqProc.checkForInvalidName(tstCls, "int"));
168: assertTrue(javaReqProc.checkForInvalidName(tstCls, "boolean"));
169:
170: // Create a an attribute and check invalid name for attribute
171: IAttribute tstAttr = tstCls.createAttribute("int", "TestAttr");
172: assertFalse(javaReqProc
173: .checkForInvalidName(tstAttr, "TestAttr"));
174: assertTrue(javaReqProc.checkForInvalidName(tstAttr, "private"));
175: // String should not be considered as data type
176: // assertTrue(javaReqProc.checkForInvalidName(tstAttr,"String"));
177:
178: // Create a an Operation and check invalid name for Operation
179: IOperation tstOper = tstCls.createOperation("int", "TestOper");
180: assertFalse(javaReqProc
181: .checkForInvalidName(tstOper, "TestOper"));
182: assertFalse(javaReqProc.checkForInvalidName(tstOper,
183: "operation"));
184: assertTrue(javaReqProc
185: .checkForInvalidName(tstOper, "protected"));
186: assertTrue(javaReqProc.checkForInvalidName(tstOper, "long"));
187: assertTrue(javaReqProc.checkForInvalidName(tstOper, "byte"));
188:
189: // Create a Package and check invalid name for Package
190: IPackage tstPack = createType("Package");
191: assertFalse(javaReqProc
192: .checkForInvalidName(tstPack, "TestPack"));
193: assertTrue(javaReqProc.checkForInvalidName(tstPack, "package"));
194: assertTrue(javaReqProc.checkForInvalidName(tstPack, "short"));
195: assertTrue(javaReqProc.checkForInvalidName(tstPack, "char"));
196:
197: // Create a Project and check invalid name for Project
198: IProject tstProject = createType("Project");
199: assertFalse(javaReqProc.checkForInvalidName(tstProject,
200: "TestProject"));
201: assertTrue(javaReqProc.checkForInvalidName(tstProject,
202: "TestProject~"));
203:
204: // Create a Parameter and check invalid name for Parameter
205: IParameter tstParam = createType("Parameter");
206: assertFalse(javaReqProc.checkForInvalidName(tstParam,
207: "TestParam"));
208: assertTrue(javaReqProc.checkForInvalidName(tstParam, "default"));
209: assertTrue(javaReqProc.checkForInvalidName(tstParam, "float"));
210: assertTrue(javaReqProc.checkForInvalidName(tstParam,
211: "synchronized"));
212: assertTrue(javaReqProc.checkForInvalidName(tstParam, "double"));
213: assertTrue(javaReqProc.checkForInvalidName(tstParam, "void"));
214:
215: }
216:
217: /**
218: * PostValidationCheckTestCase
219: * This tests that no roundtrip processing should happen for unnamed Objects.
220: */
221:
222: public void testPostValidationCheck() {
223: // Creating unnamed class
224: IClass cls = createClass("");
225:
226: // Create change request for the data type.
227: IChangeRequest chngReq = new ChangeRequest();
228: chngReq.setState(0);
229: chngReq.setBefore(cls);
230: chngReq.setAfter(cls);
231: chngReq
232: .setRequestDetailType(RequestDetailKind.RDT_FEATURE_MOVED);
233:
234: // Doing the post validation check
235: JavaRequestProcessor javaReqProc = new JavaRequestProcessor();
236: assertFalse(javaReqProc.postValidationCheck(chngReq));
237: }
238: }
|