package com.github.triceo.splitlog;

import org.assertj.core.api.Assertions;
import org.junit.Test;

import com.github.triceo.splitlog.api.Message;
import com.github.triceo.splitlog.api.MessageDeliveryStatus;
import com.github.triceo.splitlog.api.MessageMeasure;
import com.github.triceo.splitlog.api.MessageMetric;
import com.github.triceo.splitlog.api.MessageSource;

public class DefaultMessageMetricTest {

    private static final MessageMeasure<Integer> DEFAULT_MEASURE = new MessageMeasure<Integer>() {

        @Override
        public Integer update(final MessageMetric<Integer> metric, final Message evaluate,
            final MessageDeliveryStatus status, final MessageSource source) {
            final Integer value = metric.getValue();
            return (value == null) ? 1 : value + 2;
        }

    };

    @Test
    public void testGetMeasure() {
        final MessageMetric<Integer> metric = new DefaultMessageMetric<Integer>(
                DefaultMessageMetricTest.DEFAULT_MEASURE);
        Assertions.assertThat(metric.getMeasure()).isSameAs(DefaultMessageMetricTest.DEFAULT_MEASURE);
    }

    @Test
    public void testIncreases() {
        final DefaultMessageMetric<Integer> metric = new DefaultMessageMetric<Integer>(
                DefaultMessageMetricTest.DEFAULT_MEASURE);
        Assertions.assertThat(metric.getMessageCount()).isEqualTo(0);
        Assertions.assertThat(metric.getValue()).isNull();
        metric.notifyOfMessage(null, null, null); // shouldn't use nulls, but in
                                                  // this test, it doesn't
                                                  // matter
        Assertions.assertThat(metric.getMessageCount()).isEqualTo(1);
        Assertions.assertThat(metric.getValue()).isEqualTo(1);
        metric.notifyOfMessage(null, null, null);
        Assertions.assertThat(metric.getMessageCount()).isEqualTo(2);
        Assertions.assertThat(metric.getValue()).isEqualTo(3);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNullConstructor() {
        new DefaultMessageMetric<Integer>(null);
    }

}

@@ -30,16 +30,222 @@
 itlog;%0A%0A
+import java.util.concurrent.Callable;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 import o
@@ -276,16 +276,16 @@
 rtions;%0A
-
 import o
@@ -515,16 +515,78 @@
 Metric;%0A
+import com.github.triceo.splitlog.api.MessageMetricCondition;%0A
 import c
@@ -1083,16 +1083,180 @@
     %7D;%0A%0A
+    private static final MessageBuilder MESSAGE = new MessageBuilder(%22test%22);%0A%0A    private final ScheduledExecutorService e = Executors.newScheduledThreadPool(1);%0A%0A
     @Tes
@@ -1955,117 +1955,33 @@
     
-                                          // this test, it doesn't%0A                                          
+// this test, it doesn't%0A
     
@@ -2466,10 +2466,1916 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testWaiting() %7B%0A        final DefaultMessageMetric%3CInteger%3E metric = new DefaultMessageMetric%3CInteger%3E(%0A                DefaultMessageMetricTest.DEFAULT_MEASURE);%0A        Assertions.assertThat(metric.getValue()).isNull();%0A        metric.notifyOfMessage(DefaultMessageMetricTest.MESSAGE.buildFinal(), MessageDeliveryStatus.ACCEPTED, null);%0A        Assertions.assertThat(metric.getValue()).isEqualTo(1);%0A        final Future%3CMessage%3E expected = this.e.schedule(new Callable%3CMessage%3E() %7B%0A%0A            @Override%0A            public Message call() throws Exception %7B%0A                // this message will only increase the metric%0A                metric.notifyOfMessage(DefaultMessageMetricTest.MESSAGE.buildFinal(), MessageDeliveryStatus.ACCEPTED,%0A                        null);%0A                // this message will put metric over threshold%0A                final Message m = DefaultMessageMetricTest.MESSAGE.buildFinal();%0A                metric.notifyOfMessage(m, MessageDeliveryStatus.ACCEPTED, null);%0A                // and this message will, once again, do nothing; nothing else%0A                // is waiting%0A                metric.notifyOfMessage(DefaultMessageMetricTest.MESSAGE.buildFinal(), MessageDeliveryStatus.ACCEPTED,%0A                        null);%0A                return m;%0A            %7D%0A%0A        %7D, 1, TimeUnit.SECONDS);%0A        final Message result = metric.waitFor(new MessageMetricCondition%3CInteger%3E() %7B%0A%0A            @Override%0A            public boolean accept(final MessageMetric%3CInteger%3E evaluate) %7B%0A                return evaluate.getValue() == 5;%0A            %7D%0A%0A        %7D, 10, TimeUnit.SECONDS);%0A        try %7B%0A            Assertions.assertThat(result).isEqualTo(expected.get());%0A        %7D catch (final Exception e1) %7B%0A            Assertions.fail(%22Metric condition not triggered.%22, e1);%0A        %7D%0A        Assertions.assertThat(metric.getValue()).isEqualTo(7);%0A    %7D%0A
 %7D%0A

Add tests for metric waiting