<commit_before>/*
 * $Id$
 */
package org.xins.tests.server;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.xins.server.AccessRule;
import org.xins.util.text.ParseException;

/**
 * Tests for class <code>AccessRule</code>.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class AccessRuleTests extends TestCase {

   //-------------------------------------------------------------------------
   // Class functions
   //-------------------------------------------------------------------------

   /**
    * Returns a test suite with all test cases defined by this class.
    *
    * @return
    *    the test suite, never <code>null</code>.
    */
   public static Test suite() {
      return new TestSuite(AccessRuleTests.class);
   }


   //-------------------------------------------------------------------------
   // Class fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Constructor
   //-------------------------------------------------------------------------

   /**
    * Constructs a new <code>AccessRuleTests</code> test suite with
    * the specified name. The name will be passed to the superconstructor.
    *
    * @param name
    *    the name for this test suite.
    */
   public AccessRuleTests(String name) {
      super(name);
   }


   //-------------------------------------------------------------------------
   // Fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------

   /**
    * Performs setup for the tests.
    */
   protected void setUp() {
      // empty
   }

   public void testParseAccessRule() throws Throwable {

      try {
         AccessRule.parseAccessRule(null);
         fail("AccessRule.parseAccessRule(null) should throw an IllegalArgumentException.");
      } catch (IllegalArgumentException exception) {
         // as expected
      }
   }
}<commit_msg>Extended set of tests.<commit_after>/*
 * $Id$
 */
package org.xins.tests.server;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.xins.server.AccessRule;
import org.xins.server.IPFilter;
import org.xins.util.text.FastStringBuffer;
import org.xins.util.text.ParseException;

/**
 * Tests for class <code>AccessRule</code>.
 *
 * @version $Revision$ $Date$
 * @author <NAME> (<a href="mailto:<EMAIL>"><EMAIL></a>)
 */
public class AccessRuleTests extends TestCase {

   //-------------------------------------------------------------------------
   // Class functions
   //-------------------------------------------------------------------------

   /**
    * Returns a test suite with all test cases defined by this class.
    *
    * @return
    *    the test suite, never <code>null</code>.
    */
   public static Test suite() {
      return new TestSuite(AccessRuleTests.class);
   }


   //-------------------------------------------------------------------------
   // Class fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Constructor
   //-------------------------------------------------------------------------

   /**
    * Constructs a new <code>AccessRuleTests</code> test suite with
    * the specified name. The name will be passed to the superconstructor.
    *
    * @param name
    *    the name for this test suite.
    */
   public AccessRuleTests(String name) {
      super(name);
   }


   //-------------------------------------------------------------------------
   // Fields
   //-------------------------------------------------------------------------

   //-------------------------------------------------------------------------
   // Methods
   //-------------------------------------------------------------------------

   /**
    * Performs setup for the tests.
    */
   protected void setUp() {
      // empty
   }

   public void testParseAccessRule() throws Throwable {

      try {
         AccessRule.parseAccessRule(null);
         fail("AccessRule.parseAccessRule(null) should throw an IllegalArgumentException.");
      } catch (IllegalArgumentException exception) {
         // as expected
      }

      doTestParseAccessRule("1.2.3.4");
   }

   private void doTestParseAccessRule(String ip)
   throws Throwable {
      for (int mask = 0; mask <= 32; mask++) {

         doTestParseAccessRule(false, ip, mask);
         doTestParseAccessRule(true, ip, mask);
      }
   }

   private void doTestParseAccessRule(boolean allow, String ip, int mask)
   throws Throwable {
      doTestParseAccessRule(allow, ip, mask, " ",        " ");
      doTestParseAccessRule(allow, ip, mask, "\t",       "\t");
      doTestParseAccessRule(allow, ip, mask, " ",        "\t");
      doTestParseAccessRule(allow, ip, mask, " \t\n\r ", "\t\n\r");
      doTestParseAccessRule(allow, ip, mask, "\n ",      "\r");
   }

   private void doTestParseAccessRule(boolean allow, String ip, int mask, String whitespace1, String whitespace2)
   throws Throwable {

      FastStringBuffer buffer = new FastStringBuffer(250);
      if (allow) {
         buffer.append("allow");
      } else {
         buffer.append("deny");
      }
      buffer.append(whitespace1);
      buffer.append(ip);
      buffer.append('/');
      buffer.append(mask);
      buffer.append(whitespace2);
      buffer.append("*");

      AccessRule rule = AccessRule.parseAccessRule(buffer.toString());
      assertNotNull(rule);
      assertEquals(allow, rule.isAllowRule());

      IPFilter ipFilter = rule.getIPFilter();
      assertNotNull(ipFilter);

      assertEquals(ip,   ipFilter.getBaseIP());
      assertEquals(mask, ipFilter.getMask());
   }
}
{'fraction_non_alphanumeric': 0.33898584129074744, 'fraction_numerical': 0.0023049061573921633, 'mean_word_length': 1.1626913492346032, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '974874', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1398, 'n_words': 338}