use of com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudBreakpointHandler method registerBreakpoint.
/**
* Called by IntelliJ when the user has enabled or created a new breakpoint. Creates the server
* breakpoint.
*
* @param ideBreakpoint breakpoint to register
*/
@Override
public void registerBreakpoint(@NotNull final XLineBreakpoint<CloudLineBreakpointProperties> ideBreakpoint) {
if (ideBreakpoint.getSourcePosition() == null || !ideBreakpoint.isEnabled() || !(ideBreakpoint.getType() instanceof CloudLineBreakpointType)) {
return;
}
com.intellij.debugger.ui.breakpoints.Breakpoint cloudIdeBreakpoint = BreakpointManager.getJavaBreakpoint(ideBreakpoint);
if (!(cloudIdeBreakpoint instanceof CloudLineBreakpointType.CloudLineBreakpoint)) {
LOG.error("breakpoint was not of the correct type to create on the cloud. It was not a " + "CloudLineBreakpoint");
return;
}
final CloudLineBreakpointType.CloudLineBreakpoint cloudIdeLineBreakpoint = (CloudLineBreakpointType.CloudLineBreakpoint) cloudIdeBreakpoint;
if (ideBreakpoint.getProperties().isCreatedByServer()) {
// gets called during construction.
return;
}
if (ideBreakpoint.getProperties().isAddedOnServer() && !ideBreakpoint.getProperties().isDisabledByServer()) {
// disabled breakpoint
return;
}
PsiFile javaFile = psiManager.findFile(ideBreakpoint.getSourcePosition().getFile());
if (!(javaFile instanceof PsiJavaFile)) {
return;
}
SourceLocation location = new SourceLocation();
// Sending the file as com/package/example/Class.java to Cloud Debugger because it plays nice
// with the CDB plugin. See ServerToIdeFileResolver.
location.setPath(ServerToIdeFileResolver.getCloudPathFromJavaFile((PsiJavaFile) javaFile));
location.setLine(ideBreakpoint.getSourcePosition().getLine() + 1);
Breakpoint serverNewBreakpoint = new Breakpoint();
serverNewBreakpoint.setLocation(location);
if (ideBreakpoint.getConditionExpression() != null) {
serverNewBreakpoint.setCondition(ideBreakpoint.getConditionExpression().getExpression());
}
List<String> watches = cloudIdeLineBreakpoint.getWatchExpressions();
if (watches != null) {
serverNewBreakpoint.setExpressions(watches);
}
// The breakpoint will enter error state asynchronously. For now, we state that its verified.
process.getStateController().setBreakpointAsync(serverNewBreakpoint, new SetBreakpointHandler() {
@Override
public void onSuccess(@NotNull final String id) {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (!Strings.isNullOrEmpty(id)) {
if (!cloudIdeLineBreakpoint.isEnabled()) {
process.getStateController().deleteBreakpointAsync(// race condition
id);
} else {
// Success.
// Mark as added so we don't add it again.
ideBreakpoint.getProperties().setAddedOnServer(true);
cloudIdeLineBreakpoint.setErrorMessage(null);
process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
}
} else {
// TODO(joaomartins): Why couldn't the breakpoint be set? Improve this
// message.
cloudIdeLineBreakpoint.setErrorMessage(GctBundle.getString("clouddebug.errorset"));
process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
}
if (!Strings.isNullOrEmpty(id)) {
ideBreakpoint.getProperties().setDisabledByServer(false);
String oldId = ideBreakpoint.getUserData(CLOUD_ID);
if (!Strings.isNullOrEmpty(oldId)) {
ideBreakpoints.remove(oldId);
}
ideBreakpoint.putUserData(CLOUD_ID, id);
ideBreakpoints.put(id, ideBreakpoint);
}
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
runnable.run();
} else {
SwingUtilities.invokeLater(runnable);
}
}
@Override
public void onError(String errorMessage) {
cloudIdeLineBreakpoint.setErrorMessage(errorMessage);
process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
}
});
}
use of com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudBreakpointHandlerTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
fixture = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getTestName(true)).getFixture();
fixture.setUp();
project = new MockProjectEx(getTestRootDisposable());
psiManager = mock(PsiManager.class);
project.registerService(PsiManager.class, psiManager);
XDebugSession session = mock(XDebugSession.class);
when(session.getProject()).thenReturn(project);
process = mock(CloudDebugProcess.class);
when(process.getXDebugSession()).thenReturn(session);
CloudDebugProcessState processState = mock(CloudDebugProcessState.class);
existingBreakpoints = new ArrayList<Breakpoint>();
when(processState.getCurrentServerBreakpointList()).thenReturn(ContainerUtil.immutableList(existingBreakpoints));
when(process.getProcessState()).thenReturn(processState);
stateController = mock(CloudDebugProcessStateController.class);
when(process.getStateController()).thenReturn(stateController);
registrationShouldSucceed = true;
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
addedBp.set((Breakpoint) invocation.getArguments()[0]);
SetBreakpointHandler handler = (SetBreakpointHandler) invocation.getArguments()[1];
if (registrationShouldSucceed) {
handler.onSuccess(desiredResultId);
} else {
handler.onError("Registration failed");
}
return null;
}
}).when(stateController).setBreakpointAsync(any(Breakpoint.class), any(SetBreakpointHandler.class));
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
removedBp.set((String) invocation.getArguments()[0]);
return null;
}
}).when(stateController).deleteBreakpointAsync(anyString());
fileResolver = mock(ServerToIdeFileResolver.class);
handler = new CloudBreakpointHandler(process, fileResolver);
XDebuggerManager debuggerManager = mock(XDebuggerManager.class);
project.addComponent(XDebuggerManager.class, debuggerManager);
breakpointManager = mock(XBreakpointManager.class);
when(debuggerManager.getBreakpointManager()).thenReturn(breakpointManager);
}
Aggregations