Testing with a logger?

M

markspace

I'd like to get a bit more info from a class I'm unit testing. So I
added a testMode method:


public class AppletBootstrap extends JApplet
{
// Init logger
private static final Logger logger = Logger.getLogger(
AppletBootstrap.class.getName() );

static void testMode() {
ConsoleHandler h = new ConsoleHandler();
h.setLevel( Level.ALL );
logger.addHandler( h );
logger.setLevel( Level.ALL );
}
...
}


Overall this feels like code smell to me, putting a class into "test
mode" when exercising it. I think this particular use is benign, but I
thought I'd ask the floor what it thinks (that's you guys and gals).
 
R

Rzeźnik

I'd like to get a bit more info from a class I'm unit testing.  So I
added a testMode method:

public class AppletBootstrap extends JApplet
{
     // Init logger
     private static final Logger logger = Logger.getLogger(
             AppletBootstrap.class.getName() );

     static void testMode()    {
         ConsoleHandler h = new ConsoleHandler();
         h.setLevel( Level.ALL );
         logger.addHandler( h );
         logger.setLevel( Level.ALL );
     }
     ...

}

Overall this feels like code smell to me, putting a class into "test
mode" when exercising it.  I think this particular use is benign, but I
thought I'd ask the floor what it thinks (that's you guys and gals).

In this particular case you do not need to hard-code logger
configuration. Why don't you just prepare two logging properties files
and swap them if needed?
 
M

markspace

Rzeźnik said:
In this particular case you do not need to hard-code logger
configuration. Why don't you just prepare two logging properties files
and swap them if needed?


In an automated test harness? Inside an IDE? Yucky.

Perhaps I misunderstand what you mean or how to do this, but but passing
arguments to the JVM is kinda non-trivial. I might be able to set-up an
Ant task to do this (or modify an existing Ant task). Hmmm, maybe I
should look into that.
 
R

Rzeźnik

In an automated test harness?  Inside an IDE?  Yucky.

Perhaps I misunderstand what you mean or how to do this, but but passing
arguments to the JVM is kinda non-trivial.  I might be able to set-up an
Ant task to do this (or modify an existing Ant task).  Hmmm, maybe I
should look into that.


Emm, I do not understand why it is non trivial. I do not know which
IDE you are using currently, but in Eclipse it is a matter of seconds
to create testing run configuration with appropriate arguments.
NetBeans is no different. If you run tests via ant it is trivial thing
also (albeit tedious).
 
M

markspace

Rzeźnik said:
Emm, I do not understand why it is non trivial. I do not know which
IDE you are using currently, but in Eclipse it is a matter of seconds
to create testing run configuration with appropriate arguments.
NetBeans is no different. If you run tests via ant it is trivial thing
also (albeit tedious).


So I've actually never played with the runtime configuration in NetBeans
before. That was a good suggestion. I like especially that you can
have different configurations and switch between them by selecting off
of a menu.

After a bit of experimentation, I found that I could set the working
directory and the java.util.logging.config.file property and the
LogManager would find and use the property file I specified. Very
convenient.

I still think an Ant task would be more portable, since obviously the
NetBeans configuration isn't available in other environments, but that's
a nice quick solution to setting the logging properties on all tests.
 
R

Rzeźnik

So I've actually never played with the runtime configuration in NetBeans
before.  That was a good suggestion.  I like especially that you can
have different configurations and switch between them by selecting off
of a menu.

Nice to hear this.
After a bit of experimentation, I found that I could set the working
directory and the java.util.logging.config.file property and the
LogManager would find and use the property file I specified.  Very
convenient.

Exactly.

I still think an Ant task would be more portable, since obviously the
NetBeans configuration isn't available in other environments, but that's
a nice quick solution to setting the logging properties on all tests.

NetBeans actually uses ant-xml to do everything you set in
configuration. Browse project files and you'll discover it, actually
it is broken into two parts, private and public, I don't remember the
exact names but it is documented within the file. You can use this
file with no special help form NetBeans.
 
L

Lew

markspace said:
I still think an Ant task would be more portable, since obviously the
NetBeans configuration isn't available in other environments, but that's
a nice quick solution to setting the logging properties on all tests.

NetBeans uses Ant to set that configuration.
 
M

markspace

Lew said:
NetBeans uses Ant to set that configuration.


OK, I found it. Inside the nbproject/build-impl.xml file, there's an
-init-private target that does this:


<target depends="-pre-init" name="-init-private">
<property file="nbproject/private/config.properties"/>
<property file="nbproject/private/configs/${config}.properties"/>
<property file="nbproject/private/private.properties"/>
</target>

"$(config)" is the name of the configuration. So in my case, that
variable would be "test" (because I named my configuration for testing
"test"). I can see the file "test.properties" on the file system, and
it's got the logger system property I added to the command line.

So I guess all NB does is set this variable on the command line when it
invokes a target. Cool. Thanks for pointing that out, now I don't have
to add a custom ant task.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top