Example #1
0
 /**
  * Generate a relative path based on provided document for the directory where the document should
  * be stored.
  *
  * @param doc the document to export
  * @return the corresponding path
  */
 public String getDirectoryForDocument(XWikiDocument doc) {
   StringBuilder path = new StringBuilder();
   for (SpaceReference space : doc.getDocumentReference().getSpaceReferences()) {
     path.append(space.getName()).append('/');
   }
   return path.toString();
 }
  @Test
  public void getSpacePreference() throws Exception {
    this.mocker.registerMockComponent(ConfigurationSource.class, "wiki");
    ConfigurationSource spaceConfiguration =
        this.mocker.registerMockComponent(ConfigurationSource.class, "space");

    when(this.xwikiCfgConfigurationSource.getProperty(anyString(), anyString()))
        .then(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invocation) throws Throwable {
                return invocation.getArgumentAt(1, String.class);
              }
            });

    WikiReference wikiReference = new WikiReference("wiki");
    SpaceReference space1Reference = new SpaceReference("space1", wikiReference);
    SpaceReference space2Reference = new SpaceReference("space2", space1Reference);

    // Without preferences and current doc

    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals(
        "defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));

    // Without preferences but with current doc

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));

    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals(
        "defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));

    // With preferences

    final Map<String, Map<String, String>> spacesPreferences = new HashMap<>();
    Map<String, String> space1Preferences = new HashMap<>();
    space1Preferences.put("pref", "prefvalue1");
    space1Preferences.put("pref1", "pref1value1");
    Map<String, String> space2Preferences = new HashMap<>();
    space2Preferences.put("pref", "prefvalue2");
    space2Preferences.put("pref2", "pref2value2");
    spacesPreferences.put(space1Reference.getName(), space1Preferences);
    spacesPreferences.put(space2Reference.getName(), space2Preferences);

    when(spaceConfiguration.getProperty(anyString(), same(String.class)))
        .then(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invocation) throws Throwable {
                if (context.getDoc() != null) {
                  Map<String, String> spacePreferences =
                      spacesPreferences.get(
                          context.getDoc().getDocumentReference().getParent().getName());
                  if (spacePreferences != null) {
                    return spacePreferences.get(invocation.getArgumentAt(0, String.class));
                  }
                }

                return null;
              }
            });

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference)));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", this.context));

    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", this.context));

    assertEquals("", this.xwiki.getSpacePreference("nopref", space1Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("nopref", space1Reference, "defaultvalue", this.context));
    assertEquals(
        "prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, this.context));
    assertEquals(
        "prefvalue1",
        this.xwiki.getSpacePreference("pref", space1Reference, "defaultvalue", this.context));
    assertEquals(
        "pref1value1", this.xwiki.getSpacePreference("pref1", space1Reference, this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", space1Reference, this.context));

    assertEquals("", this.xwiki.getSpacePreference("nopref", space2Reference, this.context));
    assertEquals(
        "defaultvalue",
        this.xwiki.getSpacePreference("nopref", space2Reference, "defaultvalue", this.context));
    assertEquals(
        "prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals(
        "prefvalue2",
        this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
    assertEquals(
        "pref1value1", this.xwiki.getSpacePreference("pref1", space2Reference, this.context));
    assertEquals(
        "pref2value2", this.xwiki.getSpacePreference("pref2", space2Reference, this.context));
  }