Skip to content

Commit

Permalink
SystemB extends LibCAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Oct 7, 2019
1 parent ebbd769 commit 48076c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release (5.5.0)
Features
--------
* [#1131](https://github.com/java-native-access/jna/pull/1131): Add CoreFoundation, IOKit, and DiskArbitration mappings in `c.s.j.p.mac`. [@dbwiddis](https://github.com/dbwiddis).
* [#1143](https://github.com/java-native-access/jna/pull/1143): `c.s.j.p.mac.SystemB` now extends `c.s.j.p.unix.LibCAPI`. [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
21 changes: 2 additions & 19 deletions contrib/platform/src/com/sun/jna/platform/mac/SystemB.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.unix.LibCAPI;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;

public interface SystemB extends Library {
public interface SystemB extends LibCAPI, Library {

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

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

/**
* The getloadavg() function returns the number of processes in the system run
* queue averaged over various periods of time. Up to nelem samples are
* retrieved and assigned to successive elements of loadavg[]. The system
* imposes a maximum of 3 samples, representing averages over the last 1, 5, and
* 15 minutes, respectively.
*
* @param loadavg
* An array of doubles which will be filled with the results
* @param nelem
* Number of samples to return
* @return If the load average was unobtainable, -1 is returned; otherwise, the
* number of samples actually retrieved is returned.
* @see <A HREF=
* "https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
*/
int getloadavg(double[] loadavg, int nelem);

/**
* This function searches the password database for the given user uid, always
* returning the first one encountered.
Expand Down
38 changes: 38 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import static org.junit.Assert.assertNotEquals;

import java.util.Date;
import java.util.Map;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Platform;
Expand Down Expand Up @@ -167,6 +170,31 @@ public void testHostProcessorInfo() {
procInfoCount.getValue());
}

// From Unix LibCAPI
public void testGetenv() {
Map<String, String> env = System.getenv();
for (Map.Entry<String, String> ee : env.entrySet()) {
String name = ee.getKey();
String expected = ee.getValue();
String actual = SystemB.INSTANCE.getenv(name);
assertEquals(name, expected, actual);
}
}

// From Unix LibCAPI
public void testSetenv() {
String name = "SystemBTestEnv";
try {
String expected = new Date(System.currentTimeMillis()).toString();
assertEquals("setenv", 0, SystemB.INSTANCE.setenv(name, expected, 1));
assertEquals("Mismatched values", expected, SystemB.INSTANCE.getenv(name));
assertEquals("unsetenv", 0, SystemB.INSTANCE.unsetenv(name));
} finally {
SystemB.INSTANCE.unsetenv(name);
}
}

// From Unix LibCAPI
public void testGetLoadAvg() {
double[] loadavg = new double[3];
int retval = SystemB.INSTANCE.getloadavg(loadavg, 3);
Expand All @@ -176,6 +204,16 @@ public void testGetLoadAvg() {
assertTrue(loadavg[2] >= 0);
}

// From Unix LibCAPI
public void testGethostnameGetdomainname() {
byte[] buffer = new byte[256];
assertEquals("gethostname", 0, SystemB.INSTANCE.gethostname(buffer, buffer.length));
String hostname = Native.toString(buffer);
assertTrue(hostname.length() > 0);
assertEquals("getdomainname", 0, SystemB.INSTANCE.getdomainname(buffer, buffer.length));
// May have length 0
}

public void testTimeofDay() {
Timeval tp = new Timeval();
long before = System.currentTimeMillis();
Expand Down

0 comments on commit 48076c8

Please sign in to comment.