Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Where is Maven Library for containers that implement Comparable?

 
Ranch Hand
Posts: 422
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to write a JUnit test for a function that produces



When I started to implement this class:



I said to myself: "This is nuts! Surely someone has already done this for Array, LinkedList, ArrayList and friends!

But after google searching, I could not find any such examples...

Can someone point me to a maven library that will help me compare containers containing containers and maybe point me to an example?

I've been using the Hamcrest to compare Sets of Strings. Perhaps all I need is an example of using Hamcrest to compare Sets of Lists of Strings? I'm looking at the hamcrest javadocs and I'm not seeing anything obviouse.

Thanks
Siegfried
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused.  You want to write a JUnit test.  What does the class you started to implement have to do with a JUnit test?  Then you ask for a Maven library.  What does that have to do with a JUnit test?

Can you clear this up?
 
Siegfried Heintze
Ranch Hand
Posts: 422
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the confusion.

By maven library I mean JAR files available from the maven repository (probably a bad choice of terms! sorry!)

I have a function called GetPermutations that returns a Set<Collection<String>>.

I want to write a JUnit test for this function.

To write a JUnit test, I need a matcher to compare the actual and expected values.

So I create and populate a variable called expected of type Set<Collection<String>>.

Now, how do I compare "executed" with the returned value from GetPermuations?

Hamcrest is a library available on maven that will allow me to compare Sets if the elements of the set implement Comparable.

Unfortunately, ArrayLists, Arrays Lists don't implement Comparable.

Do you know of any libraries, preferably on the maven repository site, that implement collections that implement Comparable?

I could implement my own as I started to do. Surely someone has already done this and I could use their implementation.

I hope this helps.
Thank you for the very prompt response.

Siegfried
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUnit is an open-source Java Unit Testing Framework. It is the de facto standard for Java Unit Testing. JUnit is not included in JDK, but included in most of the IDEs such as Eclipse and NetBeans...

Installing JUnit:
Goto http://junit.org/ ⇒ "Download and Install Guide" ⇒ Download the "junit.jar" and "hamcrest-core.jar". You could download the API documentation as well as the source code.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are Hamcrest matchers for many different things, including checking if a collection contains the expected items. For example:

The 'contains' matcher is in the Hamcrest library, if you are using Maven, then this is the dependency to include:
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure those matchers will work for a Collection of Collections, will they?

Then again, there's one simple way to find out...
 
Siegfried Heintze
Ranch Hand
Posts: 422
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me understand why this test fails. The first assertThat passes but the assertThat in the for each loop fails.
This is probably because assertThat is doing shallow compares (comparing references) instead of deep compares (comparing the lists element by element).

Perhaps this is because List and ArrayList do not implement Comparable? If so, is there some library available in the maven repository that does have containers (such as lists) that implement Comparable?
If not, why?

 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not what you think. It's because r is a collection of String and expected is not, it's a collection of collection of String. That's like trying to compare an int[] to an int[][].
 
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claudia Joseph, welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would a Collection implement Comparable? Comparable mandates a total ordering, so can you say that ListA is greater than ListB (or less than or the same as)? If you cannot give a simple definition of that relationship, then you cannot expect the classes involved to implement Comparable.

Moving discussion: let't try the testing forum.
 
Siegfried Heintze
Ranch Hand
Posts: 422
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
I tried those hamcrest matchers and they seem to work for contianers (like sets) of strings.

However, I'm confused about how hamcrest compares containers of containers like Set<Collection<? extends String>>;

In other languages like C++, I can specify the lambda function or to be used for comparing elements of a Set container (for example). I assume in java, that function is Comparable::CompareTo. However, when I look at the documentation, I don't see that classes like List, or Set or ArrayList implement such a function. So how does class Set determine if you are adding a new element that is a duplicate? How does the hamcrest matcher know if a set contains a certain element (in any order)?

I tried creating descendants of ArrayList and List, and String that implement Comparable and failed (perhaps this should be a topic of a different post).
Like I suggested previously, I am surprised there is not some library in the maven repository that implements this already.

Can anyone improve on this awful code I wrote? It works, but it (my function compareIteratorCollections) does not use generics and is not as type-safe as it could be. Can someone show me how to write compareIteratorCollections using generics?



Here is my miserable matcher function:
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this applies to your situation, but when I want to compare two sets, I do it like this:
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No, but it will let you create a List of Lists:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic