Bug 338531 - Wrong ordering of elements in WorkingSetModel.fAllWorkingSets
Summary: Wrong ordering of elements in WorkingSetModel.fAllWorkingSets
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P1 major (vote)
Target Milestone: 3.7 M6   Edit
Assignee: Raksha Vasisht CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-01 08:52 EST by Dani Megert CLA
Modified: 2011-03-11 18:35 EST (History)
1 user (show)

See Also:
daniel_megert: review-
daniel_megert: review+


Attachments
Patch (3.83 KB, patch)
2011-03-02 07:54 EST, Raksha Vasisht CLA
daniel_megert: review-
Details | Diff
Patch_v2 (5.56 KB, patch)
2011-03-02 10:35 EST, Raksha Vasisht CLA
no flags Details | Diff
Patch_v3 (5.63 KB, patch)
2011-03-04 14:41 EST, Raksha Vasisht CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2011-03-01 08:52:27 EST
When setting the elements using WorkingSetModel.setWorkingSets(IWorkingSet[], boolean, IWorkingSet[]) or WorkingSetModel.setActiveWorkingSets(IWorkingSet[]) then the ordering in fActiveWorkingSets and fAllWorkingSets can differ. This is bad because that ordering is used in the dialog and the drop adapter.

Test Case:
1. In PackageExplorerPart.internalTestShowWorkingSets replace 
   fWorkingSetModel.setWorkingSets(workingSets, false, workingSets);
   with:
   fWorkingSetModel.setActiveWorkingSets(workingSets);
2. Run org.eclipse.jdt.ui.tests.packageview.WorkingSetDropAdapterTest
==> test failure.

This happens because previous tests created a different order and when we then set [w1,w2,w3] the order in active and all is different.

==> we need to ensure that the same relative ordering is used.
Comment 1 Markus Keller CLA 2011-03-01 09:27:03 EST
Please also have a closer look at WorkingSetModel.java 1.40, line 567:

    allElements.toArray(new IWorkingSet[elements.size()])

The "elements.size()" looks wrong to me.
Comment 2 Raksha Vasisht CLA 2011-03-02 07:54:58 EST
Created attachment 190139 [details]
Patch

(In reply to comment #0)
> When setting the elements using WorkingSetModel.setWorkingSets(IWorkingSet[],
> boolean, IWorkingSet[]) or WorkingSetModel.setActiveWorkingSets(IWorkingSet[])
> then the ordering in fActiveWorkingSets and fAllWorkingSets can differ. This is
> bad because that ordering is used in the dialog and the drop adapter.
> 
> Test Case:
> 1. In PackageExplorerPart.internalTestShowWorkingSets replace 
>    fWorkingSetModel.setWorkingSets(workingSets, false, workingSets);
>    with:
>    fWorkingSetModel.setActiveWorkingSets(workingSets);
> 2. Run org.eclipse.jdt.ui.tests.packageview.WorkingSetDropAdapterTest
> ==> test failure.
> 
> This happens because previous tests created a different order and when we then
> set [w1,w2,w3] the order in active and all is different.
> 
> ==> we need to ensure that the same relative ordering is used.

This happens only programmatically when the working sets are created in one order and set as active in another since the working set manager updates the fAllWorkingSets in the created order and fActiveWorkingSets has the newly set order. The second test fails because even though both lists of working sets are restored from the memento in the same order that the previous test left it, the second test creates working sets in a differnt order and only re-sets the fActiveWorkingSet resulting in a mismatch.  

 But this does not happen in DnD operation since org.eclipse.jdt.internal.ui.packageview.WorkingSetDropAdapter.performWorkingSetReordering() takes care of the order of both lists on performDrop(). Also in configuration dialog we have not only active list but all working sets list, so the order there is saved in WorkingSetConfigurationDialog.fAllWorkingSets.  We do not see this code path any where else other than the test.

So the test must ensure that the order of creation is the same as the order used to set active working sets (since the order is saved by PE in a memento for subsequent tests) or set both the lists with the new order using setWorkingSets(..)(-->as in HEAD)

Or to be safe we could also add a check in setActiveWorkingSets(..) for such cases. The only solution in case of a difference in order is to reset fAllWorkingSets to default from the working set managers because the actual order of all working sets is lost. (-->patch 1)
Comment 3 Raksha Vasisht CLA 2011-03-02 07:56:58 EST
Dani, could you pls review?
Comment 4 Raksha Vasisht CLA 2011-03-02 07:57:51 EST
(In reply to comment #1)
> Please also have a closer look at WorkingSetModel.java 1.40, line 567:
> 
>     allElements.toArray(new IWorkingSet[elements.size()])
> 
> The "elements.size()" looks wrong to me.

Also fixed in patch. Thanks for noticing, Markus.
Comment 5 Dani Megert CLA 2011-03-02 09:02:56 EST
The fix is not good yet:
1. In setActiveWS the order that's already present in all working sets gets destroyed (all active ones are pushed on top).
2. setWorkingSets still has the issue. We should specify in the Javadoc that the relative order must be the same.

For setWorkingSets
- add to the Javadoc that all active ws must be part of all working sets
- rename the parameter 'workingSets' to 'allWorkingSets'

> > The "elements.size()" looks wrong to me.
> 
> Also fixed in patch. Thanks for noticing, Markus.
Committed that part to HEAD.
Comment 6 Raksha Vasisht CLA 2011-03-02 10:35:00 EST
Created attachment 190159 [details]
Patch_v2

(In reply to comment #5)
> The fix is not good yet:
> 1. In setActiveWS the order that's already present in all working sets gets
> destroyed (all active ones are pushed on top).

As discussed, since this is a public method such inconsistencies in ordering could happen accidentally by passing wrong parameters to the setters, we need to ensure the correct order in all working sets by comparing with active working sets list. Fixed this.

> 2. setWorkingSets still has the issue. We should specify in the Javadoc that
> the relative order must be the same.
> 
> For setWorkingSets
> - add to the Javadoc that all active ws must be part of all working sets
> - rename the parameter 'workingSets' to 'allWorkingSets'

ADded javadoc to both setters and an additional check for order in all working sets in setWorkingSets(..)
Comment 7 Dani Megert CLA 2011-03-04 08:30:42 EST
Looks good. Some minor details to fix before committing:
- move note to Javadoc before @param.
- setOrderInAllWorkingSets -> adjustOrderingOfAllWorkingSets
  - you can reorder without additional list by swapping elements
  - also improve Javadoc
- isOrderDifferentInWorkingSetLists
  - can be implemented more easily: no need to create another collection and
    compare it. Also, one can return as soon as one detects the first wrong
    ordering mismatch.

In general avoid creating extra variables and especially collections if not needed.
Comment 8 Raksha Vasisht CLA 2011-03-04 14:41:02 EST
Created attachment 190435 [details]
Patch_v3

Patch with suggested changes.
Comment 9 Raksha Vasisht CLA 2011-03-04 14:41:51 EST
(In reply to comment #8)
> Created attachment 190435 [details] [diff]
> Patch_v3
> 
> Patch with suggested changes.

Committed to HEAD.