/*
 * (C) Copyright 2016 Boni Garcia (http://bonigarcia.github.io/)
 *
 * 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 io.github.bonigarcia.wdm.test;

import static io.github.bonigarcia.wdm.Architecture.X32;
import static io.github.bonigarcia.wdm.Architecture.X64;
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
import static io.github.bonigarcia.wdm.DriverManagerType.FIREFOX;
import static io.github.bonigarcia.wdm.DriverManagerType.OPERA;
import static io.github.bonigarcia.wdm.DriverManagerType.PHANTOMJS;
import static java.util.Arrays.asList;
import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Optional;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import io.github.bonigarcia.wdm.Architecture;
import io.github.bonigarcia.wdm.Downloader;
import io.github.bonigarcia.wdm.DriverManagerType;
import io.github.bonigarcia.wdm.WebDriverManager;

/**
 * Test for driver cache.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.4.5
 */
@RunWith(Parameterized.class)
public class CacheTest {

    @Parameter(0)
    public DriverManagerType driverManagerType;

    @Parameter(1)
    public String driverVersion;

    @Parameter(2)
    public Architecture architecture;

    @Parameters(name = "{index}: {0} {1} {2}")
    public static Collection<Object[]> data() {
        return asList(
                new Object[][] { { CHROME, "2.27", IS_OS_MAC ? X64 : X32 },
                        { OPERA, "0.2.2", X64 }, { PHANTOMJS, "2.1.1", X64 },
                        { FIREFOX, "0.17.0", X64 } });
    }

    @Before
    @After
    public void cleanCache() throws IOException {
        cleanDirectory(
                new File(new Downloader(driverManagerType).getTargetPath()));
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testCache() throws Exception {
        WebDriverManager browserManager = WebDriverManager
                .getInstance(driverManagerType);
        browserManager.forceCache().forceDownload().architecture(architecture)
                .version(driverVersion).setup();

        Method method = WebDriverManager.class.getDeclaredMethod(
                "getDriverFromCache", String.class, Architecture.class,
                String.class);
        method.setAccessible(true);

        Optional<String> driverInCachePath = (Optional<String>) method
                .invoke(browserManager, driverVersion, architecture, "");

        assertThat(driverInCachePath.get(), notNullValue());
    }

}

@@ -725,82 +725,32 @@
 wdm.
-Architecture.X32;%0D%0Aimport static io.github.bonigarcia.wdm.Architecture.X64
+DriverManagerType.CHROME
 ;%0D%0Ai
@@ -797,37 +797,35 @@
 iverManagerType.
-CHROM
+EDG
 E;%0D%0Aimport stati
@@ -948,77 +948,8 @@
 A;%0D%0A
-import static io.github.bonigarcia.wdm.DriverManagerType.PHANTOMJS;%0D%0A
 impo
@@ -1051,71 +1051,8 @@
 y;%0D%0A
-import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;%0D%0A
 impo
@@ -1552,55 +1552,8 @@
 %0D%0A%0D%0A
-import io.github.bonigarcia.wdm.Architecture;%0D%0A
 impo
@@ -1988,68 +1988,8 @@
 %0D%0A%0D%0A
-    @Parameter(2)%0D%0A    public Architecture architecture;%0D%0A%0D%0A
     
@@ -2028,12 +2028,8 @@
  %7B1%7D
- %7B2%7D
 %22)%0D%0A
@@ -2099,34 +2099,16 @@
  asList(
-%0D%0A                
 new Obje
@@ -2131,36 +2131,21 @@
 E, %22
-2.27%22, IS_OS_MAC ? X64 : X32
+81.0.4044.69%22
  %7D,%0D
@@ -2161,24 +2161,16 @@
         
-        
 %7B OPERA,
@@ -2175,61 +2175,51 @@
 A, %22
-0.2.2%22, X64 %7D, %7B PHANTOMJS, %222.1.1%22, X64 %7D,%0D%0A        
+81.0.4044.113%22 %7D, %7B EDGE, %2281.0.409.0%22 %7D,%0D%0A
     
@@ -2248,18 +2248,13 @@
  %220.
-17
+26
 .0%22
-, X64
  %7D %7D
@@ -2716,53 +2716,8 @@
 d().
-architecture(architecture)%0D%0A                .
 vers
@@ -2734,16 +2734,34 @@
 Version)
+%0D%0A                
 .setup()
@@ -2811,16 +2811,34 @@
 er.class
+%0D%0A                
 .getDecl
@@ -2848,34 +2848,16 @@
 dMethod(
-%0D%0A                
 %22getDriv
@@ -2873,59 +2873,8 @@
 he%22,
- String.class, Architecture.class,%0D%0A               
  Str
@@ -3054,26 +3054,8 @@
 sion
-, architecture, %22%22
 );%0D%0A

Fix cache test