Skip to main content

HowTo Access Configuration Parameters

17 replies [Last post]
laguz
Offline
Joined: 2008-03-29
Points: 0

Hello everybody,

I created my own Configuration Interview and added a special paramter for my test cases. The tests are implemented using JUnit. Now I would like to access the paramter I set during the interview.
How can I do this?

Thanks in advance!

Regards,
laguz

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Also, it's quite useful to look at the current values in View|Configuration|...

That way you can see the result of the interview process (esp Test Environment). The binary comes with an example test suite too, you can look to see how interview answers are transformed into things the test execution can use. Note that the demo is a bit old, so uses DefaultTestRunner+Script to execute tests - I recommend you stick with JUnitTestRunner.

laguz
Offline
Joined: 2008-03-29
Points: 0

Hello again,

thanks for your very detailed answer. I think I finally got a better understanding of how jtharness works.

What I did for now is the following. I overrode the getEnv() of my Interview and extracted the parameters i need from the Questions. Afterwards I put the extracted parameters in the TestEnvironment.
Does this fit into the concepts of jtharness?

To use the parameters i created something like a parameter dictionary as mentioned above. The parameters are inserted into it by an extended version of JUnitTestSuite.starting(Harness). Does this sound good?

Probably i will implement the annotations approach as well but at first I have to find a way that works at all. I think I am quite close to that, though.

Thanks!
Johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

> What I did for now is the following. I overrode the
> getEnv() of my Interview and extracted the parameters
> i need from the Questions. Afterwards I put the
> extracted parameters in the TestEnvironment.
> Does this fit into the concepts of jtharness?

It's not necessary to grab the question values manually, the idea is that you use Interview.export(Map), which will go query all the Question.export() methods.

public TestEnvironment getEnv() {
HashMap envProps = new HashMap();
export(envProps);
...
}

> To use the parameters i created something like a
> parameter dictionary as mentioned above. The
> parameters are inserted into it by an extended
> version of JUnitTestSuite.starting(Harness). Does
> this sound good?

You probably don't need to do anything in JUnitTestSuite.starting(Harness) - JUnitTestRunner already has access to the TestEnvironment. See TestRunner.getEnvironment().

How does that work with what you need to do in your code?

Brian

laguz
Offline
Joined: 2008-03-29
Points: 0

First of all: Thanks four your comments!

I adopted your suggestions and the resulting code is much more easier.

But I got a new issue...
I would like to dynamically set the classpath of the tests using the configuration interview. It's very easy to get the classpath using the configuration interview but I did not succeed in using the new classpath. I tried it using the URLClassloader in the TestRunner but it did not work.

Do you have suggestions on that?

Thanks a lot!
Johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Classpath is always interesting - and different for different execution methods. I'm looking for the right answer for this case...

You just want to set the classpath that the junit test runs with, right?

laguz
Offline
Joined: 2008-03-29
Points: 0

Right!

What I would like to do is to give the user the possibility to configure the classpath using the configuration interview. These additional classes should then be used for the test execution, in my case for the JUnit tests.
What I tried up to now was to create a new URLClassLoader in the TestRunner. Now I would like to use this new loader to load and execute the tests. Unfortunately I did not succeed.
Maybe you could give me some advice.

Thanks a lot!
Johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Hi, sorry for the lack of reply - any progress or is the question still open?

laguz
Offline
Joined: 2008-03-29
Points: 0

No problem. :-)

Unfortunately there has been no progress so far because I don't have any idea what to do.

As I mentioned before I tried to create and use a new classloader but that didn't work out. I did this by overriding the getMultiTest method. The new version of the method creates a new classloader using the environment parameters take from the configuration interview. Then I used this new loader to load an a MultiTest class. Additionally I created a new extended MultiClass to be able to set a classloader which should be used to instantiate the test class in the setup method.
Do you have any ideas how to fix this?

Thanks!
Johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Hi again again,

Look into the JTAG I mentioned in the other message.

Tell me this though - how do you want to send the parameters to each test? System property, command line, programatically, etc...

fda
Offline
Joined: 2005-05-27
Points: 0

Hi,

If you need to pass down a parameter value from the interview to the tests we recommend the following steps:

1) In your interview question class define the method like:

public void export(Map data) {
data.put("myParam", "myParamValue");
}

2) For tests that require the parameter set executeArgs in the TestDescription table

executeArgs -param $myParam

Such tests will receive two command line arguments: "-param" "myParamValue"

Setting system properties from the interview classes might have no effect, because tests might be executed in the different vm.

Thanks,
Dima

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Dima mentions "executeArgs in the TestDescription table". This assumes HTML style TestFinder, but you are not using HTML. So you should translate that in your head to mean that "executeArgs" needs to make it into the TestDescription properties (like when you view it in the GUI). The Finder class is responsible for identifying and inserting these properties into the TestDescription.

Brian

laguz
Offline
Joined: 2008-03-29
Points: 0

Hello everybody,

thanks for your support!

I did read the JTAG but I still don't get it. :-(
I added a method export(Map) to my configuration Question.
Why do I have to do that? Because when I open my saved configuration file I can find my configuration parameter in there.
Maybe someone could tell me where a configuration parameter which is stored like this (by using the export method) can be found (TestDescription? Environment?). Maybe this would help me.

Another thing I would like to know is where the translation from "-param $myParam" to "-param myParamValue" takes place.

Regarding the question about how i would like a parameter to be passed to a Test:
I think I would really like a way that utilizes annotations. Maybe something like "@ConfigParameter("myParam") void setMyParam(String myParam);" would do the job.
Another way I would like is some kind of dictionary. I could think of something like this: ConfigParameters.getInstance().get("myParam").

Thanks,
Johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

> I did read the JTAG but I still don't get it. :-(
> I added a method export(Map) to my configuration
> Question.
> Why do I have to do that? Because when I open my
> saved configuration file I can find my configuration
> parameter in there.

JT Harness operates at a higher level than most harnesses - we optimize for the user experience rather than "geekiness". The interview is simply for user interface, it is not consumed by the test execution engine. So, the values in the interview (and JTI file) are not meant to be used much beyond the interview subsystem.

This arrangement is partly historical - TestEnvironment has existed for over ten years and the harness core depends on it. That was important for backwards compatibility.

More interesting though is that the interview allows you to ask really simple questions and then create a composite resulting value in the TestEnv.
Interview: Q1 - port, Q2 - host, Q3 - path
Exported TestEnv value: http://myhost.com:8800/foo/bar

So you can post-process the user's responses for validity (when they answer) and transformation (when you export). In the most simple case, you can just pass the value straight from the Interview value to the exported Map (export()). Sorry, yes, it does take a few lines to do that, but of course it's not difficult code at all.

In any case, to use the APIs "correctly", you write the interview to collect the info, then push it into the TestEnv in your export() methods and get the value from the TestEnv in the core harness classes (TestRunner, etc...).

> Maybe someone could tell me where a configuration
> parameter which is stored like this (by using the
> export method) can be found (TestDescription?
> Environment?). Maybe this would help me.

When you export() it ends up in the TestEnvironment, literally. TestDescriptions are immutable and come from the TestFinder when it reads the tests (at startup).

> Another thing I would like to know is where the
> translation from "-param $myParam" to "-param
> myParamValue" takes place.

Where are you trying to use -param? If you are talking about generating a command line (or arg list) from the TestEnvironment, it is usually resolved automatically when you call TestEnvironment.lookup().

> Regarding the question about how i would like a
> parameter to be passed to a Test:
> I think I would really like a way that utilizes
> annotations. Maybe something like
> "@ConfigParameter("myParam") void setMyParam(String
> myParam);" would do the job.
> Another way I would like is some kind of dictionary.
> I could think of something like this:
> ConfigParameters.getInstance().get("myParam").

Well, did you decide to use annotation style JUnit tests (i.e. Junit 4.x)?

Brian

laguz
Offline
Joined: 2008-03-29
Points: 0

Hello everybody,

finally I found a way to be able to use my configuration parameters. I don't think that my implementation is very good, though.
What I did was to create a subclass of JUnitTestSuite and use the starting(Harness) method to get the configuration interview I had created. Finally I just fetched the parameters directly from the interview questions.

As I just said: I think that there is a much more better solution, but I unfortunately didn't get it. Maybe someone could give me any additional clue.

Thanks!

Regards,
johannes

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Hi again. I first suggest that you read the JavaTest Architect's Guide (JTAG), which is linked in the documentation section of the JT Harness project. It gives all the background on what the various classes are and (especially) how to the interview answers are used.

My suggestion would be to pass along the values you need from the interview inside code you create for TestRunner. So perhaps subclass our provided JUnitTestRunner, possibly override getTestArgs(TestDescription) so that settings are extracted from the TestEnvironment (see TestRunner.getEnvironment()). A simple way to do it might be to tag your tests (and therefore TestDescription, which come from the Finder) with things which allow you to determine what values that test needs (so only the necessary values need to be sent during getTestArgs).

If you read the JTAG, you will now know that the values in the TestEnvironment are produced by the export() methods in the Interview. That is how the settings are supposed to pass from the user interface (interview) to the core of the harness for test execution.

There are of course minor details to be worked out with respect to how to tag tests to indicate what they need, how to utilize that in getTestArgs(), what order to put them into the argument array, etc). We can discuss any issues as you have questions.

Brian

bkurotsu
Offline
Joined: 2004-12-13
Points: 0

Are you executing test using Junit 3.x or 4.x style? In other words, are you using annotation based JUnit tests?

Also, we have some contributed JUnit support updates to integrate that may be useful - we shall see. We'd also be happy to work to try to optimize the process for someone starting from scratch like you.

Brian

laguz
Offline
Joined: 2008-03-29
Points: 0

At first I'd like to thank you for your quick answer!

Regarding your question: By now I use JUnit 3.x style but I could easily switch to annotations, if you recommended so. The reason for this is that I got the JUnitSuperTestFinder to work first. :-)

Johannes