/*
 * Copyright 2000-2013 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.psi.impl.meta;

import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.psi.PsiElement;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NonNls;

/**
* @author peter
*/
public class MetaRegistryTest extends LightPlatformTestCase {

  public void testChangingMetaData() throws Throwable {
    final boolean[] flag = {false};
    MetaRegistry.addMetadataBinding(new ElementFilter() {
      @Override
      public boolean isAcceptable(Object element, PsiElement context) {
        return flag[0];
      }

      @Override
      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    }, MyTrueMetaData.class, myTestRootDisposable);
    MetaRegistry.addMetadataBinding(new ElementFilter() {
      @Override
      public boolean isAcceptable(Object element, PsiElement context) {
        return !flag[0];
      }

      @Override
      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    }, MyFalseMetaData.class, myTestRootDisposable);

    final XmlTag tag = ((XmlFile)LightPlatformTestCase.createFile("a.xml", "<a/>")).getDocument().getRootTag();
    UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyFalseMetaData.class);
    flag[0] = true;
    new WriteCommandAction(LightPlatformTestCase.getProject()) {
      @Override
      protected void run(Result result) throws Throwable {
        tag.setName("b");
      }
    }.execute();
    UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyTrueMetaData.class);
  }

  public static class MyAbstractMetaData implements PsiMetaData {
    private PsiElement myDeclaration;

    @Override
    public PsiElement getDeclaration() {
      return myDeclaration;
    }

    @Override
    public Object[] getDependences() {
      return new Object[]{myDeclaration};
    }

    @Override
    @NonNls
    public String getName() {
      return null;
    }

    @Override
    @NonNls
    public String getName(PsiElement context) {
      return null;
    }

    @Override
    public void init(PsiElement element) {
      myDeclaration = element;
    }

  }

  public static class MyTrueMetaData extends MyAbstractMetaData {}
  public static class MyFalseMetaData extends MyAbstractMetaData {}

}

@@ -21,9 +21,9 @@
 -201
-3
+5
  Jet
@@ -987,24 +987,76 @@
 rmTestCase;%0A
+import com.intellij.testFramework.PlatformTestCase;%0A
 import com.i
@@ -1219,16 +1219,99 @@
 stCase %7B
+%0A  public MetaRegistryTest() %7B%0A    PlatformTestCase.autodetectPlatformPrefix();%0A  %7D
 %0A%0A  publ

enable running MetaRegistryTest in its own classpath