improve exception throwing in java class util.utils

so that we get nice stack traces in our test error logs instead of just
the title of the exception

Change-Id: I47f30d80b9efbc7dbeff7f4456755e416e577b5a
Reviewed-on: https://gerrit.libreoffice.org/25510
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
This commit is contained in:
Noel Grandin
2016-05-26 13:13:31 +02:00
committed by Noel Grandin
parent 02b666c477
commit fd964e3b9f
4 changed files with 29 additions and 57 deletions

View File

@@ -230,9 +230,8 @@ public class utils {
XPropertySet pthSettings = (XPropertySet) AnyConverter.toObject( XPropertySet pthSettings = (XPropertySet) AnyConverter.toObject(
new Type(XPropertySet.class), settings); new Type(XPropertySet.class), settings);
return (String) pthSettings.getPropertyValue(setting); return (String) pthSettings.getPropertyValue(setting);
} catch (com.sun.star.uno.Exception e) { } catch (com.sun.star.uno.Exception ex) {
e.printStackTrace(); throw new RuntimeException(ex);
throw new RuntimeException(e);
} }
} }
@@ -368,23 +367,11 @@ public class utils {
* @param fileURL the file which existence should be checked * @param fileURL the file which existence should be checked
* @return true if the file exists, else false * @return true if the file exists, else false
*/ */
public static boolean fileExists(XMultiServiceFactory msf, String fileURL) { public static boolean fileExists(XMultiServiceFactory msf, String fileURL) throws com.sun.star.uno.Exception {
boolean exists = false; Object fileacc = msf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
try { XSimpleFileAccess simpleAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class,
Object fileacc = msf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
XSimpleFileAccess simpleAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class,
fileacc); fileacc);
if (simpleAccess.exists(fileURL)) { return simpleAccess.exists(fileURL);
exists = true;
}
} catch (Exception e) {
System.out.println("Couldn't access file '" + fileURL + "'");
e.printStackTrace();
exists = false;
}
return exists;
} }
/** /**
@@ -417,22 +404,22 @@ public class utils {
* This method copies via office a given file to a new one * This method copies via office a given file to a new one
* @param xMsf the multi service factory * @param xMsf the multi service factory
* @param source the source file * @param source the source file
* @param destinaion the destination file * @param destination the destination file
* @return true at success * @return true at success
*/ */
public static boolean copyFile(XMultiServiceFactory xMsf, String source, String destinaion) { public static boolean copyFile(XMultiServiceFactory xMsf, String source, String destination) {
boolean res = false; boolean res = false;
try { try {
Object fileacc = xMsf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess"); Object fileacc = xMsf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
XSimpleFileAccess simpleAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class, XSimpleFileAccess simpleAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class,
fileacc); fileacc);
if (!simpleAccess.exists(destinaion)) { if (!simpleAccess.exists(destination)) {
simpleAccess.copy(source, destinaion); simpleAccess.copy(source, destination);
} }
res = true; res = true;
} catch (Exception e) { } catch (Exception e) {
System.out.println("Couldn't copy file '" + source + "' -> '" + destinaion + "'"); System.out.println("Couldn't copy file '" + source + "' -> '" + destination + "'");
e.printStackTrace(); e.printStackTrace();
res = false; res = false;
} }
@@ -454,10 +441,8 @@ public class utils {
simpleAccess.copy(oldF, newF); simpleAccess.copy(oldF, newF);
} catch (InteractiveAugmentedIOException e) { } catch (InteractiveAugmentedIOException e) {
throw e; throw e;
} catch (com.sun.star.uno.Exception e) { } catch (com.sun.star.uno.Exception ex) {
System.out.println("Couldn't copy " + oldF + " to " + newF + ":"); throw new RuntimeException("Could not copy " + oldF + " to " + newF, ex);
e.printStackTrace();
throw new RuntimeException(e);
} }
} }
@@ -500,15 +485,8 @@ public class utils {
* *
*/ */
public static String getImplName(Object aObject) { public static String getImplName(Object aObject) {
String res = "Error getting Implementation name"; XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, aObject);
try { return xSI == null ? "Unknown, does not implement XServiceInfo" : xSI.getImplementationName();
XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, aObject);
res = xSI.getImplementationName();
} catch (Exception e) {
res = "Error getting Implementation name ( " + e + " )";
}
return res;
} }
/** /**
@@ -582,17 +560,11 @@ public class utils {
return rUrl[0]; return rUrl[0];
} }
public static String getOfficeURL(XMultiServiceFactory msf) { public static String getOfficeURL(XMultiServiceFactory msf) throws com.sun.star.uno.Exception {
try { Object settings = msf.createInstance("com.sun.star.util.PathSettings");
Object settings = msf.createInstance("com.sun.star.util.PathSettings"); XPropertySet settingProps = UnoRuntime.queryInterface(XPropertySet.class, settings);
XPropertySet settingProps = UnoRuntime.queryInterface(XPropertySet.class, settings); String path = (String) settingProps.getPropertyValue("Module");
String path = (String) settingProps.getPropertyValue("Module"); return path;
return path;
} catch (Exception e) {
System.out.println("Couldn't get Office Settings ");
e.printStackTrace();
}
return null;
} }
@@ -769,7 +741,7 @@ public class utils {
* @return return the expanded string * @return return the expanded string
* @see com.sun.star.util.XMacroExpander * @see com.sun.star.util.XMacroExpander
*/ */
public static String expandMacro(XMultiServiceFactory xMSF, String expand) throws java.lang.Exception { public static String expandMacro(XMultiServiceFactory xMSF, String expand) {
try { try {
XPropertySet xPS = UnoRuntime.queryInterface(XPropertySet.class, xMSF); XPropertySet xPS = UnoRuntime.queryInterface(XPropertySet.class, xMSF);
XComponentContext xContext = UnoRuntime.queryInterface(XComponentContext.class, XComponentContext xContext = UnoRuntime.queryInterface(XComponentContext.class,
@@ -778,7 +750,7 @@ public class utils {
xContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander")); xContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander"));
return xME.expandMacros(expand); return xME.expandMacros(expand);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("could not expand macro", e); throw new RuntimeException("could not expand macro", e);
} }
} }
@@ -806,9 +778,8 @@ public class utils {
* @param xMSF the <CODE>XMultiServiceFactory</CODE> * @param xMSF the <CODE>XMultiServiceFactory</CODE>
* @param xCont the <CODE>XController</CODE> to query for a XDispatchProvider * @param xCont the <CODE>XController</CODE> to query for a XDispatchProvider
* @param URL the <CODE>URL</CODE> to dispatch * @param URL the <CODE>URL</CODE> to dispatch
* @throws java.lang.Exception throws <CODE>java.lang.Exception</CODE> on any error
*/ */
private static void dispatchURL(XMultiServiceFactory xMSF, XController xCont, String URL) throws java.lang.Exception { private static void dispatchURL(XMultiServiceFactory xMSF, XController xCont, String URL) {
try { try {
XDispatchProvider xDispProv = UnoRuntime.queryInterface(XDispatchProvider.class, xCont); XDispatchProvider xDispProv = UnoRuntime.queryInterface(XDispatchProvider.class, xCont);
@@ -831,7 +802,7 @@ public class utils {
waitForEventIdle(xMSF); waitForEventIdle(xMSF);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("ERROR: could not dispatch URL '" + URL + "'", e); throw new RuntimeException("Could not dispatch URL '" + URL + "'", e);
} }
} }

View File

@@ -20,6 +20,7 @@ package ifc.configuration.backend;
import com.sun.star.configuration.backend.XLayer; import com.sun.star.configuration.backend.XLayer;
import com.sun.star.configuration.backend.XMultiLayerStratum; import com.sun.star.configuration.backend.XMultiLayerStratum;
import com.sun.star.configuration.backend.XUpdatableLayer; import com.sun.star.configuration.backend.XUpdatableLayer;
import com.sun.star.uno.Exception;
import lib.MultiMethodTest; import lib.MultiMethodTest;
import util.XLayerHandlerImpl; import util.XLayerHandlerImpl;
@@ -156,7 +157,7 @@ public class _XMultiLayerStratum extends MultiMethodTest {
tRes.tested("getUpdatableLayer()", res); tRes.tested("getUpdatableLayer()", res);
} }
public void _getUpdateLayerId() { public void _getUpdateLayerId() throws com.sun.star.uno.Exception {
boolean res = true; boolean res = true;
try { try {
@@ -202,7 +203,7 @@ public class _XMultiLayerStratum extends MultiMethodTest {
tRes.tested("getUpdateLayerId()", res); tRes.tested("getUpdateLayerId()", res);
} }
public void _listLayerIds() { public void _listLayerIds() throws com.sun.star.uno.Exception {
boolean res = true; boolean res = true;
try { try {

View File

@@ -56,7 +56,7 @@ public class DLLComponentLoader extends TestCase {
*/ */
@Override @Override
protected TestEnvironment createTestEnvironment protected TestEnvironment createTestEnvironment
(TestParameters Param, PrintWriter log) { (TestParameters Param, PrintWriter log) throws com.sun.star.uno.Exception {
XInterface oObj = null; XInterface oObj = null;
Object oInterface = null; Object oInterface = null;
XMultiServiceFactory xMSF = null; XMultiServiceFactory xMSF = null;

View File

@@ -219,7 +219,7 @@ public class CheckBookmarks {
} }
private XTextDocument reloadFrom(String sFilter, String sExtension) private XTextDocument reloadFrom(String sFilter, String sExtension)
throws com.sun.star.io.IOException throws com.sun.star.io.IOException, com.sun.star.uno.Exception
{ {
String sFileUrl = util.utils.getOfficeTemp(m_xMsf) + "/Bookmarktest." + sExtension; String sFileUrl = util.utils.getOfficeTemp(m_xMsf) + "/Bookmarktest." + sExtension;
try { try {