Java Code Examples for java.util.logging.Logger.getLevel()

The following are Jave code examples for showing how to use getLevel() of the java.util.logging.Logger class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: DefaultSourceLevelQueryImplTest.java   Source Code and License Vote up 5 votes
public void testRootCache() throws IOException {
    final FileObject wd = FileUtil.toFileObject(FileUtil.normalizeFile(getWorkDir()));
    final FileObject root1 = FileUtil.createFolder(wd, "root1");
    final FileObject module1 = createTestFile (root1, null, "module-info.java", "module Foo {}");    //NOI18N
    final FileObject java1 = createTestFile (root1, "org/nb/test", "Test.java", "package org.nb.test;\nclass Test {}");    //NOI18N
    final FileObject root2 = FileUtil.createFolder(wd, "root2");
    final FileObject module2 = createTestFile (root2, null, "module-info.java", "module Boo {}");    //NOI18N
    final FileObject java2 = createTestFile (root2, "org/nb/test", "Test2.java", "package org.nb.test;\nclass Test2 {}");    //NOI18N
    final Logger l = Logger.getLogger(DefaultSourceLevelQueryImpl.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        List<? extends FileObject> roots = h.getRoots();
        assertEquals(1, roots.size());
        assertEquals(root1, roots.get(0));
        h.reset();
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        roots = h.getRoots();
        assertEquals(0, roots.size());
        h.reset();
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java2));
        roots = h.getRoots();
        assertEquals(1, roots.size());
        assertEquals(root2, roots.get(0));
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 2
Project: incubator-netbeans   File: DefaultSourceLevelQueryImplTest.java   Source Code and License Vote up 5 votes
public void testIsModuleCache() throws IOException {
    final FileObject wd = FileUtil.toFileObject(FileUtil.normalizeFile(getWorkDir()));
    final FileObject root1 = FileUtil.createFolder(wd, "root1");
    final FileObject module1 = createTestFile (root1, null, "module-info.java", "module Foo {}");    //NOI18N
    final FileObject java1 = createTestFile (root1, "org/nb/test", "Test.java", "package org.nb.test;\nclass Test {}");    //NOI18N
    final FileObject root2 = FileUtil.createFolder(wd, "root2");
    final FileObject java2 = createTestFile (root2, "org/nb/test", "Test2.java", "package org.nb.test;\nclass Test2 {}");    //NOI18N
    final Logger l = Logger.getLogger(DefaultSourceLevelQueryImpl.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        List<? extends Boolean> isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.TRUE, isMods.get(0));
        h.reset();
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        isMods = h.getIsModules();
        assertEquals(0, isMods.size());
        h.reset();
        assertEquals(
                JavaPlatform.getDefault().getSpecification().getVersion().toString(),
                SourceLevelQuery.getSourceLevel(java2));
        isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.FALSE, isMods.get(0));
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 3
Project: incubator-netbeans   File: DefaultSourceLevelQueryImplTest.java   Source Code and License Vote up 5 votes
public void testIsModuleCache_ModuleInfoDeleted() throws IOException {
    final FileObject wd = FileUtil.toFileObject(FileUtil.normalizeFile(getWorkDir()));
    final FileObject root1 = FileUtil.createFolder(wd, "root1");
    FileObject module1 = createTestFile (root1, null, "module-info.java", "module Foo {}");    //NOI18N
    final FileObject java1 = createTestFile (root1, "org/nb/test", "Test.java", "package org.nb.test;\nclass Test {}");    //NOI18N
    final Logger l = Logger.getLogger(DefaultSourceLevelQueryImpl.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        List<? extends Boolean> isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.TRUE, isMods.get(0));
        h.reset();
        module1.delete();
        assertEquals(
                JavaPlatform.getDefault().getSpecification().getVersion().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.FALSE, isMods.get(0));
        h.reset();
        assertEquals(
                JavaPlatform.getDefault().getSpecification().getVersion().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        isMods = h.getIsModules();
        assertEquals(0, isMods.size());
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 4
Project: incubator-netbeans   File: DefaultSourceLevelQueryImplTest.java   Source Code and License Vote up 5 votes
public void testIsModuleCache_ModuleInfoCreated() throws IOException {
    final FileObject wd = FileUtil.toFileObject(FileUtil.normalizeFile(getWorkDir()));
    final FileObject root1 = FileUtil.createFolder(wd, "root1");
    final FileObject java1 = createTestFile (root1, "org/nb/test", "Test.java", "package org.nb.test;\nclass Test {}");    //NOI18N
    final Logger l = Logger.getLogger(DefaultSourceLevelQueryImpl.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        assertEquals(
                JavaPlatform.getDefault().getSpecification().getVersion().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        List<? extends Boolean> isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.FALSE, isMods.get(0));
        h.reset();
        FileObject module1 = createTestFile (root1, null, "module-info.java", "module Foo {}");    //NOI18N
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        isMods = h.getIsModules();
        assertEquals(1, isMods.size());
        assertEquals(Boolean.TRUE, isMods.get(0));
        h.reset();
        assertEquals(
                expectedSourceLevel().toString(),
                SourceLevelQuery.getSourceLevel(java1));
        isMods = h.getIsModules();
        assertEquals(0, isMods.size());
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 5
Project: incubator-netbeans   File: DefaultClassPathProviderTest.java   Source Code and License Vote up 5 votes
public void testJavaPlatformCaching() throws IOException {
    FileObject artefact = getSourceFile (FILE_IN_PACKAGE);
    Lookup.getDefault().lookup(MockSLQ.class).register(this.srcRoot, new SpecificationVersion("9"));  //NOI18N
    assertEquals("9", SourceLevelQuery.getSourceLevel(artefact)); //NOI18N
    ClassPathProvider cpp = new DefaultClassPathProvider ();
    final Logger log = Logger.getLogger(DefaultClassPathProvider.class.getName());
    final H h = new H();
    final Level origLevel = log.getLevel();
    log.setLevel(Level.FINE);
    log.addHandler(h);
    try {
        ClassPath cp = cpp.findClassPath(artefact, JavaClassPathConstants.MODULE_BOOT_PATH);
        assertEquals ("DefaultClassPathProvider returned invalid classpath for MODULE_BOOT_PATH with source level 9",
                j9.getBootstrapLibraries(),
                cp);
        List<Optional<JavaPlatform>> plts = h.getCachedPlatforms();
        assertEquals(1, plts.size());
        assertEquals(j9, plts.get(0).get());
        h.reset();
        artefact = getSourceFile (FILE_IN_PACKAGE);
        cp = cpp.findClassPath(artefact, JavaClassPathConstants.MODULE_BOOT_PATH);
        assertEquals ("DefaultClassPathProvider returned invalid classpath for MODULE_BOOT_PATH with source level 9",
                j9.getBootstrapLibraries(),
                cp);
        plts = h.getCachedPlatforms();
        assertEquals(0, plts.size());
    } finally {
        log.removeHandler(h);
        log.setLevel(origLevel);
    }
}
 
Example 6
Project: incubator-netbeans   File: DefaultClassPathProviderTest.java   Source Code and License Vote up 5 votes
public void testLRUCaching() throws IOException {
    FileObject artefact = getSourceFile (FILE_IN_PACKAGE);
    Lookup.getDefault().lookup(MockSLQ.class).register(this.srcRoot, new SpecificationVersion("9"));  //NOI18N
    assertEquals("9", SourceLevelQuery.getSourceLevel(artefact)); //NOI18N
    ClassPathProvider cpp = new DefaultClassPathProvider ();
    final Logger log = Logger.getLogger(DefaultClassPathProvider.class.getName());
    final H h = new H();
    final Level origLevel = log.getLevel();
    log.setLevel(Level.FINE);
    log.addHandler(h);
    try {
        ClassPath cp = cpp.findClassPath(artefact, JavaClassPathConstants.MODULE_BOOT_PATH);
        assertEquals ("DefaultClassPathProvider returned invalid classpath for MODULE_BOOT_PATH with source level 9",
                j9.getBootstrapLibraries(),
                cp);
        List<JavaPlatform> plts = h.getLRU();
        assertEquals(1, plts.size());
        assertEquals(j9, plts.get(0));
        h.reset();
        artefact = getSourceFile (FILE_IN_PACKAGE);
        cp = cpp.findClassPath(artefact, JavaClassPathConstants.MODULE_BOOT_PATH);
        assertEquals ("DefaultClassPathProvider returned invalid classpath for MODULE_BOOT_PATH with source level 9",
                j9.getBootstrapLibraries(),
                cp);
        plts = h.getLRU();
        assertEquals(0, plts.size());
    } finally {
        log.removeHandler(h);
        log.setLevel(origLevel);
    }
}
 
Example 7
Project: ProjectAres   File: Logging.java   Source Code and License Vote up 5 votes
public static Level getEffectiveLevel(Logger logger) {
    if(logger.getLevel() != null) {
        return logger.getLevel();
    }

    if(logger.getParent() != null) {
        return getEffectiveLevel(logger.getParent());
    }

    return null;
}
 
Example 8
Project: openjdk-jdk10   File: TestAnonymousLogger.java   Source Code and License Vote up 5 votes
public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    Logger anonymous = Logger.getAnonymousLogger();

    final TestHandler handler = new TestHandler();
    final TestFilter filter = new TestFilter();
    final ResourceBundle bundle = ResourceBundle.getBundle(TestBundle.class.getName());
    anonymous.setLevel(Level.FINEST);
    anonymous.addHandler(handler);
    anonymous.setFilter(filter);
    anonymous.setUseParentHandlers(true);
    anonymous.setResourceBundle(bundle);

    if (anonymous.getLevel() != Level.FINEST) {
        throw new RuntimeException("Unexpected level: " + anonymous.getLevel());
    } else {
        System.out.println("Got expected level: " + anonymous.getLevel());
    }
    if (!Arrays.asList(anonymous.getHandlers()).contains(handler)) {
        throw new RuntimeException("Expected handler not found in: "
                + Arrays.asList(anonymous.getHandlers()));
    } else {
        System.out.println("Got expected handler in: " + Arrays.asList(anonymous.getHandlers()));
    }
    if (anonymous.getFilter() != filter) {
        throw new RuntimeException("Unexpected filter: " + anonymous.getFilter());
    } else {
        System.out.println("Got expected filter: " + anonymous.getFilter());
    }
    if (!anonymous.getUseParentHandlers()) {
        throw new RuntimeException("Unexpected flag: " + anonymous.getUseParentHandlers());
    } else {
        System.out.println("Got expected flag: " + anonymous.getUseParentHandlers());
    }
    if (anonymous.getResourceBundle() != bundle) {
        throw new RuntimeException("Unexpected bundle: " + anonymous.getResourceBundle());
    } else {
        System.out.println("Got expected bundle: " + anonymous.getResourceBundle());
    }
    try {
        anonymous.setParent(Logger.getLogger("foo.bar"));
        throw new RuntimeException("Expected SecurityException not raised!");
    } catch (SecurityException x) {
        System.out.println("Got expected exception: " + x);
    }
    if (anonymous.getParent() != Logger.getLogger("")) {
        throw new RuntimeException("Unexpected parent: " + anonymous.getParent());
    } else {
        System.out.println("Got expected parent: " + anonymous.getParent());
    }
}
 
Example 9
Project: incubator-netbeans   File: BaseFileObjectTestHid.java   Source Code and License Vote up 5 votes
public void testDeletedFileDoesNotReturnInputStream() throws Exception {
    final FileObject testFo = FileUtil.createData(root,"testfile.data");
    final File testFile = FileUtil.toFile(testFo);
    final Logger LOGGER = Logger.getLogger(FileObj.class.getName());
    final Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {
            if ("FileObj.getInputStream_after_is_valid".equals(record.getMessage())) {
                testFile.delete();
            }
        }
        @Override
        public void flush() {
        }
        @Override
        public void close() throws SecurityException {
        }
    };
    final Level originalLevel = LOGGER.getLevel();
    LOGGER.setLevel(Level.FINEST);
    try {
        LOGGER.addHandler(handler);
        try {
            testFo.getInputStream();
            assertTrue("Exception not thrown by deleted file getInputStream()", false);
        } catch (FileNotFoundException e) {
            //pass - expected exception
        } finally {
            LOGGER.removeHandler(handler);
        }
    } finally {
        LOGGER.setLevel(originalLevel);
    }
}
 
Example 10
Project: incubator-netbeans   File: ModuleNamesTest.java   Source Code and License Vote up 5 votes
static TraceHandler register() {
    final Logger l = Logger.getLogger(ModuleNames.class.getName());
    final Level origLevel = l.getLevel();
    final TraceHandler th = new TraceHandler(l, origLevel);
    l.setLevel(Level.FINE);
    l.addHandler(th);
    return th;
}
 
Example 11
Project: incubator-netbeans   File: EmbedderFactory.java   Source Code and License Vote up 5 votes
private Level levelOf(Logger log) {
    Level lvl = log.getLevel();
    if (lvl != null) {
        return lvl;
    } else {
        Logger par = log.getParent();
        if (par != null) {
            return levelOf(par);
        } else {
            return Level.INFO;
        }
    }
}
 
Example 12
Project: Open-DM   File: ConfigurationHelper.java   Source Code and License Vote up 5 votes
private static void setupLogging() {
    // Set logging level to at least WARNING for apache so that it doesn't log auth cookies.
    try {
        Logger apacheLogger = Logger.getLogger("org.apache");
        if (apacheLogger.getLevel() == null
                || apacheLogger.getLevel().intValue() < Level.WARNING.intValue()) {
            apacheLogger.setLevel(Level.WARNING);
        }
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Error setting logger level on apache logger!", e);
    }
}
 
Example 13
Project: incubator-netbeans   File: EncodingTest.java   Source Code and License Vote up 4 votes
public void testEncodingCaching() throws Exception {
    final Logger log = Logger.getLogger(HtmlDataObject.class.getName());
    class TestHandler extends Handler {
        private final Pattern pattern = Pattern.compile("^HtmlDataObject.getFileEncoding (non)?cached .*$");   //NOI18N
        Boolean cached;
        @Override
        public void publish(LogRecord record) {
            final String message = record.getMessage();
            final Matcher matcher = pattern.matcher(message);
            if (matcher.matches()) {
                cached = matcher.group(1) == null ? Boolean.TRUE : Boolean.FALSE;
            }
        }
        @Override
        public void flush() {
        }
        @Override
        public void close() throws SecurityException {
        }
    }
    final TestHandler handler = new TestHandler();
    log.addHandler(handler);
    final Level origLevel = log.getLevel();
    log.setLevel(Level.FINEST);
    try {
        FileObject data = FileUtil.createData (FileUtil.toFileObject(getWorkDir()), "UTF8.html"); //NOI18N
        copy("UTF8.html",data); //NOI18N
        handler.cached = null;
        read(data);
        assertFalse("Encoding should be calculated",handler.cached);    //NOI18N
        handler.cached = null;
        read(data);
        assertTrue("Encoding should be cached",handler.cached);        //NOI18N
        //Modify file
        copy("UTF8.html",data); //NOI18N
        handler.cached = null;
        read(data);
        assertFalse("Encoding should be calculated",handler.cached);    //NOI18N
        handler.cached = null;
        read(data);
        assertTrue("Encoding should be cached",handler.cached);        //NOI18N
    } finally {
        log.setLevel(origLevel);
        log.removeHandler(handler);
    }
}
 
Example 14
Project: incubator-netbeans   File: ModuleOraculumTest.java   Source Code and License Vote up 4 votes
public void testRootCache() {
    Lookup.getDefault().lookup(CPP.class)
            .add(
                    root1,
                    ClassPath.SOURCE,
                    ClassPathSupport.createClassPath(root1))
            .add(
                    root2,
                    ClassPath.SOURCE,
                    ClassPathSupport.createClassPath(root2));
    final Logger l = Logger.getLogger(ModuleOraculum.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
        final JavacParser parser = new JavacParser(Collections.emptyList(), true);
        JavacTaskImpl impl = JavacParser.createJavacTask(
                javaFile1,
                null,
                cpInfo,
                parser,
                null,
                false);
        assertEquals("Test", Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        List<? extends FileObject> roots = h.getRoots();
        assertEquals(1, roots.size());
        assertEquals(root1, roots.get(0));
        h.reset();
        impl = JavacParser.createJavacTask(
                javaFile1,
                null,
                cpInfo,
                parser,
                null,
                false);
        assertEquals("Test", Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        roots = h.getRoots();
        assertEquals(0, roots.size());
        impl = JavacParser.createJavacTask(
                javaFile2,
                null,
                cpInfo,
                parser,
                null,
                false);
        assertEquals("Next", Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        roots = h.getRoots();
        assertEquals(1, roots.size());
        assertEquals(root2, roots.get(0));
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 15
Project: 484_P7_1-Java   File: TestTableDetection.java   Source Code and License Vote up 4 votes
@BeforeClass
public static void disableLogging() {
    Logger pdfboxLogger = Logger.getLogger("org.apache.pdfbox");
    defaultLogLevel = pdfboxLogger.getLevel();
    pdfboxLogger.setLevel(Level.OFF);
}
 
Example 16
Project: jdk8u-jdk   File: TestAnonymousLogger.java   Source Code and License Vote up 4 votes
public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    Logger anonymous = Logger.getAnonymousLogger();

    final TestHandler handler = new TestHandler();
    final TestFilter filter = new TestFilter();
    final ResourceBundle bundle = ResourceBundle.getBundle(TestBundle.class.getName());
    anonymous.setLevel(Level.FINEST);
    anonymous.addHandler(handler);
    anonymous.setFilter(filter);
    anonymous.setUseParentHandlers(true);
    anonymous.setResourceBundle(bundle);

    if (anonymous.getLevel() != Level.FINEST) {
        throw new RuntimeException("Unexpected level: " + anonymous.getLevel());
    } else {
        System.out.println("Got expected level: " + anonymous.getLevel());
    }
    if (!Arrays.asList(anonymous.getHandlers()).contains(handler)) {
        throw new RuntimeException("Expected handler not found in: "
                + Arrays.asList(anonymous.getHandlers()));
    } else {
        System.out.println("Got expected handler in: " + Arrays.asList(anonymous.getHandlers()));
    }
    if (anonymous.getFilter() != filter) {
        throw new RuntimeException("Unexpected filter: " + anonymous.getFilter());
    } else {
        System.out.println("Got expected filter: " + anonymous.getFilter());
    }
    if (!anonymous.getUseParentHandlers()) {
        throw new RuntimeException("Unexpected flag: " + anonymous.getUseParentHandlers());
    } else {
        System.out.println("Got expected flag: " + anonymous.getUseParentHandlers());
    }
    if (anonymous.getResourceBundle() != bundle) {
        throw new RuntimeException("Unexpected bundle: " + anonymous.getResourceBundle());
    } else {
        System.out.println("Got expected bundle: " + anonymous.getResourceBundle());
    }
    try {
        anonymous.setParent(Logger.getLogger("foo.bar"));
        throw new RuntimeException("Expected SecurityException not raised!");
    } catch (SecurityException x) {
        System.out.println("Got expected exception: " + x);
    }
    if (anonymous.getParent() != Logger.getLogger("")) {
        throw new RuntimeException("Unexpected parent: " + anonymous.getParent());
    } else {
        System.out.println("Got expected parent: " + anonymous.getParent());
    }
}
 
Example 17
Project: incubator-netbeans   File: ModuleOraculumTest.java   Source Code and License Vote up 4 votes
public void testModuleNameCache_ModuleInfoDeleted() throws IOException {
    final Logger l = Logger.getLogger(ModuleOraculum.class.getName());
    final Level origLogLevel = l.getLevel();
    final H h = new H();
    l.setLevel(Level.FINE);
    l.addHandler(h);
    try {
        final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
        final JavacParser parser = new JavacParser(Collections.emptyList(), true);
        JavacTaskImpl impl = JavacParser.createJavacTask(
                javaFile1,
                root1,
                cpInfo,
                parser,
                null,
                false);
        assertEquals("Test", Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        List<? extends String> names = h.getModuleNames();
        assertEquals(1, names.size());
        assertEquals("Test", names.get(0)); //NOI18N
        h.reset();
        moduleFile1.delete();
        impl = JavacParser.createJavacTask(
                javaFile1,
                root1,
                cpInfo,
                parser,
                null,
                false);
        assertNull(Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        names = h.getModuleNames();
        assertEquals(1, names.size());
        assertEquals(null, names.get(0));
        h.reset();
        impl = JavacParser.createJavacTask(
                javaFile1,
                root1,
                cpInfo,
                parser,
                null,
                false);
        assertNull(Options.instance(impl.getContext()).get("-XD-Xmodule:"));    //NOI18N
        names = h.getModuleNames();
        assertEquals(0, names.size());
    } finally {
        l.removeHandler(h);
        l.setLevel(origLogLevel);
    }
}
 
Example 18
Project: incubator-netbeans   File: Log.java   Source Code and License Vote up 4 votes
/** Enables logging for given logger name and given severity.
 * Everything logged to the object is going to go to the returned
 * CharSequence object which can be used to check the content or
 * converted <code>toString</code>.
 * <p>
 * The logging stops when the returned object is garbage collected.
 *
 * @param loggerName the name to capture logging for
 * @param level the level of details one wants to get
 * @return character sequence which can be check or converted to string
 * @since 1.27
 */
public static CharSequence enable(String loggerName, Level level) {
    IL il = new IL(false);
    class MyPs extends PrintStream implements CharSequence {
        private ByteArrayOutputStream os;

        public MyPs() {
            this(new ByteArrayOutputStream());
        }

        private MyPs(ByteArrayOutputStream arr) {
            super(arr);
            os = arr;
        }

        public int length() {
            return toString().length();
        }

        public char charAt(int index) {
            return toString().charAt(index);
        }

        public CharSequence subSequence(int start, int end) {
            return toString().subSequence(start, end);
        }

        @Override
        public String toString() {
            return os.toString();
        }
    }

    Logger l = Logger.getLogger(loggerName);
    if (l.getLevel() == null || l.getLevel().intValue() > level.intValue()) {
        l.setLevel(level);
    }
    MyPs ps = new MyPs();
    Log log = new Log(l, ps);
    log.setLevel(level);
    l.addHandler(log);
    return ps;
}
 
Example 19
Project: incubator-netbeans   File: ActiveConfigActionTest.java   Source Code and License Vote up 4 votes
/**
 * Test of createContextAwareInstance method, of class ActiveConfigAction.
 */
@RandomlyFails
public void testCreateContextAwareInstance() throws Exception {
    P p = PF.toCreate;
    
    OpenProjects.getDefault().open(new Project[] { p }, false);
    OpenProjects.getDefault().setMainProject(p);
    ActiveConfigAction instance = new ActiveConfigAction();
    Action result = instance;//.createContextAwareInstance(actionContext);
    
    assertTrue("menu: " + result, result instanceof Presenter.Menu);
    Presenter.Menu menu = (Presenter.Menu)result;
    
    JMenuItem item = menu.getMenuPresenter();
    
    
    assertNotNull(item);
    assertTrue("Enabled", item.isEnabled());
    final DynamicMenuContent m = (DynamicMenuContent)item;
    assertEquals("One", 1, m.getMenuPresenters().length);
    
    holder = item;
    
    OpenProjects.getDefault().close(new Project[] { p });
    assertNull("NO project selected", OpenProjects.getDefault().getMainProject());
    
    WeakReference<Object> ref = new WeakReference<Object>(p);
    p = null;
    PF.toCreate = null;

    //Await refresh
    final Logger log = Logger.getLogger(ActiveConfigAction.class.getName());
    final Level origLogLevel = log.getLevel();
    final FH handler = new FH();
    log.setLevel(Level.FINEST);
    log.addHandler(handler);
    try {
        handler.get(30, TimeUnit.SECONDS);
    } finally {
        log.setLevel(origLogLevel);
        log.removeHandler(handler);
    }
    //Rebuild the current pop up menu
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            m.getMenuPresenters();
        }
    });
    assertGC("Reference can go away", ref);
}
 
Example 20
Project: incubator-netbeans   File: Log.java   Source Code and License Vote up 3 votes
/** Starts to listen on given log and collect parameters of messages that
 * were send to it. This is supposed to be called at the beginning of a test,
 * to get messages from the programs that use 
 * <a href="http://wiki.netbeans.org/wiki/view/FitnessViaTimersCounters">timers/counters</a>
 * infrastructure. At the end one should call {@link #assertInstances}.
 * 
 * 
 * @param log logger to listen on, if null, it uses the standard timers/counters one
 * @param msg name of messages to collect, if null, all messages will be recorded
 * @param level level of messages to record
 * @since 1.44
 */
public static void enableInstances(Logger log, String msg, Level level) {
    if (log == null) {
        log = Logger.getLogger("TIMER"); // NOI18N
    }
    
    log.addHandler(new InstancesHandler(msg, level));
    
    if (log.getLevel() == null || log.getLevel().intValue() > level.intValue()) {
        log.setLevel(level);
    }
}