Skip to content

Commit 192a215

Browse files
committed
SystemB extends LibCAPI
1 parent ebbd769 commit 192a215

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

contrib/platform/src/com/sun/jna/platform/mac/SystemB.java

+2-19
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
import com.sun.jna.NativeLong;
3131
import com.sun.jna.Pointer;
3232
import com.sun.jna.Structure;
33+
import com.sun.jna.platform.unix.LibCAPI;
3334
import com.sun.jna.ptr.IntByReference;
3435
import com.sun.jna.ptr.LongByReference;
3536
import com.sun.jna.ptr.PointerByReference;
3637

37-
public interface SystemB extends Library {
38+
public interface SystemB extends LibCAPI, Library {
3839

3940
SystemB INSTANCE = Native.load("System", SystemB.class);
4041

@@ -730,24 +731,6 @@ class Timezone extends Structure {
730731
int host_processor_info(int hostPort, int flavor, IntByReference procCount, PointerByReference procInfo,
731732
IntByReference procInfoCount);
732733

733-
/**
734-
* The getloadavg() function returns the number of processes in the system run
735-
* queue averaged over various periods of time. Up to nelem samples are
736-
* retrieved and assigned to successive elements of loadavg[]. The system
737-
* imposes a maximum of 3 samples, representing averages over the last 1, 5, and
738-
* 15 minutes, respectively.
739-
*
740-
* @param loadavg
741-
* An array of doubles which will be filled with the results
742-
* @param nelem
743-
* Number of samples to return
744-
* @return If the load average was unobtainable, -1 is returned; otherwise, the
745-
* number of samples actually retrieved is returned.
746-
* @see <A HREF=
747-
* "https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
748-
*/
749-
int getloadavg(double[] loadavg, int nelem);
750-
751734
/**
752735
* This function searches the password database for the given user uid, always
753736
* returning the first one encountered.

contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
import static org.junit.Assert.assertNotEquals;
2727

28+
import java.util.Date;
29+
import java.util.Map;
30+
2831
import com.sun.jna.Memory;
2932
import com.sun.jna.Native;
3033
import com.sun.jna.Platform;
@@ -167,6 +170,31 @@ public void testHostProcessorInfo() {
167170
procInfoCount.getValue());
168171
}
169172

173+
// From Unix LibCAPI
174+
public void testGetenv() {
175+
Map<String, String> env = System.getenv();
176+
for (Map.Entry<String, String> ee : env.entrySet()) {
177+
String name = ee.getKey();
178+
String expected = ee.getValue();
179+
String actual = SystemB.INSTANCE.getenv(name);
180+
assertEquals(name, expected, actual);
181+
}
182+
}
183+
184+
// From Unix LibCAPI
185+
public void testSetenv() {
186+
String name = "SystemBTestEnv";
187+
try {
188+
String expected = new Date(System.currentTimeMillis()).toString();
189+
assertEquals("setenv", 0, SystemB.INSTANCE.setenv(name, expected, 1));
190+
assertEquals("Mismatched values", expected, SystemB.INSTANCE.getenv(name));
191+
assertEquals("unsetenv", 0, SystemB.INSTANCE.unsetenv(name));
192+
} finally {
193+
SystemB.INSTANCE.unsetenv(name);
194+
}
195+
}
196+
197+
// From Unix LibCAPI
170198
public void testGetLoadAvg() {
171199
double[] loadavg = new double[3];
172200
int retval = SystemB.INSTANCE.getloadavg(loadavg, 3);
@@ -176,6 +204,16 @@ public void testGetLoadAvg() {
176204
assertTrue(loadavg[2] >= 0);
177205
}
178206

207+
// From Unix LibCAPI
208+
public void testGethostnameGetdomainname() {
209+
byte[] buffer = new byte[256];
210+
assertEquals("gethostname", 0, SystemB.INSTANCE.gethostname(buffer, buffer.length));
211+
String hostname = Native.toString(buffer);
212+
assertTrue(hostname.length() > 0);
213+
assertEquals("getdomainname", 0, SystemB.INSTANCE.getdomainname(buffer, buffer.length));
214+
// May have length 0
215+
}
216+
179217
public void testTimeofDay() {
180218
Timeval tp = new Timeval();
181219
long before = System.currentTimeMillis();

0 commit comments

Comments
 (0)