Reputation: 3692
I have made a test case of one Method that will call actual Method in it and execute it.
That method is used to create directory.
Following is my code :
Actual Method :
public String getDefaultFolderPath() {
String path = "";
try {
String os = System.getProperty("os.name");
System.out.println("Os name Identified");
if (os.toUpperCase().indexOf("WINDOWS") != -1) {
File file = new File("C:/MARCDictionary");
if (!file.exists()) {
System.out.println("11");
file.mkdir();
}
path = "C:/MARCDictionary";
} else if (os.toUpperCase().indexOf("LINUX") != -1) {
File file = new File("/usr/MARCDictionary");
if (!file.exists()) {
System.out.println("22");
//file.mkdir();
file.createNewFile();
}
path = "/usr/MARCDictionary";
}
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
Test Case :
@Test
public void testGetDefaultFolderPath() {
System.out.println("getDefaultFolderPath");
Utilities instance = Utilities.getInstance();
String expResult = "/usr/MARCDictionary";
String result = instance.getDefaultFolderPath();
assertEquals(expResult, result);
}
Gives me an error :
getDefaultFolderPath
Os name Identified
22
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
at NGLUtility.NGLUtilities.getDefaultFolderPath(Utilities.java:108)
at utilities.NGLUtilitiesTest.testGetDefaultFolderPath(UtilitiesTest.java:85)
org.junit.ComparisonFailure:
Expected :/usr/MARCDictionary
Any suggestion Please Why it's Happening like this..
I am using Ubuntu
and Intellij IDEA.
Upvotes: 0
Views: 2248
Reputation: 9946
Your program does not have write permissions on /usr
directory. you must give write permissions on this folder or try some other folder within /usr
that has write permissions. For example to give all the permissions on /usr
you can use chmod 777 /usr
Hope this helps
Upvotes: 1