[jboss-cvs] jboss-seam/examples/wiki/src/test/org/jboss/seam/wiki/test/preferences ...

Christian Bauer christian at hibernate.org
Sat Dec 29 21:33:28 EST 2007


  User: cbauer  
  Date: 07/12/29 21:33:28

  Added:       examples/wiki/src/test/org/jboss/seam/wiki/test/preferences   
                        InstancePreferencesTests.java
                        SystemPreferencesTests.java
                        UserPreferencesTests.java
  Log:
  Complete overhaul of the preferences system
  
  Revision  Changes    Path
  1.1      date: 2007/12/30 02:33:28;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/test/org/jboss/seam/wiki/test/preferences/InstancePreferencesTests.java
  
  Index: InstancePreferencesTests.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.seam.wiki.test.preferences;
  
  import org.dbunit.operation.DatabaseOperation;
  import org.jboss.seam.wiki.core.action.PreferenceEditor;
  import org.jboss.seam.wiki.core.action.DocumentHome;
  import org.jboss.seam.wiki.core.action.prefs.DocumentEditorPreferences;
  import org.jboss.seam.wiki.core.engine.WikiTextParser;
  import org.jboss.seam.wiki.core.engine.NullWikiTextRenderer;
  import org.jboss.seam.wiki.core.engine.WikiMacro;
  import org.jboss.seam.wiki.core.ui.UIWikiFormattedText;
  import org.jboss.seam.wiki.preferences.Preferences;
  import org.jboss.seam.wiki.preferences.PreferenceValue;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceRegistry;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
  import org.jboss.seam.wiki.test.util.DBUnitSeamTest;
  import org.jboss.seam.wiki.plugin.lastmodified.LastModifiedDocumentsPreferences;
  import org.jboss.seam.core.Events;
  import org.jboss.seam.core.Conversation;
  import org.jboss.seam.contexts.Contexts;
  import org.testng.annotations.Test;
  
  import java.util.List;
  
  /**
   * @author Christian Bauer
   */
  public class InstancePreferencesTests extends DBUnitSeamTest {
  
      protected void prepareDBUnitOperations() {
          beforeTestOperations.add(
              new DataSetOperation("org/jboss/seam/wiki/test/WikiBaseData.dbunit.xml", DatabaseOperation.CLEAN_INSERT)
          );
      }
  
      @Test
      public void readInstancePreferences() throws Exception {
  
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
  
                  DocumentHome docHome = (DocumentHome)getInstance("documentHome");
                  assert docHome.getInstance().getId().equals(6l); // Init!
  
                  WikiTextParser parser = new WikiTextParser(docHome.getInstance().getContent(), true, false);
                  parser.setRenderer(new NullWikiTextRenderer() {
                      public String renderMacro(WikiMacro macro) {
                          Contexts.getEventContext().set(UIWikiFormattedText.CURRENT_MACRO_EVENT_VARIABLE, macro);
  
                          if (macro.getName().equals("lastModifiedDocuments")) {
                              LastModifiedDocumentsPreferences lmdPrefs =
                                      (LastModifiedDocumentsPreferences)Preferences.getInstance("LastModifiedDocuments", "currentMacro");
                              assert lmdPrefs.getDocumentTitleLength().equals(10l);
                          }
  
                          return null;
                      }
                  }).parse();
              }
  
          }.run();
      }
  
      @Test
      public void updateInstancePreferences() throws Exception {
  
          final String conversationId = new NonFacesRequest("/docEdit_d.xhtml") {
              protected void beforeRequest() {
                  setParameter("documentId", "6");
                  setParameter("parentDirectoryId", "3");
              }
          }.run();
  
          new FacesRequest("/docEdit_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("cid", conversationId);
              }
  
              protected void invokeApplication() throws Exception {
                  assert Conversation.instance().isLongRunning();
  
                  DocumentHome docHome = (DocumentHome)getInstance("documentHome");
                  assert docHome.getInstance().getId().equals(6l); // Init!
  
                  docHome.setFormContent("[<=lastModifiedDocuments[documentTitleLength=66]]");
  
                  assert invokeMethod("#{documentHome.update}").equals("updated");
              }
  
          }.run();
  
  
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
  
                  DocumentHome docHome = (DocumentHome)getInstance("documentHome");
                  assert docHome.getInstance().getId().equals(6l); // Init!
  
                  WikiTextParser parser = new WikiTextParser(docHome.getInstance().getContent(), true, false);
                  parser.setRenderer(new NullWikiTextRenderer() {
                      public String renderMacro(WikiMacro macro) {
                          Contexts.getEventContext().set(UIWikiFormattedText.CURRENT_MACRO_EVENT_VARIABLE, macro);
  
                          if (macro.getName().equals("lastModifiedDocuments")) {
                              LastModifiedDocumentsPreferences lmdPrefs =
                                      (LastModifiedDocumentsPreferences)Preferences.getInstance("LastModifiedDocuments", "currentMacro");
                              assert lmdPrefs.getDocumentTitleLength().equals(66l);
                          }
  
                          return null;
                      }
                  }).parse();
              }
  
          }.run();
      }
  
  }
  
  
  1.1      date: 2007/12/30 02:33:28;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/test/org/jboss/seam/wiki/test/preferences/SystemPreferencesTests.java
  
  Index: SystemPreferencesTests.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.seam.wiki.test.preferences;
  
  import org.dbunit.operation.DatabaseOperation;
  import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
  import org.jboss.seam.wiki.core.action.PreferenceEditor;
  import org.jboss.seam.wiki.preferences.Preferences;
  import org.jboss.seam.wiki.preferences.PreferenceValue;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceRegistry;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
  import org.jboss.seam.wiki.test.util.DBUnitSeamTest;
  import org.testng.annotations.Test;
  
  import java.util.List;
  
  /**
   * @author Christian Bauer
   */
  public class SystemPreferencesTests extends DBUnitSeamTest {
  
      protected void prepareDBUnitOperations() {
          beforeTestOperations.add(
              new DataSetOperation("org/jboss/seam/wiki/test/WikiBaseData.dbunit.xml", DatabaseOperation.CLEAN_INSERT)
          );
      }
  
      @Test
      public void readSystemPreferences() throws Exception {
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
                  WikiPreferences wikiPrefs = (WikiPreferences) Preferences.getInstance("Wiki");
                  assert wikiPrefs.getShowDocumentCreatorHistory();
                  assert wikiPrefs.getShowTags();
                  assert wikiPrefs.getDefaultDocumentId().equals(6l);
                  assert wikiPrefs.getMemberArea().equals("Members");
              }
  
          }.run();
      }
      
      @Test
      public void updateSystemPreferences() throws Exception {
  
          loginAdmin();
  
          final String conversationId = new NonFacesRequest("/adminHome_d.xhtml") {}.run();
  
          new FacesRequest("/adminHome_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("cid", conversationId);
              }
  
              protected void invokeApplication() throws Exception {
  
                  PreferenceRegistry registry = (PreferenceRegistry)getInstance("preferenceRegistry");
                  PreferenceEntity wikiEntity = registry.getPreferenceEntitiesByName().get("Wiki");
  
                  invokeMethod("#{adminHome.initPreferencesEditor}");
  
                  PreferenceEditor prefEditor = (PreferenceEditor)getInstance("preferenceEditor");
                  prefEditor.selectPreferenceEntity(wikiEntity);
              }
  
          }.run();
  
          new FacesRequest("/adminHome_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("cid", conversationId);
              }
  
              protected void invokeApplication() throws Exception {
                  PreferenceEditor prefEditor = (PreferenceEditor)getInstance("preferenceEditor");
                  List<PreferenceValue> values = prefEditor.getPreferenceValues();
                  // This is somewhat dodgy... no other way to get the value we want
                  for (PreferenceValue value : values) {
                      if (value.getPreferenceProperty().getFieldName().equals("permlinkSuffix")) {
                          value.setValue(".newsuffix");
                      }
                  }
                  assert invokeMethod("#{adminHome.update()}") == null;
              }
  
          }.run();
  
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
                  WikiPreferences wikiPrefs = (WikiPreferences) Preferences.getInstance("Wiki");
                  assert wikiPrefs.getPermlinkSuffix().equals(".newsuffix");
              }
  
          }.run();
      }
  
      private void loginAdmin() throws Exception {
          new FacesRequest() {
             protected void invokeApplication() throws Exception {
                setValue("#{identity.username}", "admin");
                setValue("#{identity.password}", "admin");
                invokeAction("#{identity.login}");
                assert getValue("#{identity.loggedIn}").equals(true);
             }
          }.run();
      }
  
  }
  
  
  
  1.1      date: 2007/12/30 02:33:28;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/test/org/jboss/seam/wiki/test/preferences/UserPreferencesTests.java
  
  Index: UserPreferencesTests.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.seam.wiki.test.preferences;
  
  import org.dbunit.operation.DatabaseOperation;
  import org.jboss.seam.wiki.core.action.PreferenceEditor;
  import org.jboss.seam.wiki.core.action.prefs.DocumentEditorPreferences;
  import org.jboss.seam.wiki.preferences.Preferences;
  import org.jboss.seam.wiki.preferences.PreferenceValue;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceRegistry;
  import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
  import org.jboss.seam.wiki.test.util.DBUnitSeamTest;
  import org.testng.annotations.Test;
  
  import java.util.List;
  
  /**
   * @author Christian Bauer
   */
  public class UserPreferencesTests extends DBUnitSeamTest {
  
      protected void prepareDBUnitOperations() {
          beforeTestOperations.add(
              new DataSetOperation("org/jboss/seam/wiki/test/WikiBaseData.dbunit.xml", DatabaseOperation.CLEAN_INSERT)
          );
      }
  
      @Test
      public void createUserPreferences() throws Exception {
  
          loginMember();
  
          final String conversationId = new NonFacesRequest("/userHome_d.xhtml") {
              protected void beforeRequest() {
                  setParameter("userId", "3");
              }
  
          }.run();
  
          new FacesRequest("/userHome_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("cid", conversationId);
              }
  
              protected void invokeApplication() throws Exception {
  
                  PreferenceRegistry registry = (PreferenceRegistry)getInstance("preferenceRegistry");
                  PreferenceEntity docEditorEntity = registry.getPreferenceEntitiesByName().get("DocEditor");
  
                  invokeMethod("#{userHome.initPreferencesEditor}");
  
                  PreferenceEditor prefEditor = (PreferenceEditor)getInstance("preferenceEditor");
                  prefEditor.selectPreferenceEntity(docEditorEntity);
              }
  
          }.run();
  
          new FacesRequest("/userHome_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("cid", conversationId);
              }
  
              protected void invokeApplication() throws Exception {
                  PreferenceEditor prefEditor = (PreferenceEditor)getInstance("preferenceEditor");
                  List<PreferenceValue> values = prefEditor.getPreferenceValues();
                  // This is somewhat dodgy... no other way to get the value we want
                  for (PreferenceValue value : values) {
                      if (value.getPreferenceProperty().getFieldName().equals("minorRevisionEnabled")) {
                          assert value.getValue().equals(Boolean.TRUE); // Should be the system setting
                          value.setValue(false);
                      }
                  }
                  assert invokeMethod("#{userHome.update()}").equals("updated");
              }
  
          }.run();
  
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
                  DocumentEditorPreferences docEditorPrefs = (DocumentEditorPreferences) Preferences.getInstance("DocEditor");
                  assert !docEditorPrefs.getMinorRevisionEnabled();
              }
  
          }.run();
  
  
          // Logout and check system setting, should still be true
          logout();
  
          new NonFacesRequest("/docDisplay_d.xhtml") {
  
              protected void beforeRequest() {
                  setParameter("documentId", "6");
              }
  
              protected void renderResponse() throws Exception {
                  DocumentEditorPreferences docEditorPrefs = (DocumentEditorPreferences) Preferences.getInstance("DocEditor");
                  assert docEditorPrefs.getMinorRevisionEnabled();
              }
  
          }.run();
  
      }
  
      private void loginMember() throws Exception {
          new FacesRequest() {
             protected void invokeApplication() throws Exception {
                setValue("#{identity.username}", "member");
                setValue("#{identity.password}", "member");
                invokeAction("#{identity.login}");
                assert getValue("#{identity.loggedIn}").equals(true);
             }
          }.run();
      }
  
      private void logout() throws Exception {
          new FacesRequest() {
             protected void invokeApplication() throws Exception {
                 invokeMethod("#{identity.logout}");
             }
          }.run();
      }
  
  }
  
  



More information about the jboss-cvs-commits mailing list