Ejemplo n.º 1
0
        public virtual void Mdc()
        {
            HttpServletRequest request = Org.Mockito.Mockito.Mock <HttpServletRequest>();

            Org.Mockito.Mockito.When(request.GetUserPrincipal()).ThenReturn(null);
            Org.Mockito.Mockito.When(request.GetMethod()).ThenReturn("METHOD");
            Org.Mockito.Mockito.When(request.GetPathInfo()).ThenReturn("/pathinfo");
            ServletResponse response = Org.Mockito.Mockito.Mock <ServletResponse>();
            AtomicBoolean   invoked  = new AtomicBoolean();
            FilterChain     chain    = new _FilterChain_55(invoked);

            MDC.Clear();
            Filter filter = new MDCFilter();

            filter.Init(null);
            filter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.IsTrue(invoked.Get());
            NUnit.Framework.Assert.IsNull(MDC.Get("hostname"));
            NUnit.Framework.Assert.IsNull(MDC.Get("user"));
            NUnit.Framework.Assert.IsNull(MDC.Get("method"));
            NUnit.Framework.Assert.IsNull(MDC.Get("path"));
            Org.Mockito.Mockito.When(request.GetUserPrincipal()).ThenReturn(new _Principal_78
                                                                                ());
            invoked.Set(false);
            chain = new _FilterChain_86(invoked);
            filter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.IsTrue(invoked.Get());
            HostnameFilter.HostnameTl.Set("HOST");
            invoked.Set(false);
            chain = new _FilterChain_103(invoked);
            filter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.IsTrue(invoked.Get());
            HostnameFilter.HostnameTl.Remove();
            filter.Destroy();
        }
Ejemplo n.º 2
0
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="Javax.Servlet.ServletException"/>
 public void DoFilter(ServletRequest servletRequest, ServletResponse servletResponse
                      )
 {
     NUnit.Framework.Assert.AreEqual(MDC.Get("hostname"), null);
     NUnit.Framework.Assert.AreEqual(MDC.Get("user"), "name");
     NUnit.Framework.Assert.AreEqual(MDC.Get("method"), "METHOD");
     NUnit.Framework.Assert.AreEqual(MDC.Get("path"), "/pathinfo");
     invoked.Set(true);
 }
Ejemplo n.º 3
0
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 500;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));
                        Assert.IsFalse(MDC.Contains("foo2"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.IsTrue(MDC.Contains("foo"));
                        Assert.AreEqual("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));

                        Assert.IsTrue(MDC.Contains("foo2"));
                        Assert.AreEqual("bar2", MDC.Get("foo2"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            if (exceptions.Count != 0)
            {
                Assert.Fail(exceptions[0].ToString());
            }
        }
Ejemplo n.º 4
0
        /// <summary>Logs the HTTP status code and exception in HttpFSServer's log.</summary>
        /// <param name="status">HTTP status code.</param>
        /// <param name="throwable">exception thrown.</param>
        protected internal override void Log(Response.Status status, Exception throwable)
        {
            string method  = MDC.Get("method");
            string path    = MDC.Get("path");
            string message = GetOneLineMessage(throwable);

            AuditLog.Warn("FAILED [{}:{}] response [{}] {}", new object[] { method, path, status
                                                                            , message });
            Log.Warn("[{}:{}] response [{}] {}", new object[] { method, path, status, message }, throwable);
        }
Ejemplo n.º 5
0
        public void timer_cannot_inherit_mappedcontext()
        {
            object getObject = null;
            string getValue  = null;

            var   mre    = new ManualResetEvent(false);
            Timer thread = new Timer((s) =>
            {
                try
                {
                    getObject = MDC.GetObject("DoNotExist");
                    getValue  = MDC.Get("DoNotExistEither");
                }
                finally
                {
                    mre.Set();
                }
            });

            thread.Change(0, Timeout.Infinite);
            mre.WaitOne();
            Assert.Null(getObject);
            Assert.Empty(getValue);
        }
Ejemplo n.º 6
0
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 100;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));
                        Assert.False(MDC.Contains("foo2"));
                        Assert.Equal(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.True(MDC.Contains("foo"));
                        Assert.Equal("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));

                        Assert.True(MDC.Contains("foo2"));
                        Assert.Equal("bar2", MDC.Get("foo2"));

                        Assert.Null(MDC.GetObject("foo3"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            StringBuilder exceptionsMessage = new StringBuilder();

            foreach (var ex in exceptions)
            {
                if (exceptionsMessage.Length > 0)
                {
                    exceptionsMessage.Append("\r\n");
                }

                exceptionsMessage.Append(ex.ToString());
            }

            Assert.True(exceptions.Count == 0, exceptionsMessage.ToString());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Renders the specified MDC item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected internal override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            string msg = MDC.Get(Item);

            builder.Append(ApplyPadding(msg));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Returns the estimated number of characters that are needed to
 /// hold the rendered value for the specified logging event.
 /// </summary>
 /// <param name="logEvent">Logging event information.</param>
 /// <returns>The number of characters.</returns>
 /// <remarks>
 /// If the exact number is not known or
 /// expensive to calculate this function should return a rough estimate
 /// that's big enough in most cases, but not too big, in order to conserve memory.
 /// </remarks>
 protected internal override int GetEstimatedBufferSize(LogEventInfo logEvent)
 {
     return(MDC.Get(Item).Length);
 }