Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Restructoring a java program

Thread view: 
Thomas Korsgaard - 23 Nov 2005 19:35 GMT
Hi,

I have a java aplication that needs a bit of a restructoring. It's a
program with aprox 10K lines of code all in one class, and it would be
fair to say that the code is a mess! So I need to re-organise things.  I
wanted to make a test class that could run an automated test, so every
time I'd made some changes I could just run the test, to check if
everything was allright.

Basically it runs in a console with a textbased menu structure. So a
menu is printed to the console and the user press either 1 or 2 or
whatever, in order to use the program.

The code runs like this: Every time a operation is finished a
printMenu() method is invoked at this prints the menu, at now waits for
user input. So I wanted to make the test cases like

gps.printMenu();
writeToConsole(1);
String capture = CaptureWhatIsPrinted();

So that the test class automatically writes 1 when the menu is printed
and what ever the program returns is stored in the String capture. And
then I could check the contents of the string matched what I expected.

So my question is. How do write to the console from the java program,
and how do I catch the output?

Thanks
/Thomas
adamspe@gmail.com - 23 Nov 2005 20:15 GMT
It's platform specific and maybe not so elegant but I'm not sure if
there's another way to do this.  You could build up a system command
line to run your app and invoke it via java.lang.Runtime.exec grabbing
the InputStream and OutputStream from the resulting process and using
them to read what your program writes and send input to it.

e.g.
String [] cmd = {
 "/usr/java/bin/java", "-cp", classpath, "my.class.with.a.main.Method"
}
Process p = Runtime.exec ( cmd );
InputStream in = p.getInputStream ();
OutputStream out = p.getOutputStream ()
//InputStream err = p.getErrorStream();

//wrap in/out in other streams
// read your "menu" from in
// write "1" to out
// etc.
Jimi Hullegård - 24 Nov 2005 00:25 GMT
> gps.printMenu();
> writeToConsole(1);
[quoted text clipped - 6 lines]
> So my question is. How do write to the console from the java program, and
> how do I catch the output?

Unless someone comes with a simple solution for this, here's an idea:
Try to rewrite your code so that you don't call the console readline
function directly, but with a function in between, lets call it
getConsoleInput(), that acts as a middle man and simple forwards your input
request to the actuall readline function, and then returns whatever input it
recieved. Make sure that your program still works by some simple tests.
Then rewrite your new function getConsoleInput() so that it instead of
using the console input function, it reads from a input buffer. Then you
write another function, that inserts stuff into that input buffer.

example:
...
int menuSelection = getInputInt();
if (menuSelection == 1)
...

public int getInputInt()
{
  while (inputBuffer is empty)... wait
  return Integer.parseInt(inputBuffer.getFirstInput());
}

public void addInput(String str)
{
  inputBuffer.add(str);
}

I guess this envolves threads some how, and syncronization.

This could actually be a good start to get your program refactorized. Maybe
in the future you want to change user interface to Swing or a web
application. Then you should separate the business code from the
presentation as much as possible.

/Jimi
Andrew McDonagh - 24 Nov 2005 00:41 GMT
>>gps.printMenu();
>>writeToConsole(1);
[quoted text clipped - 42 lines]
>
> /Jimi

no need to - you can make System.out & System.in point to any IoStream -
so the tests an use a MockStream, which we can prime with canned data.
iamfractal@hotmail.com - 24 Nov 2005 11:34 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> Thanks
> /Thomas

OT, but why do you need to re-organise things?

Usually, there's two answers to that:
A) The code needs to be (more) maintainable to reduce the cost of
bug-fixes.
B) There are new requirements on the way and the code needs to be
(more) cost-effectively updatable.

(Both essentially reduce to: the code needs to be more flexible.)

If only (A) holds true, then it may not be worth your while
re-structuring the code. If this is an established product, then newly
discovered bugs will become increasingly rare; maybe the cost of these
bugs will be less than the cost of a re-structuring. You've inherited a
monster, so let him sleep.

If (B) holds true, then you might want to look into MVC-ing your code;
certainly, if there's any chance that the output might one-day go to
something other than a console (e.g., a web-page, a socket, a GUI,
etc.). This being the case, if you slice-off a non-display-specific
View interface, then you could also have a TestView implementation,
which will consume all you throw at it and can return what you wish (to
verify both Controller and Model).

In short, if you are going to re-structure, then it may be worth
examing whether certain aspects should be re-designed (or even just
properly encapsulated to allow for later re-design) rather than just
re-structuring the existing design into multiple classes.

Like I said, all a bit OT.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.