Add TextUtils

This commit is contained in:
Mark Vainomaa 2016-09-22 17:55:45 +03:00
parent 21f3391a9d
commit 8ed6eb0a45
6 changed files with 122 additions and 0 deletions

27
TextUtils/pom.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>utils</artifactId>
<groupId>eu.mikroskeem</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>textutils</artifactId>
<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package eu.mikroskeem.utils.text;
import org.jetbrains.annotations.NotNull;
public class MinecraftText {
/**
* Validate Minecraft username
*
* @param name Name to validate
* @return Whether name is valid or not
*/
public static boolean validateUsername(@NotNull String name){
if(
(name.length() > 16 || name.length() < 3) || /* Check length */
!name.matches("^[A-Za-z0-9_]+$") /* Check characters */
){
return false;
}
return true;
}
/**
* Translate alternate color codes to Minecraft color codes.
*
* Note: shittier version of {@link org.bukkit.ChatColor}
* @param seq Sequence to replace
* @param text Text to do replace on
* @return Text with replaced color codes
*/
@NotNull public static String translateAltColors(char seq, @NotNull String text){
return text.replace(seq, '§');
}
}

View File

@ -0,0 +1,14 @@
package eu.mikroskeem.utils.text;
import org.jetbrains.annotations.NotNull;
public class TextUtils {
/**
* Check if string contains whitespace
* @param str String to check
* @return Whether string contained whitespace or not
*/
public static boolean hasWhitespace(@NotNull String str){
return !str.matches("\\S+");
}
}

View File

@ -0,0 +1,25 @@
package eu.mikroskeem.utils.test.text;
import eu.mikroskeem.utils.text.MinecraftText;
import org.junit.Assert;
import org.junit.Test;
public class TestMinecraftUtils {
@Test public void testValidUsername(){
Assert.assertTrue(MinecraftText.validateUsername("mikroskeem"));
Assert.assertTrue(MinecraftText.validateUsername("abcd"));
Assert.assertTrue(MinecraftText.validateUsername("abc_"));
}
@Test public void testInvalidUsername(){
Assert.assertFalse(MinecraftText.validateUsername("mikros--"));
Assert.assertFalse(MinecraftText.validateUsername("mikr_os--"));
Assert.assertFalse(MinecraftText.validateUsername("aa"));
Assert.assertFalse(MinecraftText.validateUsername(" "));
Assert.assertFalse(MinecraftText.validateUsername(" "));
}
@Test public void testColorReplacing(){
Assert.assertEquals("§c§lHello", MinecraftText.translateAltColors('&', "&c&lHello"));
}
}

View File

@ -0,0 +1,22 @@
package eu.mikroskeem.utils.test.text;
import eu.mikroskeem.utils.text.TextUtils;
import org.junit.Assert;
import org.junit.Test;
public class TestTextUtils {
@Test public void testSpaceWhitespace(){
String text = "wut lel";
Assert.assertTrue(TextUtils.hasWhitespace(text));
}
@Test public void testTabWhitespace(){
String text = "wut" + '\t' + "lel";
Assert.assertTrue(TextUtils.hasWhitespace(text));
}
@Test public void testNoWhitespace(){
String text = "wut";
Assert.assertFalse(TextUtils.hasWhitespace(text));
}
}

View File

@ -14,6 +14,7 @@
<module>EtcdConnector</module> <module>EtcdConnector</module>
<module>Paste</module> <module>Paste</module>
<module>Itemutils</module> <module>Itemutils</module>
<module>TextUtils</module>
</modules> </modules>
<!-- Properties --> <!-- Properties -->