1 19 20 package org.netbeans.modules.junit.output; 21 22 import java.io.File ; 23 import java.util.Collection ; 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 38 final class OutputUtils { 39 40 private OutputUtils() { 41 } 42 43 45 static void openCallstackFrame(Node node, 46 String frameInfo) { 47 Report report = getTestsuiteNode(node).getReport(); 48 Collection <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 64 private static TestsuiteNode getTestsuiteNode(Node node) { 65 while (!(node instanceof TestsuiteNode)) { 66 node = node.getParentNode(); 67 } 68 return (TestsuiteNode) node; 69 } 70 71 77 private static FileObject getFile(final String callstackLine, 78 final int[] lineNumStorage, 79 final ClassPath classPath) { 80 81 82 int bracketIndex = callstackLine.indexOf('('); 83 String beforeBrackets = (bracketIndex == -1) 84 ? callstackLine 85 : callstackLine.substring(0, bracketIndex) 86 .trim(); 87 String inBrackets = (bracketIndex == -1) 88 ? (String ) null 89 : callstackLine.substring( 90 bracketIndex + 1, 91 callstackLine.lastIndexOf(')')); 92 93 94 int lastDotIndex = beforeBrackets.lastIndexOf('.'); 95 String clsName = beforeBrackets.substring(0, lastDotIndex); 96 String methodName = beforeBrackets.substring(lastDotIndex + 1); 97 98 99 String fileName = null; 100 int lineNum = -1; 101 if (inBrackets != null) { 102 if (RegexpUtils.getInstance().getLocationInFilePattern() 104 .matcher(inBrackets).matches()) { 105 int ddotIndex = inBrackets.lastIndexOf(':'); 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 ex) { 117 118 assert false; 119 } 120 } 121 } 122 } 123 124 125 FileObject file; 126 String thePath; 127 128 132 String clsNameSlash = clsName.replace('.', '/'); 133 String slashName, ending; 134 int lastSlashIndex; 135 136 if (fileName == null) { 137 lastSlashIndex = clsNameSlash.length(); 138 slashName = clsNameSlash; 139 ending = ".java"; } 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 161 if (file == null) { 162 lineNum = -1; 163 } 164 lineNumStorage[0] = lineNum; 165 return file; 166 } 167 168 } 169 | Popular Tags |