Skip to main content

how can open the file running agent in the order machine ?

4 replies [Last post]
vaccani
Offline
Joined: 2009-09-14

i have this structure file:

|-- Teste1.class
|-- Teste2.class
|-- Teste3.class
|-- Teste4.class
|-- Teste5.class
|-- com
| `-- sun
| `-- javatest
| |-- Status.class
| `-- Test.class
`-- teste.txt

the class Teste5.class read the teste.txt file.....when i execute this testsuite remotely with the active agent it's necessary the teste.txt is in the machine that the agent is running ?

thanks
vaccani

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vsizikov
Offline
Joined: 2004-11-16

> how i have to make sure the resource request goes
> through the class loader ?
> do you find the example ?

Hi.

In order to load stuff from the remote machine, you need to use Classloader rather than a direct File approach. Basically, the Agent that executes the tests has a special classloader it uses to fetch various info from the JTHarness side. Among other things, it could request classes and resources.

Say, you need to fetch "teste.txt" file from JTHarness side. You could use something like this (on the Agent side, in the test):

[code]
getClass().getResourceAsStream("/teste.txt")
[/code]
This code will request the resource, and will request the resource from the underlying class loader (which happens to be the agent's special loader that knows how to obtain the classes and resources from the remote side over the agent connection).

Let me know if that works for you.

Thanks,
--Vladimir

bkurotsu
Offline
Joined: 2004-12-13

No, it doesn't need to be on the same machine, but you have to make sure the resource request goes through the class loader, which will forward it across the connection. You of course can't open a File object.

I'll have to go find you the proper code snip - I can't remember at the moment...

Brian

vaccani
Offline
Joined: 2009-09-14

hi brian,

thanks for the asnwer....
that is my test class Teste5.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import com.sun.javatest.Status;
import com.sun.javatest.Test;

/**
* @test
* @executeClass Teste5
* @sources Teste5.java
**/

public class Teste5 implements Test {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.err, true);
Test t = new Teste5();
Status s = t.run(args, out, null);
s.exit();
}

public Status run(String[] args, PrintWriter out1, PrintWriter out2) {
Status result = Status.failed("nao leu");

File file = new File("teste.txt");
try {
String teste = ler(file);
result = Status.passed(teste);
} catch (Exception e) {

e.printStackTrace();
}

return result;
}

public static String ler(File arquivo) throws Exception {

StringBuffer sb = new StringBuffer();
try {
FileReader reader = new FileReader(arquivo);
BufferedReader bufReader = new BufferedReader(reader);
String s;
do {
s = bufReader.readLine();
if (s != null) {
sb.append(s + "\r\n");
}
} while (s != null);

bufReader.close();
reader.close();
}catch (IOException e) {
throw new Exception(e);
}
return sb.toString();
}

}

how i have to make sure the resource request goes through the class loader ?
do you find the example ?

thanks a lot
vaccani

vaccani
Offline
Joined: 2009-09-14

someone can help me ?