This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Issue/#521
- Loading branch information
Showing
6 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.pi4j; | ||
|
||
/*- | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : IntegrationTests.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: https://pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2021 Pi4J | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
public interface IntegrationTests { | ||
// JUNIT CATEGORY MARKER INTERFACE | ||
} |
69 changes: 69 additions & 0 deletions
69
pi4j-core/src/test/java/com/pi4j/io/file/test/ExposedLinuxFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.pi4j.io.file.test; | ||
|
||
/* | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : ExposedLinuxFile.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: https://pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2021 Pi4J | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.pi4j.io.file.LinuxFile; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Extends the com.pi4j.io.file.LinuxFile call for JUNIT test. | ||
* This child class allows the test programs to access the POSIX File Descriptor. | ||
* | ||
* THIS CLASS SHOULD NEVER BE USED in actual code. It is for testing only. | ||
*/ | ||
public class ExposedLinuxFile extends LinuxFile { | ||
|
||
/** | ||
* Only constructor for this class. | ||
* | ||
* @param name Name of file to open. | ||
* @param mode Mode to open file with. See JavaDoc for java.io.RandomFileAccess | ||
* constructor for list of legal values for <strong>mode>/strong> | ||
* @throws FileNotFoundException | ||
*/ | ||
public ExposedLinuxFile(String name, String mode) | ||
throws FileNotFoundException | ||
{ | ||
super(name, mode); | ||
} | ||
|
||
/** | ||
* Exposes the POSIX File Descriptor associated with the open filed. | ||
* | ||
* @return Returns the POSIX file descriptor integer assigned to the file. | ||
* | ||
* @throws IOException | ||
*/ | ||
public int getMyFD() throws IOException | ||
{ | ||
return getPosixFD( getFD() ); | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
pi4j-core/src/test/java/com/pi4j/io/file/test/LinuxFileIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.pi4j.io.file.test; | ||
|
||
/* | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : LinuxFileIntegrationTest.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: https://pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2021 Pi4J | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.pi4j.IntegrationTests; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import java.io.File; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
@Category(IntegrationTests.class) | ||
public class LinuxFileIntegrationTest { | ||
|
||
private static ExposedLinuxFile myFile = null; | ||
private static String myFileName = null; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
// Build Unique File Name | ||
|
||
// Generate a number that should be unique to the program | ||
long pid = ProcessHandle.current().pid(); | ||
|
||
myFileName = new String("/tmp/pi4j-" + String.valueOf(pid) + "LinuxFileTest.txt"); | ||
|
||
// Open File | ||
myFile = new ExposedLinuxFile(myFileName, "rw"); | ||
|
||
} | ||
|
||
@After | ||
public void cleanup() throws Exception { | ||
// close and delete the file. | ||
if (myFile != null) { | ||
myFile.close(); | ||
|
||
// create new file object | ||
File tempFile = new File(myFileName); | ||
|
||
// if file exists, then delete it now | ||
if (tempFile.exists()) { | ||
tempFile.delete(); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void fileDescriptorTest() throws Exception { | ||
int fd = -1; | ||
|
||
// validate file name | ||
if (myFileName == null || myFileName.isEmpty()) { | ||
Assert.fail("Failed to generate temp file name. LinuxFileTest"); | ||
} | ||
|
||
// validate file exists | ||
if (myFile == null) { | ||
Assert.fail("File: " + myFileName + "Not Found/Open. LinuxFileTest"); | ||
} | ||
|
||
// validate POSIX file descriptor access | ||
try { | ||
fd = myFile.getMyFD(); | ||
} catch (Exception exc) { | ||
Assert.fail("Exception during getPosixFD(): " | ||
+ exc.getMessage() + " For File: " + myFileName + " - LinuxFileTest"); | ||
} | ||
|
||
// validate POSIX file descriptor value; must be a positive integer | ||
assertTrue("File: " + myFileName + ", File Descriptor: " + String.valueOf(fd) + | ||
" - LinuxFileTest", | ||
(fd > 0)); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
pi4j-core/src/test/java/com/pi4j/io/file/test/LinuxFileManualTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.pi4j.io.file.test; | ||
|
||
/*- | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : LinuxFileManualTest.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: https://pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2021 Pi4J | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* This is a test program that is intended to be run inside a debugger | ||
* and not run as part of the JUint testing. The reason for this is that | ||
* the JUnit version, @See LinuxFileTest, can only test that some value | ||
* comes back for the Posix File Descriptior number. With a debugger, | ||
* the myFile variable can be examped to see what the private FD value is | ||
* and that number can be compared to the output of this program. | ||
*/ | ||
public class LinuxFileManualTest { | ||
private static final String RW = "rw"; | ||
|
||
public static void main(String[] args) | ||
throws IOException { | ||
// I had difficulty getting the program to find the native pi4j library | ||
// in Visual Studio Code. The System.setProperty() can be used to force | ||
// the editor/IDE to look for the libpi4j-armhf.so in the directory where | ||
// it has been stored. | ||
|
||
//System.setProperty("pi4j.library.path", "/opt/pi4j/lib"); | ||
|
||
ExposedLinuxFile myFile = null; | ||
String myFileName = null; | ||
int fd = -1; | ||
|
||
// Build Unique File Name | ||
|
||
// Generate a number that should be unique to the program | ||
long pid = ProcessHandle.current().pid(); | ||
|
||
myFileName = "/tmp/pi4j-" + pid + "LinuxFileTest.txt"; | ||
|
||
// Open and create the temporary file and get its POSIX file descriptor. | ||
// Note, exceptions can occur if unable to open the file. | ||
try { | ||
myFile = new ExposedLinuxFile(myFileName, RW); | ||
|
||
// Place a break point for the following statement. Once the debugger | ||
// stops here, examine the value of myFile for the file descriptor number. | ||
// It may require examining fields within the object to find the file descriptor. | ||
fd = myFile.getMyFD(); | ||
} catch (FileNotFoundException ex) { | ||
System.out.println(ex.getMessage()); | ||
ex.printStackTrace(); | ||
|
||
fd = -2; | ||
} | ||
|
||
System.out.printf("FileDescriptor for Random.test = %d\n", fd); | ||
|
||
// Cleanup | ||
myFile.close(); | ||
|
||
File tempFile = new File(myFileName); | ||
|
||
if (tempFile.exists()) { | ||
tempFile.delete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters