UpdatingAppTestCases.java :  » UnTagged » more-than-enough » com » boodaba » todocarts » test » appupdt » Android Open Source

Android Open Source » UnTagged » more than enough 
more than enough » com » boodaba » todocarts » test » appupdt » UpdatingAppTestCases.java
package com.boodaba.todocarts.test.appupdt;

import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.util.FileUtils;
import org.junit.Before;
import org.junit.Test;

import com.boodaba.todocarts.app.JustUpdater;
import com.boodaba.todocarts.domain.ILabelRepository;
import com.boodaba.todocarts.domain.Label;
import com.boodaba.todocarts.domain.LabelRepository;
import com.boodaba.todocarts.domain.Todo;
import com.boodaba.todocarts.infstr.Db4oStoreContext;

public class UpdatingAppTestCases {
  
  private final int mCurrentVersion = 5;
  
  private String[] mLabelTitles = new String[] {
      "label with 3 todos",
      "empty list",
      "two done one not",
      "all ten done"
  };
  
  private String[] mTodoTitles = new String[] {
      "one todo",
      "make an awesome",
      "i wanna be somebody soon",
      "money makes me crazy",
      "did we say we are hard",
      "did we say we are separated",
      "citizens of bastard nation",
      "big ben someone loves",
      "go go go",
      "well that's it"
  };
  
  private List<String> mTodoTitlesList;
    
  @Before
  public void setUp() {
    if (!checkDb(mCurrentVersion)) {
      createModelDb(mCurrentVersion);
    }
    mTodoTitlesList = new ArrayList<String>();
    for (String title : mTodoTitles) {
      mTodoTitlesList.add(title);
    }
  }
  
  @Test
  public void currentSelfValidating() {
    String testPath = copy(mCurrentVersion);
    Db4oStoreContext sc = new Db4oStoreContext(testPath);
    
    LabelRepository repo = new LabelRepository(sc);
    
    assertTrue(repo.findAll().size() == 4);
    
    repo = null;
    sc.close();
    sc = null;
  }
  
  @Test
  public void testUpgradeFrom2() {
    String testPath = copy(2);
    Db4oStoreContext sc = new Db4oStoreContext(testPath);
    JustUpdater ju = new JustUpdater(sc);
    ju.update(2, mCurrentVersion);
    
    LabelRepository repo = new LabelRepository(sc);
    assertTrue(repo.findAll().size() == 4);
    assertTrue(repo.getLabelByTitle(mLabelTitles[0]).getAllTodos().size() == 3);
    assertEquals(3, repo.getLabelByTitle(mLabelTitles[0]).getIncompletedTodos().size());
    assertTrue(repo.getLabelByTitle(mLabelTitles[1]).getAllTodos().size() == 0);
    assertEquals(3, repo.getLabelByTitle(mLabelTitles[2]).getAllTodos().size());
    assertEquals(1, repo.getLabelByTitle(mLabelTitles[2]).getIncompletedTodos().size());
    assertTrue(repo.getLabelByTitle(mLabelTitles[3]).getAllTodos().size() == 10);
    assertEquals(0, repo.getLabelByTitle(mLabelTitles[3]).getIncompletedTodos().size());
    
    for (Label lItem : repo.findAll()) {
      for (Todo todo : lItem.getAllTodos()) {
        assertTrue(mTodoTitlesList.contains(todo.getTitle()));
      }
    }
        
    repo = null;
    sc.close();
    sc = null;
  }
  
  @Test
  public void testUpgradeFrom3() {
    String testPath = copy(3);
    Db4oStoreContext sc = new Db4oStoreContext(testPath);
    JustUpdater ju = new JustUpdater(sc);
    ju.update(3, mCurrentVersion);
    
    LabelRepository repo = new LabelRepository(sc);
    assertTrue(repo.findAll().size() == 4);
    assertTrue(repo.getLabelByTitle(mLabelTitles[0]).getAllTodos().size() == 3);
    assertEquals(3, repo.getLabelByTitle(mLabelTitles[0]).getIncompletedTodos().size());
    assertTrue(repo.getLabelByTitle(mLabelTitles[1]).getAllTodos().size() == 0);
    assertEquals(3, repo.getLabelByTitle(mLabelTitles[2]).getAllTodos().size());
    assertEquals(1, repo.getLabelByTitle(mLabelTitles[2]).getIncompletedTodos().size());
    assertTrue(repo.getLabelByTitle(mLabelTitles[3]).getAllTodos().size() == 10);
    assertEquals(0, repo.getLabelByTitle(mLabelTitles[3]).getIncompletedTodos().size());
    
    for (Label lItem : repo.findAll()) {
      for (Todo todo : lItem.getAllTodos()) {
        assertTrue(mTodoTitlesList.contains(todo.getTitle()));
      }
    }
        
    repo = null;
    sc.close();
    sc = null;
  }
  
  private String getPath(int num) {
    String template = "dbFile_model" + num + ".db4o";
    URL u = getClass().getResource("/com/boodaba/todocarts/test/appupdt/");
    return u.getPath() + template;
  }
  
  private Boolean checkDb(int num) {
    return (new File(getPath(num))).exists();
  }
  
  private void createModelDb(int num) {
    Db4oStoreContext sc = new Db4oStoreContext(getPath(num));
    ILabelRepository labelRepo = new LabelRepository(sc);
    
    for (Label lab : getModelLabels()) {
      labelRepo.save(lab);
    }
    
    labelRepo = null;
    sc.close();
    sc = null;
  }
  
  private String copy(int num) {
    String modelPath = getPath(num);
    String copyPath = getPath(num) + ".copy";
    try {
      FileUtils.getFileUtils().copyFile(modelPath, copyPath, null, true);
    } catch (IOException e) {
      
    }
    return copyPath;
  }
  
  private List<Label> getModelLabels() {
    ArrayList<Label> lList = new ArrayList<Label>();
    
    Label l = new Label(mLabelTitles[0]);
    l.inviteTodo(mTodoTitles[3]);
    l.inviteTodo(mTodoTitles[4]);
    l.inviteTodo(mTodoTitles[1]);
    lList.add(l);
    
    l = new Label(mLabelTitles[1]);
    lList.add(l);
    
    l = new Label(mLabelTitles[2]);
    l.inviteTodo(mTodoTitles[0]);
    l.inviteTodo(mTodoTitles[2]);
    l.inviteTodo(mTodoTitles[3]);
    l.completeTodo(mTodoTitles[3]);
    l.completeTodo(mTodoTitles[2]);
    lList.add(l);
    
    l = new Label(mLabelTitles[3]);
    for (String title : mTodoTitles) {
      l.inviteTodo(title);
      l.completeTodo(title);
    }
    lList.add(l);
        
    return lList;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.