-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add 2 test cases * Upgrade gradle to v8.10.2 * Upgrade javet to v4.0.0
- Loading branch information
Showing
5 changed files
with
301 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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,107 @@ | ||
/* | ||
* Copyright (c) 2024. caoccao.com Sam Cao | ||
* All rights reserved. | ||
* 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. | ||
*/ | ||
|
||
import * as fs from "https://deno.land/std/fs/mod.ts"; | ||
import * as path from "https://deno.land/std/path/mod.ts"; | ||
|
||
class ChangeVersion { | ||
private productionVersion: string; | ||
private rootDirPath: string; | ||
private snapshotVersion: string; | ||
|
||
constructor(productionVersion: string, snapshotVersion: string) { | ||
this.productionVersion = productionVersion; | ||
this.snapshotVersion = snapshotVersion; | ||
this.rootDirPath = path.join( | ||
path.dirname(path.fromFileUrl(import.meta.url)), | ||
"../../" | ||
); | ||
} | ||
|
||
private _change( | ||
filePath: string, | ||
patterns: Array<RegExp>, | ||
isSnapshot = true | ||
) { | ||
const sourceFilePath = path.join(this.rootDirPath, filePath); | ||
if (fs.existsSync(sourceFilePath)) { | ||
console.info(`Processing ${sourceFilePath}.`); | ||
} else { | ||
console.error(`%c${sourceFilePath} is not found.`, "color: red"); | ||
return; | ||
} | ||
const newVersion = isSnapshot | ||
? this.snapshotVersion | ||
: this.productionVersion; | ||
const positionGroups: Array<{ start: number; end: number }> = []; | ||
const currentContent = Deno.readTextFileSync(sourceFilePath); | ||
patterns.map((pattern) => { | ||
[...currentContent.matchAll(pattern)].map((match) => { | ||
const matchedString = match[0]; | ||
if (match.groups) { | ||
const currentVersion = match.groups["version"]; | ||
if (newVersion === currentVersion) { | ||
console.warn(`%c Ignored ${matchedString}`, "color: yellow"); | ||
} else { | ||
console.info(` ${matchedString} => ${newVersion}`); | ||
const start = match.index + matchedString.indexOf(currentVersion); | ||
const end = start + currentVersion.length; | ||
positionGroups.push({ start: start, end: end }); | ||
} | ||
} | ||
}); | ||
}); | ||
if (positionGroups.length > 0) { | ||
let newContent = ""; | ||
let lastEnd = 0; | ||
positionGroups.sort((pg1, pg2) => pg1.start - pg2.start); | ||
positionGroups.map((positionGroup) => { | ||
if (positionGroup.start > lastEnd) { | ||
newContent += currentContent.substring(lastEnd, positionGroup.start); | ||
} | ||
newContent += newVersion; | ||
lastEnd = positionGroup.end; | ||
}); | ||
if (lastEnd < currentContent.length) { | ||
newContent += currentContent.substring(lastEnd, currentContent.length); | ||
} | ||
if (newContent !== currentContent) { | ||
Deno.writeTextFileSync(sourceFilePath, newContent); | ||
} | ||
} | ||
} | ||
|
||
change() { | ||
this._change( | ||
"README.md", | ||
[ | ||
/<artifactId>javet-buddy<\/artifactId>\s*<version>(?<version>\d+\.\d+\.\d+)<\/version>/gi, | ||
/javet-buddy:(?<version>\d+\.\d+\.\d+)"/gim, | ||
], | ||
false | ||
); | ||
this._change("build.gradle.kts", [ | ||
/^\s*const val JAVET_BUDDY = "(?<version>\d+\.\d+\.\d+)"/gim, | ||
]); | ||
this._change(".github/workflows/build.yml", [ | ||
/^\s*JAVET_BUDDY_VERSION:\s*(?<version>\d+\.\d+\.\d+)/gim, | ||
]); | ||
} | ||
} | ||
|
||
const changeVersion = new ChangeVersion("0.2.0", "0.3.0"); | ||
changeVersion.change(); |
144 changes: 144 additions & 0 deletions
144
src/test/java/com/caoccao/javet/buddy/interop/proxy/TestJavetReflectionObjectFactory.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,144 @@ | ||
/* | ||
* Copyright (c) 2024. caoccao.com Sam Cao | ||
* | ||
* 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.caoccao.javet.buddy.interop.proxy; | ||
|
||
import com.caoccao.javet.annotations.V8Function; | ||
import com.caoccao.javet.exceptions.JavetException; | ||
import com.caoccao.javet.interfaces.IJavetAnonymous; | ||
import com.caoccao.javet.interop.V8Host; | ||
import com.caoccao.javet.interop.V8Runtime; | ||
import com.caoccao.javet.interop.converters.JavetProxyConverter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class TestJavetReflectionObjectFactory { | ||
@Test | ||
public void testDynamicClassAutoCloseable() throws JavetException { | ||
IJavetAnonymous anonymous = new IJavetAnonymous() { | ||
@V8Function | ||
public void test(DynamicClassAutoCloseable mockedDynamicClass) throws Exception { | ||
DynamicClassAutoCloseable regularDynamicClass = new DynamicClassAutoCloseable(); | ||
assertEquals(0, regularDynamicClass.add(1, 2)); | ||
assertEquals(3, mockedDynamicClass.add(1, 2), "Add should work."); | ||
((AutoCloseable) mockedDynamicClass).close(); | ||
} | ||
}; | ||
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) { | ||
JavetProxyConverter javetProxyConverter = new JavetProxyConverter(); | ||
javetProxyConverter.getConfig().setReflectionObjectFactory(JavetReflectionObjectFactory.getInstance()); | ||
v8Runtime.setConverter(javetProxyConverter); | ||
v8Runtime.getGlobalObject().set("a", anonymous); | ||
String codeString = "a.test({\n" + | ||
" add: (a, b) => a + b,\n" + | ||
"});"; | ||
v8Runtime.getExecutor(codeString).executeVoid(); | ||
v8Runtime.getGlobalObject().delete("a"); | ||
v8Runtime.lowMemoryNotification(); | ||
assertEquals(0, v8Runtime.getReferenceCount()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDynamicClassForceCloseable() throws JavetException { | ||
IJavetAnonymous anonymous = new IJavetAnonymous() { | ||
@V8Function | ||
public void test(DynamicClassForceCloseable mockedDynamicClass) throws Exception { | ||
DynamicClassForceCloseable regularDynamicClass = new DynamicClassForceCloseable(); | ||
assertEquals(0, regularDynamicClass.add(1, 2)); | ||
assertEquals(3, mockedDynamicClass.add(1, 2), "Add should work."); | ||
assertEquals("a", regularDynamicClass.getDescription()); | ||
assertEquals("b", mockedDynamicClass.getDescription()); | ||
assertEquals("a", regularDynamicClass.getName()); | ||
assertEquals("b", mockedDynamicClass.getName(), "String function without arguments should work."); | ||
assertEquals(1, regularDynamicClass.getNumber(2)); | ||
assertEquals(2, mockedDynamicClass.getNumber(2), "Int function with arguments should work."); | ||
assertEquals("a", regularDynamicClass.getTitle()); | ||
assertEquals("b", mockedDynamicClass.getTitle(), "Property as function should work."); | ||
assertEquals(0, regularDynamicClass.getValue()); | ||
assertEquals(1, mockedDynamicClass.getValue(), "Getter for value should work."); | ||
mockedDynamicClass.setValue(2); | ||
assertEquals(2, mockedDynamicClass.getValue(), "Setter for value should work."); | ||
assertFalse(regularDynamicClass.isPassed()); | ||
assertTrue(mockedDynamicClass.isPassed()); | ||
((AutoCloseable) mockedDynamicClass).close(); | ||
} | ||
}; | ||
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) { | ||
JavetProxyConverter javetProxyConverter = new JavetProxyConverter(); | ||
javetProxyConverter.getConfig().setReflectionObjectFactory(JavetReflectionObjectFactory.getInstance()); | ||
v8Runtime.setConverter(javetProxyConverter); | ||
v8Runtime.getGlobalObject().set("a", anonymous); | ||
String codeString = "a.test({\n" + | ||
" add: (a, b) => a + b,\n" + | ||
" description: 'b',\n" + | ||
" getName: () => 'b',\n" + | ||
" getNumber: (n) => n,\n" + | ||
" getTitle: 'b',\n" + | ||
" passed: true,\n" + | ||
" value: 1,\n" + | ||
"});"; | ||
v8Runtime.getExecutor(codeString).executeVoid(); | ||
v8Runtime.getGlobalObject().delete("a"); | ||
v8Runtime.lowMemoryNotification(); | ||
assertEquals(0, v8Runtime.getReferenceCount()); | ||
} | ||
} | ||
|
||
public static class DynamicClassAutoCloseable implements AutoCloseable { | ||
public int add(int a, int b) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
} | ||
} | ||
|
||
public static class DynamicClassForceCloseable { | ||
public int add(int a, int b) { | ||
return 0; | ||
} | ||
|
||
public String getDescription() { | ||
return "a"; | ||
} | ||
|
||
public String getName() { | ||
return "a"; | ||
} | ||
|
||
public int getNumber(int n) { | ||
return 1; | ||
} | ||
|
||
public String getTitle() { | ||
return "a"; | ||
} | ||
|
||
public int getValue() { | ||
return 0; | ||
} | ||
|
||
public boolean isPassed() { | ||
return false; | ||
} | ||
|
||
public void setValue(int value) { | ||
} | ||
} | ||
} |