KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > ForEachTest1


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.jmi.javamodel.codegen;
21
22 import java.util.Collections JavaDoc;
23 import junit.textui.TestRunner;
24 import org.netbeans.jmi.javamodel.Expression;
25 import org.netbeans.jmi.javamodel.ForEachStatement;
26
27 import org.netbeans.jmi.javamodel.JavaClass;
28 import org.netbeans.jmi.javamodel.JavaModelPackage;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.jmi.javamodel.Method;
32 import org.netbeans.jmi.javamodel.MethodInvocation;
33 import org.netbeans.jmi.javamodel.MultipartId;
34 import org.netbeans.jmi.javamodel.Parameter;
35 import org.netbeans.jmi.javamodel.Statement;
36 import org.netbeans.jmi.javamodel.StatementBlock;
37 import org.netbeans.jmi.javamodel.TypeReference;
38 import org.openide.filesystems.AbstractFileSystem.List;
39 import org.openide.filesystems.FileStateInvalidException;
40
41 /**
42  *
43  * @author Pavel Flaska
44  */

45 public class ForEachTest1 extends NbTestCase {
46
47     /** Creates a new instance of ForEachTest1 */
48     public ForEachTest1() {
49         super("ForEachTest1");
50     }
51
52     public static NbTestSuite suite() {
53         NbTestSuite suite = new NbTestSuite(ForEachTest1.class);
54         return suite;
55     }
56     
57     Method method;
58     JavaModelPackage pkg;
59     
60     protected void setUp() {
61         JavaClass clazz = Utility.findClass("org.netbeans.test.codegen.ForEachTest1");
62         pkg = (JavaModelPackage) clazz.refImmediatePackage();
63         method = (Method) clazz.getContents().iterator().next();
64     }
65
66     public void testForCreation() throws java.io.IOException JavaDoc, FileStateInvalidException {
67         boolean fail = true;
68         Utility.beginTrans(true);
69         try {
70             // parameter
71
TypeReference typeRef = pkg.getMultipartId().createMultipartId("String", null, null);
72             Parameter parameter = pkg.getParameter().createParameter("s", null, false, typeRef, 0, false);
73             // iterable
74
Expression iterable = pkg.getMultipartId().createMultipartId("c", null, null);
75             // method invocation
76
Expression par = pkg.getMultipartId().createMultipartId("s", null, null);
77             MultipartId parent = pkg.getMultipartId().createMultipartId("System.out", null, null);
78             MethodInvocation invocation = pkg.getMethodInvocation().createMethodInvocation("println", Collections.singletonList(par), parent, false);
79             Statement statement = pkg.getExpressionStatement().createExpressionStatement(invocation);
80             // for body
81
StatementBlock forBody = pkg.getStatementBlock().createStatementBlock();
82             forBody.getStatements().add(statement);
83             // for (String s : c) {
84
// System.out.println(s);
85
// }
86
ForEachStatement fes = pkg.getForEachStatement().createForEachStatement(forBody, parameter, iterable);
87             // add to method
88
method.getBody().getStatements().add(fes);
89             fail = false;
90         } finally {
91             Utility.endTrans(fail);
92         }
93         assertFile("File is not correctly generated.",
94             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ForEachTest1.java"),
95             getGoldenFile("testForCreation_ForEachTest1.pass"),
96             getWorkDir()
97         );
98     }
99     /**
100      * @param args the command line arguments
101      */

102     public static void main(String JavaDoc[] args) {
103         TestRunner.run(suite());
104     }
105 }
106
Popular Tags