KickJava   Java API By Example, From Geeks To Geeks.

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


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;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import junit.textui.TestRunner;
28 import org.netbeans.jmi.javamodel.*;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.modules.javacore.jmiimpl.javamodel.MetadataElement;
32 import org.openide.filesystems.FileStateInvalidException;
33
34 /**
35  *
36  * @author Pavel Flaska
37  */

38 public class ArrayAttrValueTest extends NbTestCase {
39     
40     JavaClass clazz;
41     JavaModelPackage pkg;
42     
43     /** Creates a new instance of AnnotationTest */
44     public ArrayAttrValueTest() {
45         super("ArrayAttrValueTest");
46     }
47
48     public static NbTestSuite suite() {
49         NbTestSuite suite = new NbTestSuite(ArrayAttrValueTest.class);
50         return suite;
51     }
52
53     protected void setUp() {
54         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.ArrayAttrValueClass");
55         pkg = (JavaModelPackage) clazz.refImmediatePackage();
56     }
57     
58     public void testAddAnnotation() throws java.io.IOException JavaDoc, FileStateInvalidException {
59         boolean fail = true;
60         Utility.beginTrans(true);
61         try {
62             Method method = (Method) clazz.getContents().get(0);
63             Annotation ann1 = createNamedQueryAnn("\"foo1\"", "\"query1\"", true);
64             Annotation ann2 = createNamedQueryAnn("\"foo2\"", "\"query2\"", true);
65             Annotation ann = createNamedQueriesAnn(new Object JavaDoc[] { ann1, ann2 }, true);
66             method.getAnnotations().add(ann);
67             fail = false;
68        }
69         finally {
70             Utility.endTrans(fail);
71         }
72         assertFile("File is not correctly generated.",
73             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ArrayAttrValueClass.java"),
74             getGoldenFile("testAddAnnotation_ArrayAttrValueClass.pass"),
75             getWorkDir()
76         );
77     }
78     
79     public void testAddAnnotationWithoutAttrName() throws java.io.IOException JavaDoc, FileStateInvalidException {
80         boolean fail = true;
81         Utility.beginTrans(true);
82         try {
83             Method method = (Method) clazz.getContents().get(1);
84             Annotation ann1 = createNamedQueryAnn("\"foo1\"", "\"query1\"", false);
85             Annotation ann2 = createNamedQueryAnn("\"foo2\"", "\"query2\"", false);
86             Annotation ann = createNamedQueriesAnn(new Object JavaDoc[] { ann1, ann2 }, false);
87             method.getAnnotations().add(ann);
88             fail = false;
89        }
90         finally {
91             Utility.endTrans(fail);
92         }
93         assertFile("File is not correctly generated.",
94             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ArrayAttrValueClass.java"),
95             getGoldenFile("testAddAnnotationWithoutAttrName_ArrayAttrValueClass.pass"),
96             getWorkDir()
97         );
98     }
99     
100     /**
101      * @param args the command line arguments
102      */

103     public static void main(String JavaDoc[] args) {
104         TestRunner.run(suite());
105     }
106     
107     private Annotation createNamedQueryAnn(String JavaDoc attr1, String JavaDoc attr2, boolean useAttrName) {
108         MultipartIdClass multiProxy = pkg.getMultipartId();
109         Annotation ann = pkg.getAnnotation().createAnnotation();
110         ann.setTypeName(multiProxy.createMultipartId("NamedQuery", null, null));
111         List JavaDoc attrList = new ArrayList JavaDoc();
112         AttributeValueClass avProxy = pkg.getAttributeValue();
113         attrList.add(avProxy.createAttributeValue(useAttrName ? "name" : null, multiProxy.createMultipartId(attr1, null, null)));
114         attrList.add(avProxy.createAttributeValue(useAttrName ? "query" : null, multiProxy.createMultipartId(attr2, null, null)));
115         ann.getAttributeValues().addAll(attrList);
116         return ann;
117     }
118
119     private Annotation createNamedQueriesAnn(Object JavaDoc[] annotation, boolean useAttrName) {
120         MultipartIdClass multiProxy = pkg.getMultipartId();
121         Annotation ann = pkg.getAnnotation().createAnnotation();
122         ann.setTypeName(multiProxy.createMultipartId("NamedQueries", null, null));
123         List JavaDoc attrList = new ArrayList JavaDoc();
124         AttributeValueClass avProxy = pkg.getAttributeValue();
125         ArrayInitialization ai = pkg.getArrayInitialization().createArrayInitialization(Arrays.asList(annotation));
126         attrList.add(avProxy.createAttributeValue(useAttrName ? "value" : null, ai));
127         ann.getAttributeValues().addAll(attrList);
128         return ann;
129     }
130 }
131
Popular Tags