Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/sde/SDEDebugger.java
41162 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share.jdi.sde;2324import java.io.*;25import java.nio.channels.FileChannel;26import java.util.*;27import com.sun.jdi.*;28import com.sun.jdi.event.BreakpointEvent;29import com.sun.jdi.event.Event;30import com.sun.jdi.event.StepEvent;31import nsk.share.ClassFileFinder;32import nsk.share.TestBug;33import nsk.share.jdi.EventHandler;34import nsk.share.jdi.TestDebuggerType2;3536/*37* Class is used as base debugger for SDE tests.38*39* This class contains several methods called 'prepareDefaultPatchedClassFile_TypeXXX' which create40* class file with added SourceDebugExtension attribute and return debug information about created file.41* (this methods was moved in SDEDebugger class if similar generated class files were used in 2 or more tests)42*43*44*/45public class SDEDebugger extends TestDebuggerType2 {46protected static final int INIT_LINE = 32; // n.s.j.sde.TestClass1::<init>47protected static final int METHOD1_LINE = 43; // n.s.j.sde.TestClass1::sde_testMethod148protected static final int METHOD2_LINE = 54; // n.s.j.sde.TestClass1::sde_testMethod249public static String javaStratumName = "Java";5051// Set debug data for "Java" stratum for TestClass152public static List<DebugLocation> javaStratumLocations_TestClass1 = new ArrayList<DebugLocation>();5354public static String javaSourceName_TestClass1 = "TestClass1.java";5556public static String javaSourcePath_TestClass1 = "nsk" + File.separator + "share" + File.separator + "jdi"57+ File.separator + "sde" + File.separator + "TestClass1.java";5859static {60for (int i = 0; i < 8; i++) {61javaStratumLocations_TestClass1.add(new DebugLocation(javaSourceName_TestClass1, javaSourcePath_TestClass1,62"<init>", INIT_LINE + i, INIT_LINE + i));63javaStratumLocations_TestClass1.add(new DebugLocation(javaSourceName_TestClass1, javaSourcePath_TestClass1,64"sde_testMethod1", METHOD1_LINE + i, METHOD1_LINE + i));65javaStratumLocations_TestClass1.add(new DebugLocation(javaSourceName_TestClass1, javaSourcePath_TestClass1,66"sde_testMethod2", METHOD2_LINE + i, METHOD2_LINE + i));67}68}6970// insert debug information about "Java" stratum in the given Map<String,71// LocationsData>72static protected void addJavaLocations(Map<String, LocationsData> testStratumData, boolean setJavaStratumDefault) {73LocationsData locationsData = new LocationsData(javaStratumName);7475if (setJavaStratumDefault)76locationsData.isDefault = true;7778locationsData.paths.add(javaSourcePath_TestClass1);79locationsData.allLocations.addAll(javaStratumLocations_TestClass1);80locationsData.sourceLocations.put(javaSourceName_TestClass1, javaStratumLocations_TestClass1);8182testStratumData.put(javaStratumName, locationsData);8384}8586// common base names for test stratum, stratum source name and stratum87// source path88public static final String testStratumName = "TestStratum";8990public static final String testStratumSourceName = "testSource";9192public static final String testStratumSourcePath = "testSourcePath";9394// Event listener thread which saves all received StepEvents95public class StepEventListener extends EventHandler.EventListener {96// is BreakpointEvent was received (debuggee should stop at breakpoint97// after StepEvents generation)98private volatile boolean breakpointEventReceived;99100private List<Location> stepLocations = new ArrayList<Location>();101102public List<Location> stepLocations() {103return stepLocations;104}105106public void clearLocations() {107stepLocations.clear();108}109110public boolean eventReceived(Event event) {111if (event instanceof StepEvent) {112StepEvent stepEvent = (StepEvent) event;113114stepLocations.add(stepEvent.location());115116vm.resume();117118return true;119}120// debuggee should stop after event generation121else if (event instanceof BreakpointEvent) {122breakpointEventReceived = true;123vm.resume();124125return true;126}127128return false;129}130131public void waitBreakpointEvent() {132while (!breakpointEventReceived)133Thread.yield();134}135}136137// debug information about location (implements Comparable to make possible138// using DebugLocation with java.util.Set)139public static class DebugLocation implements Comparable<DebugLocation> {140public DebugLocation(String sourceName, String sourcePath, String methodName, int inputLine, int outputLine) {141this.sourceName = sourceName;142this.sourcePath = sourcePath;143this.inputLine = inputLine;144this.outputLine = outputLine;145this.methodName = methodName;146}147148public String sourceName;149150public String sourcePath;151152public String methodName;153154public int inputLine;155156public int outputLine;157158public String toString() {159return "Line number: " + inputLine + " SourceName: " + sourceName + " SourcePath: " + sourcePath;160}161162// compare DebugLocation with com.sun.jdi.Location163public boolean compare(Location location, String stratum) {164try {165if (stratum == null) {166return (location.lineNumber() == inputLine) && location.sourceName().equals(sourceName)167&& location.sourcePath().equals(sourcePath);168} else {169return (location.lineNumber(stratum) == inputLine)170&& location.sourceName(stratum).equals(sourceName)171&& location.sourcePath(stratum).equals(sourcePath);172}173} catch (AbsentInformationException e) {174return false;175}176}177178// used to find locations for given line179public boolean isConform(String sourceName, int lineNumber) {180boolean sourceConform = (sourceName == null ? true : this.sourceName.equals(sourceName));181182return sourceConform && (this.inputLine == lineNumber);183}184185// implements this method to make possible using DebugLocation with java.util.Set186public int compareTo(DebugLocation location) {187return (this.sourceName.equals(location.sourceName) && this.inputLine == location.inputLine) ? 0 : 1;188}189}190191// Class contains debug information about sources, paths, locations192// available for class193public static class LocationsData {194public LocationsData(String stratumName) {195this.stratumName = stratumName;196}197198public List<String> sourceNames() {199List<String> sourceNames = new ArrayList<String>();200sourceNames.addAll(sourceLocations.keySet());201202return sourceNames;203}204205public String stratumName;206207// is stratum default for ReferenceType208public boolean isDefault;209210// locations for source files211public Map<String, List<DebugLocation>> sourceLocations = new TreeMap<String, List<DebugLocation>>();212213// all locations for stratum214public List<DebugLocation> allLocations = new ArrayList<DebugLocation>();215216// all paths for stratum217public List<String> paths = new ArrayList<String>();218}219220static protected String locationToString(Location location, String stratum) {221String result;222223result = "Line number: " + (stratum == null ? location.lineNumber() : location.lineNumber(stratum));224225try {226result += (" Source name: " + (stratum == null ? location.sourceName() : location.sourceName(stratum)));227} catch (AbsentInformationException e) {228result += (" Source name: " + " INFORMATION IS ABSENT");229}230231try {232result += (" Source path: " + (stratum == null ? location.sourcePath() : location.sourcePath(stratum)));233} catch (AbsentInformationException e) {234result += (" Source path: " + " INFORMATION IS ABSENT");235}236237return result;238}239240// seach class file for given class and create copy of this class file in241// 'testWorkDir' directory242protected File createLocalClassfileCopy(String className) throws IOException {243int index = className.lastIndexOf(".");244245String path;246247if (index < 0)248path = "";249else250path = className.substring(0, index).replace(".", "/");251252File dirs = new File(testWorkDir + "/" + path);253dirs.mkdirs();254255File oldFile = null;256{257java.nio.file.Path tmp = ClassFileFinder.findClassFile(className, classpath);258oldFile = tmp == null ? null : tmp.toFile();259}260File newFile = copyFile(oldFile,261testWorkDir + "/" + className.replace(".", "/") + ".class");262263return newFile;264}265266// create class file with multiple strata267protected void savePathcedClassFile(String className, SmapGenerator smapGenerator, String smapFileName) {268File patchedClassFile = null;269270try {271patchedClassFile = createLocalClassfileCopy(className);272} catch (IOException e) {273e.printStackTrace(log.getOutStream());274throw new TestBug("Unexpected IO exception: " + e);275}276277smapFileName = testWorkDir + "/" + smapFileName;278smapGenerator.setOutputFileName(smapFileName);279280File smapFile = null;281282try {283smapFile = smapGenerator.saveToFile(smapFileName);284} catch (IOException e) {285e.printStackTrace(log.getOutStream());286throw new TestBug("Unexpected IO exception: " + e);287}288289log.display("Add for class " + className + " following SDE: ");290log.display(smapGenerator.getString());291292try {293InstallSDE.install(patchedClassFile, smapFile, false);294} catch (IOException e) {295e.printStackTrace(log.getOutStream());296throw new TestBug("Unexpected IO exception: " + e);297}298}299300// common for SDE tests debuggee name301protected String debuggeeClassName() {302if (classpath == null) {303throw new TestBug("Debugger requires 'testClassPath' parameter");304}305306return SDEDebuggee.class.getName() + " -testClassPath " + testWorkDir;307}308309// parses common for all SDE - tests parameters310protected String[] doInit(String args[], PrintStream out) {311args = super.doInit(args, out);312313if (classpath == null) {314throw new TestBug("Debugger requires 'testClassPath' parameter");315}316if (testWorkDir == null) {317throw new TestBug("Debugger requires 'testWorkDir' parameter");318}319320return args;321}322323// single input line is mapped to the single output line, test stratum has324// single source325protected Map<String, LocationsData> prepareDefaultPatchedClassFile_Type1(String className, int testStratumCount,326boolean setJavaStratumDefault) {327/*328* "Java" "TestStratum"329*330* <init>331* 32 --> 1000, source1332* 33 --> 1001, source1333* ...334* ...335* 39 --> 1007, source1336*337* sde_testMethod1338*339* 43 --> 1100, source1340* 44 --> 1101, source1341* ...342* ...343* 50 --> 1107, source1344*345* sde_testMethod1346* 54 --> 1200, source1347* 55 --> 1201, source1348* ...349* ...350* 61 --> 1207, source1351*/352353Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();354355String smapFileName = "TestSMAP.smap";356SmapGenerator smapGenerator = new SmapGenerator();357358for (int i = 0; i < testStratumCount; i++) {359String stratumName = testStratumName + (i + 1);360361LocationsData locationsData = new LocationsData(stratumName);362363String sourceName = testStratumSourceName + (i + 1);364String sourcePath = testStratumSourcePath + (i + 1);365366locationsData.paths.add(sourcePath);367368SmapStratum smapStratum = new SmapStratum(stratumName);369370List<DebugLocation> sourceLocations = new ArrayList<DebugLocation>();371372int baseLineNumber = 1000 * (i + 1);373374for (int j = 0; j < 8; j++) {375sourceLocations.add(new DebugLocation(sourceName, sourcePath,376"<init>", baseLineNumber + j, INIT_LINE + j));377sourceLocations.add(new DebugLocation(sourceName, sourcePath,378"sde_testMethod1", baseLineNumber + 100 + j, METHOD1_LINE + j));379sourceLocations.add(new DebugLocation(sourceName, sourcePath,380"sde_testMethod2", baseLineNumber + 200 + j, METHOD2_LINE + j));381}382383locationsData.sourceLocations.put(sourceName, sourceLocations);384locationsData.allLocations.addAll(sourceLocations);385386testStratumData.put(stratumName, locationsData);387388smapStratum.addFile(sourceName, sourcePath);389390for (DebugLocation debugLocation : sourceLocations) {391smapStratum.addLineData(debugLocation.inputLine, sourceName, 1, debugLocation.outputLine, 1);392}393394// if setJavaStratumDefault == false do first test stratum default395if (!(setJavaStratumDefault) && (i == 0)) {396locationsData.isDefault = true;397smapGenerator.addStratum(smapStratum, true);398} else399smapGenerator.addStratum(smapStratum, false);400}401402savePathcedClassFile(className, smapGenerator, smapFileName);403404addJavaLocations(testStratumData, setJavaStratumDefault);405406return testStratumData;407}408409// single input line is mapped to the single output line, test stratum has 3410// sources411protected Map<String, LocationsData> prepareDefaultPatchedClassFile_Type2(String className, int testStratumCount) {412/*413* "Java" "TestStratum"414*415* <init>416* 32 --> 1000, source1, path1417* 33 --> 1001, source2, path2418* 34 --> 1002, source3, path3419* ...420* ...421*422* sde_testMethod1423* 43 --> 1100, source1, path1424* 44 --> 1101, source2, path2425* 45 --> 1102, source3, path3426* ...427* ...428*429* sde_testMethod2430* 54 --> 1200, source1, path1431* 55 --> 1201, source2, path2432* 56 --> 1207, source3, path3433* ...434* ...435*/436437Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();438439String smapFileName = "TestSMAP.smap";440SmapGenerator smapGenerator = new SmapGenerator();441442for (int i = 0; i < testStratumCount; i++) {443String stratumName = testStratumName + (i + 1);444SmapStratum smapStratum = new SmapStratum(stratumName);445446LocationsData locationsData = new LocationsData(stratumName);447448String sourceName1 = testStratumSourceName + (i + 1) + "_1";449String sourcePath1 = testStratumSourcePath + (i + 1) + "_1";450locationsData.paths.add(sourcePath1);451smapStratum.addFile(sourceName1, sourcePath1);452453String sourceName2 = testStratumSourceName + (i + 1) + "_2";454String sourcePath2 = testStratumSourcePath + (i + 1) + "_2";455locationsData.paths.add(sourcePath2);456smapStratum.addFile(sourceName2, sourcePath2);457458String sourceName3 = testStratumSourceName + (i + 1) + "_3";459String sourcePath3 = testStratumSourcePath + (i + 1) + "_3";460locationsData.paths.add(sourcePath3);461smapStratum.addFile(sourceName3, sourcePath3);462463List<DebugLocation> source1Locations = new ArrayList<DebugLocation>();464List<DebugLocation> source2Locations = new ArrayList<DebugLocation>();465List<DebugLocation> source3Locations = new ArrayList<DebugLocation>();466467for (int j = 0; j < 8; j++) {468if (j % 3 == 0) {469source1Locations.add(new DebugLocation(sourceName1, sourcePath1,470"<init>", 1000 + j, INIT_LINE + j));471source1Locations.add(new DebugLocation(sourceName1, sourcePath1,472"sde_testMethod1", 1101 + j, METHOD1_LINE + j));473source1Locations.add(new DebugLocation(sourceName1, sourcePath1,474"sde_testMethod2", 1201 + j, METHOD2_LINE + j));475} else if (j % 3 == 1) {476source2Locations.add(new DebugLocation(sourceName2, sourcePath2,477"<init>", 1000 + j, INIT_LINE + j));478source2Locations.add(new DebugLocation(sourceName2, sourcePath2,479"sde_testMethod1", 1101 + j, METHOD1_LINE + j));480source2Locations.add(new DebugLocation(sourceName2, sourcePath2,481"sde_testMethod2", 1201 + j, METHOD2_LINE + j));482} else {483source3Locations.add(new DebugLocation(sourceName3, sourcePath3,484"<init>", 1000 + j, INIT_LINE + j));485source3Locations.add(new DebugLocation(sourceName3, sourcePath3,486"sde_testMethod1", 1101 + j, METHOD1_LINE + j));487source3Locations.add(new DebugLocation(sourceName3, sourcePath3,488"sde_testMethod2", 1201 + j, METHOD2_LINE + j));489}490}491492locationsData.sourceLocations.put(sourceName1, source1Locations);493locationsData.sourceLocations.put(sourceName2, source2Locations);494locationsData.sourceLocations.put(sourceName3, source3Locations);495496locationsData.allLocations.addAll(source1Locations);497locationsData.allLocations.addAll(source2Locations);498locationsData.allLocations.addAll(source3Locations);499500for (DebugLocation debugLocation : locationsData.allLocations) {501smapStratum.addLineData(502debugLocation.inputLine,503debugLocation.sourceName,5041,505debugLocation.outputLine,5061);507}508509smapGenerator.addStratum(smapStratum, false);510511testStratumData.put(stratumName, locationsData);512}513514savePathcedClassFile(className, smapGenerator, smapFileName);515516addJavaLocations(testStratumData, true);517518return testStratumData;519}520521// single input line is mapped to two output lines522protected Map<String, LocationsData> prepareDefaultPatchedClassFile_Type3(String className, int testStratumCount,523boolean setJavaStratumDefault) {524/*525* "Java" "TestStratum"526*527* <init>528* 32 --> 1001, source1529* 34 --> 1002, source1530* 36 --> 1003, source1531* 38 --> 1004, source1532*533* sde_testMethod1534* 43 --> 1101, source1535* 45 --> 1102, source1536* 47 --> 1103, source1537* 49 --> 1104, source1538*539* sde_testMethod2540* 54 --> 1201, source1541* 56 --> 1202, source1542* 58 --> 1203, source1543* 60 --> 1204, source1544*/545546Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();547548String smapFileName = "TestSMAP.smap";549SmapGenerator smapGenerator = new SmapGenerator();550551for (int i = 0; i < testStratumCount; i++) {552String stratumName = testStratumName + (i + 1);553LocationsData locationsData = new LocationsData(stratumName);554555String sourceName = testStratumSourceName + (i + 1);556String sourcePath = testStratumSourcePath + (i + 1);557locationsData.paths.add(sourcePath);558559SmapStratum smapStratum = new SmapStratum(stratumName);560561List<DebugLocation> sourceLocations = new ArrayList<DebugLocation>();562563int baseLineNumber = 1000 * (i + 1);564565for (int j = 0; j < 4; j++) {566sourceLocations.add(new DebugLocation(sourceName, sourcePath,567"<init>", baseLineNumber + j, INIT_LINE + j * 2));568sourceLocations.add(new DebugLocation(sourceName, sourcePath,569"sde_testMethod1", baseLineNumber + 100 + j, METHOD1_LINE + j * 2));570sourceLocations.add(new DebugLocation(sourceName, sourcePath,571"sde_testMethod2", baseLineNumber + 200 + j, METHOD2_LINE + j * 2));572}573574locationsData.allLocations.addAll(sourceLocations);575locationsData.sourceLocations.put(sourceName, sourceLocations);576577testStratumData.put(stratumName, locationsData);578579smapStratum.addFile(sourceName, sourcePath);580581for (DebugLocation debugLocation : locationsData.allLocations) {582smapStratum.addLineData(debugLocation.inputLine, sourceName, 1, debugLocation.outputLine, 1);583}584585// if setJavaStratumDefault == false do first stratum default586if (!setJavaStratumDefault && (i == 0)) {587locationsData.isDefault = true;588smapGenerator.addStratum(smapStratum, true);589} else590smapGenerator.addStratum(smapStratum, false);591}592593savePathcedClassFile(className, smapGenerator, smapFileName);594595addJavaLocations(testStratumData, setJavaStratumDefault);596597return testStratumData;598}599600// 3 different test stratums define disjoint locations sets601protected Map<String, LocationsData> prepareDefaultPatchedClassFile_Type4(String className) {602/*603* "Java" "TestStratum1" "TestStratum2" "TestStratum3"604*605* <init>606* 32 --> 1000607* ...608* ...609* 39 --> 1007610*611* sde_testMethod1612* 43 --> 1100613* ...614* ...615* 50 --> 1107616*617* sde_testMethod2618* 54 --> 1200619* ...620* ...621* 61 --> 1207622*/623Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();624625String smapFileName = "TestSMAP.smap";626627SmapGenerator smapGenerator = new SmapGenerator();628629String stratumName = testStratumName + "1";630LocationsData locationsData = new LocationsData(stratumName);631List<DebugLocation> sourceLocations = new ArrayList<DebugLocation>();632String methodName = "<init>";633for (int i = 0; i < 8; i++) {634sourceLocations.add(new DebugLocation(635testStratumSourceName, testStratumSourcePath, methodName,6361000 + i, INIT_LINE + i));637}638locationsData.allLocations.addAll(sourceLocations);639locationsData.sourceLocations.put(testStratumSourceName, sourceLocations);640testStratumData.put(stratumName, locationsData);641642stratumName = testStratumName + "2";643locationsData = new LocationsData(stratumName);644sourceLocations = new ArrayList<DebugLocation>();645methodName = "sde_testMethod1";646for (int i = 0; i < 8; i++) {647sourceLocations.add(new DebugLocation(648testStratumSourceName, testStratumSourcePath, methodName,6491100 + i, METHOD1_LINE + i));650}651locationsData.allLocations.addAll(sourceLocations);652locationsData.sourceLocations.put(testStratumSourceName, sourceLocations);653testStratumData.put(stratumName, locationsData);654655stratumName = testStratumName + "3";656locationsData = new LocationsData(stratumName);657sourceLocations = new ArrayList<DebugLocation>();658methodName = "sde_testMethod2";659for (int i = 0; i < 8; i++) {660sourceLocations.add(new DebugLocation(testStratumSourceName, testStratumSourcePath,661methodName, 1200 + i, METHOD2_LINE + i));662}663locationsData.allLocations.addAll(sourceLocations);664locationsData.sourceLocations.put(testStratumSourceName, sourceLocations);665testStratumData.put(stratumName, locationsData);666667for (String stratum : testStratumData.keySet()) {668SmapStratum smapStratum = new SmapStratum(stratum);669smapStratum.addFile(testStratumSourceName, testStratumSourcePath);670671for (DebugLocation debugLocation : testStratumData.get(stratum).allLocations)672smapStratum.addLineData(673debugLocation.inputLine,674debugLocation.sourceName,6751,676debugLocation.outputLine,6771);678679smapGenerator.addStratum(smapStratum, false);680}681682savePathcedClassFile(className, smapGenerator, smapFileName);683684return testStratumData;685}686687// single input line is mapped to the single output line, test stratum has 3688// sources,689// lines in each method has same numbers, each method has locations in 3690// sources691protected Map<String, LocationsData> prepareDefaultPatchedClassFile_Type5(String className, int testStratumCount) {692/*693* "Java" "TestStratum"694*695* <init>696* 32 --> 1000, source1, path1697* 33 --> 1000, source2, path2698* 34 --> 1000, source3, path3699* ...700* ...701*702* sde_testMethod1703* 43 --> 1100, source1, path1704* 44 --> 1100, source2, path2705* 45 --> 1100, source3, path3706* ...707* ...708*709* sde_testMethod2710* 54 --> 1200, source1, path1711* 55 --> 1200, source2, path2712* 56 --> 1200, source3, path3713* ...714* ...715*/716717Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();718719String smapFileName = "TestSMAP.smap";720SmapGenerator smapGenerator = new SmapGenerator();721722for (int i = 0; i < testStratumCount; i++) {723String stratumName = testStratumName + (i + 1);724SmapStratum smapStratum = new SmapStratum(stratumName);725726LocationsData locationsData = new LocationsData(stratumName);727728String sourceName1 = testStratumSourceName + (i + 1) + "_1";729String sourcePath1 = testStratumSourcePath + (i + 1) + "_1";730locationsData.paths.add(sourcePath1);731smapStratum.addFile(sourceName1, sourcePath1);732733String sourceName2 = testStratumSourceName + (i + 1) + "_2";734String sourcePath2 = testStratumSourcePath + (i + 1) + "_2";735locationsData.paths.add(sourcePath2);736smapStratum.addFile(sourceName2, sourcePath2);737738String sourceName3 = testStratumSourceName + (i + 1) + "_3";739String sourcePath3 = testStratumSourcePath + (i + 1) + "_3";740locationsData.paths.add(sourcePath3);741smapStratum.addFile(sourceName3, sourcePath3);742743List<DebugLocation> source1Locations = new ArrayList<DebugLocation>();744List<DebugLocation> source2Locations = new ArrayList<DebugLocation>();745List<DebugLocation> source3Locations = new ArrayList<DebugLocation>();746747for (int j = 0; j < 8; j++) {748if (j % 3 == 0) {749source1Locations.add(new DebugLocation(sourceName1, sourcePath1,750"<init>", 1000, INIT_LINE + j));751source1Locations.add(new DebugLocation(sourceName1, sourcePath1,752"sde_testMethod1", 1100, METHOD1_LINE + j));753source1Locations.add(new DebugLocation(sourceName1, sourcePath1,754"sde_testMethod2", 1200, METHOD2_LINE + j));755} else if (j % 3 == 1) {756source2Locations.add(new DebugLocation(sourceName2, sourcePath2,757"<init>", 1000, INIT_LINE + j));758source2Locations.add(new DebugLocation(sourceName2, sourcePath2,759"sde_testMethod1", 1100, METHOD1_LINE + j));760source2Locations.add(new DebugLocation(sourceName2, sourcePath2,761"sde_testMethod2", 1200, METHOD2_LINE + j));762} else {763source3Locations.add(new DebugLocation(sourceName3, sourcePath3,764"<init>", 1000, INIT_LINE + j));765source3Locations.add(new DebugLocation(sourceName3, sourcePath3,766"sde_testMethod1", 1100, METHOD1_LINE + j));767source3Locations.add(new DebugLocation(sourceName3, sourcePath3,768"sde_testMethod2", 1200, METHOD2_LINE + j));769}770}771772locationsData.sourceLocations.put(sourceName1, source1Locations);773locationsData.sourceLocations.put(sourceName2, source2Locations);774locationsData.sourceLocations.put(sourceName3, source3Locations);775776locationsData.allLocations.addAll(source1Locations);777locationsData.allLocations.addAll(source2Locations);778locationsData.allLocations.addAll(source3Locations);779780for (DebugLocation debugLocation : locationsData.allLocations) {781smapStratum.addLineData(782debugLocation.inputLine,783debugLocation.sourceName,7841,785debugLocation.outputLine,7861);787}788789smapGenerator.addStratum(smapStratum, false);790791testStratumData.put(stratumName, locationsData);792}793794savePathcedClassFile(className, smapGenerator, smapFileName);795796return testStratumData;797}798799public static File copyFile(File srcFile, String newFileName) throws IOException {800FileChannel inChannel = new FileInputStream(srcFile).getChannel();801802File newFile = new File(newFileName);803newFile.createNewFile();804FileChannel outChannel = new FileOutputStream(newFile).getChannel();805806outChannel.transferFrom(inChannel, 0, inChannel.size());807808outChannel.close();809inChannel.close();810811return newFile;812}813814// find all locations of method with given name815// (used to check result of 'Method.allLineLocations()')816static protected List<DebugLocation> locationsOfMethod(List<DebugLocation> debugLocations, String methodName) {817List<DebugLocation> result = new ArrayList<DebugLocation>();818819for (DebugLocation debugLocation : debugLocations) {820if (debugLocation.methodName.equals(methodName))821result.add(debugLocation);822}823824return result;825}826827// find all locations for given line and source name828// (used to check result of 'Method.locationsOfLine()' and829// 'ReferenceType.locationsOfLine()')830static protected List<DebugLocation> locationsOfLine(List<DebugLocation> debugLocations, String sourceName,831int lineNumber) {832List<DebugLocation> result = new ArrayList<DebugLocation>();833834for (DebugLocation debugLocation : debugLocations) {835if (debugLocation.isConform(sourceName, lineNumber))836result.add(debugLocation);837}838839return result;840}841842// find locations unique by line number and source name843// (used in 'check_ReferenceType_locationsOfLine' and844// 'check_Method_locationsOfLine' to find all line numbers available for845// ReferenceType or Method)846static protected Set<DebugLocation> allUniqueLocations(List<DebugLocation> debugLocations) {847Set<DebugLocation> result = new TreeSet<DebugLocation>();848849for (DebugLocation debugLocation : debugLocations) {850if (!result.contains(debugLocation)) {851result.add(debugLocation);852}853}854855return result;856}857858// check is list of Locations contains only expected locations859protected void compareLocations(List<Location> locations, List<DebugLocation> expectedLocations, String stratum) {860boolean success = true;861862List<Location> tempLocations = new LinkedList<Location>(locations);863864List<DebugLocation> tempExpectedLocations = new LinkedList<DebugLocation>(expectedLocations);865866for(Iterator<Location> locationsIterator = tempLocations.iterator(); locationsIterator.hasNext();) {867boolean isExpected = false;868Location location = locationsIterator.next();869870for(Iterator<DebugLocation> expectedLocationsIterator = tempExpectedLocations.iterator(); expectedLocationsIterator.hasNext();) {871DebugLocation expectedLocation = expectedLocationsIterator.next();872if (expectedLocation.compare(location, stratum)) {873isExpected = true;874locationsIterator.remove();875expectedLocationsIterator.remove();876break;877}878}879if (!isExpected) {880success = false;881log.complain("Location " + location + " were not found in expected locations");882}883}884885if (tempLocations.size() != 0) {886success = false;887setSuccess(false);888log.complain("Not all locations were found in expected");889}890891if (tempExpectedLocations.size() != 0) {892success = false;893setSuccess(false);894log.complain("Following expected locations were not found in received");895for (DebugLocation expectedLocation : tempExpectedLocations) {896log.complain(expectedLocation.toString());897}898}899900if (!success) {901setSuccess(false);902log.complain("Expected and actual locations differ");903904log.complain("Actual locations: ");905for (Location location : locations) {906log.complain(locationToString(location, stratum));907}908909log.complain("Expected locations: ");910for (DebugLocation expectedLocation : expectedLocations) {911log.complain(expectedLocation.toString());912}913}914}915916// test all SDE related methods of ReferenceType917protected void checkReferenceType(String stratum, ReferenceType referenceType, List<String> expectedSourceNames,918List<String> expectedSourcePaths, List<DebugLocation> expectedLocations) {919log.display("Check sourceNames");920check_ReferenceType_sourceNames(referenceType, stratum, expectedSourceNames);921log.display("Check sourcePaths");922check_ReferenceType_sourcePaths(referenceType, stratum, expectedSourcePaths);923log.display("Check allLocations");924check_ReferenceType_allLineLocations(referenceType, stratum, expectedLocations);925log.display("Check locationsOfLine");926check_ReferenceType_locationsOfLine(referenceType, stratum, false, expectedLocations);927928for (Method method : referenceType.methods()) {929List<DebugLocation> expectedLocationsOfMethod = locationsOfMethod(expectedLocations, method.name());930931log.display("Check allLineLocations for method '" + method.name() + "'");932check_Method_allLineLocations(method, stratum, expectedLocationsOfMethod);933log.display("Check locationsOfLine for method '" + method.name() + "'");934check_Method_locationsOfLine(method, stratum, false, expectedLocationsOfMethod);935}936}937938// check is 'ReferenceType.sourceNames' returns only expected sources939protected void check_ReferenceType_sourceNames(ReferenceType referenceType, String stratum,940List<String> expectedSourceNames) {941try {942if (stratum == null) {943String sourceName = referenceType.sourceName();944String expectedSourceName = expectedSourceNames.get(0);945946if (!sourceName.equals(expectedSourceName)) {947setSuccess(false);948log.complain("Unexpected result of ReferenceType.sourceName(): " + sourceName + ", expected is "949+ expectedSourceName);950}951} else {952boolean success = true;953954List<String> sourceNames = referenceType.sourceNames(stratum);955956if (!expectedSourceNames.containsAll(sourceNames)) {957success = false;958log.complain("ReferenceType.sourceNames() returns unexpected names");959}960961if (!sourceNames.containsAll(expectedSourceNames)) {962success = false;963log.complain("Not all expected source names was returned by ReferenceType.sourceNames()");964}965966if (!success) {967log.complain("Expected source names:");968for (String name : expectedSourceNames)969log.complain(name);970log.complain("Actual source names:");971for (String name : sourceNames)972log.complain(name);973}974}975} catch (AbsentInformationException e) {976setSuccess(false);977log.complain("Unexpected exception: " + e);978e.printStackTrace(log.getOutStream());979}980}981982// check is 'ReferenceType.sourcePaths' returns only expected paths983protected void check_ReferenceType_sourcePaths(ReferenceType referenceType, String stratum,984List<String> expectedSourcePaths) {985try {986boolean success = true;987988List<String> sourcePaths = referenceType.sourcePaths(stratum);989990if (!expectedSourcePaths.containsAll(sourcePaths)) {991success = false;992log.complain("ReferenceType.sourcePaths() returns unexpected paths");993}994995if (!sourcePaths.containsAll(expectedSourcePaths)) {996success = false;997log.complain("Not all expected paths was returned by ReferenceType.sourcePaths()");998}9991000if (!success) {1001log.complain("Expected paths:");1002for (String path : expectedSourcePaths)1003log.complain(path);1004log.complain("Actual paths:");1005for (String path : sourcePaths)1006log.complain(path);1007}1008} catch (AbsentInformationException e) {1009setSuccess(false);1010log.complain("Unexpected exception: " + e);1011e.printStackTrace(log.getOutStream());1012}1013}10141015// check that method 'ReferenceType.allLineLocations' returns only expected1016// locations1017protected void check_ReferenceType_allLineLocations(ReferenceType referenceType, String stratum,1018List<DebugLocation> expectedLocations) {1019try {1020List<Location> locations = referenceType.allLineLocations();1021compareLocations(locations, expectedLocations, stratum);1022} catch (AbsentInformationException e) {1023setSuccess(false);1024log.complain("Unexpected exception: " + e);1025e.printStackTrace(log.getOutStream());1026}10271028}10291030// check that method 'Method.allLineLocations' returns only expected1031// locations1032protected void check_Method_allLineLocations(Method method, String stratum,1033List<DebugLocation> expectedLocationsOfMethod) {1034try {1035List<Location> methodAllLineLocations = method.allLineLocations();1036compareLocations(methodAllLineLocations, expectedLocationsOfMethod, stratum);1037} catch (AbsentInformationException e) {1038setSuccess(false);1039log.complain("Unexpected exception: " + e);1040e.printStackTrace(log.getOutStream());1041}1042}10431044// for each line available for method check result of1045// 'Method.locationsOfLine'1046protected void check_Method_locationsOfLine(Method method, String stratum, boolean allSources,1047List<DebugLocation> expectedLocationsOfMethod) {1048try {1049for (DebugLocation uniqueLocation : allUniqueLocations(expectedLocationsOfMethod)) {1050String sourceName = allSources ? null : uniqueLocation.sourceName;10511052List<DebugLocation> expectedLocationsOfLine = locationsOfLine(1053expectedLocationsOfMethod,1054sourceName,1055uniqueLocation.inputLine);10561057List<Location> locationsOfLine = (stratum == null) ? method.locationsOfLine(uniqueLocation.inputLine)1058: method.locationsOfLine(stratum, sourceName, uniqueLocation.inputLine);10591060compareLocations(locationsOfLine, expectedLocationsOfLine, stratum);1061}1062} catch (AbsentInformationException e) {1063setSuccess(false);1064log.complain("Unexpected exception: " + e);1065e.printStackTrace(log.getOutStream());1066}1067}10681069// for each line available for ReferenceType check result of1070// 'ReferenceType.locationsOfLine'1071protected void check_ReferenceType_locationsOfLine(ReferenceType referenceType, String stratum, boolean allSources,1072List<DebugLocation> expectedLocations) {1073try {1074for (DebugLocation uniqueLocation : allUniqueLocations(expectedLocations)) {1075String sourceName = allSources ? null : uniqueLocation.sourceName;10761077List<DebugLocation> expectedLocationsOfLine = locationsOfLine(1078expectedLocations,1079sourceName,1080uniqueLocation.inputLine);10811082List<Location> locations = (stratum == null) ? referenceType.locationsOfLine(uniqueLocation.inputLine)1083: referenceType.locationsOfLine(stratum, sourceName, uniqueLocation.inputLine);10841085compareLocations(locations, expectedLocationsOfLine, stratum);1086}1087} catch (AbsentInformationException e) {1088setSuccess(false);1089log.complain("Unexpected exception: " + e);1090e.printStackTrace(log.getOutStream());1091}10921093}10941095// check that method 'ReferenceType.availableStrata' returns only expected1096// stratums1097protected void check_ReferenceType_availableStrata(ReferenceType referenceType, List<String> expectedStrata) {1098boolean success = true;10991100List<String> strata = referenceType.availableStrata();11011102if (!expectedStrata.containsAll(strata)) {1103success = false;1104log.complain("ReferenceType.availableStrata() returns unexpected values");1105}11061107if (!strata.containsAll(expectedStrata)) {1108success = false;1109log.complain("Not all expected stratums was returned by ReferenceType.availableStrata()");1110}11111112if (!success) {1113log.complain("Expected stratums:");1114for (String name : expectedStrata)1115log.complain(name);1116log.complain("Actual stratums:");1117for (String name : strata)1118log.complain(name);1119}1120}11211122}112311241125