/** Make sure we can actually fetch a MAC address. */ @Test public void verifyMac() { byte[] mac; try { mac = MacUtils.macAddress(); } catch (UnsupportedOperationException e) { // hmm... maybe we have no valid MAC's? e.printStackTrace(); System.setProperty(MacUtils.OVERRIDE_MAC_PROP, "00:DE:AD:BE:EF:00"); mac = MacUtils.macAddress(); } Assert.assertNotNull("Could not retrieve MAC", mac); Assert.assertTrue("Invalid MAC address", mac.length == 6); }
/** * The implementation that the other methods delegate to. This provides the caller with all * available options for customizing the <tt>JFileChooser</tt> instance. If a <tt>FileDialog</tt> * is displayed instead of a <tt>JFileChooser</tt> (on OS X, for example), most or all of these * options have no effect. * * @param parent the <tt>Component</tt> that should be the dialog's parent * @param titleKey the key for the locale-specific string to use for the file dialog title * @param approveKey the key for the locale-specific string to use for the approve button text * @param directory the directory to open the dialog to * @param mode the "mode" to open the <tt>JFileChooser</tt> in from the <tt>JFileChooser</tt> * class, such as <tt>JFileChooser.DIRECTORIES_ONLY</tt> * @param option the option to look for in the return code, such as * <tt>JFileChooser.APPROVE_OPTION</tt> * @param allowMultiSelect true if the chooser allows multiple files to be chosen * @param filter the <tt>FileFilter</tt> instance for customizing the files that are displayed -- * if this is null, no filter is used * @return the selected <tt>File</tt> instance, or <tt>null</tt> if a file was not selected * correctly */ public static List<File> getInput( Component parent, String titleKey, String approveKey, File directory, int mode, int option, boolean allowMultiSelect, final FileFilter filter) { if (!OSUtils.isAnyMac()) { JFileChooser fileChooser = getDirectoryChooser(titleKey, approveKey, directory, mode, filter); fileChooser.setMultiSelectionEnabled(allowMultiSelect); try { if (fileChooser.showOpenDialog(parent) != option) return null; } catch (NullPointerException npe) { // ignore NPE. can't do anything with it ... return null; } if (allowMultiSelect) { File[] chosen = fileChooser.getSelectedFiles(); if (chosen.length > 0) setLastInputDirectory(chosen[0]); return Arrays.asList(chosen); } else { File chosen = fileChooser.getSelectedFile(); setLastInputDirectory(chosen); return Collections.singletonList(chosen); } } else { FileDialog dialog; if (mode == JFileChooser.DIRECTORIES_ONLY) dialog = MacUtils.getFolderDialog(); else dialog = new FileDialog(GUIMediator.getAppFrame(), ""); dialog.setTitle(I18n.tr(titleKey)); if (filter != null) { FilenameFilter f = new FilenameFilter() { public boolean accept(File dir, String name) { return filter.accept(new File(dir, name)); } }; dialog.setFilenameFilter(f); } dialog.setVisible(true); String dirStr = dialog.getDirectory(); String fileStr = dialog.getFile(); if ((dirStr == null) || (fileStr == null)) return null; setLastInputDirectory(new File(dirStr)); // if the filter didn't work, pretend that the person picked // nothing File f = new File(dirStr, fileStr); if (filter != null && !filter.accept(f)) return null; return Collections.singletonList(f); } }
@Test public void slightlyBogusOverride() { System.setProperty(MacUtils.OVERRIDE_MAC_PROP, "00:DE:AD:BE:EF:QQ"); byte[] mac = MacUtils.getOverride(); Assert.assertNull("Retrieved a bogus MAC", mac); System.clearProperty(MacUtils.OVERRIDE_MAC_PROP); }
@Test public void bogusOverride() { System.setProperty(MacUtils.OVERRIDE_MAC_PROP, "totally not a MAC"); byte[] mac = MacUtils.getOverride(); Assert.assertNull("Retrieved a bogus MAC", mac); System.clearProperty(MacUtils.OVERRIDE_MAC_PROP); }
@Test public void verifyOverride() { System.setProperty(MacUtils.OVERRIDE_MAC_PROP, "00:DE:AD:BE:EF:11"); byte[] mac = MacUtils.macAddress(); Assert.assertNotNull("Could not retrieve MAC", mac); Assert.assertTrue("Invalid MAC address", mac.length == 6); Assert.assertEquals( "Unexpected MAC address", "[0, -34, -83, -66, -17, 17]", Arrays.toString(mac)); System.clearProperty(MacUtils.OVERRIDE_MAC_PROP); }