|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Microsoft Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.core.adapter.handler; |
| 13 | + |
| 14 | +import static org.easymock.EasyMock.expect; |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.nio.file.Paths; |
| 21 | + |
| 22 | +import org.easymock.EasyMockSupport; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.TemporaryFolder; |
| 26 | + |
| 27 | +import com.microsoft.java.debug.core.adapter.DebugAdapterContext; |
| 28 | +import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider; |
| 29 | +import com.microsoft.java.debug.core.adapter.ProviderContext; |
| 30 | +import com.microsoft.java.debug.core.adapter.Source; |
| 31 | +import com.microsoft.java.debug.core.adapter.SourceType; |
| 32 | +import com.microsoft.java.debug.core.protocol.Types; |
| 33 | + |
| 34 | +public class StackTraceRequestHandlerTest extends EasyMockSupport { |
| 35 | + @Rule |
| 36 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void convertDebuggerSourceShouldPreferSourcePathsOverCachedJdtUri() throws Exception { |
| 40 | + String fullyQualifiedName = "com.example.Foo"; |
| 41 | + String sourceName = "Foo.java"; |
| 42 | + String relativeSourcePath = Paths.get("com", "example", sourceName).toString(); |
| 43 | + String jdtUri = "jdt://contents/foo.jar/com.example/Foo.java?=handle"; |
| 44 | + Path sourceRoot = tempFolder.newFolder("sources").toPath(); |
| 45 | + Path sourceFile = sourceRoot.resolve(relativeSourcePath); |
| 46 | + Files.createDirectories(sourceFile.getParent()); |
| 47 | + Files.write(sourceFile, "package com.example; class Foo {}\n".getBytes(StandardCharsets.UTF_8)); |
| 48 | + |
| 49 | + ISourceLookUpProvider sourceProvider = createMock(ISourceLookUpProvider.class); |
| 50 | + expect(sourceProvider.getSource(fullyQualifiedName, relativeSourcePath)) |
| 51 | + .andReturn(new Source(jdtUri, SourceType.LOCAL)) |
| 52 | + .once(); |
| 53 | + replayAll(); |
| 54 | + |
| 55 | + ProviderContext providerContext = new ProviderContext(); |
| 56 | + providerContext.registerProvider(ISourceLookUpProvider.class, sourceProvider); |
| 57 | + DebugAdapterContext context = new DebugAdapterContext(null, providerContext); |
| 58 | + context.setSourcePaths(new String[0]); |
| 59 | + |
| 60 | + Types.Source jdtSource = StackTraceRequestHandler.convertDebuggerSourceToClient(fullyQualifiedName, sourceName, |
| 61 | + relativeSourcePath, context); |
| 62 | + assertEquals(jdtUri, jdtSource.path); |
| 63 | + |
| 64 | + context.setSourcePaths(new String[] { sourceRoot.toString() }); |
| 65 | + Types.Source localSource = StackTraceRequestHandler.convertDebuggerSourceToClient(fullyQualifiedName, sourceName, |
| 66 | + relativeSourcePath, context); |
| 67 | + |
| 68 | + assertEquals(sourceFile.toString(), localSource.path); |
| 69 | + assertEquals(0, localSource.sourceReference); |
| 70 | + |
| 71 | + context.setClientPathsAreUri(true); |
| 72 | + Types.Source localUriSource = StackTraceRequestHandler.convertDebuggerSourceToClient(fullyQualifiedName, |
| 73 | + sourceName, relativeSourcePath, context); |
| 74 | + |
| 75 | + assertEquals(sourceFile.toUri().toString(), localUriSource.path); |
| 76 | + verifyAll(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void convertDebuggerSourceShouldHandleSourceWithNullType() throws Exception { |
| 81 | + String fullyQualifiedName = "com.example.Bar"; |
| 82 | + String sourceName = "Bar.java"; |
| 83 | + Path sourceFile = tempFolder.newFile(sourceName).toPath(); |
| 84 | + String sourceUri = sourceFile.toUri().toString(); |
| 85 | + |
| 86 | + ISourceLookUpProvider sourceProvider = createMock(ISourceLookUpProvider.class); |
| 87 | + expect(sourceProvider.getSource(fullyQualifiedName, sourceName)) |
| 88 | + .andReturn(new Source(sourceUri, null)) |
| 89 | + .once(); |
| 90 | + replayAll(); |
| 91 | + |
| 92 | + ProviderContext providerContext = new ProviderContext(); |
| 93 | + providerContext.registerProvider(ISourceLookUpProvider.class, sourceProvider); |
| 94 | + DebugAdapterContext context = new DebugAdapterContext(null, providerContext); |
| 95 | + |
| 96 | + Types.Source source = StackTraceRequestHandler.convertDebuggerSourceToClient(fullyQualifiedName, sourceName, |
| 97 | + sourceName, context); |
| 98 | + |
| 99 | + assertEquals(sourceFile.toString(), source.path); |
| 100 | + assertEquals(0, source.sourceReference); |
| 101 | + verifyAll(); |
| 102 | + } |
| 103 | +} |
0 commit comments