|
| 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.plugin.internal; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.nio.charset.Charset; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | + |
| 22 | +import org.eclipse.core.resources.IFile; |
| 23 | +import org.eclipse.core.resources.IProject; |
| 24 | +import org.eclipse.core.resources.IProjectDescription; |
| 25 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 26 | +import org.eclipse.jdt.core.IClasspathEntry; |
| 27 | +import org.eclipse.jdt.core.IJavaProject; |
| 28 | +import org.eclipse.jdt.core.JavaCore; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.rules.TemporaryFolder; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.junit.runners.Parameterized; |
| 35 | +import org.junit.runners.Parameterized.Parameters; |
| 36 | + |
| 37 | +import com.microsoft.java.debug.core.JavaBreakpointLocation; |
| 38 | +import com.microsoft.java.debug.core.protocol.Types.SourceBreakpoint; |
| 39 | + |
| 40 | +@RunWith(Parameterized.class) |
| 41 | +public class JdtSourceLookUpProviderTest { |
| 42 | + private enum SourceKind { |
| 43 | + EXTERNAL, WORKSPACE, FILE_CHARSET, PROJECT_CHARSET |
| 44 | + } |
| 45 | + |
| 46 | + private static final String PACKAGE_NAME = "d\u00e9mo"; // A non-ASCII identifier exposes incorrect decoding. |
| 47 | + private static final String SOURCE = String.join("\r\n", |
| 48 | + "package " + PACKAGE_NAME + ";", |
| 49 | + "", |
| 50 | + "public class A {", |
| 51 | + " public static void main(String[] args) {", |
| 52 | + " int x = 1;", |
| 53 | + " System.out.println(x);", |
| 54 | + " }", |
| 55 | + "}", |
| 56 | + ""); |
| 57 | + |
| 58 | + @Rule |
| 59 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 60 | + |
| 61 | + private final Charset charset; |
| 62 | + private final boolean withBom; |
| 63 | + private final SourceKind sourceKind; |
| 64 | + private IProject project; |
| 65 | + |
| 66 | + public JdtSourceLookUpProviderTest(Charset charset, boolean withBom, SourceKind sourceKind) { |
| 67 | + this.charset = charset; |
| 68 | + this.withBom = withBom; |
| 69 | + this.sourceKind = sourceKind; |
| 70 | + } |
| 71 | + |
| 72 | + @Parameters(name = "{index}: {0}, BOM={1}, {2}") |
| 73 | + public static Object[][] encodings() { |
| 74 | + return new Object[][] { |
| 75 | + {StandardCharsets.UTF_16LE, true, SourceKind.EXTERNAL}, |
| 76 | + {StandardCharsets.UTF_16BE, true, SourceKind.EXTERNAL}, |
| 77 | + {StandardCharsets.UTF_8, true, SourceKind.EXTERNAL}, |
| 78 | + {StandardCharsets.UTF_8, false, SourceKind.EXTERNAL}, |
| 79 | + {Charset.defaultCharset(), false, SourceKind.EXTERNAL}, |
| 80 | + {StandardCharsets.UTF_16LE, true, SourceKind.WORKSPACE}, |
| 81 | + {StandardCharsets.UTF_16BE, true, SourceKind.WORKSPACE}, |
| 82 | + {StandardCharsets.UTF_8, true, SourceKind.WORKSPACE}, |
| 83 | + {StandardCharsets.UTF_16LE, false, SourceKind.FILE_CHARSET}, |
| 84 | + {StandardCharsets.UTF_16BE, false, SourceKind.FILE_CHARSET}, |
| 85 | + {StandardCharsets.UTF_8, false, SourceKind.FILE_CHARSET}, |
| 86 | + {StandardCharsets.ISO_8859_1, false, SourceKind.FILE_CHARSET}, |
| 87 | + {StandardCharsets.ISO_8859_1, false, SourceKind.PROJECT_CHARSET}, |
| 88 | + {StandardCharsets.UTF_16LE, false, SourceKind.PROJECT_CHARSET} |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + @After |
| 93 | + public void deleteProject() throws Exception { |
| 94 | + if (project != null && project.exists()) { |
| 95 | + project.delete(true, true, null); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void resolvesBreakpointClassName() throws Exception { |
| 101 | + byte[] contents = (withBom ? "\uFEFF" + SOURCE : SOURCE).getBytes(charset); |
| 102 | + String uri; |
| 103 | + if (sourceKind == SourceKind.EXTERNAL) { |
| 104 | + Path file = temporaryFolder.getRoot().toPath().resolve("A.java"); |
| 105 | + Files.write(file, contents); |
| 106 | + uri = file.toUri().toASCIIString(); |
| 107 | + } else { |
| 108 | + IFile file = createWorkspaceFile(contents); |
| 109 | + uri = file.getLocationURI().toASCIIString(); |
| 110 | + } |
| 111 | + |
| 112 | + JavaBreakpointLocation[] locations = new JdtSourceLookUpProvider().getBreakpointLocations(uri, |
| 113 | + new SourceBreakpoint[] {new SourceBreakpoint(5, -1)}); |
| 114 | + |
| 115 | + assertEquals(1, locations.length); |
| 116 | + assertEquals(PACKAGE_NAME + ".A", locations[0].className()); |
| 117 | + assertEquals(5, locations[0].lineNumber()); |
| 118 | + assertEquals(5, locations[0].availableBreakpointLocations()[0].line); |
| 119 | + } |
| 120 | + |
| 121 | + private IFile createWorkspaceFile(byte[] contents) throws Exception { |
| 122 | + project = ResourcesPlugin.getWorkspace().getRoot().getProject("source-encoding-test"); |
| 123 | + project.create(null); |
| 124 | + project.open(null); |
| 125 | + project.setDefaultCharset(StandardCharsets.UTF_8.name(), null); |
| 126 | + IProjectDescription description = project.getDescription(); |
| 127 | + description.setNatureIds(new String[] {JavaCore.NATURE_ID}); |
| 128 | + project.setDescription(description, null); |
| 129 | + IJavaProject javaProject = JavaCore.create(project); |
| 130 | + javaProject.setRawClasspath(new IClasspathEntry[] {JavaCore.newSourceEntry(project.getFullPath())}, |
| 131 | + project.getFullPath().append("bin"), null); |
| 132 | + |
| 133 | + IFile file = project.getFile("A.java"); |
| 134 | + file.create(new ByteArrayInputStream(contents), true, null); |
| 135 | + if (sourceKind == SourceKind.FILE_CHARSET) { |
| 136 | + file.setCharset(charset.name(), null); |
| 137 | + } else if (sourceKind == SourceKind.PROJECT_CHARSET) { |
| 138 | + project.setDefaultCharset(charset.name(), null); |
| 139 | + } |
| 140 | + return file; |
| 141 | + } |
| 142 | +} |
0 commit comments