github.com/luchsh/agentlib.go@v0.0.0-20221115155834-ffd0caec4d72/jgo/java_test.go (about)

     1  //
     2  // Copyright 2022 chuanshenglu@gmail.com
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  // The goal is to decouple the exposed interface from CGO, JNI or JVMTI
    18  
    19  package jgo
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestCreateJavaVM(t *testing.T) {
    28  	vm,e := Exec([]string{}) // cannot create more than one VMs
    29  	assert.Nil(t, vm)
    30  	assert.NotNil(t, e)
    31  }
    32  
    33  func TestGetProperties(t *testing.T) {
    34  	wantedPrpos := []string {
    35  		"java.vm.vendor",
    36  		"java.vm.version",
    37  		"java.vm.name",
    38  		"java.vm.info",
    39  		"java.library.path",
    40  		"java.class.path",
    41  	}
    42  	props, e := jvm.GetSystemProperties()
    43  	assert.Nil(t, e)
    44  	assert.GreaterOrEqual(t, len(props), len(wantedPrpos))
    45  	for _,wp := range wantedPrpos {
    46  		//t.Logf("%s=%s", wp, props[wp])
    47  		if _,ok := props[wp]; !ok {
    48  			t.Fatalf("mandatory property %s not found", wp)
    49  		}
    50  	}
    51  
    52  	for _,wp := range wantedPrpos {
    53  		vs := jvm.GetSystemProperty(wp)
    54  		assert.Greater(t, len(vs), 0)
    55  	}
    56  }
    57  
    58  func TestDumpThreads(t *testing.T) {
    59  	thrds,e := jvm.DumpThreads()
    60  	assert.Nil(t, e)
    61  	assert.NotNil(t, thrds)
    62  	assert.Greater(t, len(thrds), 0)
    63  	for i,th := range thrds {
    64  		t.Logf("Thread-%d %s\n", i, th.String())
    65  	}
    66  }
    67  
    68  /*
    69  // TODO: run this test at agent OnLoad phase
    70  func TestGetSetProperty(t *testing.T) {
    71  	//key := fmt.Sprintf("java.test%d", time.Now().Nanosecond())
    72  	key := "java.vm.vendor"
    73  	v := jvm.GetSystemProperty(key)
    74  	assert.Equal(t, len(v), 0) // should not exist at first
    75  
    76  	value := fmt.Sprintf("value%d", time.Now().Nanosecond())
    77  	e := jvm.SetSystemProperty(key, value)
    78  	assert.Nil(t, e)
    79  
    80  	nv := jvm.GetSystemProperty(key)
    81  	assert.NotEqual(t, len(nv), 0)
    82  	assert.Equal(t, nv, value)
    83  }
    84  */