/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.components.impl;

import consulo.disposer.Disposable;
import com.intellij.openapi.components.RoamingType;
import com.intellij.openapi.components.StateStorage;
import com.intellij.openapi.components.StateStorageException;
import com.intellij.openapi.components.TrackingPathMacroSubstitutor;
import consulo.components.impl.stores.storage.StorageData;
import consulo.components.impl.stores.storage.XmlElementStorage;
import consulo.disposer.Disposer;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.testFramework.MockApplicationTestCase;
import org.jdom.Element;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import static com.intellij.openapi.util.JDOMBuilder.attr;
import static com.intellij.openapi.util.JDOMBuilder.tag;

/**
 * @author mike
 */
public  class XmlElementStorageTest extends MockApplicationTestCase {
  private Disposable myParentDisposable;

  @RequiredUIAccess
  @Override
  public void setUp() throws Exception {
    super.setUp();
    myParentDisposable = Disposable.newDisposable();
  }

  @Override
  public void tearDown() throws Exception {
    Disposer.dispose(myParentDisposable);
    super.tearDown();
  }

  public void testGetStateSucceeded() throws Exception {
    MyXmlElementStorage storage =
            new MyXmlElementStorage(tag("root", tag("component", attr("name", "test"), tag("foo"))));
    Element state = storage.getState(this, "test", Element.class);
    assertNotNull(state);
    assertEquals("component", state.getName());
    assertNotNull(state.getChild("foo"));
  }

  public void testGetStateNotSucceeded() throws Exception {
    MyXmlElementStorage storage = new MyXmlElementStorage(tag("root"));
    Element state = storage.getState(this, "test", Element.class);
    assertNull(state);
  }

  public void testSetStateOverridesOldState() throws Exception {
    MyXmlElementStorage storage =
            new MyXmlElementStorage(tag("root", tag("component", attr("name", "test"), tag("foo"))));
    Element newState = tag("component", attr("name", "test"), tag("bar"));
    StateStorage.ExternalizationSession externalizationSession = storage.startExternalization();
    externalizationSession.setState(this, "test", newState, null);
    externalizationSession.createSaveSession(false).save(force);
    assertNotNull(storage.mySavedElement);
    assertNotNull(storage.mySavedElement.getChild("component").getChild("bar"));
    assertNull(storage.mySavedElement.getChild("component").getChild("foo"));
  }


  private static class MyXmlElementStorage extends XmlElementStorage {
    private final Element myElement;
    private Element mySavedElement;

    public MyXmlElementStorage(Element element) throws StateStorageException {
      super("", RoamingType.PER_USER, new MyPathMacroManager(), "root", null);
      myElement = element;
    }

    @Override
    protected Element loadLocalData() {
      return myElement;
    }

    @Override
    protected XmlElementStorageSaveSession createSaveSession(@Nonnull StorageData storageData) {
      return new XmlElementStorageSaveSession(storageData) {
        @Override
        protected void doSave(@Nullable Element element) {
          mySavedElement = element == null ? null : element.clone();
        }
      };
    }
  }

  private static class MyPathMacroManager implements TrackingPathMacroSubstitutor {
    @Override
    public void expandPaths(@Nonnull final Element element) {
    }

    @Override
    public void reset() {
    }

    @Nonnull
    @Override
    public Set<String> getComponents(@Nonnull Collection<String> macros) {
      return Collections.emptySet();
    }

    @Override
    public void collapsePaths(@Nonnull final Element element) {
    }

    @Override
    public String expandPath(final String path) {
      throw new UnsupportedOperationException("Method expandPath not implemented in " + getClass());
    }

    @Override
    public String collapsePath(@Nullable String path) {
      throw new UnsupportedOperationException("Method collapsePath not implemented in " + getClass());
    }

    @Nonnull
    @Override
    public Set<String> getUnknownMacros(final String componentName) {
      return Collections.emptySet();
    }

    @Override
    public void invalidateUnknownMacros(@Nonnull Set<String> macros) {
    }

    @Override
    public void addUnknownMacros(@Nonnull String componentName, @Nonnull Collection<String> unknownMacros) {
    }
  }
}

@@ -2984,11 +2984,11 @@
 ve(f
-orc
+als
 e);%0A

fix compilation