简体   繁体   中英

Java: How can i persist RandomStringUUID object at runtime

Here is the scenario: I have:

public class RandomStringUUID {

    public RandomStringUUID() {
        final String uuidstring = UUID.randomUUID().toString().replaceAll("-", "");
    }


    public String getRandomStringUUID() {
        final String uuidst = UUID.randomUUID().toString().replaceAll("-", "");
        return uuidst;
    }
}

Where i am generating a Unique Value.

Then in :

public class AddSingleDomain {

    public static RandomStringUUID RUUID = new RandomStringUUID();

    @Test
    public void addSingleDomain() {
        AssertJUnit.assertEquals(DomainsPage.getTitle(), "View Account");


        String randomUIDSeed = RUUID.getRandomStringUUID();

        System.out.println("Random Domanin Name Prefix: "
                + randomUIDSeed);
        System.out.println("Random Domanin Name Prefix: " + RUUID);


        getDriver().findElement(By.name("vo.zoneName")).sendKeys(
                randomUIDSeed + ".tv");

I use it, and then, yet in another class:

public class VerifyDBSingleDomain {

String url = "jdbc:oracle:thin:@a.b.c.d:1521:ms";

@Test
public void VerifyDBSingleDomainTest()  {

    Properties props = new Properties();
    props.setProperty("user", "user");
    props.setProperty("password", "pass");


String sql = "Select zoneid from zone where zonename =" +  randomUIDSeed + ".tv";

I want to use the SAME VALUE "randomUIDSeed" If in class VerifyDBSingleDomain i : String randomUIDSeed = RUUID.getRandomStringUUID(); would it be the same object ? How can i use this as same object, and hence guranteeing the same value across ALL classes during runtime.

I'm not quite sure what you're trying to achieve, however;

The short answer to your question is "Yes you will get the same String instance". The slightly longer answer is "But you're doing it wrong".

You really should use a persistence framework to help you deal with these issues.

You could start by making this little change:

public class RandomStringUUID {

    private static final String MYUUID = UUID.randomUUID().toString().replaceAll("-", "");

    public static String getRandomStringUUID() { return MYUUID; }
}

You can then use it like this:

// Anywhere in your code: vv Note that we are using the classname! The method is static.
String theCommonID = RandomStringUUID.getRandomStringUUID();

This is just a hint into the direction. This is not a "perfect" clean solution. You might want to google "Singleton Design Pattern". And I can imagine ~10 other ways to do this right now ... But anyway.

This will create one of your IDs once for the runtime of your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM