Archived

This forum has been archived. Please start a new discussion on GitHub.

Creating BasicStream from Input/OutputStream instances for unit tests

Hi,
I've built a small set of utilities classes in java to help intercept BasicStreams to read arguments/write responses for the sake of mocking Ice.ObjectPrx instances in unit tests. I want to write some additional tests to validate the utility classes that read/write different types (int, int seq, objectprx, serializable etc)... however, I want to do this in a bare minimum manner. From the ice manual I can see how to create an OutputStream, write to it, and get the byte array using finished(). From this byte array I can then initialize an InputStream that can be read out. However, the methods in the ArrayHelper classes generated by slice2java expect a BasicStream instance. How could I go about converting my Input/Output streams to that?

For example, the below snippet manages to write/read the string array, however i'd like to be able to use the StringArrayHelper class to write the array - when my utility classes read, and read the array when my utility classes write.:
                Ice.Communicator comm = Ice.Util.initialize();
		Ice.OutputStream oStream = Ice.Util.createOutputStream(comm);
                // StringArrayHelper.write( oStream, new String[] { "test1","test2" } );
		oStream.startEncapsulation();
		oStream.writeSize(2);
		oStream.writeString("test1");
		oStream.writeString("test2");
		oStream.endEncapsulation();
		
		byte [] bytesWritten = oStream.finished();
		Ice.InputStream iStream = Ice.Util.createInputStream(comm, bytesWritten);

		iStream.startEncapsulation();
		int nElements = iStream.readSize();
		String [] vals = new String[nElements];
		for ( int idx = 0 ; idx < nElements ; ++idx ){
			vals[idx] = iStream.readString();
		}
		iStream.endEncapsulation();
		//String[] vals = StringArrayHelper.read(iStream);
		assert vals.length == 2;

Thank you.

Comments

  • mes
    mes California
    Hi,

    Welcome to the forum.

    If you run slice2java with the --stream option, it will generate additional code to support the public stream API.

    Regards,
    Mark
  • Thank you, Mark,
    adding that option did indeed generate the overloads that accept BasicStream. I'm a bit unclear about initializing the BasicStream for 'reading' though as the following code generates an Ice.UnsupportedEncodingException at startReadEncaps.
    		Ice.Communicator comm = Ice.Util.initialize();
    		BasicStream stream = new BasicStream( Ice.Util.getInstance(comm));
    		stream.startWriteEncaps();
    		StringArrayHelper.write(stream, new String[] { "test1","test2"});
    		stream.endWriteEncaps();
    
                    stream.startReadEncaps();
    		int nElements = stream.readSize();
    		String [] vals = new String[nElements];
    		for ( int idx = 0 ; idx < nElements ; ++idx ){
    			vals[idx] = stream.readString();
    		}
                    stream.endReadEncaps();
    		assert vals.length == 2;
    

    Upon tinkering around in the java src Incoming/Outgoing I am starting to get a 'maybe you should write the appropriate headers properly' feeling - but I haven't been able to figure out the bare minimum required to set the stream up.

    Thank you, again.
  • mes
    mes California
    Hi,

    I'm afraid we don't provide support for questions regarding internal Ice APIs. Of course you're welcome to read the source code, and feel free to ask if you're having trouble using a public API.

    Regards,
    Mark