Skip to content

Commit

Permalink
Merge pull request sakaiproject#370 from jonespm/KNL-1342
Browse files Browse the repository at this point in the history
KNL-1342 - Add unit testing for content resource sorting
  • Loading branch information
ottenhoff committed Apr 6, 2015
2 parents 5e4da54 + 5742758 commit fb11610
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.time.api.Time;
import org.sakaiproject.time.cover.TimeService;
import org.sakaiproject.util.BaseResourcePropertiesEdit;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -71,6 +72,9 @@ public class MockContentEntity implements ContentEntity, GroupAwareEdit
protected Map<String, MockContentEntity> memberMap = new HashMap<String, MockContentEntity>();
protected boolean isActiveEdit;

public MockContentEntity() {
this.resourceProperties = new BaseResourcePropertiesEdit();
}
/* (non-Javadoc)
* @see org.sakaiproject.content.api.ContentEntity#getContainingCollection()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ public class MockContentResource extends MockContentEntity implements ContentRes
protected String contentType;
protected String resourceId;

protected ResourcePropertiesEdit resourceProperties;

/**
* @param collectionId
* @param resourceId
*/
public MockContentResource(String collectionId, String resourceId)
{
super();
this.collectionId = collectionId;
this.resourceId = resourceId;
//Setup this property for sorting purposes
this.getPropertiesEdit().addProperty(ResourceProperties.PROP_DISPLAY_NAME, resourceId);
this.getPropertiesEdit().addProperty(ResourceProperties.PROP_CONTENT_LENGTH, "0");
}

/* (non-Javadoc)
Expand Down Expand Up @@ -109,6 +111,7 @@ public void setContent(byte[] content)
{
this.content = content;
this.contentLength = this.content.length;

}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@

package org.sakaiproject.content.impl.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.content.api.ContentResourceEdit;
import org.sakaiproject.content.impl.ContentHostingComparator;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;

import junit.framework.Assert;
import junit.framework.TestCase;

public class SortTest extends TestCase {
Expand All @@ -31,9 +41,10 @@ public SortTest(String name) {
super(name);
}

@SuppressWarnings("unchecked")
public void testSorts() {
ContentHostingComparator c = new ContentHostingComparator(null, true);

ContentHostingComparator c = new ContentHostingComparator(ResourceProperties.PROP_DISPLAY_NAME, true);
assertEquals(-1, c.compareLikeMacFinder("AAAAA", "BBBBBB"));
assertEquals(0, c.compareLikeMacFinder("AAAA", "AAAA"));
assertEquals(1, c.compareLikeMacFinder("BBBB", "AAAA"));
Expand Down Expand Up @@ -70,8 +81,46 @@ public void testSorts() {
*/
//assertEquals(0, c.compareLikeMacFinder("Leon", "leon"));
//assertEquals(0, c.compareLikeMacFinder("leon", "léon"));


}

private List createResources(List <String>resources) {
List <MockContentResource> testList = new ArrayList<MockContentResource>();
for (String resource:resources) {
testList.add(new MockContentResource("test",resource));
}
return testList;
}

public void testNameCompare() {
ContentHostingComparator c = new ContentHostingComparator(ResourceProperties.PROP_DISPLAY_NAME, true, false);
//Test ids to use
List <String> testIds = Arrays.asList("11","10","10x","1","1x","2x","2","4","4","4x","4x","6");
//Expected sort after the "smart sort"
List <String> smartSortIds = Arrays.asList("1","2","4","10","10x","6","11","1x","2x","4","4x","4x");
List <String> regularSortIds = Arrays.asList("11","10","10x","1","1x","2x","2","4","4","4x","4x","6");

List <MockContentResource> testResources = createResources(testIds);
List <MockContentResource> smartSortResources = createResources(smartSortIds);
List <MockContentResource> regularSortResources = createResources(regularSortIds);
Collections.sort(testResources,c);
for (int i=0;i<testResources.size();i++) {
assertEquals(testResources.get(i).resourceId,smartSortResources.get(i).resourceId);
// System.out.printf("\"%s\" ",testResources.get(i).resourceId);
}
System.out.println();

//Switch to content length (which is 0 in mock) and regular sorting
c = new ContentHostingComparator(ResourceProperties.PROP_CONTENT_LENGTH, false, true);
testResources = createResources(testIds);
Collections.sort(testResources,c);
for (int i=0;i<testResources.size();i++) {
assertEquals(testResources.get(i).resourceId,regularSortResources.get(i).resourceId);
// System.out.printf("\"%s\" ",testResources.get(i).resourceId);
}
System.out.println();
}

public void testLocaleSorts() {
ContentHostingComparator c = new ContentHostingComparator(null, true);
Expand Down

0 comments on commit fb11610

Please sign in to comment.