KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > indent > AnnIndentTest


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 package org.netbeans.jmi.javamodel.codegen.indent;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.Annotation;
26 import org.netbeans.jmi.javamodel.JavaClass;
27 import org.netbeans.jmi.javamodel.JavaModelPackage;
28 import org.netbeans.jmi.javamodel.Method;
29 import org.netbeans.jmi.javamodel.MultipartId;
30 import org.netbeans.jmi.javamodel.codegen.Utility;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.junit.NbTestSuite;
33 import org.openide.filesystems.FileStateInvalidException;
34
35 /**
36  * Tests indentation of newly generated elements.
37  *
38  * @author Pavel Flaska
39  */

40 public class AnnIndentTest extends NbTestCase {
41     
42     JavaClass clazz;
43     JavaModelPackage pkg;
44     
45     /** Creates a new instance of AnnIndentTest */
46     public AnnIndentTest() {
47         super("AnnIndentTest");
48     }
49     
50     public static NbTestSuite suite() {
51         NbTestSuite suite = new NbTestSuite(AnnIndentTest.class);
52         return suite;
53     }
54     
55     protected void setUp() {
56         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.AnnIndentClass");
57         pkg = (JavaModelPackage) clazz.refImmediatePackage();
58     }
59     
60     public void testAddAnnotations() throws java.io.IOException JavaDoc, FileStateInvalidException {
61         boolean fail = true;
62         Utility.beginTrans(true);
63         try {
64             Method m = (Method) clazz.getContents().get(1);
65             MultipartId type1 = pkg.getMultipartId().createMultipartId("Deprecated()", null, Collections.EMPTY_LIST);
66             MultipartId type2 = pkg.getMultipartId().createMultipartId("Deprecated2()", null, Collections.EMPTY_LIST);
67             Annotation ann1 = pkg.getAnnotation().createAnnotation(type1, Collections.EMPTY_LIST);
68             Annotation ann2 = pkg.getAnnotation().createAnnotation(type2, Collections.EMPTY_LIST);
69             m.getAnnotations().add(ann1);
70             m.getAnnotations().add(ann2);
71             fail = false;
72         }
73         finally {
74             Utility.endTrans(fail);
75         }
76         assertFile("File is not correctly generated.",
77             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/AnnIndentClass.java"),
78             getGoldenFile("testAddAnnotations_AnnIndentTest.pass"),
79             getWorkDir()
80         );
81     }
82     
83     public void testAddAnnotationsToClass() throws java.io.IOException JavaDoc, FileStateInvalidException {
84         boolean fail = true;
85         Utility.beginTrans(true);
86         try {
87             MultipartId type1 = pkg.getMultipartId().createMultipartId("Deprecated()", null, Collections.EMPTY_LIST);
88             MultipartId type2 = pkg.getMultipartId().createMultipartId("Deprecated2()", null, Collections.EMPTY_LIST);
89             Annotation ann1 = pkg.getAnnotation().createAnnotation(type1, Collections.EMPTY_LIST);
90             Annotation ann2 = pkg.getAnnotation().createAnnotation(type2, Collections.EMPTY_LIST);
91             clazz.getAnnotations().add(ann1);
92             clazz.getAnnotations().add(ann2);
93             fail = false;
94         }
95         finally {
96             Utility.endTrans(fail);
97         }
98         assertFile("File is not correctly generated.",
99             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/AnnIndentClass.java"),
100             getGoldenFile("testAddAnnotationsToClass_AnnIndentTest.pass"),
101             getWorkDir()
102         );
103     }
104     
105     public void testAddAnnotationWithMethod() throws java.io.IOException JavaDoc, FileStateInvalidException {
106         boolean fail = true;
107         Utility.beginTrans(true);
108         try {
109             MultipartId type1 = pkg.getMultipartId().createMultipartId("Deprecated()", null, Collections.EMPTY_LIST);
110             MultipartId type2 = pkg.getMultipartId().createMultipartId("Deprecated2()", null, Collections.EMPTY_LIST);
111             Annotation ann1 = pkg.getAnnotation().createAnnotation(type1, Collections.EMPTY_LIST);
112             Annotation ann2 = pkg.getAnnotation().createAnnotation(type2, Collections.EMPTY_LIST);
113             List JavaDoc annList = new ArrayList JavaDoc();
114             annList.add(ann1);
115             annList.add(ann2);
116             Method m = pkg.getMethod().createMethod("newMethod", annList, Modifier.PUBLIC, null, null, null, null, null, null, null,
117                     pkg.getMultipartId().createMultipartId("void", null, null), 0);
118             clazz.getContents().add(m);
119             fail = false;
120         }
121         finally {
122             Utility.endTrans(fail);
123         }
124         assertFile("File is not correctly generated.",
125             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/AnnIndentClass.java"),
126             getGoldenFile("testAddAnnotationWithMethod_AnnIndentTest.pass"),
127             getWorkDir()
128         );
129     }
130     
131     public void testAddAnnotationWithClass() throws java.io.IOException JavaDoc, FileStateInvalidException {
132         boolean fail = true;
133         Utility.beginTrans(true);
134         try {
135             MultipartId type1 = pkg.getMultipartId().createMultipartId("Deprecated()", null, Collections.EMPTY_LIST);
136             MultipartId type2 = pkg.getMultipartId().createMultipartId("Deprecated2()", null, Collections.EMPTY_LIST);
137             Annotation ann1 = pkg.getAnnotation().createAnnotation(type1, Collections.EMPTY_LIST);
138             Annotation ann2 = pkg.getAnnotation().createAnnotation(type2, Collections.EMPTY_LIST);
139             List JavaDoc annList = new ArrayList JavaDoc();
140             annList.add(ann1);
141             annList.add(ann2);
142             JavaClass javaClass = pkg.getJavaClass().createJavaClass("SecondTop", annList, 0, null, null, Collections.EMPTY_LIST, null, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
143             clazz.getResource().getClassifiers().add(javaClass);
144             fail = false;
145         }
146         finally {
147             Utility.endTrans(fail);
148         }
149         assertFile("File is not correctly generated.",
150             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/AnnIndentClass.java"),
151             getGoldenFile("testAddAnnotationWithClass_AnnIndentTest.pass"),
152             getWorkDir()
153         );
154     }
155
156 }
157
Popular Tags