Skip to content

Commit

Permalink
test: 加入一個 test 實際跑 libjvm 以確保升級後還可以正常呼叫
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Jan 20, 2024
1 parent 9d2d48e commit 64ae5a7
Showing 1 changed file with 65 additions and 47 deletions.
112 changes: 65 additions & 47 deletions calc/calculator_test.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,100 @@
package calc

import (
"testing"
"strings"
"testing"
)

type MockContributor struct {
Called bool
Called bool
}

func (m *MockContributor) Contribute() error {
m.Called = true
return nil
m.Called = true
return nil
}

func TestCalculator_Contribute(t *testing.T) {
mockContributor := &MockContributor{}
mockContributor := &MockContributor{}

err := contribute(mockContributor)
if err != nil {
t.Fatalf("contribute returned an error: %v", err)
}
err := contribute(mockContributor)
if err != nil {
t.Fatalf("contribute returned an error: %v", err)
}

if !mockContributor.Called {
t.Errorf("Contributor's Contribute method was not called")
}
if !mockContributor.Called {
t.Errorf("Contributor's Contribute method was not called")
}
}

func TestCalculator_buildHelpers_HasCaCerts(t *testing.T) {
calculator := NewCalculator()
calculator.JVMCacerts.Set("some-value")
calculator := NewCalculator()
calculator.JVMCacerts.Set("some-value")

helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}
helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}

if _, ok := helpers[helperOpensslCertificateLoader]; !ok {
t.Errorf(helperOpensslCertificateLoader + " helper not found in helpers")
}
if _, ok := helpers[helperOpensslCertificateLoader]; !ok {
t.Errorf(helperOpensslCertificateLoader + " helper not found in helpers")
}
}

func TestCalculator_buildHelpers_NoCaCerts(t *testing.T) {
calculator := NewCalculator()
calculator := NewCalculator()

helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}
helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}

if _, ok := helpers[helperOpensslCertificateLoader]; ok {
t.Errorf(helperOpensslCertificateLoader + " helper should not be present")
}
if _, ok := helpers[helperOpensslCertificateLoader]; ok {
t.Errorf(helperOpensslCertificateLoader + " helper should not be present")
}
}

func TestCalculator_buildHelpers_EnableNmt(t *testing.T) {
calculator := NewCalculator()
*calculator.EnableNmt = true
calculator := NewCalculator()
*calculator.EnableNmt = true

helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}
helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}

if _, ok := helpers[helperNmt]; !ok {
t.Errorf(helperNmt + " helper should be present")
}
if _, ok := helpers[helperNmt]; !ok {
t.Errorf(helperNmt + " helper should be present")
}
}

func TestCalculator_buildHelpers_DisableNmt(t *testing.T) {
calculator := NewCalculator()
*calculator.EnableNmt = false
calculator := NewCalculator()
*calculator.EnableNmt = false

helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}
helpers, err := calculator.buildHelpers()
if err != nil {
t.Fatalf("buildHelpers returned an error: %v", err)
}

if _, ok := helpers[helperNmt]; ok {
t.Errorf(helperNmt + " helper should not be present")
}
if _, ok := helpers[helperNmt]; ok {
t.Errorf(helperNmt + " helper should not be present")
}
}

func TestCalculator_Execute_Success(t *testing.T) {
calculator := NewCalculator()
*calculator.LoadedClassCount = 1000

jto, err := calculator.Execute()
if err != nil {
t.Fatalf("Execute returned an error: %v", err)
}
options := jto.String()
for _, o := range contributeOptions {
options = strings.ReplaceAll(options, o, "")
}
if options == "" {
t.Errorf("Execute returned an empty string")
}
}

0 comments on commit 64ae5a7

Please sign in to comment.