// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.lang.properties;

import com.intellij.lang.properties.structureView.GroupByWordPrefixes;
import com.intellij.lang.properties.structureView.PropertiesFileStructureViewModel;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import com.intellij.util.ui.tree.TreeUtil;

public class PropertiesFileTreeStructureTest extends LightPlatformCodeInsightFixtureTestCase {
  public void testGrouping() {
    doTest("a.b.c=dddd\n" +
           "a.b.d=dddd\n" +
           "a.x=i\n" +
           "b.n.p=ooo",

           "Test.properties\n" +
           " a\n" +
           "  b\n" +
           "   c\n" +
           "   d\n" +
           "  x\n" +
           " b.n.p\n");
  }

  public void testNesting() {
    doTest("a=dddd\n" +
           "a.b=dddd\n" +
           "a.b.c=i\n" +
           "a.b.c.d=ooo",

           "Test.properties\n" +
           " a\n" +
           "  <property>\n" +
           "  b\n" +
           "   <property>\n" +
           "   c\n" +
           "    <property>\n" +
           "    d\n");
  }

  public void testGroupSort() {
    doTest("log4j.category.x=dd\n" +
           "log4j.category.xdo=dd\n" +
           "log4j.category.middlegen.swing.ss=dd\n" +
           "log4j.category.middlegen.plugins.ss=dd\n" +
           "log4j.appender.middlegen.plugins.ss=dd",

           "Test.properties\n" +
           " log4j\n" +
           "  appender.middlegen.plugins.ss\n" +
           "  category\n" +
           "   middlegen\n" +
           "    plugins.ss\n" +
           "    swing.ss\n" +
           "   x\n" +
           "   xdo\n");
  }

  public void testFunkyGroups() {
    doTest("errors.byte={0} must be a BYTE type \n" +
           "errors.short={0} must be a SHORT type \n" +
           "error.date={0} must be a DATE types \n" +
           "error.range={0} is not between {1} and {2} \n" +
           "errors.email={0} is not a valid Email Address \n" +
           "errors.ipaddress={0} is not a valid IP address",

           "Test.properties\n" +
           " error\n" +
           "  date\n" +
           "  range\n" +
           " errors\n" +
           "  byte\n" +
           "  email\n" +
           "  ipaddress\n" +
           "  short\n");
  }

  private void doTest(String classText, String expected) {
    myFixture.configureByText("Test.properties", classText);
    myFixture.testStructureView(svc -> {
      svc.setActionActive(GroupByWordPrefixes.ID, true);
      svc.setActionActive(PropertiesFileStructureViewModel.KIND_SORTER_ID, true);
      TreeUtil.expandAll(svc.getTree(), () -> PlatformTestUtil.assertTreeStructureEquals(svc.getTree().getModel(), expected));
    });
  }
}

@@ -173,16 +173,72 @@
 rties;%0A%0A
+import com.intellij.ide.util.treeView.smartTree.Sorter;%0A
 import c
@@ -524,49 +524,29 @@
 se;%0A
+%0A
 import 
-com.intellij.util.ui.tree.TreeUtil
+javax.swing.*
 ;%0A%0Ap
@@ -778,32 +778,33 @@
 %22,%0A%0A           %22
+-
 Test.properties%5C
@@ -813,32 +813,33 @@
  +%0A           %22 
+-
 a%5Cn%22 +%0A         
@@ -835,32 +835,33 @@
 +%0A           %22  
+-
 b%5Cn%22 +%0A         
@@ -934,18 +934,16 @@
  %22 b.n.p
-%5Cn
 %22);%0A  %7D%0A
@@ -1079,32 +1079,33 @@
 %22,%0A%0A           %22
+-
 Test.properties%5C
@@ -1122,16 +1122,17 @@
       %22 
+-
 a%5Cn%22 +%0A 
@@ -1174,16 +1174,17 @@
      %22  
+-
 b%5Cn%22 +%0A 
@@ -1220,32 +1220,33 @@
 %0A           %22   
+-
 c%5Cn%22 +%0A         
@@ -1285,18 +1285,16 @@
   %22    d
-%5Cn
 %22);%0A  %7D%0A
@@ -1570,32 +1570,33 @@
 %22,%0A%0A           %22
+-
 Test.properties%5C
@@ -1613,16 +1613,17 @@
       %22 
+-
 log4j%5Cn%22
@@ -1688,16 +1688,17 @@
      %22  
+-
 category
@@ -1718,16 +1718,17 @@
     %22   
+-
 middlege
@@ -1836,18 +1836,16 @@
  %22   xdo
-%5Cn
 %22);%0A  %7D%0A
@@ -2242,16 +2242,17 @@
        %22
+-
 Test.pro
@@ -2273,24 +2273,25 @@
           %22 
+-
 error%5Cn%22 +%0A 
@@ -2351,16 +2351,17 @@
       %22 
+-
 errors%5Cn
@@ -2461,18 +2461,16 @@
 %22  short
-%5Cn
 %22);%0A  %7D%0A
@@ -2631,16 +2631,73 @@
 vc -%3E %7B%0A
+      svc.setActionActive(Sorter.ALPHA_SORTER_ID, true);%0A
       sv
@@ -2837,115 +2837,119 @@
     
+J
 Tree
-Util.expandAll(svc.getTree(), () -%3E PlatformTestUtil.assertTreeStructureEquals(svc.getTree().getModel()
+ tree = svc.getTree();%0A      PlatformTestUtil.expandAll(tree);%0A      PlatformTestUtil.assertTreeEqual(tree
 , ex
@@ -2955,17 +2955,16 @@
 xpected)
-)
 ;%0A    %7D)

fix PropertiesFileTreeStructureTest