 So, welcome in Advan Javadevagey's workshop. I just would like to remind you that if you go out working the girl for the door, there is some noise from that that can be stopped at us. So, the workshop will be led by Andrei Podhatsky. So, welcome. Okay, hello. My name is Andrei Podhatsky. And I have been working at Red Hat for four years as a quality engineer. And my responsibility is for testing integration tools for JbDS, which is based on Eclipse IDE. And in my job, I spend lots of time by debugging the code. And during my career, I noticed that many people do not use their IDE as effectively as possible. And that's why I decided to make such a workshop for debugging the Java code. So, what is debugging? Debugging simply said it is a process of finding and fixing bugs. Here is a small example. There is an output exception in line number three. And please take your time to have a look at the code. It's quite simple. There is an output exception that can be caused by now in user or in its nickname. Oh, yeah, number three. So, what's the simplest way how to debug code like this? Many people usually put the system of print line to the existing code just to see whether user is now or whether its nickname is now. I'm not saying that this approach is bad, but I think that there are two disadvantages. First, you are missing the existing code. And the second, this is not always possible, especially if you are using third party libraries. You can't change the existing code. Okay, so before we move on, I would like to ask you for checking whether you have all the requirements. If you want to go together with me, you will need JDK, which is obvious. You will also need Apache Maven. Maven is a building tool, and if you don't know it, don't be afraid. Don't worry, we will use just two simple commands. And finally, we will need Eclipse IDE for Java or Java developers. During this workshop, we will use framework application, which is a very simple application, and it's not necessary to get all logic of this application. It is available on my GitHub as well as all these slides together with the application. So everyone who wants to go with me in this workshop, please tell me whether you have some technical issues. For example, Java or Maven or Eclipse IDE. This workshop is for Eclipse IDE, because I use Eclipse IDE as my primary development environment. But I bet that almost all such features will be available in other better IDs. And if not, you have to choose other IDs in your time. If you are configuring Maven, I would like to mention that you need to set Java home environment. Does anyone need more time? Okay, leave that. So, please go to the GitHub. And if you are familiar with Git, you can clone the project. Or you can download the zip file. Let's have a look. There is top releases. And here is a source called zip or tarjz. It's up to you. So you can download it. Once you unzip this zip file, sorry, it looks like there is print book and print book with all fixes and slides for this presentation. So change your directory to print book. And run the first Maven command. Maven clean verify. This will download all dependencies needed for this presentation, for this workshop. It will build the application and it will run the tests. You can see there are some failures and errors which we will fix during this workshop. Okay, if you are done, I would like to say something about this cool application. It's very simple. Print book is a collection of users, of registered users, which can be loaded from text file or you can save them to a text file. Each user can ask for a friendship, other user, and the other user can confirm this friendship or reject the friendship. As I said before, it's not necessary to understand the logic of this application because I will try to keep this workshop as simple as possible. Yes, of course. Yeah, it's on the GitHub. Here is the link, github.com. You can clone this repository or you can download the zip file. So let's start your Eclipse IDE. I would like to mention if you have any connection problems, I can give you the needed files on USB. I'll start your Eclipse IDE. When you are starting Eclipse IDE for the first time, it will ask you for selecting a workspace. So select any folder. It doesn't matter. Yeah, you can close this welcome screen. Select Java with respect to you. If you have already started this IDE, import our project. Typical Maven project, nothing special. Here is package explorer. So please invoke context menu and import. Since it is a Maven project, type Maven. Select existing Maven project. And using browse button, navigate to the project. This you should see is an L file, which is the cornerstone of Maven and press finish. If you have imported the project, let's have a look what is it in it. There are three. Java classes, user, friend book and friend book exception. Nothing more. We can see what are test classes. There are only two test classes, user test here and friend book test. There is also a first class, which is a test rule, but we will use it later in this workshop. So select user test, invoke context menu, and press J unit test. We can see that there are four tests and all tests pass, which is good. Do the same for the friend book test. And now we can see that there are some failures and errors. Before we start fixing these problems, these issues, I would like to say something about breakpoints, which is the cornerstone of debugging process. It is a place where the code execution will pause. For example, open friend book test here, invoke context menu, and select show line numbers. I will increase the size of the text. Can you see it? Should I increase it? Okay, there are no complaints. So I can continue. For example, this looks good. For example, we want to run to stop the code execution at line number 90. So put a breakpoint just by double-clicking. And let's put another breakpoint, for example, at line 94. And let's debug it here. Here is for executing programs in debug mode. And here are executing the programs without debugging mode. We will use the debugging mode. Select this friend book test. If you are running an application in debug mode for the first time, Eclipse asks you for switching to a debug perspective, which is good, and we will switch to the debug perspective. Here we can see that the code execution pauses at line number 90. Another very important view is variables view. Here we can see all related variables. And moving between breakpoints is very easy. The main button is resume here, resume button. So if you select the resume button, you will go to the next breakpoint. And you can see the code pauses at line number 94. Here in variables view we can see all users and all its attributes. And here I would like to mention very important view, which is debug view, which is a stack trace. You can see here all visited methods during the debugging process. There is a terminate button, so I think that's very meaningful. And here are three types of stepping. I usually use step over, so let's try step over. You will go line by line in the code. There is a step into. So go to line, for example, number 97. And when you press step into, you will go to the find user method. So try it. And here you are in the method find user. If you press step return, it means that the rest of the code is executed and the code execution will be paused in the method from which you step in here. And you can see that we are back in line number 97. Okay, and we can terminate. So I showed you very basic working with Java debugging and Eclipse IDE, where you can put a break point on the line with code, on an attribute, or on a method, or on a class. I won't show you such situation. This will be your homework. And you can also put a break point on an exception. And we will also try, I will also show you such situation. I showed you variables view where you can watch all related variables and even you can change the value of any variable here. So let's go to fix some tests. For example, this null pointer exception here we have. So we will go to fix find user tests. Here, just double click on the first line. Okay, nothing happens. So let's try to another one. And we are here where the null pointer exception happens. So put a break point to line number 52, the old. And we want to run only one test. So here again, I'll show debug configurations. And here by button search, specify find user tests, apply debug. So we are the code execution suspended. We can watch the user. The user is not null nor it's a nickname. So go to the next break point again, again, again, again. And now here we can see that the nickname is null. So this is the place where null pointer exception is from. And we can select which one is it. It is Mike, funny. We can go back and select the previous frame. And here we can see that we try to register the user whose nickname is null. Okay, we can terminate this execution. The problem here was that we were stepping to next break point again, again, again. And this is not very useful and it is very time consuming and annoying as well. That's why we have to specify exactly where we want to suspend the code execution. For this purpose, we can use, for example, setting a break point on the null pointer exception. Okay, so delete all break points. And here at Java exception break point, you can specify, in our case, null pointer exception. Select and start the debugging process again. Now we can see that here is user exactly with name null. And I bet it is our problematic Mike, funny. Yes, okay, this is very useful. But sometimes the place where we want to suspend our code execution is a little bit complicated. So here we can also specify our own condition. Let's put the break point as in the previous example. And here it is hidden now. When you double click on break points view, you can see checkbox conditional. Here you must find the arrow. And now you can specify your own condition. For example, if user null or if user... Do you have a code completion in that? Do you have a code completion in that? Yes, of course. You wrote return from scratch and use as well, so that's why I was wondering. From scratch? Yes, you wrote it on the keyboard. But I use code completion all the time, so that's why I was wondering about something. Yes, yes, of course. I use code completion. Thanks for your point. You can use code completion everywhere in Eclipse. Tell me social about that. It is Eclipse. Okay. So you can specify any logic for your condition and press Ctrl S, you have to say. And when you start again, you can see the code. Yes. You can double click on this break point view. And here is a conditional. But here is an arrow and you have to find this arrow. Yeah, this is... I'm not sure whether it is bugged, but it's not very user friendly. I agree. Sorry, sorry. You can't specify your own condition on exceptional break point or break point on attribute. You can specify the break point only if it is set on a line with code. And in your case, you have now pointed an exceptional break point. So please remove it and set a break point on friend book class on line number 52. Then you can see and you are allowed to specify the condition. Okay, you can terminate it. And if you, for example, specify a break point on an attribute, you can specify whether you want to suspend the code on any access of the attribute or on any modification of the attribute. Very similar. It is in break point specified on a method. And of course you can... I'm sorry. I was wrong when I said you can't specify your own condition on a method break point. But moreover, you can specify break point on every entry of the method or every exit of the method. Here is a small summary that you can... It is very important to define when you want to... Or where you want to stop the code execution. And it depends on your experiences and on your intuition, of course. I have already mentioned all such situations. And now I will show you some more important things, which usually people don't know about debugging. And it is very important to see what you really want to see, what will help you to find where is the bug and how to fix it. For example, I will use this break point. 52. Check whether this is the only one break point around the execution. Once the code is suspended, you can select user, involve context menu, and there is an option display. You can see this is exactly what you see in variables view. It is calling method of two string for the user. Or you can select more. You can select a nickname, select display. Well, now you can see that it is very happy. This is a nickname for Paul Happy. You can select the whole condition as well and select display. Now you can see the result, which is false. And you can do this for any part of the code. You can also specify code, for example, here is display. Okay, sorry. You must be on line number 21, then you can specify more. Here I would like to show you also how to improve the variables view. I will maximize it. And here we can see user and its nickname. For example, I want to see also user's name and surname, for example. So select here on the right side, user, involve context menu. And here is an option called new detail formatter. You can specify it. And here we want to get name plus surname and also nickname. The only one disadvantage of this is that there is no code checker. So for example, it doesn't warn me that there is missing plus. But I think that we can leave it. Okay, and now we can see exactly what we wanted. Paul Happy is happy. So let's fix our test. We found out that the problem was this null pointer. Oh, and one more thing, which is quite cool. It is a display view, but this view is hidden, but it's very useful. Please window, show view. And here is display. And here in display, you can type any code you want. For example, I want to see we can, for example, we can use our favorite system, our print line. And we can print getting name, for example, to uppercase. This is just for example, it doesn't matter what you do. Then you select code, and here is a button, execute the selected text. And it is. You are still suspended in the code execution, but you can evoke and execute any method you wish. Of course, it must be related to the context. I didn't try to be honest. I usually don't debug multi-traded applications, and I'm not sure. I'm not sure. I know that there is an option for suspending any thread view collapse. Here we have two threads, and when you specify and select some thread, here is a button, suspend. But I'm not very familiar with this. So this is quite cool, and we can sum it up. What kind of view? Yes. There is an option. Oh, I'm not seeing. Okay, display. Select display result. Oh, okay. Again. No, it's okay. There are two options. Displaying result in the display view. You can see exactly the string and its value. Or you can execute, for example, the code with system outprint line. You can put any loop or any code. Okay, so I showed you how you can specify detail parameters to view what you want to see in the variables view, how to display and watch parts of the code, how to execute your own code to see some more information. And I forgot one issue. I will show you in a minute. So let's see what other tests must be fixed. These run configurations and delete test method. Apply run. Now here is a remove test failure. Something wrong here. So this method, this test method, just check whether removing the user from the collection is working. So we can see that the problem happened on line number 51. So it means that friend book remove user John Doe is working, but the second user is not working. Okay, have a look at it. So let's put the breakpoint also on line number 46 and start debugging. Remove this breakpoint, sorry. I forgot to remove it. Now we are in our test method. Breakpoint, for example, what we want to see. We want to see users. Now in the users, which is a hash set, there are two users. There is Paul Happy and John Doe. So step over. Now we see that John Doe was removed successfully. Step over. Over. Paul Happy is still there. Remove it. And he is still there. So something's wrong. Does anyone know what can cause this behavior? Why one user is not removed from the collection? Yes, exactly. Thank you. You deserve a gift. Because as a collection for users, I use hash set. And the most important thing in hash set is hash code. But let's check whether the hash code is really changed. For example, we can do it by specifying a formatter. So select the hash set in detail formatter. And we can use string buffer as a result. And for any object in the set, a result object to string. And we want to see its hash code. And return our result. I think it's okay. It should be fine. Press okay. And I have one error. Same problem you've missing. Okay, let's start it once again. Friend book and users. Oh, what about something like this? Result up and new line. Now we can see users with its hash codes. So step over. John Doe was removed successfully. And Paul Happy has hash code which ends with 591. Okay, we will steal. And now we can see that the hash code was changed. And I suspect that it was caused by method set surname. So let's have a look what's here. And this is the user class. And I'm wondering what's the implementation of hash code. We can see that the hash code depends on its name, nickname and surname. But in our application, we require only the nickname. Because, you know, sometimes you have to allow users to change its name, his or her name. So fix it. Just remove all lines related to the name and surname. And of course, when we change equals hash code method, we have to change equals method. So delete also lines related to surname and name in equals method. You can see the friend book test. You can see that all tests passed. Yeah, and I wanted to show you also logical structure in variables view. Let's do it one again. I don't have a break point. Yeah, I'm here. And I maximized variables view. Now here is hash set. And when we expand users field, there is map, entry set, key set. By default, you are confronted by implementation details. But when we want to watch for some helpful information, it is nice to display this information in another way. So select users and there is button logical structure. And now we can see user and we can watch his attributes. Cool, we can terminate it. The next topic is about repeating code and methods. You saw that we had to terminate and run the test again and again and again. And sometimes this is quite annoying, especially if there are some settings of testing environment, which may take a long time. For example, show you how to repeat the code. Very simple example. Remove break points. And for example, friend who will here register user. Check whether the test method is empty. Debug it and we can see, we can watch users, users is empty. Step over and John Doe is here. And how to repeat this code once again. Here is debug view and you can select the frame. And here is a magic button called drop to frame. No, 13, 14. The most important are generally double sessions. So if you need to repeat the same in 15, the next one begins at 2 p.m. Okay, so I will continue. There is a magic button, drop to frame. And you can jump again to this method. Then you can go step over, step over. See the users. There is one user again. There is one user because, still one user because we use Hashtag. But we use another implementation of our collection. For example, error list. See the users will be accumulated. Step over and now we can see that we have two John Doe's. Here I would like to mention that you are not going back. You are just repeating the code. So which means that all variables which were modified, they remain modified. So you have to take it into account. In general, you can jump to any frame listing debug view. For example, into these you cannot. In general, you can jump into any frame except methods which are native methods and methods which are called directly from such native methods. This is our example because JUnit uses some native methods and this method directly calls a clean friend book. But there is a very simple workaround and a little bit stupid. You can try it, create new method, public void full for example and put the whole code into it and now call full. Try to run it once again. Here we can jump to register user and we can also jump to the full method. I don't know why there is such restriction in Eclipse IDE but you can do it in such a way and the same applies also in test methods. But what about if you have about 20 test methods? It's a little bit annoying to create such full methods for each method but I usually use a test rule which is this class. This test rule was introduced in JUnit and it allows you to add some more functionality to your test methods. Here online number 15 based is your test method and by calling this evaluate the test method is called. I usually put system of print line or it can be any line with code and I usually put breakpoints in line number 16 and line number 18 so that I can repeat our test method. So let's put the breakpoints this working out requires changing the code a little bit. You have to register the test rule but there are only two lines of code which is I think okay still better than creating new full methods for 20 times. Let's debug test for example our favorite find user test I usually disable the breakpoints in the test rule because I want to use it only in some specific situations for example in full method okay go next something's wrong let's do it once again okay disable it print book test for example this one now the code is suspended in line number 15 and we can move on and if we want to repeat it once again just enable the breakpoints go to next and now we are here in our test rule and we can jump to it and run the test method again aha sorry again step over and let's proceed the execution to our test rule here and we can jump and we are again in the same method and we can continue debugging the same method again and again until we find the problem cool so here is a small summary we can jump in the stack trace we can jump to native frame or any of its successor and here is a workaround for JUnit which I usually use and the last topic is remote debugging which is very useful for example if you run your test on Jenkins and something wrong has happened on the remote slave but locally everything seems to be working so you have to run the application in debug mode here is a magic command you have to use and the most important thing here you have to specify address which means that the application is executed in debugging mode and is suspended and waits and our listening on port you specify for example we fix the test but try to run it and we still see that there is some problem let's have a look where is the problem friend book line 72 so find friend book find friend book it's not here friend book line 72 here and we will start the test in debug mode from Maven have a look at the POM XML file and here is our magic command in Maven you have to set it in property called Maven should fire debug and it is specified in profile and this profile is activated whenever you define property debug port so let's run our application in debug mode by specifying debug port I usually use 801 I think the default one is 505 but it doesn't matter it must be port which is not in use that's all start it and we can see that code execution suspended and is listening on port 801 go back to Eclipse IDE and here in debug configurations we till this moment we use only JUnit configurations but here is also remote Java application so create a new launch here is a button for it and let's call it remote friend book test the project is friend book connection type is OK here you can specify the host if it is on some window slave we will just specify the host and our port is 801 also check allow termination of remote VM I think this is quite meaningful apply and debug we see that here we can see that once we connect it to the to the remote application the application start it and pauses at our breakpoint so let's see what's wrong in variables view and we can see that data is really empty line is empty and file is oops there is friend book error now we have to find out what has changed that we are using this error file so in the debug view just jump we are not jumping just switching to the context of the frame and see here we expect friend book OK file let's display what's in this and we can see that the error file was set by our system property now we have to find out what changes the system property and because we are running the test or running the application from Maven so probably Maven is setting the system property so stop it and let's have a look on the POM file and here it is we accidentally set the wrong testing file so we can fix it run again Maven clean verify and it should be OK now cool now our application and test are fixed and everything is working and I would like to mention that there is there are two projects friend book with all fixes there are much more tests and of course there is jcoco which is code coverage so if you want you can have a look how such project looks like and from my side and my last message to you is play with your IDE to find what kind of debugging features provides it will really save your time in the future and if you are interesting more in debugging Eclipse IDE here are some sites you can visit also something more and something is described in more detail so please have a look at it any questions no, thank you for your attention enjoy debugging thank you for your attendance and I would like to just share you can make a feedback so here the feedback is available for each session and please can I have a look at my feedback or the readers and the organizers it is the organizers I think that it will be in your position so it will be in your position and you can it is in front of the desktop it is when you go to the real it is in the right and here is the and here is the and here is the and here is the you can come I have something to explain from you I can get your feedback it is really interesting like maybe there is a I don't know how to say it. It's mostly things that are hidden in people's minds. You know I'm also a scientist. Do you know where we are? Well, it's just that it's hidden there, right? And the person doesn't find it. That's why it's the first thing you can do. You can let go of a person. It's just that we're here, you know. Do you know where we are? It's just that it's behind the curtain. Do you know where we are? We're going to talk to you about something. I don't know. It's a great place. It's a great place. It's a great place. It's a great place. It's a great place. It's a great place. It's a great place. It's a great place. It's a great place. The slingshot is very important to keep track of the situation. I feel very safe here. I have live-time interviews with other staff, I hope they find out where they are. I think the situation is very important, because the second meeting is also a part of this project. I think it's important to keep track of the situation. I think it's also very important to keep track of the situation. Especially after the events that take place. This is a very important project. My work is more than just a project. Thank you very much! I'm a little bit worried about that, but you're doing it. It's the same. It's the same. We can do it. It's the same. It's the same. You can do it. It's the same. I don't know. It's the same. It's the same. It's the same. I understand the person here, but this is the last real thing that I want to describe to you. This is the second half of the day. It's all right. No, it's not too much. It's all right. It's all right. It's all right. It's all right. It's all right. It's all right. It's all right. It's all right. Alright, please change the camera, paradise is not alone. It's all right. In general, the hard work performed is relatively simple during this interview. Everything is fine through our channel just margins. Definitely on our channel, I mean, thanks for the interview. Alright, ok, it's all right. How does it work? What does it tell me? Let's see... It does. And this click disappears. You can't see anything. What would be the worst that a person would have to rent a laptop like this? We would have minimized it and a guy would do some gimp logo. Good day. I'm not sure. I don't know, I have some kind of a sadistic history. So you're going to change your mind. Yeah, there are no people here, so it will be really interesting. It will be interesting to have some time to interact with your team. Your name will tell you who is ready, then you will be behind the scenes, you will want to take it, then you will be here, and you will change your mind. And sleep. Good. I think it would be better to play between open-stank peki-jingue and platform peki-jingue So I can do it Tell me about the pre-plot Well, on the platform Yes It's been a long time since the last time Oh, yeah, okay, I will put it to my presentation Right now Oh, this is a funny resolution Okay So Yeah Oh, fuck this