我有一个示例ASP.NET MVC 3 Web应用程序,遵循Jonathan McCracken的Test-Drive Asp.NET MVC(顺便说一句好书),我偶然发现了一个问题.请注意,我使用的是MVCContrib,Rhino和NUnit.
[Test]
public void ShouldSetLoggedInUserToViewBag() {
var todoController = new TodoController();
var builder = new TestControllerBuilder();
builder.InitializeController(todoController);
builder.HttpContext.User = new GenericPrincipal(new GenericIdentity("John Doe"), null);
Assert.That(todoController.Index().AssertViewRendered().ViewData["UserName"], Is.EqualTo("John Doe"));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码总是抛出这个错误:
System.AccessViolationException:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
控制器操作代码如下:
[HttpGet]
public ActionResult Index() {
ViewData.Model = Todo.ThingsToBeDone;
ViewBag.UserName = HttpContext.User.Identity.Name;
return View();
}
Run Code Online (Sandbox Code Playgroud)
根据我的想法,应用程序似乎崩溃,因为控制器操作中的两个分配.但是,我看不出有多么错误!?
任何人都可以帮我找出解决这个问题的方法.
谢谢.
编辑1
我做了一些实验来看看问题是什么.删除ViewData,Model分配时,问题会超越Expected result to be of type ViewResult. It is actually of type ViewResult..该ViewData分配是如此基本,我不认为是这样,我认为这个问题有什么问题或者犀牛或MVCcontrib结合MVC 3.
我之前为同一控制器操作编写了以下测试:
[Test]
public void ShouldDisplayAListOfTodoItems() …Run Code Online (Sandbox Code Playgroud) 当我尝试在扩展ScriptableObject的 java-script文件中创建类的对象时......将出现以下错误.
js: uncaught JavaScript runtime exception: TypeError: Cannot find default value for object.”
Run Code Online (Sandbox Code Playgroud)
类文件是
package sumit2;
import org.mozilla.javascript.ScriptableObject;
public class Sumit extends ScriptableObject {
public String getClassName(){
return "Sumit";
}
public void foo() {
System.out.println("Sumit!!!!!!!");
}
}
Run Code Online (Sandbox Code Playgroud)
Java脚本文件是: -
importPackage(Packages.sumit2);
var vv=new Sumit();
print(vv.foo());
Run Code Online (Sandbox Code Playgroud) 我用firefoxdriver和firefox 21对htmlunit进行了对硒
的性能测试.性能测试是在我的windows7机器上通过Eclipse进行的.
当两者都禁用javascript时,性能是相同的.
当两个都打开javascript htmlunit 2.12 比firefox慢150%.
我想这是因为spidermonkey引擎对犀牛的优越性.
有没有办法配置rhino会更快?
有没有更好的方式我们可以加速htmlunit?
package utils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.util.Date;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class PerformanceTest {
public static void main(String[] args) {
String[] urls = new String[] {
...
};
Date beforeSelenium = new Date();
System.out.println("Going to run selenium");
testSelenium(urls);
Date afterSelenium = new Date();
Date beforehtmlUnit= new Date();
System.out.println("Going to run htmlunit");
testHtmlUnit(urls);
Date …Run Code Online (Sandbox Code Playgroud) 我希望能够根据某些条件更改javascript文件中的变量和函数名称,然后输出修改后的javascript.这类似于javascript minifier的功能.这是一个例子:我有这个javascript代码
var something=5;
something++;
function doSomething(n){
alert(n);
}
doSomething(something):
Run Code Online (Sandbox Code Playgroud)
现在我想更换something,并doSomething用somethingElse和doSomethingElse,并返回下面的JavaScript代码:
var somethingElse=5;
somethingElse++;
function doSomethingElse(n){
alert(n);
}
doSomethingElse(somethingElse):
Run Code Online (Sandbox Code Playgroud)
我认为这可以在rhino或google closure编译(基于rhino)中完成,但不知道如何.我也对其他建议持开放态度.
谢谢
在集成Proguard之后,我面临着Rhino API的问题.
没有proguard,下面的代码工作正常.
org.mozilla.javascript.Context context = org.mozilla.javascript.Context.enter();
context.setOptimizationLevel(-1);
ScriptableObject scope = context.initStandardObjects();
try {
InputStream inputStream = mContext.getAssets().open(JAVASCRIPT_FILE);
InputStreamReader reader = new InputStreamReader(inputStream);
context.evaluateReader(scope, reader, JAVASCRIPT_FILE, 1, null);
} catch (IOException exception) {
throw new CustomException(null, exception);
}
Function functionAdd = (Function) scope.get(JAVASCRIPT_FUNCTION_NAME);
Object returnObject = functionAdd.call(context, scope, scope, new Object[] {
parameter1, parameter2, parameter3
});
Run Code Online (Sandbox Code Playgroud)
但在整合Proguard后,我得到以下错误:
02-26 14:58:13.200: E/AndroidRuntime(11607): Caused by: java.lang.IllegalStateException: Failed to create VMBridge instance
02-26 14:58:13.200: E/AndroidRuntime(11607): at org.a.b.ds.<clinit>(Unknown Source)
02-26 14:58:13.200: E/AndroidRuntime(11607): ... 11 more
Run Code Online (Sandbox Code Playgroud) 据我了解,Salesforce CommerceCloud 使用Rhino JavaScript,但遗憾的是我找不到它是什么版本,这对于理解兼容性很重要。是否有任何文档描述了这一点,和/或有没有办法输出 JavaScript 引擎版本?
Rhino是一个JavaScript在JVM之上运行的解释器.我想这对于服务器端编程很有用JavaScript.现在我们有Node.js.所以我想知道它Rhino是否仍然具有相关性以及它是否有用.
我用以下简单的程序简要测试了这三个引擎(rhino/spidermonkey/v8):
function p(n) {
for (var i = 2;i * i <= n;i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
var sum = 0;
for (var k = 2;k < 10000000;k++) {
if (p(k)) {
sum++;
}
}
print(sum);
Run Code Online (Sandbox Code Playgroud)
并获得以下结果:
$ time rhino -O 9 sample.js
664579
real 0m40.495s
user 0m40.793s
sys 0m0.180s
$ time js sample.js
664579
real 0m9.465s
user 0m9.477s
sys 0m0.000s
$ time d8 sample.js
664579
real 0m8.941s
user …Run Code Online (Sandbox Code Playgroud) 示例程序
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
System.out.println(jsEngine.eval("a>10 || b<10 || c=10"));
Run Code Online (Sandbox Code Playgroud)
在我的程序中,我使用上面的代码来评估 1000 个使用 javascript 函数的表达式,例如 indexof()、replace、date 函数等等。这种表达式曾经在 rhino 中进行评估(当平台 java 版本是 java 7 时)比 nashorn 快得多……我应该说至少快 15~20 倍……
在 Java 8 中使用 rhino 似乎是一项乏味的工作......有人可以提出解决方法以使其更快......
rhino ×9
javascript ×6
java ×4
android ×1
c#-4.0 ×1
demandware ×1
htmlunit ×1
nashorn ×1
nunit ×1
performance ×1
salesforce ×1
spidermonkey ×1
v8 ×1