任何方式执行代码,知道它在eclipse调试器中运行? [英] Any way for executing code to know it's running in the eclipse debugger?

查看:379
本文介绍了任何方式执行代码,知道它在eclipse调试器中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Pax考试考试。要正常执行测试,我只是在Eclipse中运行JUnit类。如果我想要遍历Eclipse调试器中的代码,我必须使其设置调试选项,包括使其等待调试器连接的标志,这是我必须运行的单独进程。我目前正在使用这个代码检查一个调试系统属性来启用此功能,但这很烦人。



如果@Configuration方法可以查看系统属性或其他条件,如果代码在调试器中执行,则该属性将始终为真,因此我可以将其用作启动这些标志的触发器,而不是手动设置的debug系统属性。 p>

我已经尝试在此方法的顶部设置断点,并在调试器中检查可能设置的所有系统属性,但是我没有看到任何东西。



更新:



要清楚,我需要点了解一些关于Pax Exam测试如何工作的细节,以更好地解释为什么我正在寻找一种改进此过程的方法。测试运行时,它需要一个Karaf容器运行测试。为了在调试器中运行一个测试,你必须强制在容器中运行的代码设置suspend = y标志,这将等待用于调试器连接。你绝对不想这样做,如果你不调试。



开始pax考试测试运行后,你必须运行另一个调试配置,使与karaf容器的远程连接。在技​​术上,单元测试本身的运行配置不需要是调试配置。



所以,最简单的方法是让代码启动容器检查调试系统属性(或任何您想要调用的属性),并且设置该属性时,设置调试器端口和suspend = y标志。如果该属性未设置,则不会这样做。



因此,如果您在没有调试的情况下运行测试,则必须确保系统属性没有设置。如果你正在调试,你必须确保它的设置。每次需要来回运行时都需要编辑运行配置是一件麻烦的事情。



所以,我打算将启动单元测试运行配置调试配置(即使不需要),以及启动karaf容器检测它正在作为调试配置运行的代码,并在这种情况下设置suspend = y标志。 p>

我得出结论,代码本身没有办法检测到这一点,但是我将在自己的答案中详细介绍如何让调试器帮助我。

解决方案

我将自行解答我原来的问题,虽然这不是我原来的答案问题,第一个答案确实试图解决。这个答案并不能帮助我,但是,我真正需要的是能够运行我的Pax Exam测试,这样当我第一次运行单元测试时,正在运行Pax Exam测试的服务器部分,如果我要使用调试器,它将知道为服务器提供正确的-Xdebug参数,而不是如果我没有使用调试器。我有代码检查调试系统属性,并使用它来设置正确的-Xdebug参数,但是如果我需要更改如何更改如何操作,我不想手动添加或删除该参数从运行配置我正在运行测试(在调试之间进行而不是调试)。



所以,据我所见,我能做的最好的就是让我在调试器中运行单元测试的服务器部分(否则实际上不需要在调试器中,因为它只是客户端需要),这将导致我正在检查的系统属性要设置,所以它将设置正确的标志。



我不知道Eclipse中的任何功能,让我在开始任何时候运行特定的预定义代码段调试会话(我的意思是任何,而不是特定调试会话),但是有一些接近的东西,即使它有点黑客。



我做的是在顶部设置一个断点设置要启动的karaf配置的方法,我以断点作为条件,使用以下表达式:

 (System。 setProperty(debug,true)!= null)&& false 

这将设置我需要的系统属性,但是不能停止,因为最终的表达式将



从技术上来说,它甚至不需要在这种方法中,只需要在karaf选项设置之前进行命中。



这是一个工作区设置,所以我每次启动Eclipse时都不需要重新添加。



更新



随着升级到Oxygen,这可以稍微简单一些,新的追踪点功能( https://www.eclipse.org/eclipse/news/4.7/jdt.php#切换轨迹点)。只需切换跟踪点,并将表达式设置为 System.setProperty(debug,true)'


I have some Pax Exam tests. To execute the test normally, I just run the JUnit class in Eclipse. If I want to step through the code in the Eclipse debugger, I have to make it set the debug options, including the flag to make it wait for the debugger connection, which is a separate process I have to run. I'm presently having this code check for a "debug" system property to enable this, but that's sort of annoying.

It would be really nice if the @Configuration method can look at a system property or some other condition that will always be true if the code is executing in the debugger, so I could use that as a trigger to enable those flags, instead of a manually set "debug" system property.

I've already tried setting a breakpoint at the top of this method and inspecting all the system properties for something that might be set while in the debugger, but I didn't see anything.

Update:

Just to be clear, I need to point out some details about how Pax Exam tests work, to better explain why I'm looking for a way to improve this process. When the test runs, it forks a Karaf container to run the test in. In order to run a test in the debugger, you have to force the code that runs in the container to set the "suspend=y" flag, which will wait for a debugger connection. You definitely don't want to do that if you're not debugging.

After starting the pax exam test running, you then have to run another debug configuration, to make a remote connection to the karaf container. Technically, the run configuration for the unit test itself doesn't need to be a debug configuration.

So, the easiest way to make this happen is to have the the code that initiates the container check for a "debug" system property (or whatever you want to call it), and when that is set, to set the debugger port and the "suspend=y" flag. If the property is not set, it doesn't do that.

So, if you're running the test without debug, you have to make sure that system property is not set. If you're debugging, you have to make sure it's set. It's an annoyance to have to edit the run configuration each time you need to go back and forth.

So, what I was intending was to start the unit test run configuration as a debug configuration (even though it doesn't need to be), and for the code that starts the karaf container to detect that it's being run as a debug configuration, and set the "suspend=y" flag in that case.

I've concluded that there is no way for the code itself to detect this, but I'll detail in my own answer how I get the debugger to help me a bit.

解决方案

I'm going to self-answer to address my original problem, although it isn't quite the answer to my original question, which the first answer does attempt to address. That answer doesn't help me, however.

My real need was to able to run my Pax Exam test so that when I first run the unit test, which is running the "server" portion of the Pax Exam test, it will know to provide the correct "-Xdebug" parameters to the server if I'm going to be using the debugger, and NOT to if I'm not using the debugger. I have code that checks for the "debug" system property and uses that to set the correct "-Xdebug" parameters, but I don't want to have to manually add or remove that parameter from the run configuration if I need to change how I'm running the test (going between debugging and not debugging).

So, as far as I can see, the best I can do is make it so that when I run the "server" portion of the unit test in the debugger (which otherwise doesn't actually have to be in the debugger, as it's only the client side that needs it), this will cause the system property I'm checking for to be set, so it will set the correct flags.

I'm not aware of any feature in Eclipse that lets me run particular predefined snippets of code when I start any debugging session (I mean "any", not a "particular" debugging session), but there is something that comes close, even though it's a bit of a hack.

What I did was set a breakpoint at the top of the method that sets the karaf configuration to be started, and I made the breakpoint conditional, with the following expression:

(System.setProperty("debug", "true") != null) && false

This will set the system property I need, but then not stop, as the final expression will be false.

Technically, it doesn't even need to be in this method, it just has to be hit before the karaf options are set.

This stays as a workspace setting, so I don't need to re-add this every time I start Eclipse.

Update:

With the upgrade to Oxygen, this can be slightly simpler, with the new "tracepoints" feature (https://www.eclipse.org/eclipse/news/4.7/jdt.php#toggle-trace-point). Just "toggle tracepoint" and set the expression to 'System.setProperty("debug", "true")'

这篇关于任何方式执行代码,知道它在eclipse调试器中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆