Merge pull request #6649 from kacperkoza/BAEL
extension function in java
This commit is contained in:
commit
8424a1047e
@ -0,0 +1,9 @@
|
|||||||
|
@file:JvmName("Strings")
|
||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
fun String.escapeForXml() : String {
|
||||||
|
return this
|
||||||
|
.replace("&", "&")
|
||||||
|
.replace("<", "<")
|
||||||
|
.replace(">", ">")
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.kotlin;
|
||||||
|
|
||||||
|
import kotlin.text.StringsKt;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.baeldung.kotlin.Strings.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class StringUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldEscapeXmlTagsInString() {
|
||||||
|
String xml = "<a>hi</a>";
|
||||||
|
|
||||||
|
String escapedXml = escapeForXml(xml);
|
||||||
|
|
||||||
|
Assert.assertEquals("<a>hi</a>", escapedXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void callingBuiltInKotlinExtensionMethod() {
|
||||||
|
String name = "john";
|
||||||
|
|
||||||
|
String capitalizedName = StringsKt.capitalize(name);
|
||||||
|
|
||||||
|
Assert.assertEquals("John", capitalizedName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -6,13 +6,6 @@ import org.junit.Test
|
|||||||
class ExtensionMethods {
|
class ExtensionMethods {
|
||||||
@Test
|
@Test
|
||||||
fun simpleExtensionMethod() {
|
fun simpleExtensionMethod() {
|
||||||
fun String.escapeForXml() : String {
|
|
||||||
return this
|
|
||||||
.replace("&", "&")
|
|
||||||
.replace("<", "<")
|
|
||||||
.replace(">", ">")
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.assertEquals("Nothing", "Nothing".escapeForXml())
|
Assert.assertEquals("Nothing", "Nothing".escapeForXml())
|
||||||
Assert.assertEquals("<Tag>", "<Tag>".escapeForXml())
|
Assert.assertEquals("<Tag>", "<Tag>".escapeForXml())
|
||||||
Assert.assertEquals("a&b", "a&b".escapeForXml())
|
Assert.assertEquals("a&b", "a&b".escapeForXml())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user