Skip to content

Commit

Permalink
Yaml -> Props conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Mar 20, 2024
1 parent 8bf29f4 commit 3a0ee63
Show file tree
Hide file tree
Showing 8 changed files with 690 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.jdt.core,
org.springsource.ide.eclipse.commons.livexp,
org.springsource.ide.eclipse.commons.frameworks.core,
org.springsource.ide.eclipse.commons.frameworks.test.util,
org.springframework.ide.eclipse.boot.launch,
org.eclipse.jdt.launching,
org.eclipse.debug.ui,
org.mockito,
org.springframework.ide.eclipse.boot,
org.eclipse.core.expressions,
org.springframework.ide.eclipse.boot.test,
Expand All @@ -26,7 +24,6 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.equinox.security,
org.reactivestreams.reactive-streams;bundle-version="1.0.2",
io.projectreactor.reactor-core;bundle-version="[3.3.1,3.3.2)",
javax.ws.rs;bundle-version="2.0.1",
org.eclipse.osgi,
org.hamcrest.library
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.eclipse.boot.refactoring.test;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.springsource.ide.eclipse.commons.tests.util.StsTestCase.assertContains;
import static org.springsource.ide.eclipse.commons.tests.util.StsTestCase.createFile;

import java.util.concurrent.CompletableFuture;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ide.eclipse.boot.refactoring.ConvertYamlToPropertiesRefactoring;
import org.springframework.ide.eclipse.boot.refactoring.test.ConvertPropertiesToYamlTest.Checker;
import org.springframework.ide.eclipse.boot.test.BootProjectTestHarness;
import org.springsource.ide.eclipse.commons.frameworks.core.util.IOUtil;
import org.springsource.ide.eclipse.commons.tests.util.StsTestUtil;

public class ConvertYamlToPropertiesTest {

BootProjectTestHarness projects = new BootProjectTestHarness(getWorkspace());

protected IWorkspace getWorkspace() {
return ResourcesPlugin.getWorkspace();
}

@Before public void setup() throws Exception {
StsTestUtil.deleteAllProjects();
}

private void do_conversionTest(String input, String expectedOutput) throws Exception {
do_conversionTest(input, expectedOutput, (status) -> {
assertEquals(RefactoringStatus.OK, status.getSeverity());
});
}

private void do_conversionTest(String input, String expectedOutput, Checker<RefactoringStatus> statusChecker) throws Exception {
IProject project = projects.createProject("conversionTestProject");
IFile yamlFile = createFile(project, "application.yml", input);
IFile propsFile = project.getFile("application.properties");
if (propsFile.exists()) {
propsFile.delete(true, new NullProgressMonitor());
}

assertTrue(yamlFile.exists());
assertFalse(propsFile.exists());

ConvertYamlToPropertiesRefactoring refactoring = new ConvertYamlToPropertiesRefactoring(yamlFile);
statusChecker.check(refactoring.checkAllConditions(new NullProgressMonitor()));
perform(refactoring);

assertFalse(yamlFile.exists());
assertTrue(propsFile.exists());

assertEquals(expectedOutput, IOUtil.toString(propsFile.getContents()));
}

private void assertFile(IProject project, String path, String expectedContents) throws Exception {
IFile file = project.getFile(path);
assertTrue(file.getFullPath().toString(), file.exists());
assertEquals(expectedContents, IOUtil.toString(file.getContents()));
}

private void perform(ConvertYamlToPropertiesRefactoring refactoring) throws Exception {
Change change = refactoring.createChange(new NullProgressMonitor());
IWorkspace workspace = getWorkspace();
CompletableFuture<Void> result = new CompletableFuture<>();
Job job = new Job(refactoring.getName()) {

@Override
protected IStatus run(IProgressMonitor monitor) {
try {
workspace.run(new PerformChangeOperation(change), monitor);
result.complete(null);
} catch (Throwable e) {
result.completeExceptionally(e);
}
return Status.OK_STATUS;
}
};
job.setRule(workspace.getRuleFactory().buildRule());
job.schedule();
result.get();
}

private ConvertYamlToPropertiesRefactoring do_hasComments_test(IProject project, String comment) throws Exception {
IFile yamlFile = createFile(project, "src/main/resources/application.yml",
"some:\n property: somevalue\n"+
comment +"\n" +
"other:\n property: othervalue"
);
ConvertYamlToPropertiesRefactoring refactoring = new ConvertYamlToPropertiesRefactoring(yamlFile);
assertOkStatus(refactoring.checkInitialConditions(new NullProgressMonitor()));
assertStatus(refactoring.checkFinalConditions(new NullProgressMonitor()), RefactoringStatus.WARNING, "has comments, which will be lost");
return refactoring;
}

private ConvertYamlToPropertiesRefactoring do_hasInlineComments_test(IProject project, String comment) throws Exception {
IFile yamlFile = createFile(project, "src/main/resources/application.yml",
"some:\n property: somevalue\n"+
"foo: bar " + comment +"\n" +
"other:\n property: othervalue"
);
ConvertYamlToPropertiesRefactoring refactoring = new ConvertYamlToPropertiesRefactoring(yamlFile);
assertOkStatus(refactoring.checkInitialConditions(new NullProgressMonitor()));
assertStatus(refactoring.checkFinalConditions(new NullProgressMonitor()), RefactoringStatus.WARNING, "has comments, which will be lost");
return refactoring;
}

private void assertOkStatus(RefactoringStatus s) {
assertEquals(RefactoringStatus.OK, s.getSeverity());
}

private void assertStatus(RefactoringStatus status, int expectedSeverity, String expectedMessageFragment) {
assertEquals(expectedSeverity, status.getSeverity());
StringBuilder allMessages = new StringBuilder();
for (RefactoringStatusEntry entry : status.getEntries()) {
allMessages.append(entry.getMessage());
allMessages.append("\n-------------\n");
}
assertContains(expectedMessageFragment, allMessages.toString());
}

@Test public void fileIsInTheWay() throws Exception {
IProject project = projects.createBootProject("fileIsInTheWay");
IFile yamlFile = createFile(project, "src/main/resources/application.yml", "someting: already-in-here");
IFile propsFile = project.getFile("src/main/resources/application.properties");
assertTrue(propsFile.exists());
ConvertYamlToPropertiesRefactoring refactoring = new ConvertYamlToPropertiesRefactoring(yamlFile);

RefactoringStatus status = refactoring.checkInitialConditions(new NullProgressMonitor());
assertStatus(status, RefactoringStatus.FATAL, "'/fileIsInTheWay/src/main/resources/application.properties' already exists");
}

@Test public void hasLineComments() throws Exception {
IProject project = projects.createBootProject("hasComments");
IFile propsFile = project.getFile("src/main/resources/application.properties");
if (propsFile.exists()) {
propsFile.delete(true, new NullProgressMonitor());
}

do_hasComments_test(project, "#comment");
ConvertYamlToPropertiesRefactoring refactoring = do_hasComments_test(project, " #!comment");

perform(refactoring); //Despite the warning, the refactoring should be executable.
assertFalse(project.getFile("src/main/resources/application.yml").exists());
assertFile(project, "src/main/resources/application.properties",
"some.property=somevalue\n" +
"other.property=othervalue\n"
);
}

@Test public void hasInlineComments() throws Exception {
IProject project = projects.createBootProject("hasComments");
IFile propsFile = project.getFile("src/main/resources/application.properties");
if (propsFile.exists()) {
propsFile.delete(true, new NullProgressMonitor());
}

do_hasInlineComments_test(project, "#comment");
ConvertYamlToPropertiesRefactoring refactoring = do_hasInlineComments_test(project, " #!comment");

perform(refactoring); //Despite the warning, the refactoring should be executable.
assertFalse(project.getFile("src/main/resources/application.yml").exists());
assertFile(project, "src/main/resources/application.properties",
"some.property=somevalue\n" +
"foo=bar\n" +
"other.property=othervalue\n"
);
}

@Test public void almostHasComments() throws Exception {
do_conversionTest(
"my:\n" +
" goodbye: 'See ya # later'\n" +
" hello: Good morning!\n"
, // ==>
"my.goodbye=See ya \\# later\n" +
"my.hello=Good morning\\!\n"
);
}

@Test public void conversionWithListItems() throws Exception {
do_conversionTest(
"some:\n" +
" thing:\n" +
" - a: first-a\n" +
" b: first-b\n" +
" - a: second-a\n" +
" b: second-b\n"
, // ==>
"some.thing[0].a=first-a\n" +
"some.thing[0].b=first-b\n" +
"some.thing[1].a=second-a\n" +
"some.thing[1].b=second-b\n"
);
}

@Test public void simpleConversion() throws Exception {
do_conversionTest(
"some:\n" +
" other:\n" +
" thing: blah\n" +
" thing: vvvv\n"
, // ==>
"some.other.thing=blah\n" +
"some.thing=vvvv\n"
);
}

@Test public void emptyFileConversion() throws Exception {
do_conversionTest(
""
, // ==>
""
);
}

@Test public void unusualName() throws Exception {
IProject project = projects.createProject("unusualName");
IFile input = createFile(project, "no-extension",
"server:\n port: 6789\n"
);
ConvertYamlToPropertiesRefactoring refactoring = new ConvertYamlToPropertiesRefactoring(input);
assertOkStatus(refactoring.checkAllConditions(new NullProgressMonitor()));
perform(refactoring);
assertEquals(
"server.port=6789\n",
IOUtil.toString(project.getFile("no-extension.properties").getContents())
);
}

@Test public void list() throws Exception {
do_conversionTest(
"some:\n" +
" property:\n" +
" - something\n" +
" - something-else\n"
, // ==>
"some.property[0]=something\n" +
"some.property[1]=something-else\n"
);
}

@Test public void mapAndSequenceConflict() throws Exception {
do_conversionTest(
"some:\n" +
" property:\n" +
" '0': zero\n" +
" '1': one\n" +
" abc: val1\n" +
" def: val2\n"
,
"some.property.0=zero\n" +
"some.property.1=one\n" +
"some.property.abc=val1\n" +
"some.property.def=val2\n"
);
}



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="target/classes"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
Loading

0 comments on commit 3a0ee63

Please sign in to comment.