/*
 * Copyright 2009 Google Inc.
 *
 * 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.google.common.css.compiler.passes;

import static org.easymock.EasyMock.expect;

import com.google.common.css.SubstitutionMap;
import com.google.common.css.compiler.ast.BackDoorNodeMutation;
import com.google.common.css.compiler.ast.CssBlockNode;
import com.google.common.css.compiler.ast.CssClassSelectorNode;
import com.google.common.css.compiler.ast.CssIdSelectorNode;
import com.google.common.css.compiler.ast.CssRefinerListNode;
import com.google.common.css.compiler.ast.CssRootNode;
import com.google.common.css.compiler.ast.CssRulesetNode;
import com.google.common.css.compiler.ast.CssSelectorNode;
import com.google.common.css.compiler.ast.CssTree;
import com.google.common.css.compiler.ast.MutatingVisitController;
import com.google.common.css.compiler.passes.testing.AstPrinter;

import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;

/**
 * Unit tests the {@link CssClassRenaming} compiler pass.
 * 
 * @author oana@google.com (Oana Florescu)
 */
public class CssClassRenamingTest extends TestCase {

  public void testRunPass() {
    IMocksControl controller = EasyMock.createStrictControl();
    MutatingVisitController visitController = controller.createMock(
        MutatingVisitController.class);
    
    CssClassRenaming pass = 
        new CssClassRenaming(visitController, null, null);
    visitController.startVisit(pass);
    controller.replay();

    pass.runPass();
    controller.verify();
  }

  public void testNoSubstitutionWithNullMap() {
    CssClassSelectorNode node = new CssClassSelectorNode("FOO", null);
    CssClassRenaming pass = new CssClassRenaming(null, null, null);
    pass.enterClassSelector(node);
  }

  public void testNoClassSubstitutionWhenClassNotFoundInMap() {
    CssClassSelectorNode refinerNode = new CssClassSelectorNode("FOO", null);
    IMocksControl controller = EasyMock.createStrictControl();
    SubstitutionMap cssClassRenamingMap = controller.createMock(
        SubstitutionMap.class);
    expect(cssClassRenamingMap.get("FOO")).andReturn(null).anyTimes();
    controller.replay();
    
    CssClassRenaming pass
        = new CssClassRenaming(null, cssClassRenamingMap, null);
    pass.enterClassSelector(refinerNode);
  }

  public void testNoIdSubstitutionWhenIdNotFoundInMap() {
    CssIdSelectorNode refinerNode = new CssIdSelectorNode("ID", null);
    IMocksControl controller = EasyMock.createStrictControl();
    SubstitutionMap idRenamingMap = controller.createMock(
        SubstitutionMap.class);
    expect(idRenamingMap.get("ID")).andReturn(null).anyTimes();
    controller.replay();
    CssClassRenaming pass = new CssClassRenaming(null, null, idRenamingMap);
    pass.enterIdSelector(refinerNode);
  }
  
  public void testEnterClassRefiner() {
    CssClassSelectorNode refinerNode = new CssClassSelectorNode("CSS_FOO",
        null);
    CssRefinerListNode refiners = new CssRefinerListNode();
    BackDoorNodeMutation.addChildToBack(refiners, refinerNode);

    CssRulesetNode ruleset = new CssRulesetNode();
    CssSelectorNode sel = new CssSelectorNode("", null);
    sel.setRefiners(refiners);
    ruleset.addSelector(sel);
    
    CssBlockNode body = new CssBlockNode(false);
    BackDoorNodeMutation.addChildToBack(body, ruleset);
    
    CssRootNode root = new CssRootNode(body);
    CssTree tree = new CssTree(null, root);
    
    SubstitutionMap classMap = new SubstitutionMap() {
        /** {@inheritDoc} */
        @Override
        public String get(String key) {
          return key.startsWith("CSS_") ? key + '_' : key;
        }
    };
    CssClassRenaming pass = new CssClassRenaming(
        tree.getMutatingVisitController(), classMap, null);
    pass.runPass();
    assertEquals("[[.CSS_FOO_]{[]}]", AstPrinter.print(tree));
  }

  public void testEnterIdRefiner() {
    CssIdSelectorNode refinerNode = new CssIdSelectorNode("ID_FOO", null);
    CssRefinerListNode refiners = new CssRefinerListNode();
    BackDoorNodeMutation.addChildToBack(refiners, refinerNode);

    CssRulesetNode ruleset = new CssRulesetNode();
    CssSelectorNode sel = new CssSelectorNode("", null);
    sel.setRefiners(refiners);
    ruleset.addSelector(sel);
    
    CssBlockNode body = new CssBlockNode(false);
    BackDoorNodeMutation.addChildToBack(body, ruleset);
    
    CssRootNode root = new CssRootNode(body);
    CssTree tree = new CssTree(null, root);
    
    SubstitutionMap idMap = new SubstitutionMap() {
        /** {@inheritDoc} */
        @Override
        public String get(String key) {
          return key.startsWith("ID_") ? key + '_' : key;
        }
    };
    CssClassRenaming pass
        = new CssClassRenaming(tree.getMutatingVisitController(), null, idMap);
    pass.runPass();
    assertEquals("[[#ID_FOO_]{[]}]", AstPrinter.print(tree));
  }
}

@@ -659,32 +659,70 @@
 org.
-easymock.EasyMock.expect
+mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.verify
 ;%0A%0Ai
@@ -1428,17 +1428,16 @@
 rinter;%0A
-%0A
 import j
@@ -1466,74 +1466,8 @@
 e;%0A%0A
-import org.easymock.EasyMock;%0Aimport org.easymock.IMocksControl;%0A%0A
 /**%0A
@@ -1663,71 +1663,8 @@
 ) %7B%0A
-    IMocksControl controller = EasyMock.createStrictControl();%0A
     
@@ -1709,39 +1709,13 @@
 r = 
-controller.createMock(%0A        
+mock(
 Muta
@@ -1742,20 +1742,16 @@
 class);%0A
-    
 %0A    Css
@@ -1771,25 +1771,16 @@
  pass = 
-%0A        
 new CssC
@@ -1868,71 +1868,20 @@
     
-controller.replay();%0A%0A    pass.runPass();%0A    controller.verify
+pass.runPass
 ();%0A
@@ -2262,264 +2262,75 @@
     
-IMocksControl controller = EasyMock.createStrictControl();%0A    SubstitutionMap cssClassRenamingMap = controller.createMock(%0A        SubstitutionMap.class);%0A    expect(cssClassRenamingMap.get(%22FOO%22)).andReturn(null).anyTimes();%0A    controller.replay();%0A    
+SubstitutionMap cssClassRenamingMap = mock(SubstitutionMap.class);%0A
 %0A   
@@ -2454,24 +2454,69 @@
 efinerNode);
+%0A%0A    verify(cssClassRenamingMap).get(%22FOO%22);
 %0A  %7D%0A%0A  publ
@@ -2646,246 +2646,69 @@
     
-IMocksControl controller = EasyMock.createStrictControl();%0A    SubstitutionMap idRenamingMap = controller.createMock(%0A        SubstitutionMap.class);%0A    expect(idRenamingMap.get(%22ID%22)).andReturn(null).anyTimes();%0A    controller.replay();
+SubstitutionMap idRenamingMap = mock(SubstitutionMap.class);%0A
 %0A   
@@ -2815,24 +2815,62 @@
 efinerNode);
+%0A%0A    verify(idRenamingMap).get(%22ID%22);
 %0A  %7D%0A  %0A  pu

Migrate CssClassRenamingTest from EasyMock to Mockito.