void doTest(String filename, Integer expectedInt, Class<?> exceptionClass) throws Exception {

    rulesMap.put(new ElementSelector("test"), new NOPAction());
    rulesMap.put(new ElementSelector("test/badBegin"), new BadBeginAction());
    rulesMap.put(new ElementSelector("test/badBegin/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    rulesMap.put(new ElementSelector("test/isolate"), new NOPAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/isolate/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
    tc.setContext(context);
    tc.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/skip/" + filename);

    String str = context.getProperty(HelloAction.PROPERTY_KEY);
    assertEquals("Hello John Doe.", str);

    Integer i = (Integer) context.getObject(TouchAction.KEY);
    if (expectedInt == null) {
      assertNull(i);
    } else {
      assertEquals(expectedInt, i);
    }

    // check the existence of an ERROR status
    List<Status> statusList = sm.getCopyOfStatusList();
    Status s0 = statusList.get(0);
    assertEquals(Status.ERROR, s0.getLevel());
    assertTrue(s0.getThrowable().getClass() == exceptionClass);
  }
 @Test
 public void noName() {
   atts.setValue("value", "v1");
   propertyAction.begin(ec, null, atts);
   assertEquals(1, context.getStatusManager().getCount());
   assertTrue(checkError());
 }
Beispiel #3
0
 /**
  * Returns true if the StatusManager associated with the context passed as parameter has one or
  * more StatusListener instances registered. Returns false otherwise.
  *
  * @param context
  * @return true if one or more StatusListeners registered, false otherwise
  * @since 1.0.8
  */
 public static boolean contextHasStatusListener(Context context) {
   StatusManager sm = context.getStatusManager();
   if (sm == null) return false;
   List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
   if (listeners == null || listeners.size() == 0) return false;
   else return true;
 }
 @Test
 public void testLoadNotPossible() {
   atts.setValue("file", "toto");
   propertyAction.begin(ec, null, atts);
   assertEquals(1, context.getStatusManager().getCount());
   assertTrue(checkFileErrors());
 }
 @Test
 public void noAttributes() {
   propertyAction.begin(ec, null, atts);
   assertEquals(1, context.getStatusManager().getCount());
   assertTrue(checkError());
   StatusPrinter.print(context);
 }
  @Test
  public void testAppendFailure() {
    appender.start();

    // make sure the append method does not work
    appender.topicPublisher = null;

    ILoggingEvent le = createLoggingEvent();
    for (int i = 1; i <= 3; i++) {
      appender.append(le);
      assertEquals(i, context.getStatusManager().getCount());
      assertTrue(appender.isStarted());
    }
    appender.append(le);
    assertEquals(4, context.getStatusManager().getCount());
    assertFalse(appender.isStarted());
  }
 @Test
 public void nameValuePairWithPrerequisiteSubsitution() {
   context.putProperty("w", "wor");
   atts.setValue("name", "v1");
   atts.setValue("value", "${w}k");
   propertyAction.begin(ec, null, atts);
   assertEquals("work", ec.getProperty("v1"));
 }
 @Test
 public void noClass() throws JoranException {
   simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NOCLASS_XML);
   String inContextFoo = context.getProperty("foo");
   assertNull(inContextFoo);
   checker.assertContainsMatch(
       Status.ERROR, "Missing class name for property definer. Near \\[define\\] line 1");
 }
 @Test
 public void testLoadResourceWithPrerequisiteSubsitution() {
   context.putProperty("STEM", "asResource/joran");
   atts.setValue("resource", "${STEM}/propertyActionTest.properties");
   propertyAction.begin(ec, null, atts);
   assertEquals("tata", ec.getProperty("r1"));
   assertEquals("toto", ec.getProperty("r2"));
 }
 @Test
 public void testLoadFileWithPrerequisiteSubsitution() {
   context.putProperty("STEM", CoreTestConstants.TEST_SRC_PREFIX + "input/joran");
   atts.setValue("file", "${STEM}/propertyActionTest.properties");
   propertyAction.begin(ec, null, atts);
   assertEquals("tata", ec.getProperty("v1"));
   assertEquals("toto", ec.getProperty("v2"));
 }
 @Test
 public void testFileNotLoaded() {
   atts.setValue("file", "toto");
   atts.setValue("value", "work");
   propertyAction.begin(ec, null, atts);
   assertEquals(1, context.getStatusManager().getCount());
   assertTrue(checkError());
 }
Beispiel #12
0
 public static void addStatus(Context context, Status status) {
   if (context == null) {
     return;
   }
   StatusManager sm = context.getStatusManager();
   if (sm != null) {
     sm.add(status);
   }
 }
 @Test
 public void testBadClass() throws JoranException {
   simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + BADCLASS_XML);
   // get from context
   String inContextFoo = context.getProperty("foo");
   assertNull(inContextFoo);
   // check context errors
   checker.assertContainsMatch(Status.ERROR, "Could not create an PropertyDefiner of type");
 }
 @Test
 public void noName() throws JoranException {
   simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NONAME_XML);
   // get from context
   String inContextFoo = context.getProperty("foo");
   assertNull(inContextFoo);
   // check context errors
   checker.assertContainsMatch(
       Status.ERROR, "Missing property name for property definer. Near \\[define\\] line 1");
 }
  @Test
  public void testProcessingOfTextWithEntityCharacters() throws Exception {
    List<StaxEvent> seList = doTest("ampEvent.xml");
    StatusManager sm = context.getStatusManager();
    assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
    // dump(seList);
    assertEquals(3, seList.size());

    BodyEvent be = (BodyEvent) seList.get(1);
    assertEquals("xxx & yyy", be.getText());
  }
 @Test
 public void testAttributeProcessing() throws Exception {
   List<StaxEvent> seList = doTest("inc.xml");
   StatusManager sm = context.getStatusManager();
   assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
   assertEquals(4, seList.size());
   StartEvent se = (StartEvent) seList.get(1);
   Attribute attr = se.getAttributeByName("increment");
   assertNotNull(attr);
   assertEquals("1", attr.getValue());
 }
 @Test
 public void testNoStart() {
   Appender<E> appender = getAppender();
   appender.setContext(context);
   appender.setName("doh");
   // is null OK?
   appender.doAppend(null);
   StatusChecker checker = new StatusChecker(context.getStatusManager());
   StatusPrinter.print(context);
   assertTrue(checker.containsMatch("Attempted to append to non started appender \\[doh\\]."));
 }
  private void executeHarness(int duration, boolean withDelay) throws InterruptedException {
    MultiThreadedHarness multiThreadedHarness = new MultiThreadedHarness(duration);
    this.runnableArray = buildRunnableArray(withDelay);
    multiThreadedHarness.execute(runnableArray);

    StatusChecker checker = new StatusChecker(context.getStatusManager());
    if (!checker.isErrorFree(0)) {
      StatusPrinter.print(context);
      fail("errors reported");
    }
  }
  @Test
  public void propertiesInContextAreIncluded() throws Exception {
    Map<String, String> propertyMap = new HashMap<String, String>();
    propertyMap.put("thing_one", "One");
    propertyMap.put("thing_two", "Three");

    final Context context = mock(Context.class);
    when(context.getCopyOfPropertyMap()).thenReturn(propertyMap);

    IAccessEvent event = mockBasicILoggingEvent();

    encoder.setContext(context);
    encoder.doEncode(event);
    closeQuietly(outputStream);

    JsonNode node = MAPPER.readTree(outputStream.toByteArray());

    assertThat(node.get("thing_one").textValue(), is("One"));
    assertThat(node.get("thing_two").textValue(), is("Three"));
  }
 public void addStatus(Status status) {
   if (context == null) {
     if (noContextWarning++ == 0) {
       System.out.println("LOGBACK: No context given for " + this);
     }
     return;
   }
   StatusManager sm = context.getStatusManager();
   if (sm != null) {
     sm.add(status);
   }
 }
 @Test
 public void illFormedXML() {
   TrivialStatusListener tsl = new TrivialStatusListener();
   tsl.start();
   String filename = CoreTestConstants.TEST_DIR_PREFIX + "input/joran/" + "illformed.xml";
   context.getStatusManager().add(tsl);
   try {
     doTest(filename);
   } catch (Exception e) {
   }
   assertTrue(tsl.list.size() >= 1);
   Status s0 = tsl.list.get(0);
   assertTrue(s0.getMessage().startsWith(CoreConstants.XML_PARSING));
 }
 @Test
 public void inexistentFile() {
   TrivialStatusListener tsl = new TrivialStatusListener();
   tsl.start();
   String filename = CoreTestConstants.TEST_DIR_PREFIX + "input/joran/" + "nothereBLAH.xml";
   context.getStatusManager().add(tsl);
   try {
     doTest(filename);
   } catch (Exception e) {
     assertTrue(e.getMessage().startsWith("Could not open ["));
   }
   assertTrue(tsl.list.size() + " should be greater than or equal to 1", tsl.list.size() >= 1);
   Status s0 = tsl.list.get(0);
   assertTrue(s0.getMessage().startsWith("Could not open ["));
 }
  @Test
  public void testBuildEnvPropertiesWithNullCredentials() {
    appender.setInitialContextFactoryName("icfn");
    appender.setProviderURL("url");
    appender.setURLPkgPrefixes("pkgPref");
    appender.setSecurityPrincipalName("user");
    appender.setSecurityCredentials(null);

    Properties props = appender.buildEnvProperties();
    assertEquals(4, props.size());
    assertEquals(
        appender.getInitialContextFactoryName(),
        props.getProperty(Context.INITIAL_CONTEXT_FACTORY));
    assertEquals(appender.getProviderURL(), props.getProperty(Context.PROVIDER_URL));
    assertEquals(appender.getURLPkgPrefixes(), props.getProperty(Context.URL_PKG_PREFIXES));
    assertEquals(
        appender.getSecurityPrincipalName(), props.getProperty(Context.SECURITY_PRINCIPAL));
    assertEquals(null, props.getProperty(Context.SECURITY_CREDENTIALS));

    assertEquals(1, context.getStatusManager().getCount());
  }
 public StatusManager getStatusManager() {
   if (context == null) {
     return null;
   }
   return context.getStatusManager();
 }
Beispiel #25
0
 public StatusChecker(Context context) {
   this.sm = context.getStatusManager();
 }
 private boolean checkError() {
   Iterator it = context.getStatusManager().getCopyOfStatusList().iterator();
   ErrorStatus es = (ErrorStatus) it.next();
   return PropertyAction.INVALID_ATTRIBUTES.equals(es.getMessage());
 }
 private boolean checkFileErrors() {
   Iterator it = context.getStatusManager().getCopyOfStatusList().iterator();
   ErrorStatus es1 = (ErrorStatus) it.next();
   return "Could not find properties file [toto].".equals(es1.getMessage());
 }
/**
 * Test the way Interpreter skips child elements in case of exceptions thrown by Actions. It also
 * tests addition of status messages in case of exceptions.
 *
 * @author Ceki Gulcu
 */
public class SkippingInInterpreterTest {

  HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
  Context context = new ContextBase();
  StatusManager sm = context.getStatusManager();

  SAXParser createParser() throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    return spf.newSAXParser();
  }

  void doTest(String filename, Integer expectedInt, Class<?> exceptionClass) throws Exception {

    rulesMap.put(new ElementSelector("test"), new NOPAction());
    rulesMap.put(new ElementSelector("test/badBegin"), new BadBeginAction());
    rulesMap.put(new ElementSelector("test/badBegin/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    rulesMap.put(new ElementSelector("test/isolate"), new NOPAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/isolate/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
    tc.setContext(context);
    tc.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/skip/" + filename);

    String str = context.getProperty(HelloAction.PROPERTY_KEY);
    assertEquals("Hello John Doe.", str);

    Integer i = (Integer) context.getObject(TouchAction.KEY);
    if (expectedInt == null) {
      assertNull(i);
    } else {
      assertEquals(expectedInt, i);
    }

    // check the existence of an ERROR status
    List<Status> statusList = sm.getCopyOfStatusList();
    Status s0 = statusList.get(0);
    assertEquals(Status.ERROR, s0.getLevel());
    assertTrue(s0.getThrowable().getClass() == exceptionClass);
  }

  @Test
  public void testSkippingRuntimeExInBadBegin() throws Exception {
    doTest("badBegin1.xml", null, IllegalStateException.class);
  }

  @Test
  public void testSkippingActionExInBadBegin() throws Exception {
    doTest("badBegin2.xml", null, ActionException.class);
  }

  @Test
  public void testSkippingRuntimeExInBadEnd() throws Exception {
    doTest("badEnd1.xml", new Integer(2), IllegalStateException.class);
  }

  @Test
  public void testSkippingActionExInBadEnd() throws Exception {
    doTest("badEnd2.xml", new Integer(2), ActionException.class);
  }
}