@Test
  public void deleteStudy_shouldDeleteGivenStudy() throws Exception {

    final String studyName = "Study Name";

    List<StudyDef> studies = studyManagerService.getStudies();
    Assert.assertEquals("There are 4 studies", 4, studies.size());
    Assert.assertNull(getStudy(studyName, studies));

    StudyDef study = new StudyDef();
    study.setName(studyName);
    study.setCreator(userService.getUsers().get(0));
    study.setDateCreated(new Date());

    studyManagerService.saveStudy(study);
    studies = studyManagerService.getStudies();
    Assert.assertEquals("Added 1 study, now there are 5", 5, studies.size());

    study = getStudy(studyName, studies);
    Assert.assertNotNull(study);

    studyManagerService.deleteStudy(study);

    studies = studyManagerService.getStudies();
    Assert.assertEquals("Deleted the study so there are 4 studies again", 4, studies.size());
    Assert.assertNull(getStudy(studyName, studies));
  }
  /**
   * Adds a new Item according to selected item on the <tt>Tree View.</tt>
   *
   * <p>If a <Study</tt> is selected, a new <tt>Study</tt> will be added.
   *
   * <p>If a <tt>Form</tt> is selected, a new <tt>Form</tt> will be added.
   *
   * <p>If a <tt>Form Version</tt> is selected, a new <tt>Form Version</tt> will be added.
   */
  public static void addNewItem(String xForm, Tree tree, List<StudyDef> studies, PopupPanel popup) {

    TreeItem item = tree.getSelectedItem();
    if (item == null || item.getUserObject() instanceof StudyDef) {
      StudyDef studyDef = new StudyDef(0, "New Study" + (tree.getItemCount() + 1));
      studyDef.setCreator(Context.getAuthenticatedUser());
      studyDef.setDateCreated(new Date());
      studyDef.setDirty(true);

      TreeItem root =
          new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup));
      root.setUserObject(studyDef);
      tree.addItem(root);
      studies.add(studyDef);
      tree.setSelectedItem(root);

      // Automatically add a new form
      addNewChildItem(tree, studies, popup);

    } else if (item.getUserObject() instanceof FormDef) {
      TreeItem parent = item.getParentItem();
      FormDef formDef =
          new FormDef(
              0, "New Form" + (parent.getChildCount() + 1), (StudyDef) parent.getUserObject());
      formDef.setCreator(Context.getAuthenticatedUser());
      formDef.setDateCreated(new Date());
      formDef.setDirty(true);

      item = addImageItem(parent, formDef.getName(), images.drafts(), formDef, null, popup);
      ((StudyDef) parent.getUserObject()).addForm(formDef);
      tree.setSelectedItem(item);
      parent.setState(true);

      // Automatically add a new form version
      addNewChildItem(tree, studies, popup);
    } else if (item.getUserObject() instanceof FormDefVersion) {
      TreeItem parent = item.getParentItem();
      FormDefVersion formDefVersion =
          new FormDefVersion(
              0, "v" + (parent.getChildCount() + 1), (FormDef) parent.getUserObject());
      formDefVersion.setCreator(Context.getAuthenticatedUser());
      formDefVersion.setDateCreated(new Date());
      formDefVersion.getFormDef().turnOffOtherDefaults(formDefVersion);
      formDefVersion.setDirty(true);

      if (xForm != null) formDefVersion.setXform(xForm);

      item =
          addImageItem(
              parent, formDefVersion.getName(), images.markRead(), formDefVersion, null, popup);
      ((FormDef) parent.getUserObject()).addVersion(formDefVersion);
      tree.setSelectedItem(item);
      parent.setState(true);
    }
  }
  /**
   * Bind <tt>StudyDef</tt> contents.
   *
   * @param tree <tt>Tree View</tt> to bind <tt>StudyDef</tt> to.
   * @param popup <tt>Popup</tt> to bind to the <tt>Tree View.</tt>
   * @param editable <tt>Editable</tt> we are checking contents for.
   */
  private static void setStudyContent(final Tree tree, final PopupPanel popup, Editable editable) {
    StudyDef studyDef = (StudyDef) editable;
    studyDef.setCreator(Context.getAuthenticatedUser());
    studyDef.setDateCreated(new Date());
    studyDef.setDirty(true);

    TreeItem root =
        new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup));
    root.setUserObject(studyDef);
    tree.addItem(root);
    Context.getStudies().add(studyDef);
    tree.setSelectedItem(root);

    List<FormDef> forms = studyDef.getForms();
    if (forms != null) {
      for (FormDef formDef : forms) importForm(tree, root, formDef, popup);
    }
  }