-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed source formatter problems with variables, closes #104
- Loading branch information
Showing
10 changed files
with
912 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
jenkins-editor-plugin/src/main/java/de/jcup/jenkinseditor/VariableEscapeSupport.java
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
jenkins-editor-plugin/src/main/java/de/jcup/jenkinseditor/VariableLineEscapeSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2024 Albert Tregnaghi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
* | ||
*/ | ||
package de.jcup.jenkinseditor; | ||
|
||
import java.util.UUID; | ||
|
||
public class VariableLineEscapeSupport { | ||
|
||
public static final String END_VAR_ESCAPE = "END___VAR_ESCAPE_"; | ||
public static final String START_VAR_ESCAPE = "_START_VAR_ESCAPE_"; | ||
|
||
private final String unescapedPrefix="${"; | ||
private final String unescapedPostfix="}"; | ||
|
||
private final String escapedPrefix; | ||
private final String escapedPostfix; | ||
|
||
VariableLineEscapeSupport(UUID uuid) { | ||
escapedPrefix = attach( START_VAR_ESCAPE, uuid); | ||
escapedPostfix = attach(END_VAR_ESCAPE, uuid); | ||
} | ||
|
||
private String attach(String string, UUID uuid) { | ||
String attachment = ""; | ||
if (uuid != null) { | ||
attachment = uuid.toString(); | ||
} | ||
return string + attachment; | ||
} | ||
|
||
public String escape(String line) { | ||
int prefixPos = line.indexOf(unescapedPrefix); | ||
if (prefixPos == -1) { | ||
return line; | ||
} | ||
int unescapedPrefixLength = unescapedPrefix.length(); | ||
int unescapedPostfixLength = unescapedPostfix.length(); | ||
|
||
StringBuilder sb = new StringBuilder(line); | ||
while (prefixPos != -1) { | ||
sb.replace(prefixPos, prefixPos + unescapedPrefixLength, escapedPrefix); | ||
|
||
int postFixPos = sb.indexOf(unescapedPostfix, prefixPos + unescapedPrefixLength); | ||
if (postFixPos == -1) { | ||
break; | ||
} | ||
sb.replace(postFixPos, postFixPos + unescapedPostfixLength, escapedPostfix); | ||
|
||
prefixPos = sb.indexOf(unescapedPrefix); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public String unescape(String line) { | ||
int prefixPos = line.indexOf(escapedPrefix); | ||
if (prefixPos == -1) { | ||
return line; | ||
} | ||
int escapedPrefixLength = escapedPrefix.length(); | ||
|
||
StringBuilder sb = new StringBuilder(line); | ||
while (prefixPos != -1) { | ||
sb.replace(prefixPos, prefixPos + escapedPrefixLength, unescapedPrefix); | ||
|
||
prefixPos = sb.indexOf(escapedPrefix); | ||
} | ||
|
||
int escapedPostfixLength = escapedPostfix.length(); | ||
int postFixPos = sb.indexOf(escapedPostfix); | ||
while (postFixPos != -1) { | ||
sb.replace(postFixPos, postFixPos + escapedPostfixLength, unescapedPostfix); | ||
|
||
postFixPos = sb.indexOf(escapedPostfix); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
jenkins-editor-plugin/src/test/java/de/jcup/jenkinseditor/VariableEscapeSupportTest.java
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
jenkins-editor-plugin/src/test/java/de/jcup/jenkinseditor/VariableLineEscapeSupportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2024 Albert Tregnaghi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
* | ||
*/ | ||
package de.jcup.jenkinseditor; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class VariableLineEscapeSupportTest { | ||
|
||
private VariableLineEscapeSupport supportToTest; | ||
private String expectedEscapedPrefix; | ||
private String expectedEscapedPostfix; | ||
|
||
@Before | ||
public void before() { | ||
|
||
UUID uuid = UUID.randomUUID(); | ||
expectedEscapedPrefix = VariableLineEscapeSupport.START_VAR_ESCAPE + uuid.toString(); | ||
expectedEscapedPostfix = VariableLineEscapeSupport.END_VAR_ESCAPE + uuid.toString(); | ||
|
||
supportToTest = new VariableLineEscapeSupport(uuid); | ||
} | ||
|
||
@Test | ||
public void escape_hello_world() { | ||
assertEquals("hello world", supportToTest.escape("hello world")); | ||
} | ||
|
||
@Test | ||
public void unescape_hello_world() { | ||
assertEquals("hello world", supportToTest.unescape("hello world")); | ||
} | ||
|
||
@Test | ||
public void escape_hello_var1() { | ||
assertEquals("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix, supportToTest.escape("hello ${env.var1}")); | ||
} | ||
|
||
@Test | ||
public void unescape_hello_var1() { | ||
assertEquals("hello ${env.var1}", supportToTest.unescape("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix)); | ||
} | ||
|
||
@Test | ||
public void escape_hello_var1_and_var2() { | ||
assertEquals("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, | ||
supportToTest.escape("hello ${env.var1} and ${env.var2}")); | ||
} | ||
|
||
@Test | ||
public void unescape_hello_var1_and_var2() { | ||
assertEquals("hello ${env.var1} and ${env.var2}", | ||
supportToTest.unescape("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix)); | ||
} | ||
|
||
@Test | ||
public void escape_var1_and_var2() { | ||
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, | ||
supportToTest.escape("${env.var1} and ${env.var2}")); | ||
} | ||
|
||
@Test | ||
public void escape_var1_var2() { | ||
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + "" + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, supportToTest.escape("${env.var1}${env.var2}")); | ||
} | ||
|
||
@Test | ||
public void escape_var1_dollar_var2() { | ||
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + "$" + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, supportToTest.escape("${env.var1}$${env.var2}")); | ||
} | ||
|
||
@Test | ||
public void unescape_var1_and_var2() { | ||
assertEquals("${env.var1} and ${env.var2}", | ||
supportToTest.unescape(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix)); | ||
} | ||
} |
Oops, something went wrong.