FileDocCategorySizeDatePackage
StressCategory.javaAPI DocApache log4j 1.2.156059Sat Aug 25 00:09:34 BST 2007org.apache.log4j

StressCategory

public class StressCategory extends Object

Fields Summary
static Level[]
level
static Level
defaultLevel
static int
LENGTH
static String[]
names
static Logger[]
cat
static CT[]
ct
static Random
random
Constructors Summary
Methods Summary
static voidcatDump()

    for(int j = 0; j < LENGTH; j++) {
      if(cat[j] != null)
	System.out.println("cat[" + j + "] = (" + cat[j].name + "," +
			   cat[j].getLevel() + ")");
      else
	System.out.println("cat[" + j + "] = undefined"); 
    }
  
static booleancheckCorrectness(int i)

    CT localCT = ct[i];

    // Can't perform test if logger is not instantiated
    if(localCT == null) 
      return true;
    
    // find expected level
    Level expected = getExpectedPrioriy(localCT);

			    
    Level purported = cat[i].getEffectiveLevel();

    if(expected != purported) {
      System.out.println("Expected level for " + localCT.catstr + " is " +
		       expected);
      System.out.println("Purported level for "+ cat[i].name + " is "+purported);
      return false;
    }
    return true;
      
  
static voidcreateLoop(int n)

    if(n == LENGTH) {  
      //System.out.println("..............Creating cat[]...........");
      for(int i = 0; i < LENGTH; i++) {
	if(ct[i] == null)
	  cat[i] = null;
	else {
	  cat[i] = Logger.getLogger(ct[i].catstr);
	  cat[i].setLevel(ct[i].level);
	}
      }
      test();
      // Clear hash table for next round
      Hierarchy h = (Hierarchy) LogManager.getLoggerRepository();
      h.clear();
    }
    else {      
      ct[n]  = null;
      createLoop(n+1);  

      ct[n]  = new CT(names[n], null);
      createLoop(n+1);  
      
      int r = random.nextInt(); if(r < 0) r = -r;
      ct[n]  = new CT(names[n], level[r%5]);
      createLoop(n+1);
    }
  
static voidctDump()

    for(int j = 0; j < LENGTH; j++) {
       if(ct[j] != null) 
	    System.out.println("ct [" + j + "] = ("+ct[j].catstr+"," + 
			       ct[j].level + ")");
       else 
	 System.out.println("ct [" + j + "] = undefined");
    }
  
static org.apache.log4j.LevelgetExpectedPrioriy(org.apache.log4j.StressCategory$CT ctParam)

    Level level = ctParam.level;
    if(level != null) 
      return level;

    
    String catstr = ctParam.catstr;    
    
    for(int i = catstr.lastIndexOf('.", catstr.length()-1); i >= 0; 
	                              i = catstr.lastIndexOf('.", i-1))  {
      String substr = catstr.substring(0, i);

      // find the level of ct corresponding to substr
      for(int j = 0; j < LENGTH; j++) {	
	if(ct[j] != null && substr.equals(ct[j].catstr)) {
	  Level p = ct[j].level;
	  if(p != null) 
	    return p;	  
	}
      }
    }
    return defaultLevel;
  
public static voidmain(java.lang.String[] args)


       
    
    LENGTH = args.length;

    if(LENGTH == 0) {
      System.err.println( "Usage: java " + StressCategory.class.getName() +
			  " name1 ... nameN\n.");      
      System.exit(1);
    }
    if(LENGTH >= 7) {
      System.err.println(
        "This stress test suffers from combinatorial explosion.\n"+
        "Invoking with seven arguments takes about 90 minutes even on fast machines");
    }

    names = new String[LENGTH];
    for(int i=0; i < LENGTH; i++) {
      names[i] = args[i];
    }    
    cat = new Logger[LENGTH];
    ct = new CT[LENGTH]; 


    permute(0); 

    // If did not exit, then passed all tests.
  
public static voidpermutationDump()

    System.out.print("Current permutation is - ");
    for(int i = 0; i < LENGTH; i++) {
      System.out.print(names[i] + " ");
    }
    System.out.println();
  
static voidpermute(int n)

    if(n == LENGTH)
      createLoop(0);
    else
      for(int i = n; i < LENGTH; i++) {
	swap(names, n, i);
	permute(n+1);
	swap(names, n, i);	
      }
  
static voidswap(java.lang.String[] names, int i, int j)

    String t = names[i];
    names[i] = names[j];
    names[j] = t;
  
static voidtest()

    
    //System.out.println("++++++++++++TEST called+++++++++++++");
    //permutationDump();
    //catDump();

    for(int i = 0; i < LENGTH; i++) {
      if(!checkCorrectness(i)) {
	System.out.println("Failed stress test.");
	permutationDump();
	
	//Hierarchy._default.fullDump();
	ctDump();
	catDump();
	System.exit(1);
      }
    }