Skip to content

Commit

Permalink
jpype...
Browse files Browse the repository at this point in the history
  • Loading branch information
ioggstream committed Jun 13, 2024
1 parent 4f52c96 commit e594a9d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions java-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
34 changes: 34 additions & 0 deletions java-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jpype1
cffi
Binary file added tests/guava-28.2-android.jar
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/jpype_script_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import jpype

def test_noop():

jpype.startJVM(jpype.getDefaultJVMPath())

Random = jpype.JClass('java.util.Random')
String = jpype.JClass('java.lang.String')
System = jpype.JClass('java.lang.System')
# Create an instance of java.util.Random
random = Random()
for _ in range(20):
System.out.println(String.format("Random number: %d", random.nextInt()))

# Shutdown the JVM
jpype.shutdownJVM()
50 changes: 50 additions & 0 deletions tests/test_java.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import pytest
import jpype
from jpype import startJVM, shutdownJVM


@pytest.fixture(scope="session", autouse=False)
def setupJVM():
"""
Setup a JVM configured to use the Europe/Rome timezone.
"""
os.environ["TZ"] = "Europe/Rome"
startJVM(
convertStrings=False,
classpath=["java-project/target/guava-28.2-android.jar"],
)
yield
shutdownJVM()


def test_guava_ascii(setupJVM):
Ascii = jpype.JClass("com.google.common.base.Ascii")
assert Ascii.toLowerCase("EuroPython") == "europython"


@pytest.mark.parametrize("inet_addr,is_valid", [
("1.2.3.4", True),
("3ffe::1", True),
("3ffe::1z", False),
("333.1.1.1", False),
])
def test_InetAddress(setupJVM, inet_addr, is_valid):
InetAddress = jpype.JClass("com.google.common.net.InetAddresses")
assert InetAddress.isInetAddress(inet_addr) == is_valid


def test_automatic_imports(setupJVM):
java = jpype.JPackage("java")
from java.lang import String

assert String.format("Hello, %s", "World") == "Hello, World"


def test_bigdecimal(setupJVM):
BigDecimal = jpype.JClass("java.math.BigDecimal")
a, b = 0.1, 0.2
b_a = BigDecimal(a)
b_b = BigDecimal(b)
assert b_a.add(b_b) == BigDecimal(a+b)
raise ValueError("This test is expected to fail")
2 changes: 0 additions & 2 deletions tests/test_noop.py

This file was deleted.

2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py3, safety
envlist = py3

# By default, we do not publish a module.
skipsdist=True
Expand Down

0 comments on commit e594a9d

Please sign in to comment.