FastOds { public static boolean openFile(final File f) { if (desktop != null && f.exists() && f.isFile()) { try { desktop.open(f); return true; } catch (final IOException e) { Logger.getLogger(FastOds.class.getName()) .log(Level.SEVERE, "Can't open file " + f + " in appropriate application", e); } } return false; } private  FastOds(); static XMLUtil getXMLUtil(); static boolean openFile(final File f);  }
@Test public final void testOpenDir() { Assert.assertFalse(FastOds.openFile(new File("."))); }
@Test public final void testOpenFile() throws IOException { FastOds.desktop = PowerMock.createMock(Desktop.class); final File f = new File(".", "pom.xml"); PowerMock.resetAll(); FastOds.desktop.open(f); PowerMock.replayAll(); final boolean opened = FastOds.openFile(f); PowerMock.verifyAll(); Assert.assertTrue(opened); }
@Test public final void testOpenInexistentFile() { final File f = PowerMock.createMock(File.class); PowerMock.resetAll(); if (FastOds.desktop != null) { EasyMock.expect(f.exists()).andReturn(false); } PowerMock.replayAll(); Assert.assertFalse(FastOds.openFile(f)); PowerMock.verifyAll(); }
@Test public final void testOpenDirMock() { final File f = PowerMock.createMock(File.class); PowerMock.resetAll(); EasyMock.expect(f.exists()).andReturn(true); EasyMock.expect(f.isFile()).andReturn(false); PowerMock.replayAll(); Assert.assertFalse(FastOds.openFile(f)); PowerMock.verifyAll(); }
@Test public final void testOpenFileError() throws IOException { final Logger rootLogger = Logger.getLogger(""); rootLogger.setLevel(Level.OFF); for (final Handler h : rootLogger.getHandlers()) { h.setLevel(Level.OFF); } final File f = new File(".", "pom.xml"); FastOds.desktop = PowerMock.createMock(Desktop.class); PowerMock.resetAll(); FastOds.desktop.open(f); EasyMock.expectLastCall().andThrow(new IOException()); PowerMock.replayAll(); Assert.assertFalse(FastOds.openFile(f)); PowerMock.verifyAll(); }
@Test public final void testOpenNonExisting() { Assert.assertFalse(FastOds.openFile(new File(".", "@"))); }