-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
tests/src/test/java/org/mozilla/javascript/tests/ImportClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** */ | ||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ContextFactory; | ||
import org.mozilla.javascript.Script; | ||
import org.mozilla.javascript.drivers.TestUtils; | ||
import org.mozilla.javascript.tools.shell.Global; | ||
import org.mozilla.javascript.tools.shell.ShellContextFactory; | ||
|
||
/** | ||
* @author donnamalayeri | ||
*/ | ||
public class ImportClassTest { | ||
|
||
protected final Global global = new Global(); | ||
|
||
public ImportClassTest() { | ||
global.init(contextFactory); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
TestUtils.setGlobalContextFactory(contextFactory); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
TestUtils.setGlobalContextFactory(null); | ||
} | ||
|
||
private ContextFactory contextFactory = | ||
new ShellContextFactory() { | ||
@Override | ||
protected boolean hasFeature(Context cx, int featureIndex) { | ||
if (featureIndex == Context.FEATURE_ENHANCED_JAVA_ACCESS) return true; | ||
return super.hasFeature(cx, featureIndex); | ||
} | ||
}; | ||
|
||
@Test | ||
public void importPackageAndClass() { | ||
Object result = | ||
runScript( | ||
"importPackage(java.util);\n" | ||
+ "UUID.randomUUID();\n" // calls getPkgProperty("UUID", global, | ||
// false) | ||
+ "importClass(java.util.UUID);\n" | ||
+ "UUID.randomUUID();\n"); // calls getPkgProperty("UUID", | ||
// NativeJavaPackage, true) | ||
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID); | ||
} | ||
|
||
@Test | ||
public void importInSameContext() { | ||
Object result = runScript("importClass(java.util.UUID);UUID.randomUUID();"); | ||
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID); | ||
result = runScript("importClass(java.util.UUID);UUID.randomUUID();"); | ||
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID); | ||
} | ||
|
||
private Object runScript(final String scriptSourceText) { | ||
|
||
return contextFactory.call( | ||
context -> { | ||
Script script = context.compileString(scriptSourceText, "", 1, null); | ||
Object exec = script.exec(context, global); | ||
return exec; | ||
}); | ||
} | ||
} |