KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > OutputUtils


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.modules.junit.output;
21
22 import java.io.File JavaDoc;
23 import java.util.Collection JavaDoc;
24 import org.netbeans.api.java.classpath.ClassPath;
25 import org.netbeans.api.project.FileOwnerQuery;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.SourceGroup;
28 import org.netbeans.modules.junit.wizards.Utils;
29 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.nodes.Node;
33
34 /**
35  *
36  * @author Marian Petras
37  */

38 final class OutputUtils {
39     
40     private OutputUtils() {
41     }
42     
43     /**
44      */

45     static void openCallstackFrame(Node node,
46                                    String JavaDoc frameInfo) {
47         Report report = getTestsuiteNode(node).getReport();
48         Collection JavaDoc<FileObject> srcRoots = report.classpathSourceRoots;
49         if ((srcRoots == null) || srcRoots.isEmpty()) {
50             return;
51         }
52
53         FileObject[] srcRootsArr = new FileObject[srcRoots.size()];
54         srcRoots.toArray(srcRootsArr);
55         ClassPath srcClassPath = ClassPathSupport.createClassPath(srcRootsArr);
56
57         final int[] lineNumStorage = new int[1];
58         FileObject file = getFile(frameInfo, lineNumStorage, srcClassPath);
59         Utils.openFile(file, lineNumStorage[0]);
60     }
61         
62     /**
63      */

64     private static TestsuiteNode getTestsuiteNode(Node node) {
65         while (!(node instanceof TestsuiteNode)) {
66             node = node.getParentNode();
67         }
68         return (TestsuiteNode) node;
69     }
70     
71     /**
72      * Returns FileObject corresponding to the given callstack line.
73      *
74      * @param callstackLine string representation of a callstack window
75      * returned by the JUnit framework
76      */

77     private static FileObject getFile(final String JavaDoc callstackLine,
78                                       final int[] lineNumStorage,
79                                       final ClassPath classPath) {
80
81         /* Get the part before brackets (if any brackets present): */
82         int bracketIndex = callstackLine.indexOf('(');
83         String JavaDoc beforeBrackets = (bracketIndex == -1)
84                                 ? callstackLine
85                                 : callstackLine.substring(0, bracketIndex)
86                                   .trim();
87         String JavaDoc inBrackets = (bracketIndex == -1)
88                             ? (String JavaDoc) null
89                             : callstackLine.substring(
90                                     bracketIndex + 1,
91                                     callstackLine.lastIndexOf(')'));
92
93         /* Get the method name and the class name: */
94         int lastDotIndex = beforeBrackets.lastIndexOf('.');
95         String JavaDoc clsName = beforeBrackets.substring(0, lastDotIndex);
96         String JavaDoc methodName = beforeBrackets.substring(lastDotIndex + 1);
97
98         /* Get the file name and line number: */
99         String JavaDoc fileName = null;
100         int lineNum = -1;
101         if (inBrackets != null) {
102             // RegexpUtils.getInstance() retns instance from ResultPanelTree
103
if (RegexpUtils.getInstance().getLocationInFilePattern()
104                     .matcher(inBrackets).matches()) {
105                 int ddotIndex = inBrackets.lastIndexOf(':'); //srch from end
106
if (ddotIndex == -1) {
107                     fileName = inBrackets;
108                 } else {
109                     fileName = inBrackets.substring(0, ddotIndex);
110                     try {
111                         lineNum = Integer.parseInt(
112                                        inBrackets.substring(ddotIndex + 1));
113                         if (lineNum <= 0) {
114                             lineNum = 1;
115                         }
116                     } catch (NumberFormatException JavaDoc ex) {
117                         /* should never happen as it passed the regexp */
118                         assert false;
119                     }
120                 }
121             }
122         }
123
124         /* Find the file: */
125         FileObject file;
126         String JavaDoc thePath;
127
128         //PENDING - Once 'thePath' is found for a given <clsName, fileName>
129
// pair, it could be cached for further uses
130
// (during a single AntSession).
131

132         String JavaDoc clsNameSlash = clsName.replace('.', '/');
133         String JavaDoc slashName, ending;
134         int lastSlashIndex;
135
136         if (fileName == null) {
137             lastSlashIndex = clsNameSlash.length();
138             slashName = clsNameSlash;
139             ending = ".java"; //NOI18N
140
} else {
141             lastSlashIndex = clsNameSlash.lastIndexOf('/');
142             slashName = (lastSlashIndex != -1)
143                         ? clsNameSlash.substring(0, lastSlashIndex)
144                         : clsNameSlash;
145             ending = '/' + fileName;
146         }
147         file = classPath.findResource(thePath = (slashName + ending));
148         while ((file == null) && (lastSlashIndex != -1)) {
149             slashName = slashName.substring(0, lastSlashIndex);
150             file = classPath.findResource(thePath = (slashName + ending));
151             if (file == null) {
152                 lastSlashIndex = slashName.lastIndexOf(
153                                                 '/', lastSlashIndex - 1);
154             }
155         }
156         if ((file == null) && (fileName != null)) {
157             file = classPath.findResource(thePath = fileName);
158         }
159
160         /* Return the file (or null if no matching file was found): */
161         if (file == null) {
162             lineNum = -1;
163         }
164         lineNumStorage[0] = lineNum;
165         return file;
166     }
167
168 }
169
Popular Tags