Skip to content

Commit 27807b3

Browse files

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

libraries-bytecode/pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@
6262
<scope>system</scope>
6363
<systemPath>${project.basedir}/src/main/resources/dinosaur-2.jar</systemPath>
6464
</dependency>-->
65+
<dependency>
66+
<groupId>org.teavm</groupId>
67+
<artifactId>teavm-core</artifactId>
68+
<version>${teavm.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.teavm</groupId>
72+
<artifactId>teavm-classlib</artifactId>
73+
<version>${teavm.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.teavm</groupId>
77+
<artifactId>teavm-tooling</artifactId>
78+
<version>${teavm.version}</version>
79+
</dependency>
6580
</dependencies>
6681

6782
<build>
@@ -96,6 +111,7 @@
96111
<properties>
97112
<javaassist.version>3.29.2-GA</javaassist.version>
98113
<asm.version>5.2</asm.version>
114+
<teavm.version>0.10.1</teavm.version>
99115
</properties>
100116

101117
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.teavm;
2+
3+
import org.teavm.jso.JSExport;
4+
import org.teavm.jso.dom.html.*;
5+
6+
public class Calculator {
7+
8+
public static void main(String[] args) {
9+
HTMLDocument document = HTMLDocument.current();
10+
HTMLElement container = document.getElementById("calculator-container");
11+
12+
// Create input fields
13+
HTMLInputElement input1 = (HTMLInputElement) document.createElement("input");
14+
input1.setType("number");
15+
container.appendChild(input1);
16+
17+
HTMLInputElement input2 = (HTMLInputElement) document.createElement("input");
18+
input2.setType("number");
19+
container.appendChild(input2);
20+
21+
// Create a button
22+
HTMLButtonElement button = (HTMLButtonElement) document.createElement("button");
23+
button.appendChild(document.createTextNode("Calculate Sum"));
24+
container.appendChild(button);
25+
26+
// Create a div to display the result
27+
HTMLElement resultDiv = document.createElement("div");
28+
container.appendChild(resultDiv);
29+
30+
// Add click event listener to the button
31+
button.addEventListener("click", (evt) -> {
32+
try {
33+
long num1 = Long.parseLong(input1.getValue());
34+
long num2 = Long.parseLong(input2.getValue());
35+
long sum = num1 + num2;
36+
resultDiv.setTextContent("Result: " + sum);
37+
} catch (NumberFormatException e) {
38+
resultDiv.setTextContent("Please enter valid integer numbers.");
39+
}
40+
});
41+
}
42+
43+
@JSExport
44+
public static int sum(int a, int b) {
45+
return a + b;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.teavm;
2+
3+
import org.teavm.tooling.TeaVMTool;
4+
import org.teavm.tooling.TeaVMTargetType;
5+
import org.teavm.vm.TeaVMOptimizationLevel;
6+
7+
import java.io.File;
8+
9+
public class TeaVMRunner {
10+
public static void main(String[] args) throws Exception {
11+
TeaVMTool tool = new TeaVMTool();
12+
tool.setTargetDirectory(new File("target/teavm"));
13+
tool.setTargetFileName("calculator.js");
14+
tool.setMainClass("com.baeldung.teavm.Calculator");
15+
tool.setTargetType(TeaVMTargetType.JAVASCRIPT);
16+
tool.setOptimizationLevel(TeaVMOptimizationLevel.ADVANCED);
17+
tool.setDebugInformationGenerated(false);
18+
tool.setIncremental(false);
19+
tool.setObfuscated(true);
20+
tool.generate();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.teavm;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
public class CalculatorUnitTest {
7+
8+
@Test
9+
public void whenAddingTwoPositiveNumbers_thenCorrectSum() {
10+
// when
11+
int result = Calculator.sum(3, 7);
12+
13+
// then
14+
assertEquals(10, result);
15+
}
16+
17+
@Test
18+
public void whenAddingNegativeAndPositiveNumber_thenCorrectSum() {
19+
// when
20+
int result = Calculator.sum(-5, 10);
21+
22+
// then
23+
assertEquals(5, result);
24+
}
25+
26+
@Test
27+
public void whenAddingTwoNegativeNumbers_thenCorrectSum() {
28+
// when
29+
int result = Calculator.sum(-4, -6);
30+
31+
// then
32+
assertEquals(-10, result);
33+
}
34+
}

0 commit comments

Comments
 (0)