Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemB extends LibCAPI #1143

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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