You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ola Berg <ol...@ports.se> on 2002/10/21 14:20:49 UTC
[Lang] Assertions?
How about utilities for assertions?
I can imagine:
class Assert
notNull( Object)
noNullElements( Object[])
notEmpty( Object[]) //works for collections too
plus either adding methods for common object types:
notEmpty( String)
or adding assert* methods to the lang *Utils:
assertNotEmpty( String)
When an assertion fails, the methods will throw IllegalArgumentException
Good or bad? I can do the coding, if people like it.
/O
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Paul Cantrell <ca...@pobox.com>.
(1) JUnit throws a subclass of Error; validators show throw subclasses
of RuntimeException (usually IAE).
(2) JUnit's methods are organized around tests, which have expected and
actual values; validator methods would be organized around common
internal integrity checks, which would likely lead to a different set
of methods.
Paul
On Tuesday, October 22, 2002, at 10:26 AM, Steve Downey wrote:
> Why not just use JUnit? The style would be a little different, but it
> can be
> used outside the testing framework.
_________________________________________________________________
Paul Cantrell http://eidola.org http://innig.net
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Steve Downey <st...@netfolio.com>.
Why not just use JUnit? The style would be a little different, but it can be
used outside the testing framework.
On Monday 21 October 2002 08:20 am, Ola Berg wrote:
> How about utilities for assertions?
>
> I can imagine:
>
> class Assert
> notNull( Object)
> noNullElements( Object[])
> notEmpty( Object[]) //works for collections too
>
> plus either adding methods for common object types:
>
> notEmpty( String)
>
> or adding assert* methods to the lang *Utils:
>
> assertNotEmpty( String)
>
> When an assertion fails, the methods will throw IllegalArgumentException
>
> Good or bad? I can do the coding, if people like it.
>
> /O
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Ola Berg <ol...@ports.se>.
From: "Henri Yandell" <ba...@generationjava.com>
> First thoughts on assertion code:
>
> Does JUnit already have this?
Sort of. They have what I would imagine should go into a lang.Assert class (true,not null etc). They also have a couple of asserts that applies to tests (equals, array equals etc).
> What do we feel about JUnits version if it
> does?
Too focused on tests and not on indata validation (doesn't throw IAE). Not really useful for that purpose.
> Does JDK 1.4 contain much more than the 'assert' keyword?
No... ..that I know of... and I haven't read anything from the process on incorporating built in assertion functionality into the language/runtime.
/O
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Michael Davey <Mi...@Sun.COM>.
Henri Yandell wrote:
> First thoughts on assertion code:
>
> Does JUnit already have this?
Mostly.
> What do we feel about JUnits version if it
> does?
>
> Does JDK 1.4 contain much more than the 'assert' keyword?
It includes a simple yet powerful assertion facility:
<http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html>
> What does 1.4 throw when an assertion fails?
java.lang.AssertionError:
<http://java.sun.com/j2se/1.4/docs/api/java/lang/AssertionError.html>
(junit throws junit.framework.AssertionFailedError)
--
Michael
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by "Fischer, Oliver" <pl...@snafu.de>.
Paul `Order in Chaos` Cantrell wrote:
> Here's a page explaining 1.4's assertions. I highly recommend reading
> it -- it will short-circuit a lot of the discussion that Ola's question
> is bound to generate:
>
> http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html
>
> JDK 1.4's assertion facilities make most of Ola's examples too trivial
> to warrant helper methods, IMO -- the following all seem quite clear and
> concise :
>
> assert object != null;
> assert array.length != 0;
> assert !coll.isEmpty();
> assert str.size() != 0;
>
But there are people like me how must run a JDK below 1.4...
Bye
Oliver
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Paul `Order in Chaos` Cantrell <pc...@pobox.com>.
Here's a page explaining 1.4's assertions. I highly recommend reading
it -- it will short-circuit a lot of the discussion that Ola's question
is bound to generate:
http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html
JDK 1.4's assertion facilities make most of Ola's examples too trivial
to warrant helper methods, IMO -- the following all seem quite clear
and concise :
assert object != null;
assert array.length != 0;
assert !coll.isEmpty();
assert str.size() != 0;
Note that one can also do this:
assert (result != null) : "foo() result is null; expected bar
element in" + elements;
An assertion utility could, of course, take the string argument:
Assert.notNull(result, "foo() result is null; expected bar element
in " + elements);
However, the Java version has a big advantage: In the latter example,
the code will *always* call elements.toString() -- even if the
assertion succeeds, and even if assertions are turned off! The Java
native version doesn't evalutate the condition unless assertions are
enabled, and doesn't evaluate the message unless the assertion fails.
The only one of the examples in Ola's message that isn't trivial with
1.4's assert is "noNullElements". If there is actually a need for
assert utilities, it's for deeper structural checks like that.
What does seem much more generally useful is a hasNullElements()
method, which would make the assertion easy to write as a nice side
benefit.
Henri: Java assertions throw AssertionError. JUnit does provide some
basic assertion facilities, but only for test classes.
Cheers,
Paul
On Monday, October 21, 2002, at 09:12 AM, Henri Yandell wrote:
> First thoughts on assertion code:
>
> Does JUnit already have this? What do we feel about JUnits version if
> it
> does? Does JDK 1.4 contain much more than the 'assert' keyword?
>
> What does 1.4 throw when an assertion fails?
>
> Hen
>
> On Mon, 21 Oct 2002, Ola Berg wrote:
>
>> How about utilities for assertions?
>>
>> I can imagine:
>>
>> class Assert
>> notNull( Object)
>> noNullElements( Object[])
>> notEmpty( Object[]) //works for collections too
>>
>> plus either adding methods for common object types:
>>
>> notEmpty( String)
>>
>> or adding assert* methods to the lang *Utils:
>>
>> assertNotEmpty( String)
>>
>> When an assertion fails, the methods will throw
>> IllegalArgumentException
>>
>> Good or bad? I can do the coding, if people like it.
>>
>> /O
_________________________________________________________________
Paul Cantrell http://eidola.org http://innig.net
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Paul Cantrell <ca...@pobox.com>.
I think the point they're making here (IIRC, this is Josh Bloch
writing) is that assertions are for runtime integrity checks that
should theoretically *never* fail, ever -- things where removing the
check shouldn't change the behavior of the code.
Assertions are essentially runtime unit testing.
The point is that invariants and postconditions are assertions in this
model, but _preconditions_ are not if they depend on values supplied by
the client. The reasons they give are quite reasonable: (1) a module
should do defensive checks whether or not assertions are enabled, and
(2) when a violation of a precondition signals a client programming
error, the exception should be descriptive.
Argument (2) still seems to rule out generic assertion facilities,
since what you really want for preconditions are things like this:
if(hasNullElement(listArg))
throw new IllegalArgumentException("listArg == null");
if(destination == null)
throw new IllegalStateException("provide a destination first");
Again, it doesn't seem like generic assertion utilities have much to
offer here. Generic deep structural check utilities seem more useful,
as in the first example above.
Cheers,
Paul
> Here's what the refered document
> (http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html) says:
>
> --->8---
> * Do not use assertions for argument checking in public methods.
>
> Argument checking is typically part of the published specifications
> (or contract) of a method, and these specifications must be obeyed
> whether assertions are enabled or disabled. Another problem with using
> assertions for argument checking is that erroneous arguments should
> result in an appropriate runtime exception (such as
> IllegalArgumentException, IndexOutOfBoundsException, or
> NullPointerException). An assertion failure will not throw an
> appropriate exception.
>
> * Do not use assertions to do any work that your application requires
> for correct operation.
>
> Because assertions may be disabled, programs must not assume that the
> boolean expression contained in an assertion will be evaluated.
> ---8<---
>
> The use of Assert (or whatever) I thought of would be precisely that
> illegal use: to faciliate argument checking in public methods (and
> throw IAE), in order to enforce object contracts.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Ola Berg <ol...@ports.se>.
>Case closed! I will go and read up on 1.4.
To myself: Not so quick, young Ola. Here's what the refered document (http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html) says:
--->8---
* Do not use assertions for argument checking in public methods.
Argument checking is typically part of the published specifications (or contract) of a method, and these specifications must be obeyed whether assertions are enabled or disabled. Another problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime exception (such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException). An assertion failure will not throw an appropriate exception.
* Do not use assertions to do any work that your application requires for correct operation.
Because assertions may be disabled, programs must not assume that the boolean expression contained in an assertion will be evaluated.
---8<---
The use of Assert (or whatever) I thought of would be precisely that illegal use: to faciliate argument checking in public methods (and throw IAE), in order to enforce object contracts. If it is easy to enforce a contract, more people will enforce their contracts better and the amount of stable code in the world will increase.
<random-tought>
Assertions as a runtime property will render a lot of the use-cases for assertions useless... I wonder why they did this?
</random-tought>
/O
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Ola Berg <ol...@ports.se>.
> Here's a page explaining 1.4's assertions. I highly recommend reading
> it -- it will short-circuit a lot of the discussion that Ola's question
> is bound to generate:
>
> http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html
Excellent. I've just recently upgraded and haven't had the time to check the new features.
> The only one of the examples in Ola's message that isn't trivial with
> 1.4's assert is "noNullElements". If there is actually a need for
> assert utilities, it's for deeper structural checks like that.
But for this we already have the boolean is*** methods in ArrayUtils that one can use in assert statements.
Case closed! I will go and read up on 1.4.
/O
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Henri Yandell <ba...@generationjava.com>.
On Mon, 21 Oct 2002, Paul Cantrell wrote:
> JDK 1.4's assertion facilities make most of Ola's examples too trivial
> to warrant helper methods, IMO -- the following all seem quite clear
> and concise :
>
> assert object != null;
Yeah, one reason for Ola's suggestion would be to offer similar
functionality to JDK 1.3 and before users.
> However, the Java version has a big advantage: In the latter example,
> the code will *always* call elements.toString() -- even if the
> assertion succeeds, and even if assertions are turned off! The Java
> native version doesn't evalutate the condition unless assertions are
> enabled, and doesn't evaluate the message unless the assertion fails.
Yeah, I think it'd be accepted that people would gain better performance
using 1.4. When Lang becomes 1.4 dependent, thigns could be deprecated
and/or refactored.
>
> The only one of the examples in Ola's message that isn't trivial with
> 1.4's assert is "noNullElements". If there is actually a need for
> assert utilities, it's for deeper structural checks like that.
A good thing to focus on then. We'd need to have a good mental separation
between things that would deprecate obviously at 1.4 and things that would
refactor at 1.4.
> Henri: Java assertions throw AssertionError. JUnit does provide some
> basic assertion facilities, but only for test classes.
>
Then rather than throwing IllegalArgumentException I would suggest we
throw something closer to that. Either AssertionError, or more likely
AssertError or something. Close enough to feel the same, but not enough to
clash.
Hen
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Paul Cantrell <ca...@pobox.com>.
Here's a page explaining 1.4's assertions. I highly recommend reading
it -- it will short-circuit a lot of the discussion that Ola's question
is bound to generate:
http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html
JDK 1.4's assertion facilities make most of Ola's examples too trivial
to warrant helper methods, IMO -- the following all seem quite clear
and concise :
assert object != null;
assert array.length != 0;
assert !coll.isEmpty();
assert str.size() != 0;
Note that one can also do this:
assert (result != null) : "foo() result is null; expected bar
element in" + elements;
An assertion utility could, of course, take the string argument:
Assert.notNull(result, "foo() result is null; expected bar element
in " + elements);
However, the Java version has a big advantage: In the latter example,
the code will *always* call elements.toString() -- even if the
assertion succeeds, and even if assertions are turned off! The Java
native version doesn't evalutate the condition unless assertions are
enabled, and doesn't evaluate the message unless the assertion fails.
The only one of the examples in Ola's message that isn't trivial with
1.4's assert is "noNullElements". If there is actually a need for
assert utilities, it's for deeper structural checks like that.
What does seem much more generally useful is a hasNullElements()
method, which would make the assertion easy to write as a nice side
benefit.
Henri: Java assertions throw AssertionError. JUnit does provide some
basic assertion facilities, but only for test classes.
Cheers,
Paul
On Monday, October 21, 2002, at 09:12 AM, Henri Yandell wrote:
> First thoughts on assertion code:
>
> Does JUnit already have this? What do we feel about JUnits version if
> it
> does? Does JDK 1.4 contain much more than the 'assert' keyword?
>
> What does 1.4 throw when an assertion fails?
>
> Hen
>
> On Mon, 21 Oct 2002, Ola Berg wrote:
>
>> How about utilities for assertions?
>>
>> I can imagine:
>>
>> class Assert
>> notNull( Object)
>> noNullElements( Object[])
>> notEmpty( Object[]) //works for collections too
>>
>> plus either adding methods for common object types:
>>
>> notEmpty( String)
>>
>> or adding assert* methods to the lang *Utils:
>>
>> assertNotEmpty( String)
>>
>> When an assertion fails, the methods will throw
>> IllegalArgumentException
>>
>> Good or bad? I can do the coding, if people like it.
>>
>> /O
_________________________________________________________________
Paul Cantrell http://eidola.org http://innig.net
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Lang] Assertions?
Posted by Henri Yandell <ba...@generationjava.com>.
First thoughts on assertion code:
Does JUnit already have this? What do we feel about JUnits version if it
does? Does JDK 1.4 contain much more than the 'assert' keyword?
What does 1.4 throw when an assertion fails?
Hen
On Mon, 21 Oct 2002, Ola Berg wrote:
> How about utilities for assertions?
>
> I can imagine:
>
> class Assert
> notNull( Object)
> noNullElements( Object[])
> notEmpty( Object[]) //works for collections too
>
> plus either adding methods for common object types:
>
> notEmpty( String)
>
> or adding assert* methods to the lang *Utils:
>
> assertNotEmpty( String)
>
> When an assertion fails, the methods will throw IllegalArgumentException
>
> Good or bad? I can do the coding, if people like it.
>
> /O
>
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>