Example usage for org.apache.commons.lang RandomStringUtils randomAlphanumeric

List of usage examples for org.apache.commons.lang RandomStringUtils randomAlphanumeric

Introduction

In this page you can find the example usage for org.apache.commons.lang RandomStringUtils randomAlphanumeric.

Prototype

public static String randomAlphanumeric(int count) 

Source Link

Document

Creates a random string whose length is the number of characters specified.

Characters will be chosen from the set of alpha-numeric characters.

Usage

From source file:org.apache.phoenix.pherf.rules.RulesApplier.java

/**
 * Get data value based on the supplied rule
 *
 * @param column {@link org.apache.phoenix.pherf.configuration.Column} Column rule to get data for
 * @return {@link org.apache.phoenix.pherf.rules.DataValue} Container Type --> Value mapping
 *///from  w  w w.  j av  a 2  s. co m
public DataValue getDataValue(Column column) throws Exception {
    DataValue data = null;
    int length = column.getLength();
    int nullChance = column.getNullChance();
    List<DataValue> dataValues = column.getDataValues();

    // Return an empty value if we we fall within the configured probability
    if ((nullChance != Integer.MIN_VALUE) && (isValueNull(nullChance))) {
        return new DataValue(column.getType(), "");
    }

    switch (column.getType()) {
    case VARCHAR:
        // Use the specified data values from configs if they exist
        if ((column.getDataValues() != null) && (column.getDataValues().size() > 0)) {
            data = generateDataValue(dataValues);
        } else {
            Preconditions.checkArgument(length > 0, "length needs to be > 0");
            if (column.getDataSequence() == DataSequence.SEQUENTIAL) {
                data = getSequentialDataValue(column);
            } else {
                String varchar = RandomStringUtils.randomAlphanumeric(length);
                data = new DataValue(column.getType(), varchar);
            }
        }
        break;
    case CHAR:
        if ((column.getDataValues() != null) && (column.getDataValues().size() > 0)) {
            data = generateDataValue(dataValues);
        } else {
            Preconditions.checkArgument(length > 0, "length needs to be > 0");
            if (column.getDataSequence() == DataSequence.SEQUENTIAL) {
                data = getSequentialDataValue(column);
            } else {
                String varchar = RandomStringUtils.randomAlphanumeric(length);
                data = new DataValue(column.getType(), varchar);
            }
        }
        break;
    case DECIMAL:
        if ((column.getDataValues() != null) && (column.getDataValues().size() > 0)) {
            data = generateDataValue(dataValues);
        } else {
            int precision = column.getPrecision();
            double minDbl = column.getMinValue();
            Preconditions.checkArgument((precision > 0) && (precision <= 18),
                    "Precision must be between 0 and 18");
            Preconditions.checkArgument(minDbl >= 0, "minvalue must be set in configuration");
            Preconditions.checkArgument(column.getMaxValue() > 0, "maxValue must be set in configuration");
            StringBuilder maxValueStr = new StringBuilder();

            for (int i = 0; i < precision; i++) {
                maxValueStr.append(9);
            }

            double maxDbl = Math.min(column.getMaxValue(), Double.parseDouble(maxValueStr.toString()));
            final double dbl = RandomUtils.nextDouble(minDbl, maxDbl);
            data = new DataValue(column.getType(), String.valueOf(dbl));
        }
        break;
    case INTEGER:
        if ((column.getDataValues() != null) && (column.getDataValues().size() > 0)) {
            data = generateDataValue(dataValues);
        } else {
            int minInt = column.getMinValue();
            int maxInt = column.getMaxValue();
            Preconditions.checkArgument((minInt > 0) && (maxInt > 0),
                    "min and max values need to be set in configuration");
            int intVal = RandomUtils.nextInt(minInt, maxInt);
            data = new DataValue(column.getType(), String.valueOf(intVal));
        }
        break;
    case DATE:
        if ((column.getDataValues() != null) && (column.getDataValues().size() > 0)) {
            data = generateDataValue(dataValues);
        } else {
            int minYear = column.getMinValue();
            int maxYear = column.getMaxValue();
            Preconditions.checkArgument((minYear > 0) && (maxYear > 0),
                    "min and max values need to be set in configuration");

            String dt = generateRandomDate(minYear, maxYear);
            data = new DataValue(column.getType(), dt);
        }
        break;
    default:
        break;
    }
    Preconditions.checkArgument(data != null,
            "Data value could not be generated for some reason. Please check configs");
    return data;
}

From source file:org.apache.phoenix.pherf.rules.RulesApplier.java

/**
 * Add a numerically increasing counter onto the and of a random string.
 * Incremented counter should be thread safe.
 *
 * @param column {@link org.apache.phoenix.pherf.configuration.Column}
 * @return {@link org.apache.phoenix.pherf.rules.DataValue}
 *//*  w  ww  .  j  a  va  2 s  .c om*/
private DataValue getSequentialDataValue(Column column) {
    DataValue data = null;
    long inc = COUNTER.getAndIncrement();
    String strInc = String.valueOf(inc);
    String varchar = RandomStringUtils.randomAlphanumeric(column.getLength() - strInc.length());
    data = new DataValue(column.getType(), strInc + varchar);
    return data;
}

From source file:org.apache.pig.impl.streaming.TestExecutableManager.java

@Test
public void testAddJobConfToEnv() {
    StringBuilder streamingEnv = null;
    Configuration conf = new Configuration();
    Map<String, String> all = Maps.newHashMap();
    for (int i = 0; i < 10000; i++) {
        String key = RandomStringUtils.randomAlphanumeric(10);
        String value = RandomStringUtils.randomAlphanumeric(10);
        all.put(key, value);/*from w  w w  . ja  va 2 s  .c  o m*/
    }
    Map<String, String> toInclude = Maps.newHashMap();
    for (Map.Entry<String, String> entry : all.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        conf.set(key, value);
        if (r.nextDouble() < .5) {
            toInclude.put(key, value);
            if (streamingEnv == null) {
                streamingEnv = new StringBuilder();
            } else {
                streamingEnv.append(",");
            }
            streamingEnv.append(key);
        }
    }
    conf.set(PIG_STREAMING_ENVIRONMENT, streamingEnv.toString());
    Map<String, String> env = new HashMap<String, String>();
    ExecutableManager manager = new ExecutableManager();
    manager.addJobConfToEnvironment(conf, env);

    for (Map.Entry<String, String> entry : env.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        String value2 = toInclude.remove(key);
        assertEquals("Key [" + key + "] should be set in environment", value, value2);
    }
    assertTrue("There should be no remaining pairs in the included map", toInclude.isEmpty());
}

From source file:org.apache.pig.impl.streaming.TestStreamingUtil.java

@Test
public void testAddJobConfToEnv() {
    StringBuilder streamingEnv = null;
    Configuration conf = new Configuration();
    Map<String, String> all = Maps.newHashMap();
    for (int i = 0; i < 10000; i++) {
        String key = RandomStringUtils.randomAlphanumeric(10);
        String value = RandomStringUtils.randomAlphanumeric(10);
        all.put(key, value);//from  ww w .  java2  s  . c  om
    }
    Map<String, String> toInclude = Maps.newHashMap();
    for (Map.Entry<String, String> entry : all.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        conf.set(key, value);
        if (r.nextDouble() < .5) {
            toInclude.put(key, value);
            if (streamingEnv == null) {
                streamingEnv = new StringBuilder();
            } else {
                streamingEnv.append(",");
            }
            streamingEnv.append(key);
        }
    }
    conf.set(PIG_STREAMING_ENVIRONMENT, streamingEnv.toString());
    Map<String, String> env = new HashMap<String, String>();
    StreamingUtil.addJobConfToEnvironment(conf, env);

    for (Map.Entry<String, String> entry : env.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        String value2 = toInclude.remove(key);
        assertEquals("Key [" + key + "] should be set in environment", value, value2);
    }
    assertTrue("There should be no remaining pairs in the included map", toInclude.isEmpty());
}

From source file:org.apache.ranger.authorization.sqoop.authorizer.RangerSqoopAuthorizerTest.java

/**
 * Help function: get random link name
 */
private String getRandomLinkName() {
    return RandomStringUtils.randomAlphanumeric(10) + "-link";
}

From source file:org.apache.ranger.authorization.sqoop.authorizer.RangerSqoopAuthorizerTest.java

/**
 * Help function: get random job name
 */
private String getRandomJobName() {
    return RandomStringUtils.randomAlphanumeric(10) + "-job";
}

From source file:org.apache.ratis.RaftTestUtil.java

public static File getTestDir(Class<?> caller) throws IOException {
    File dir = new File(System.getProperty("test.build.data", "target/test/data") + "/"
            + RandomStringUtils.randomAlphanumeric(10), caller.getSimpleName());
    if (dir.exists() && !dir.isDirectory()) {
        throw new IOException(dir + " already exists and is not a directory");
    } else if (!dir.exists() && !dir.mkdirs()) {
        throw new IOException("Cannot create directory " + dir);
    }/*from w  ww .  j a  v  a  2  s .c  o  m*/
    return dir;
}

From source file:org.apache.reef.io.checkpoint.RandomNameCNS.java

@Override
public String getNewName() {
    return this.prefix + RandomStringUtils.randomAlphanumeric(lengthOfRandomSuffix);
}

From source file:org.apache.roller.weblogger.ui.struts2.core.Register.java

public void myValidate() {

    // if usingSSO, we don't want to error on empty password/username from HTML form.
    setFromSso(false);/*  w  w  w  .j a  va 2 s . c o m*/
    boolean usingSSO = WebloggerConfig.getBooleanProperty("users.sso.enabled");
    if (usingSSO) {
        boolean storePassword = WebloggerConfig.getBooleanProperty("users.sso.passwords.saveInRollerDb");
        String password = WebloggerConfig.getProperty("users.sso.passwords.defaultValue", "<unknown>");

        // Preserve username and password, Acegi case             
        User fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication(getServletRequest());
        if (fromSSO != null) {
            if (storePassword) {
                password = fromSSO.getPassword();
            }
            getBean().setPasswordText(password);
            getBean().setPasswordConfirm(password);
            getBean().setUserName(fromSSO.getUserName());
            setFromSso(true);
        }

        // Preserve username and password, CMA case             
        else if (getServletRequest().getUserPrincipal() != null) {
            getBean().setUserName(getServletRequest().getUserPrincipal().getName());
            getBean().setPasswordText(password);
            getBean().setPasswordConfirm(password);
            setFromSso(true);
        }
    }

    String allowed = WebloggerConfig.getProperty("username.allowedChars");
    if (allowed == null || allowed.trim().length() == 0) {
        allowed = DEFAULT_ALLOWED_CHARS;
    }

    // check that username only contains safe characters
    String safe = CharSetUtils.keep(getBean().getUserName(), allowed);
    if (!safe.equals(getBean().getUserName())) {
        addError("error.add.user.badUserName");
    }

    // check password, it is required if OpenID and SSO are disabled
    if (getOpenIdConfiguration().equals("disabled") && !getFromSso()) {
        if (StringUtils.isEmpty(getBean().getPasswordText())) {
            addError("error.add.user.passwordEmpty");
            return;
        }
    }

    // User.password does not allow null, so generate one
    if (getOpenIdConfiguration().equals("only")) {
        String randomString = RandomStringUtils.randomAlphanumeric(255);
        getBean().setPasswordText(randomString);
        getBean().setPasswordConfirm(randomString);
    }

    // check that passwords match 
    if (!getBean().getPasswordText().equals(getBean().getPasswordConfirm())) {
        addError("Register.error.passowordMismatch");
    }

    // check that username is not taken
    if (!StringUtils.isEmpty(getBean().getUserName()))
        try {
            UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
            if (mgr.getUserByUserName(getBean().getUserName(), null) != null) {
                addError("error.add.user.userNameInUse");
                // reset user name
                getBean().setUserName(null);
            }
        } catch (WebloggerException ex) {
            log.error("error checking for user", ex);
            // TODO: i18n
            addError("unexpected error");
        }
}

From source file:org.apache.slider.core.conf.AggregateConf.java

@JsonIgnore
public String getPassphrase() {
    if (passphrase == null) {
        passphrase = RandomStringUtils.randomAlphanumeric(Integer.valueOf(SliderKeys.PASS_LEN));
    }/* w w  w .  j a  va  2 s.  c o m*/

    return passphrase;
}