File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Validating data[] and criteria[] length in find method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Validating data[] and criteria[] length in find method" Watch "Validating data[] and criteria[] length in find method" New topic
Author

Validating data[] and criteria[] length in find method

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

Got a question. Did you checked if the criteria[] array passed to the find(String[]) method has less or equal number of elements than data[]?

At this moment, when I've got less elements in criteria[] array than in data[] - it's fine, I treat them as they would never exist. But if there are more criterias than fields I'm not handling such a situation, and ArrayIndexOutOfBound is thrown.

Did you used some validation on Data class level?

And another one - did you validate the number of fields passed to create(String[]) method? I mean - did you check the user provided more (or less) fields than expected from the database file schema?

I've got my own InvalidRecordFormatException which is thrown when something is not OK with the record while reading or writing. The problem is, it is a low-level exception (not on Data Class but in underlying Data Access Manager) and it is checked. Therefore I cannot add it as a throwable from my Data Class...

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

Howdy, Pedro!

Did you checked if the criteria[] array passed to the find(String[]) method has less or equal number of elements than data[]?


Well champ, in my case, if you pass null or an array whose size is different from the number of fields that a record has, then an IllegalArgumentException is thrown.

And another one - did you validate the number of fields passed to create(String[]) method?


Yup. The same way, if you pass null, or an array whose size is different from the number of fields of a record, or if any of the elements in the array is null, then an IllegalArgumentException is thrown.

Although I think it is a good idea to have an exception like InvalidRecordFormatException, I'd stay with the ones that already exist in the Java API. But if you decide to stay with InvalidRecordFormatException, then you can make it extend RuntimeException, then you'll be able to throw it wherever you find necessary.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4910
    
  10

I agree with Roberto: all the parameters of the methods in the Data class. If one is invalid (null, wrong length, length of a single value too long,...) an IllegalArgumentException is thrown.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks guys for such rapid answers!

I've decided to leave my InvalidRecordFormatException only for the lower layer, as I use it to indicate possible file corruption. It will still be checked.

In Data class, however, I will use the IllegalArgumentException to indicate that that:

(crate and update methods)
- inserted/updated record is null,
- inserted/updated record's fields number is not equal to defined in the schema,

(find method)
- criteria's size is BIGGER than the fields number defined in the schema, (less data is OK, as I will assume that the rest of the criterias are not important); null value passed as a criteria is also OK for me - I treat is as a 'find all' situation, just like a criterias filled with nulls, empty strings or mixed nulls and empty strings.

other methods are not tested for null values, as they are primitives.

Just wondering - did you test if the record's number is lower than 0?

Cheers!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4910
    
  10

In my implementation a record number less than 0 will result in a RecordNotFoundException
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

That's what I hoped to hear :-)

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating data[] and criteria[] length in find method
 
Similar Threads
Passed!
Passed 386/400
SCJD passed
passed SCJD with 360/400
Passed SCJD.