From 9fab45944ec220d5d047a2ee68b68bb7362b7a06 Mon Sep 17 00:00:00 2001 From: Sean Flanigan Date: Wed, 27 Sep 2017 16:37:41 +1000 Subject: [PATCH] Add RandomStringUtils --- server/zanata-model/pom.xml | 5 ++ .../org/zanata/util/RandomStringUtils.java | 53 +++++++++++++++++++ .../zanata/util/RandomStringUtilsTest.java | 44 +++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 server/zanata-model/src/main/java/org/zanata/util/RandomStringUtils.java create mode 100644 server/zanata-model/src/test/java/org/zanata/util/RandomStringUtilsTest.java diff --git a/server/zanata-model/pom.xml b/server/zanata-model/pom.xml index 3d65467af9..9c4fa87d6b 100644 --- a/server/zanata-model/pom.xml +++ b/server/zanata-model/pom.xml @@ -236,6 +236,11 @@ commons-lang3 + + org.apache.commons + commons-text + + javax.enterprise cdi-api diff --git a/server/zanata-model/src/main/java/org/zanata/util/RandomStringUtils.java b/server/zanata-model/src/main/java/org/zanata/util/RandomStringUtils.java new file mode 100644 index 0000000000..b01e9ae38b --- /dev/null +++ b/server/zanata-model/src/main/java/org/zanata/util/RandomStringUtils.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017, Red Hat, Inc. and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.zanata.util; + +import org.apache.commons.text.RandomStringGenerator; + +import static org.apache.commons.text.CharacterPredicates.DIGITS; +import static org.apache.commons.text.CharacterPredicates.LETTERS; + +/** + * Incomplete replacement for commons-lang's RandomStringUtils, now deprecated + * @author Sean Flanigan sflaniga@redhat.com + */ +public class RandomStringUtils { + + private RandomStringUtils() { + } + + public static String randomAlphabetic(int length) { + return new RandomStringGenerator.Builder() + .withinRange('A', 'z') + .filteredBy(LETTERS) + .build() + .generate(length); + } + + public static String randomAlphanumeric(int length) { + return new RandomStringGenerator.Builder() + .withinRange('0', 'z') + .filteredBy(LETTERS, DIGITS) + .build() + .generate(length); + } + +} diff --git a/server/zanata-model/src/test/java/org/zanata/util/RandomStringUtilsTest.java b/server/zanata-model/src/test/java/org/zanata/util/RandomStringUtilsTest.java new file mode 100644 index 0000000000..924224248e --- /dev/null +++ b/server/zanata-model/src/test/java/org/zanata/util/RandomStringUtilsTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2017, Red Hat, Inc. and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.zanata.util; + +import java.util.regex.Pattern; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Sean Flanigan sflaniga@redhat.com + */ +public class RandomStringUtilsTest { + @Test + public void testAlphabetic() { + String alphabetic = RandomStringUtils.randomAlphabetic(999); + assertThat(alphabetic).matches(Pattern.compile("[a-zA-Z]{999}")); + } + + @Test + public void testAlphanumeric() { + String alphabetic = RandomStringUtils.randomAlphanumeric(999); + assertThat(alphabetic).matches(Pattern.compile("[a-zA-Z0-9]{999}")); + } +}