alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (LogTest.java)

This example Android source code file (LogTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, iterations, log_tag, logtest, os, override, performancetest, performancetestcase, property_tag, string, suppress, test, testcase, util, utilities, utils, verbose

The LogTest.java Android example source code

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.util;

import junit.framework.Assert;
import junit.framework.TestCase;

import android.os.SystemProperties;
import android.test.PerformanceTestCase;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;

//This is an empty TestCase.
@Suppress
public class LogTest extends TestCase {
    private static final String PROPERTY_TAG = "log.tag.LogTest";
    private static final String LOG_TAG = "LogTest";


    // TODO: remove this test once we uncomment out the following test.
    public void testLogTestDummy() {
      return;
    }


    /* TODO: This test is commented out because we will not be able to set properities. Fix the test.
    public void testIsLoggable() {
        // First clear any SystemProperty setting for our test key.
        SystemProperties.set(PROPERTY_TAG, null);
        
        String value = SystemProperties.get(PROPERTY_TAG);
        Assert.assertTrue(value == null || value.length() == 0);
        
        // Check to make sure that all levels expect for INFO, WARN, ERROR, and ASSERT are loggable. 
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be VERBOSE for this tag.
        SystemProperties.set(PROPERTY_TAG, "VERBOSE");
        
        // Test to make sure all log levels >= VERBOSE are loggable.
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be DEBUG for this tag.
        SystemProperties.set(PROPERTY_TAG, "DEBUG");
        
        // Test to make sure all log levels >= DEBUG are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be INFO for this tag.
        SystemProperties.set(PROPERTY_TAG, "INFO");
        
        // Test to make sure all log levels >= INFO are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be WARN for this tag.
        SystemProperties.set(PROPERTY_TAG, "WARN");
        
        // Test to make sure all log levels >= WARN are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be ERROR for this tag.
        SystemProperties.set(PROPERTY_TAG, "ERROR");
        
        // Test to make sure all log levels >= ERROR are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be ASSERT for this tag.
        SystemProperties.set(PROPERTY_TAG, "ASSERT");
        
        // Test to make sure all log levels >= ASSERT are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertTrue(Log.isLoggable(LOG_TAG, Log.ASSERT));
        
        // Set the log level to be SUPPRESS for this tag.
        SystemProperties.set(PROPERTY_TAG, "SUPPRESS");
        
        // Test to make sure all log levels >= ASSERT are loggable.
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.VERBOSE));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.DEBUG));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.INFO));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.WARN));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.ERROR));
        Assert.assertFalse(Log.isLoggable(LOG_TAG, Log.ASSERT));
    }
    */
    
    public static class PerformanceTest extends TestCase implements PerformanceTestCase {
        private static final int ITERATIONS = 1000;

        @Override
        public void setUp() {
            SystemProperties.set(LOG_TAG, "VERBOSE");
        }
        
        public boolean isPerformanceOnly() {
            return true;
        }
        
        public int startPerformance(PerformanceTestCase.Intermediates intermediates) {
            intermediates.setInternalIterations(ITERATIONS * 10);
            return 0;
        }

        public void testIsLoggable() {
            boolean canLog = false;
            for (int i = ITERATIONS - 1; i >= 0; i--) {
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
                canLog = Log.isLoggable(LOG_TAG, Log.VERBOSE);
            }
        }
    }
}

Other Android examples (source code examples)

Here is a short list of links related to this Android LogTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.