You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Benson Margulies <bi...@gmail.com> on 2011/06/12 21:23:37 UTC

org.codehaus.plexus.util.CollectionUtils.intersection

I think I've written a unit test for the contract of this function as
written in the javadoc, but it fails. The intersection function
returns an empty collection when the inputs most definitely have a
non-null intersection. What am I missing?

    @SuppressWarnings( "rawtypes" )
    @Test
    public void testIntersection() throws Exception {
        Collection<String> c1 = new ArrayList<String>();
        Collection<String> c2 = new ArrayList<String>();
        /*
         * An exhaustive black box test here
         * would involve generating a great deal of data,
         * perhaps even different sizes and collection classes.
         */

        c1.add("red");
        c1.add("blue");
        c1.add("green");
        c1.add("socialist");
        c1.add("red");
        c1.add("purple");
        c1.add("porpoise");
        c1.add("green");
        c1.add("blue");
        c1.add("gray");

        c1.add("blue");
        c1.add("12");
        c1.add("15");
        c1.add("blue");
        c1.add("porpoise");
        c1.add("33.3");
        c1.add("jabberwock");

        Multiset<String> correct = HashMultiset.create();
        correct.add( "blue" );
        correct.add( "blue" );
        correct.add( " porpoise ");

        @SuppressWarnings( "unchecked" )
        Collection<String> res = CollectionUtils.intersection( c1, c2 );
        Multiset<String> actual = HashMultiset.create();
        actual.addAll(res);
        assertEquals( correct, actual );

    }

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: org.codehaus.plexus.util.CollectionUtils.intersection

Posted by Jeff Jensen <je...@upstairstechnology.com>.
With what JUnit version?  I just tested is() and hasItems() and the
failure reporting message is 99% the same as assertEquals() with JUnit
4.8.2.  They both display toString()s of the expected and actuals,
just prefixed with different names/words.  What am I missing?


On Sun, Jun 12, 2011 at 2:52 PM, Stephen Connolly
<st...@gmail.com> wrote:
> At the very leaset
>
> assertThat(actual, is(expected));
>
> gives better diagnostics
>
> then there is
>
> assertThat(actual, not(is(unexpected));
>
> nice ones are
>
> assertThat(actual, hasItems(expectedContents));
>
> On 12 June 2011 20:43, Benson Margulies <bi...@gmail.com> wrote:
>> Next class. I haven't learned that one yet.
>>
>> On Sun, Jun 12, 2011 at 3:39 PM, Stephen Connolly
>> <st...@gmail.com> wrote:
>>> On 12 June 2011 20:23, Benson Margulies <bi...@gmail.com> wrote:
>>>> I think I've written a unit test for the contract of this function as
>>>> written in the javadoc, but it fails. The intersection function
>>>> returns an empty collection when the inputs most definitely have a
>>>> non-null intersection. What am I missing?
>>>>
>>>>    @SuppressWarnings( "rawtypes" )
>>>>    @Test
>>>>    public void testIntersection() throws Exception {
>>>>        Collection<String> c1 = new ArrayList<String>();
>>>>        Collection<String> c2 = new ArrayList<String>();
>>>>        /*
>>>>         * An exhaustive black box test here
>>>>         * would involve generating a great deal of data,
>>>>         * perhaps even different sizes and collection classes.
>>>>         */
>>>>
>>>>        c1.add("red");
>>>>        c1.add("blue");
>>>>        c1.add("green");
>>>>        c1.add("socialist");
>>>>        c1.add("red");
>>>>        c1.add("purple");
>>>>        c1.add("porpoise");
>>>>        c1.add("green");
>>>>        c1.add("blue");
>>>>        c1.add("gray");
>>>>
>>>>        c1.add("blue");
>>>>        c1.add("12");
>>>>        c1.add("15");
>>>>        c1.add("blue");
>>>>        c1.add("porpoise");
>>>>        c1.add("33.3");
>>>>        c1.add("jabberwock");
>>>>
>>>>        Multiset<String> correct = HashMultiset.create();
>>>>        correct.add( "blue" );
>>>>        correct.add( "blue" );
>>>>        correct.add( " porpoise ");
>>>>
>>>>        @SuppressWarnings( "unchecked" )
>>>>        Collection<String> res = CollectionUtils.intersection( c1, c2 );
>>>>        Multiset<String> actual = HashMultiset.create();
>>>>        actual.addAll(res);
>>>>        assertEquals( correct, actual );
>>>
>>> I hate the bog standard assertEquals on collections... if you'd used
>>> the new style assertThat( actual, ... ) with the appropriate matcher
>>> you'd have better debug info ;-)
>>>
>>>>
>>>>    }
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: org.codehaus.plexus.util.CollectionUtils.intersection

Posted by Stephen Connolly <st...@gmail.com>.
At the very leaset

assertThat(actual, is(expected));

gives better diagnostics

then there is

assertThat(actual, not(is(unexpected));

nice ones are

assertThat(actual, hasItems(expectedContents));

On 12 June 2011 20:43, Benson Margulies <bi...@gmail.com> wrote:
> Next class. I haven't learned that one yet.
>
> On Sun, Jun 12, 2011 at 3:39 PM, Stephen Connolly
> <st...@gmail.com> wrote:
>> On 12 June 2011 20:23, Benson Margulies <bi...@gmail.com> wrote:
>>> I think I've written a unit test for the contract of this function as
>>> written in the javadoc, but it fails. The intersection function
>>> returns an empty collection when the inputs most definitely have a
>>> non-null intersection. What am I missing?
>>>
>>>    @SuppressWarnings( "rawtypes" )
>>>    @Test
>>>    public void testIntersection() throws Exception {
>>>        Collection<String> c1 = new ArrayList<String>();
>>>        Collection<String> c2 = new ArrayList<String>();
>>>        /*
>>>         * An exhaustive black box test here
>>>         * would involve generating a great deal of data,
>>>         * perhaps even different sizes and collection classes.
>>>         */
>>>
>>>        c1.add("red");
>>>        c1.add("blue");
>>>        c1.add("green");
>>>        c1.add("socialist");
>>>        c1.add("red");
>>>        c1.add("purple");
>>>        c1.add("porpoise");
>>>        c1.add("green");
>>>        c1.add("blue");
>>>        c1.add("gray");
>>>
>>>        c1.add("blue");
>>>        c1.add("12");
>>>        c1.add("15");
>>>        c1.add("blue");
>>>        c1.add("porpoise");
>>>        c1.add("33.3");
>>>        c1.add("jabberwock");
>>>
>>>        Multiset<String> correct = HashMultiset.create();
>>>        correct.add( "blue" );
>>>        correct.add( "blue" );
>>>        correct.add( " porpoise ");
>>>
>>>        @SuppressWarnings( "unchecked" )
>>>        Collection<String> res = CollectionUtils.intersection( c1, c2 );
>>>        Multiset<String> actual = HashMultiset.create();
>>>        actual.addAll(res);
>>>        assertEquals( correct, actual );
>>
>> I hate the bog standard assertEquals on collections... if you'd used
>> the new style assertThat( actual, ... ) with the appropriate matcher
>> you'd have better debug info ;-)
>>
>>>
>>>    }
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: org.codehaus.plexus.util.CollectionUtils.intersection

Posted by Benson Margulies <bi...@gmail.com>.
Next class. I haven't learned that one yet.

On Sun, Jun 12, 2011 at 3:39 PM, Stephen Connolly
<st...@gmail.com> wrote:
> On 12 June 2011 20:23, Benson Margulies <bi...@gmail.com> wrote:
>> I think I've written a unit test for the contract of this function as
>> written in the javadoc, but it fails. The intersection function
>> returns an empty collection when the inputs most definitely have a
>> non-null intersection. What am I missing?
>>
>>    @SuppressWarnings( "rawtypes" )
>>    @Test
>>    public void testIntersection() throws Exception {
>>        Collection<String> c1 = new ArrayList<String>();
>>        Collection<String> c2 = new ArrayList<String>();
>>        /*
>>         * An exhaustive black box test here
>>         * would involve generating a great deal of data,
>>         * perhaps even different sizes and collection classes.
>>         */
>>
>>        c1.add("red");
>>        c1.add("blue");
>>        c1.add("green");
>>        c1.add("socialist");
>>        c1.add("red");
>>        c1.add("purple");
>>        c1.add("porpoise");
>>        c1.add("green");
>>        c1.add("blue");
>>        c1.add("gray");
>>
>>        c1.add("blue");
>>        c1.add("12");
>>        c1.add("15");
>>        c1.add("blue");
>>        c1.add("porpoise");
>>        c1.add("33.3");
>>        c1.add("jabberwock");
>>
>>        Multiset<String> correct = HashMultiset.create();
>>        correct.add( "blue" );
>>        correct.add( "blue" );
>>        correct.add( " porpoise ");
>>
>>        @SuppressWarnings( "unchecked" )
>>        Collection<String> res = CollectionUtils.intersection( c1, c2 );
>>        Multiset<String> actual = HashMultiset.create();
>>        actual.addAll(res);
>>        assertEquals( correct, actual );
>
> I hate the bog standard assertEquals on collections... if you'd used
> the new style assertThat( actual, ... ) with the appropriate matcher
> you'd have better debug info ;-)
>
>>
>>    }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: org.codehaus.plexus.util.CollectionUtils.intersection

Posted by Stephen Connolly <st...@gmail.com>.
On 12 June 2011 20:23, Benson Margulies <bi...@gmail.com> wrote:
> I think I've written a unit test for the contract of this function as
> written in the javadoc, but it fails. The intersection function
> returns an empty collection when the inputs most definitely have a
> non-null intersection. What am I missing?
>
>    @SuppressWarnings( "rawtypes" )
>    @Test
>    public void testIntersection() throws Exception {
>        Collection<String> c1 = new ArrayList<String>();
>        Collection<String> c2 = new ArrayList<String>();
>        /*
>         * An exhaustive black box test here
>         * would involve generating a great deal of data,
>         * perhaps even different sizes and collection classes.
>         */
>
>        c1.add("red");
>        c1.add("blue");
>        c1.add("green");
>        c1.add("socialist");
>        c1.add("red");
>        c1.add("purple");
>        c1.add("porpoise");
>        c1.add("green");
>        c1.add("blue");
>        c1.add("gray");
>
>        c1.add("blue");
>        c1.add("12");
>        c1.add("15");
>        c1.add("blue");
>        c1.add("porpoise");
>        c1.add("33.3");
>        c1.add("jabberwock");
>
>        Multiset<String> correct = HashMultiset.create();
>        correct.add( "blue" );
>        correct.add( "blue" );
>        correct.add( " porpoise ");
>
>        @SuppressWarnings( "unchecked" )
>        Collection<String> res = CollectionUtils.intersection( c1, c2 );
>        Multiset<String> actual = HashMultiset.create();
>        actual.addAll(res);
>        assertEquals( correct, actual );

I hate the bog standard assertEquals on collections... if you'd used
the new style assertThat( actual, ... ) with the appropriate matcher
you'd have better debug info ;-)

>
>    }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: org.codehaus.plexus.util.CollectionUtils.intersection

Posted by Benson Margulies <bi...@gmail.com>.
Sure enough. Thanks.

On Sun, Jun 12, 2011 at 3:26 PM, Peter Janes <pe...@peterjanes.ca> wrote:
> Maybe it's that you're not adding anything to c2?
>
> On 12/06/11 03:23 PM, Benson Margulies wrote:
>>
>> I think I've written a unit test for the contract of this function as
>> written in the javadoc, but it fails. The intersection function
>> returns an empty collection when the inputs most definitely have a
>> non-null intersection. What am I missing?
>>
>>     @SuppressWarnings( "rawtypes" )
>>     @Test
>>     public void testIntersection() throws Exception {
>>         Collection<String>  c1 = new ArrayList<String>();
>>         Collection<String>  c2 = new ArrayList<String>();
>>         /*
>>          * An exhaustive black box test here
>>          * would involve generating a great deal of data,
>>          * perhaps even different sizes and collection classes.
>>          */
>>
>>         c1.add("red");
>>         c1.add("blue");
>>         c1.add("green");
>>         c1.add("socialist");
>>         c1.add("red");
>>         c1.add("purple");
>>         c1.add("porpoise");
>>         c1.add("green");
>>         c1.add("blue");
>>         c1.add("gray");
>>
>>         c1.add("blue");
>>         c1.add("12");
>>         c1.add("15");
>>         c1.add("blue");
>>         c1.add("porpoise");
>>         c1.add("33.3");
>>         c1.add("jabberwock");
>>
>>         Multiset<String>  correct = HashMultiset.create();
>>         correct.add( "blue" );
>>         correct.add( "blue" );
>>         correct.add( " porpoise ");
>>
>>         @SuppressWarnings( "unchecked" )
>>         Collection<String>  res = CollectionUtils.intersection( c1, c2 );
>>         Multiset<String>  actual = HashMultiset.create();
>>         actual.addAll(res);
>>         assertEquals( correct, actual );
>>
>>     }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org