All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.remote.html5.AddDatabaseStorage Maven / Gradle / Ivy

The newest version!
/*
Copyright 2007-2010 WebDriver committers
Copyright 2007-2010 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 org.openqa.selenium.remote.html5;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.html5.DatabaseStorage;
import org.openqa.selenium.html5.ResultSet;
import org.openqa.selenium.html5.ResultSetRows;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.InterfaceImplementation;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class AddDatabaseStorage implements AugmenterProvider {

  public Class getDescribedInterface() {
    return DatabaseStorage.class;
  }

  public InterfaceImplementation getImplementation(Object value) {
    return new InterfaceImplementation() {
      public Object invoke(ExecuteMethod executeMethod, Object self, Method method, Object... args) {
        String databaseName = (String) args[0];
        String query = (String) args[1];
        Object[] arguments = (Object[]) args[2];
        
        query.replaceAll("\"", "\\\"");
        Iterable convertedArgs = Iterables.transform(
            Lists.newArrayList(arguments), new WebElementToJsonConverter());
        
        Map params = ImmutableMap.of(
            "dbName", databaseName,
            "query", query,
            "args", Lists.newArrayList(convertedArgs));
        
        Map resultAsMap =
            (Map) executeMethod.execute(DriverCommand.EXECUTE_SQL, params);
        ResultSet rs = new ResultSet(((Long) resultAsMap.get("insertId")).intValue(),
            ((Long) resultAsMap.get("rowsAffected")).intValue(),
             new ResultSetRows((List>) resultAsMap.get("rows")));
        return rs;
      }
    };
  }
}