private PrintStream getLog() { if (log != null) { PrintStream ps = log.get(); if (ps == null) { // gc => remove the handler setLevel(Level.OFF); logger.removeHandler(this); } return ps; } NbTestCase c = current; Runnable off = Log.internalLog(); try { return c == null ? System.err : c.getLog(); } finally { off.run(); } }
public static void assertGC(String msg, String... names) { AssertionFailedError t = null; List<Reference> refs = new ArrayList<Reference>(); List<String> txts = new ArrayList<String>(); int count = 0; Set<String> nameSet = names == null || names.length == 0 ? null : new HashSet<String>(Arrays.asList(names)); synchronized (instances) { for (Iterator<Map.Entry<Object, String>> it = instances.entrySet().iterator(); it.hasNext(); ) { Entry<Object, String> entry = it.next(); if (nameSet != null && !nameSet.contains(entry.getValue())) { continue; } refs.add(new WeakReference<Object>(entry.getKey())); txts.add(entry.getValue()); it.remove(); count++; } } if (count == 0) { Assert.fail("No instance of this type reported"); } for (int i = 0; i < count; i++) { Reference<?> r = refs.get(i); try { NbTestCase.assertGC(msg + " " + txts.get(i), r); } catch (AssertionFailedError ex) { if (t == null) { t = ex; } else { Throwable last = t; while (last.getCause() != null) { last = last.getCause(); } last.initCause(ex); } } } if (t != null) { throw t; } }
static void configure(Level lev, String root, NbTestCase current) { IL il = new IL(false); String c = "handlers=" + Log.class.getName() + "\n" + root + ".level=" + lev.intValue() + "\n"; ByteArrayInputStream is = new ByteArrayInputStream(c.getBytes()); try { LogManager.getLogManager().readConfiguration(is); } catch (IOException ex) { // exception ex.printStackTrace(); } Log.current = current; Log.messages.setLength(0); Log.messages.append("Starting test "); Log.messages.append(current.getName()); Log.messages.append('\n'); Log.initialMessages = Log.messages.length(); }