private void addWikiTranslation(String key, String message, Locale locale) throws XWikiException {
    XWikiDocument document =
        this.oldcore
            .getMockXWiki()
            .getDocument(this.defaultWikiTranslationReference, this.oldcore.getXWikiContext());

    if (!locale.equals(Locale.ROOT)) {
      XWikiDocument translatedDocument =
          document.getTranslatedDocument(locale, this.oldcore.getXWikiContext());
      if (translatedDocument == document) {
        translatedDocument = new XWikiDocument(document.getDocumentReference(), locale);
        translatedDocument.setDefaultLocale(document.getDefaultLocale());
      }
      document = translatedDocument;
    }

    document.setSyntax(Syntax.PLAIN_1_0);

    StringBuilder builder = new StringBuilder(document.getContent());

    builder.append('\n');
    builder.append(key);
    builder.append('=');
    builder.append(message);

    document.setContent(builder.toString());

    this.oldcore.getMockXWiki().saveDocument(document, "", this.oldcore.getXWikiContext());

    setBundles(document.getFullName());
  }
Exemplo n.º 2
0
  protected static BaseClass getXWikiGroupRelationClass(XWikiContext context)
      throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = context.getWiki();
    boolean needsUpdate = false;

    try {
      doc = xwiki.getDocument("XWiki.GroupRelationClass", context);
    } catch (Exception e) {
      doc = new XWikiDocument();
      doc.setSpace("XWiki");
      doc.setName("GroupRelationClass");
      needsUpdate = true;
    }

    BaseClass bclass = doc.getxWikiClass();
    bclass.setName("XWiki.GroupRelationClass");
    needsUpdate |= bclass.addTextField("name", "Name", 30);
    needsUpdate |= bclass.addTextField("parentpage", "Parent", 30);
    needsUpdate |= bclass.addTextAreaField("description", "Description", 40, 5);

    String content = doc.getContent();
    if ((content == null) || (content.equals(""))) {
      needsUpdate = true;
      doc.setContent("1 XWikiGroup");
      doc.setSyntax(Syntax.XWIKI_1_0);
    }

    if (needsUpdate) xwiki.saveDocument(doc, context);
    return bclass;
  }
  @Before
  public void before() throws Exception {
    Locale.setDefault(Locale.ROOT);

    // checking

    this.preferencesDocumentReference =
        new DocumentReference(
            this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences");

    XWikiDocument preferencesDocument = new XWikiDocument(this.preferencesDocumentReference);
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setXClassReference(
        new DocumentReference(
            this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences"));
    preferencesDocument.addXObject(preferencesObject);
    preferencesDocument.setSyntax(Syntax.PLAIN_1_0);
    this.oldcore
        .getMockXWiki()
        .saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());

    this.defaultWikiTranslationReference =
        new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "Translations");

    XWikiDocument defaultWikiTranslation = new XWikiDocument(this.defaultWikiTranslationReference);
    defaultWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
    this.oldcore
        .getMockXWiki()
        .saveDocument(defaultWikiTranslation, "", this.oldcore.getXWikiContext());

    // MessageTool

    this.tool =
        new XWikiMessageTool(
            this.oldcore
                .getMocker()
                .<ContextualLocalizationManager>getInstance(ContextualLocalizationManager.class));
  }
  /**
   * Check if class template document exists in this context and update. Create if not exists.
   *
   * @param context the XWiki context.
   * @throws XWikiException error when saving document.
   */
  protected void checkClassTemplateDocument(XWikiContext context) throws XWikiException {
    if (this.checkingClassTemplate) {
      return;
    }

    this.checkingClassTemplate = true;

    try {
      XWikiDocument doc;
      XWiki xwiki = context.getWiki();
      boolean needsUpdate = false;

      try {
        doc = xwiki.getDocument(getClassTemplateFullName(), context);
      } catch (Exception e) {
        doc = new XWikiDocument();
        doc.setSpace(getClassTemplateSpace());
        doc.setName(getClassTemplateName());
        needsUpdate = true;
      }

      if (doc.getObject(getClassFullName()) == null) {
        doc.createNewObject(getClassFullName(), context);

        needsUpdate = true;
      }

      if (doc.isNew()) {
        String content =
            getResourceDocumentContent(
                DOCUMENTCONTENT_TEMPLATE_PREFIX + getClassTemplateFullName() + DOCUMENTCONTENT_EXT);
        doc.setContent(content != null ? content : getClassTemplateDefaultContent());
        doc.setSyntax(Syntax.XWIKI_1_0);

        doc.setParent(getClassFullName());
      }

      needsUpdate |= updateClassTemplateDocument(doc);

      if (doc.isNew() || needsUpdate) {
        xwiki.saveDocument(doc, context);
      }
    } finally {
      this.checkingClassTemplate = false;
    }
  }
  @Test
  public void updateWikiPreferencesCache() throws XWikiException, ComponentLookupException {
    addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);

    Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));

    XWikiDocument otherWikiTranslation =
        new XWikiDocument(
            new DocumentReference(
                this.oldcore.getXWikiContext().getWikiId(), "XWiki", "OtherTranslations"));
    otherWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
    otherWikiTranslation.setContent("wiki.othertranslation=Other translation");
    this.oldcore
        .getMockXWiki()
        .saveDocument(otherWikiTranslation, "", this.oldcore.getXWikiContext());

    setBundles(" " + otherWikiTranslation.getFullName());

    Assert.assertEquals("Other translation", this.tool.get("wiki.othertranslation"));

    Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
  }
  /**
   * Check if class sheet document exists in this context and update. Create if not exists.
   *
   * @param context the XWiki context.
   * @throws XWikiException error when saving document.
   */
  protected void checkClassSheetDocument(XWikiContext context) throws XWikiException {
    if (this.checkingClassSheet) {
      return;
    }

    this.checkingClassSheet = true;

    try {
      XWikiDocument doc;
      XWiki xwiki = context.getWiki();
      boolean needsUpdate = false;

      try {
        doc = xwiki.getDocument(getClassSheetFullName(), context);
      } catch (Exception e) {
        doc = new XWikiDocument();
        doc.setSpace(getClassSheetSpace());
        doc.setName(getClassSheetName());
        needsUpdate = true;
      }

      if (doc.isNew()) {
        String documentContentPath =
            DOCUMENTCONTENT_SHEET_PREFIX + getClassSheetFullName() + DOCUMENTCONTENT_EXT;
        String content = getResourceDocumentContent(documentContentPath);
        doc.setContent(content != null ? content : getClassSheetDefaultContent());
        doc.setSyntax(Syntax.XWIKI_1_0);
        doc.setParent(getClassFullName());
      }

      if (doc.isNew() || needsUpdate) {
        xwiki.saveDocument(doc, context);
      }
    } finally {
      this.checkingClassSheet = false;
    }
  }