• Dan Belwood
  • NEWBIE
  • 0 Points
  • Member since 2007

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hello all,

Hopefully a quick question:

My page code:

Code:
<div class="middle">
        <c:PaginationComponent items="{!allTags}" var="t" itemsPerPage="10">
         <apex:outputLink value="/{!t.tag.Id}">{!t.tag.Name}</apex:outputLink>
         <br/>
        </c:PaginationComponent>
</div>

 
My component code:
Code:
<apex:component controller="PaginationComponentController">
 <apex:attribute name="items" type="Pageable[]" description="Results to paginate" required="true" assignTo="{!resultsVar}"/>
 <apex:attribute name="itemsPerPage" type="Integer" description="Items to display per page" required="true" assignTo="{!itemsPerPageVar}"/>
 <apex:attribute name="var" type="String" description="internal variable" required="true"/>
 <apex:outputText value="{!start} to {!stop} of {!size}"/>
 <br/>
 <apex:repeat value="{!items}" var="result" first="{!start - 1}" rows="{!itemsPerPage}">
  <apex:variable var="{!var}" value="{!result}"/>
  <apex:componentBody >

  </apex:componentBody>
 </apex:repeat>
 <br/>
 <apex:outputText value="Pages:"/>
 <apex:repeat first="0" rows="1" value="{!pages}" var="p">
  &nbsp;{!p}
 </apex:repeat>
 <apex:repeat first="1" value="{!pages}" var="p">
  &nbsp;&#164;&nbsp;{!p}
 </apex:repeat>
</apex:component>

 
My controller:
Code:
public class PaginationComponentController {
 private Integer index;
 public Integer start {
  get {
   return this.index + 1;
  }
 }
 public Integer stop {
  get {
   if ((this.index + this.itemsPerPageVar) > this.size) {
    return this.size;
   }
   else {
    return this.index + this.itemsPerPageVar;
   }
  }
 }
 public Integer size {
  get {
   return this.resultsVar.size();
  }
 }
 public Integer page {get;set;}
 public Integer itemsPerPageVar {get;set;}
 public Pageable[] resultsVar {get;set;}
 public Integer pageCount {
  get {
   if (this.resultsVar.size() == 0 || this.resultsVar == null) {
    return 0;
   }
   else {
    Integer count = resultsVar.size() / this.itemsPerPageVar;
    if (Math.mod(resultsVar.size(), this.itemsPerPageVar) > 0) {
     count++;
    }
    return count;
   }
  }
 }
 
 public String[] pages {
  get {
   List<String> pageArr = new List<String>();
   for (Integer i=0;i<this.pageCount;i++) {
    pageArr.add(String.valueOf(i+1));
   }
   return pageArr;
  }
 }
 
 public PageReference previous() {
  if (this.index <= this.itemsPerPageVar) {
   this.index -= this.itemsPerPageVar;
  }
  else {this.index = 0;}
  return null;
 }
 public PageReference next() {
  this.index += this.itemsPerPageVar;
  return null;
 }
 public PageReference gotoPage() {
  this.index = (this.page - 1) * this.itemsPerPageVar;
  return null;
 }
 public PageReference first() {
  this.index = 0;
  return null;
 }
 
 public PageReference last() {
  this.index = (this.pageCount - 1) * this.itemsPerPageVar;
  return null;
 }
 
 public PaginationComponentController() {
  index = 0;
 }
 
 private static Pageable[] createResults() {
  Integer total = 47;
  List<Pageable> results = new List<Pageable>();
  Code_Project__c proj;
  Pageable p;
  for (Integer i=0;i<total;i++) {
   proj = new Code_Project__c(Name='Test', Description__c='Test', Download_URL__c='http://www.google.com');
   p = new Pageable();
   p.value = proj;
   results.add(p);
  }
  return results;
 }
 
 static testMethod void testPageCount() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  System.assertEquals(5, con.pageCount);
 }
 static testMethod void testPageCountNone() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  System.assertEquals(0, con.pageCount);
 }
 static testMethod void testNext() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  System.assertEquals(0, con.index);
  con.next();
  System.assertEquals(10, con.index);
 }
 static testMethod void testPrevious() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  System.assertEquals(0, con.index);
  con.next();
  System.assertEquals(10, con.index);
  con.previous();
  System.assertEquals(0, con.index);
 }
 static testMethod void testPreviousFirst() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  System.assertEquals(0, con.index);
  con.previous();
  System.assertEquals(0, con.index);
 }
 static testMethod void testNextLast() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  con.page = 5;
  con.gotoPage();
  con.next();
  System.assertEquals(40, con.index);
 }
 static testMethod void testGotoPage() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  System.assertEquals(0, con.index);
  con.page = 5;
  con.gotoPage();
  System.assertEquals(40, con.index);
  con.page = 3;
  con.gotoPage();
  System.assertEquals(20, con.index);
 }
 static testMethod void testPages() {
  PaginationComponentController con = new PaginationComponentController();
  con.itemsPerPageVar = 10;
  con.resultsVar = createResults();
  List<String> testArr = new List<String>{'1', '2', '3', '4' , '5'};
  System.assertEquals(testArr, con.pages);
 }
}

 
the {!allTags} method  returns a list of Pageable objects which are just SObjects, but I can use a List of SObjects as an attribute value.  When I run this page, the first tag's data is displayed, then 10 blank rows.  Any ideas?

Many thanks,
Dan
Hello all,

Unless I've missed a tick, I can't see how to unit test the presence of Error messages on a VF page.

Here's my controller method:

Code:
public PageReference save() {
  Code_Project__c project = new Code_Project__c(Name=this.metaData.Name__c, Description__c = this.metaData.Description__c);
  try {
   insert project;
   Project_Link__c[] links = new List<Project_Link__c>();
   links.add(new Project_Link__c(Name='Contact', URL__c=this.metaData.Contact_URL__c, Project__c=project.Id));
   links.add(new Project_Link__c(Name='Download', URL__c=this.metaData.Download_URL__c, Project__c=project.Id));
   if(this.metaData.Homepage_URL__c != '') {links.add(new Project_Link__c(Name='Homepage', URL__c=this.metaData.Homepage_URL__c, Project__c=project.Id));}
   insert links; 
  }
  catch (System.DMLException de) {
   ApexPages.addMessages(de);
   return null;
  } 
  catch (Exception e) {
   ApexPages.addMessages(e);
   return null;
  }
  return Page.ProjectCreateSuccess;
 }

 
I then call a test method where I don't set a required field:
Code:
static testMethod void testProjectCreateNoDescription() {
  Test.setCurrentPage(Page.ProjectWizardPage);
  CreateProjectWizardController testController = new CreateProjectWizardController();
  String name = 'Test Project ' + String.valueOf(System.currentTimeMillis());
  testController.metaData.Name__c = name;
  testController.metaData.Contact_URL__c = 'mailto:test@test.com';
  testController.metaData.Download_URL__c = 'http://test_project.google.com/files/release1.zip';
  testController.metaData.Homepage_URL__c = 'http://code.google.com/p/test_project';
  String nextPage = testController.save().getUrl();
  System.assertEquals('/apex/projectcreatesuccess', nextPage);
  Code_Project__c testProject = [select Name, Description__c, Status__c, (Select Name, URL__c from Links__r) from Code_Project__c where Name = :name];
  System.assertEquals(name, testProject.Name);
  System.assertEquals('Test Description', testProject.Description__c);
  System.assertEquals('Submitted', testProject.Status__c);
  System.assertEquals(3, testProject.Links__r.size());
  for (Project_Link__c link : testProject.Links__r) {
   if (link.Name == 'Contact') {System.assertEquals('mailto:test@test.com', link.URL__c);}
   if (link.Name == 'Download') {System.assertEquals('http://test_project.google.com/files/release1.zip', link.URL__c);}
   if (link.Name == 'Homepage') {System.assertEquals('http://code.google.com/p/test_project', link.URL__c);}
  }
 }

When I run this test, I receive the following run-time error:
System.Exception: ApexPages.addMessages can only be called from a Visualforce page

Any ideas?

Best regards,
Dan