jtulach commented on code in PR #9691: URL: https://github.com/apache/netbeans/pull/9691#discussion_r4261323038
########## java/debugger.jpda.ui/test/unit/src/org/netbeans/modules/debugger/jpda/ui/BreakpointOutputTest.java: ########## @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.debugger.jpda.ui; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.ArrayList; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import junit.framework.Test; +import org.netbeans.api.debugger.DebuggerManager; +import org.netbeans.api.debugger.Session; +import org.netbeans.api.debugger.jpda.ExceptionBreakpoint; +import org.netbeans.api.debugger.jpda.JPDADebugger; +import org.netbeans.api.debugger.jpda.JPDASupport; +import org.netbeans.api.debugger.jpda.LineBreakpoint; +import org.netbeans.api.debugger.jpda.MethodBreakpoint; +import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent; +import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener; +import org.netbeans.junit.NbTestCase; +import org.openide.util.Pair; +import org.openide.util.Utilities; + +/** Testing {@link BreakpointOutput#printExpressions} + */ +public class BreakpointOutputTest extends NbTestCase { + + public BreakpointOutputTest(String name) { + super(name); + } + + public static Test suite() { + return JPDASupport.createTestSuite(BreakpointOutputTest.class); + } + + public void testBreakpointOutputLinks() throws Throwable { + ExceptionBreakpoint breakPoint = ExceptionBreakpoint.create( + "java.lang.IllegalStateException", + ExceptionBreakpoint.TYPE_EXCEPTION_CAUGHT_UNCAUGHT + ); + JPDABreakpointListener[] onBreakpoint = { null }; + breakPoint.addJPDABreakpointListener((event) -> { + onBreakpoint[0].breakpointReached(event); + }); + DebuggerManager.getDebuggerManager().addBreakpoint(breakPoint); + + final URI cpUrl = test.SampleMain.class.getProtectionDomain().getCodeSource().getLocation().toURI(); + System.setProperty ("test.dir.src", findSourcePath()); + JPDASupport support = JPDASupport.attach(new String[0], "test.SampleMain", new String[0], new File[] { + Utilities.toFile(cpUrl) + }); + final Session session = support.getDebugger().getSession(); + + // + // now the real test and collect the printed messages + // + + var collected = new StringWriter(); + var writer = new PrintWriter(collected); + var format = "breakpoint hit {=i}, {=when}, {=arr}, {=obj}"; + BreakpointOutput out = new BreakpointOutput(session); + onBreakpoint[0] = (event) -> { + writer.append("on ").append(event.getThread().getName()).append("\n"); + try { + printExpressions(out, event, format, (msg, action) -> { + if (action != null) { + writer.append("<a "); + } + writer.append(msg); + if (action != null) { + writer.append(">"); + } + }, (msg) -> { + writer.append(msg).append("\n"); + }); + } catch (ReflectiveOperationException ex) { + ex.printStackTrace(writer); + } + }; + + // + // launch the debugger and run the test + // + + for (;;) { + support.waitState(JPDADebugger.STATE_STOPPED); + if (support.getDebugger().getState() == JPDADebugger.STATE_DISCONNECTED) { + break; + } + support.doContinue(); + } + + // + // verify result + // + assertEquals(""" + on main + breakpoint hit 42, <a "Now">, <a #94(length=1)>, <a class test.SampleMain> + """, collected.toString()); + } + + private String findSourcePath() throws IOException { + File curDir = getWorkDir(); + for (;;) { + var testSrc = new File(new File(new File(curDir, "test"), "unit"), "src"); + if (testSrc.isDirectory()) { + return testSrc.getPath(); + } + curDir = curDir.getParentFile(); Review Comment: Exception as exception... mýrnixtýrnix. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
