/*
 * Copyright 2000-2016 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 org.jetbrains.idea.maven.project;

import org.jetbrains.idea.maven.MavenTestCase;
import org.jetbrains.idea.maven.server.MavenEmbedderWrapper;

public class MavenEmbeddersManagerTest extends MavenTestCase {
  private MavenEmbeddersManager myManager;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    myManager = new MavenEmbeddersManager(myProject);
  }

  @Override
  protected void tearDown() throws Exception {
    try {
      myManager.releaseForcefullyInTests();
    }
    catch (Throwable e) {
      addSuppressedException(e);
    }
    finally {
      super.tearDown();
    }
  }

  public void testBasics() {
    MavenEmbedderWrapper one = myManager.getEmbedder(MavenEmbeddersManager.FOR_FOLDERS_RESOLVE, null, null);
    MavenEmbedderWrapper two = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);

    assertNotSame(one, two);
  }

  public void testForSameId() {
    MavenEmbedderWrapper one1 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one2 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);

    assertNotSame(one1, one2);

    myManager.release(one1);

    MavenEmbedderWrapper one3 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);

    assertSame(one1, one3);
  }

  public void testCachingOnlyOne() {
    MavenEmbedderWrapper one1 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one2 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);

    assertNotSame(one1, one2);

    myManager.release(one1);
    myManager.release(one2);

    MavenEmbedderWrapper one11 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one22 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);

    assertSame(one1, one11);
    assertNotSame(one2, one22);
  }

  public void testResettingAllCachedAndInUse() {
    MavenEmbedderWrapper one1 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one2 = myManager.getEmbedder(MavenEmbeddersManager.FOR_FOLDERS_RESOLVE, null, null);

    myManager.release(one1);
    myManager.reset();

    myManager.release(one2);

    MavenEmbedderWrapper one11 = myManager.getEmbedder(MavenEmbeddersManager.FOR_DEPENDENCIES_RESOLVE, null, null);
    MavenEmbedderWrapper one22 = myManager.getEmbedder(MavenEmbeddersManager.FOR_FOLDERS_RESOLVE, null, null);

    assertNotSame(one1, one11);
    assertNotSame(one2, one22);
  }
}

@@ -1343,36 +1343,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A    MavenEmbe
@@ -1468,36 +1468,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    assertNo
@@ -1661,36 +1661,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A    MavenEmbe
@@ -1787,36 +1787,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    assertNo
@@ -1976,36 +1976,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    assertSa
@@ -2173,36 +2173,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A    MavenEmbe
@@ -2299,36 +2299,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    assertNo
@@ -2518,36 +2518,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A    MavenEmbe
@@ -2645,36 +2645,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    assertSa
@@ -2887,36 +2887,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A    MavenEmbe
@@ -3008,36 +3008,47 @@
 _RESOLVE, null, 
-null
+myDir.getPath()
 );%0A%0A    myManage
@@ -3227,20 +3227,31 @@
 , null, 
-null
+myDir.getPath()
 );%0A    M
@@ -3349,20 +3349,31 @@
 , null, 
-null
+myDir.getPath()
 );%0A%0A    

fix MavenEmbeddersManagerTest