    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j;
    18
    19  import junit.framework.TestCase;
    20
    21  import java.io.CharArrayWriter;
    22  import java.text.MessageFormat;
    23  import java.text.NumberFormat;
    24  import java.util.Date;
    25  import java.text.DateFormat;
    26
    27
    28  /**
    29   * Unit test for LogMF.
    30   */
    31  public class TestLogMF extends TestCase {
    32      /**
    33       * Trace level.
    34       */
    35      private static final Level TRACE = getTraceLevel();
    36
    37      /**
    38       * Gets Trace level.
    39       * Trace level was not defined prior to log4j 1.2.12.
    40       * @return trace level
    41       */
    42      private static Level getTraceLevel() {
    43          try {
    44              return (Level) Level.class.getField("TRACE").get(null);
    45          } catch(Exception ex) {
    46              return new Level(5000, "TRACE", 7);
    47          }
    48      }
    49
    50      /**
    51       * Logger.
    52       */
    53      private final Logger logger = Logger.getLogger(
    54              "org.apache.log4j.formatter.TestLogMF");
    55
    56      /**
    57       * Create the test case
    58       *
    59       * @param testName name of the test case
    60       */
    61      public TestLogMF(String testName) {
    62          super(testName);
    63      }
    64
    65
    66      /**
    67       * Post test clean up.
    68       */
    69      public void tearDown() {
    70          LogManager.resetConfiguration();
    71      }
    72
    73      /**
    74       * Test class name when logging through LogMF.
    75       */
    76      public void testClassName() {
    77          CharArrayWriter writer = new CharArrayWriter();
    78          PatternLayout layout = new PatternLayout("%C");
    79          WriterAppender appender = new WriterAppender(layout, writer);
    80          appender.activateOptions();
    81          Logger.getRootLogger().addAppender(appender);
    82          LogMF.debug(logger, null, Math.PI);
    83          assertEquals(TestLogMF.class.getName(), writer.toString());
    84      }
    85
    86      /**
    87       * Test LogMF.trace with null pattern.
    88       */
    89      public void testTraceNullPattern() {
    90          LogCapture capture = new LogCapture(TRACE);
    91          logger.setLevel(TRACE);
    92          LogMF.trace(logger, null, Math.PI);
    93          assertNull(capture.getMessage());
    94      }
    95
    96      /**
    97       * Test LogMF.trace with no-field pattern.
    98       */
    99      public void testTraceNoArg() {
    100         LogCapture capture = new LogCapture(TRACE);
    101         logger.setLevel(TRACE);
    102         LogMF.trace(logger, "Hello, World", Math.PI);
    103         assertEquals("Hello, World", capture.getMessage());
    104     }
    105
    106     /**
    107      * Test LogMF.trace with malformed pattern.
    108      */
    109     public void testTraceBadPattern() {
    110         LogCapture capture = new LogCapture(TRACE);
    111         logger.setLevel(TRACE);
    112         LogMF.trace(logger, "Hello, {.", Math.PI);
    113         assertEquals("Hello, {.", capture.getMessage());
    114     }
    115
    116     /**
    117      * Test LogMF.trace with missing argument.
    118      */
    119     public void testTraceMissingArg() {
    120         LogCapture capture = new LogCapture(TRACE);
    121         logger.setLevel(TRACE);
    122         LogMF.trace(logger, "Hello, {0}World", new Object[0]);
    123         assertEquals("Hello, {0}World", capture.getMessage());
    124     }
    125
    126     /**
    127      * Test LogMF.trace with single field pattern with string argument.
    128      */
    129     public void testTraceString() {
    130         LogCapture capture = new LogCapture(TRACE);
    131         logger.setLevel(TRACE);
    132         LogMF.trace(logger, "Hello, {0}", "World");
    133         assertEquals("Hello, World", capture.getMessage());
    134     }
    135
    136     /**
    137      * Test LogMF.trace with single field pattern with null argument.
    138      */
    139     public void testTraceNull() {
    140         LogCapture capture = new LogCapture(TRACE);
    141         logger.setLevel(TRACE);
    142         LogMF.trace(logger, "Hello, {0}", (Object) null);
    143         assertEquals("Hello, null", capture.getMessage());
    144     }
    145
    146     /**
    147      * Test LogMF.trace with single field pattern with int argument.
    148      */
    149     public void testTraceInt() {
    150         LogCapture capture = new LogCapture(TRACE);
    151         logger.setLevel(TRACE);
    152
    153         int val = 42;
    154         LogMF.trace(logger, "Iteration {0}", val);
    155         assertEquals("Iteration 42", capture.getMessage());
    156     }
    157
    158     /**
    159      * Test LogMF.trace with single field pattern with byte argument.
    160      */
    161     public void testTraceByte() {
    162         LogCapture capture = new LogCapture(TRACE);
    163         logger.setLevel(TRACE);
    164
    165         byte val = 42;
    166         LogMF.trace(logger, "Iteration {0}", val);
    167         assertEquals("Iteration 42", capture.getMessage());
    168     }
    169
    170     /**
    171      * Test LogMF.trace with single field pattern with short argument.
    172      */
    173     public void testTraceShort() {
    174         LogCapture capture = new LogCapture(TRACE);
    175         logger.setLevel(TRACE);
    176
    177         short val = 42;
    178         LogMF.trace(logger, "Iteration {0}", val);
    179         assertEquals("Iteration 42", capture.getMessage());
    180     }
    181
    182     /**
    183      * Test LogMF.trace with single field pattern with long argument.
    184      */
    185     public void testTraceLong() {
    186         LogCapture capture = new LogCapture(TRACE);
    187         logger.setLevel(TRACE);
    188
    189         long val = 42;
    190         LogMF.trace(logger, "Iteration {0}", val);
    191         assertEquals("Iteration 42", capture.getMessage());
    192     }
    193
    194     /**
    195      * Test LogMF.trace with single field pattern with char argument.
    196      */
    197     public void testTraceChar() {
    198         LogCapture capture = new LogCapture(TRACE);
    199         logger.setLevel(TRACE);
    200
    201         char val = 'C';
    202         LogMF.trace(logger, "Iteration {0}", val);
    203         assertEquals("Iteration C", capture.getMessage());
    204     }
    205
    206     /**
    207      * Test LogMF.trace with single field pattern with boolean argument.
    208      */
    209     public void testTraceBoolean() {
    210         LogCapture capture = new LogCapture(TRACE);
    211         logger.setLevel(TRACE);
    212
    213         boolean val = true;
    214         LogMF.trace(logger, "Iteration {0}", val);
    215         assertEquals("Iteration true", capture.getMessage());
    216     }
    217
    218     /**
    219      * Test LogMF.trace with single field pattern with float argument.
    220      */
    221     public void testTraceFloat() {
    222         LogCapture capture = new LogCapture(TRACE);
    223         logger.setLevel(TRACE);
    224
    225         float val = 3.14f;
    226         NumberFormat format = NumberFormat.getInstance();
    227         LogMF.trace(logger, "Iteration {0}", val);
    228         assertEquals("Iteration "+ format.format(val), capture.getMessage());
    229     }
    230
    231     /**
    232      * Test LogMF.trace with single field pattern with double argument.
    233      */
    234     public void testTraceDouble() {
    235         LogCapture capture = new LogCapture(TRACE);
    236         logger.setLevel(TRACE);
    237
    238         double val = 3.14;
    239         NumberFormat format = NumberFormat.getInstance();
    240         LogMF.trace(logger, "Iteration {0}", val);
    241         assertEquals("Iteration "+ format.format(val), capture.getMessage());
    242     }
    243
    244     /**
    245      * Test LogMF.trace with two arguments.
    246      */
    247     public void testTraceTwoArg() {
    248         LogCapture capture = new LogCapture(TRACE);
    249         logger.setLevel(TRACE);
    250         LogMF.trace(logger, "{1}, {0}.", "World", "Hello");
    251         assertEquals("Hello, World.", capture.getMessage());
    252     }
    253
    254     /**
    255      * Test LogMF.trace with three arguments.
    256      */
    257     public void testTraceThreeArg() {
    258         LogCapture capture = new LogCapture(TRACE);
    259         logger.setLevel(TRACE);
    260         LogMF.trace(logger, "{1}{2} {0}.", "World", "Hello", ",");
    261         assertEquals("Hello, World.", capture.getMessage());
    262     }
    263
    264     /**
    265      * Test LogMF.trace with four arguments.
    266      */
    267     public void testTraceFourArg() {
    268         LogCapture capture = new LogCapture(TRACE);
    269         logger.setLevel(TRACE);
    270         LogMF.trace(logger, "{1}{2} {0}{3}", "World", "Hello", ",", ".");
    271         assertEquals("Hello, World.", capture.getMessage());
    272     }
    273
    274     /**
    275      * Test LogMF.trace with Object[] argument.
    276      */
    277     public void testTraceArrayArg() {
    278         LogCapture capture = new LogCapture(TRACE);
    279         logger.setLevel(TRACE);
    280
    281         Object[] args = new Object[] { "World", "Hello", ",", "." };
    282         LogMF.trace(logger, "{1}{2} {0}{3}", args);
    283         assertEquals("Hello, World.", capture.getMessage());
    284     }
    285
    286     /**
    287      * Test LogMF.trace with null Object[] argument.
    288      */
    289     public void testTraceNullArrayArg() {
    290         LogCapture capture = new LogCapture(TRACE);
    291         logger.setLevel(TRACE);
    292
    293         Object[] args = null;
    294         LogMF.trace(logger, "{1}{2} {0}{3}", args);
    295         assertEquals("{1}{2} {0}{3}", capture.getMessage());
    296     }
    297
    298
    299     /**
    300      * Test LogMF.debug with null pattern.
    301      */
    302     public void testDebugNullPattern() {
    303         LogCapture capture = new LogCapture(Level.DEBUG);
    304         LogMF.debug(logger, null, Math.PI);
    305         assertEquals(null, capture.getMessage());
    306     }
    307
    308     /**
    309      * Test LogMF.debug with no-field pattern.
    310      */
    311     public void testDebugNoArg() {
    312         LogCapture capture = new LogCapture(Level.DEBUG);
    313         LogMF.debug(logger, "Hello, World", Math.PI);
    314         assertEquals("Hello, World", capture.getMessage());
    315     }
    316
    317     /**
    318      * Test LogMF.debug with malformed pattern.
    319      */
    320     public void testDebugBadPattern() {
    321         LogCapture capture = new LogCapture(Level.DEBUG);
    322         LogMF.debug(logger, "Hello, {.", Math.PI);
    323         assertEquals("Hello, {.", capture.getMessage());
    324     }
    325
    326     /**
    327      * Test LogMF.debug with missing argument.
    328      */
    329     public void testDebugMissingArg() {
    330         LogCapture capture = new LogCapture(Level.DEBUG);
    331         LogMF.debug(logger, "Hello, {0}World", new Object[0]);
    332         assertEquals("Hello, {0}World", capture.getMessage());
    333     }
    334
    335     /**
    336      * Test LogMF.debug with single field pattern with string argument.
    337      */
    338     public void testDebugString() {
    339         LogCapture capture = new LogCapture(Level.DEBUG);
    340         LogMF.debug(logger, "Hello, {0}", "World");
    341         assertEquals("Hello, World", capture.getMessage());
    342     }
    343
    344     /**
    345      * Test LogMF.debug with single field pattern with null argument.
    346      */
    347     public void testDebugNull() {
    348         LogCapture capture = new LogCapture(Level.DEBUG);
    349         LogMF.debug(logger, "Hello, {0}", (Object) null);
    350         assertEquals("Hello, null", capture.getMessage());
    351     }
    352
    353     /**
    354      * Test LogMF.debug with single field pattern with int argument.
    355      */
    356     public void testDebugInt() {
    357         LogCapture capture = new LogCapture(Level.DEBUG);
    358         int val = 42;
    359         LogMF.debug(logger, "Iteration {0}", val);
    360         assertEquals("Iteration 42", capture.getMessage());
    361     }
    362
    363     /**
    364      * Test LogMF.debug with single field pattern with byte argument.
    365      */
    366     public void testDebugByte() {
    367         LogCapture capture = new LogCapture(Level.DEBUG);
    368         byte val = 42;
    369         LogMF.debug(logger, "Iteration {0}", val);
    370         assertEquals("Iteration 42", capture.getMessage());
    371     }
    372
    373     /**
    374      * Test LogMF.debug with single field pattern with short argument.
    375      */
    376     public void testDebugShort() {
    377         LogCapture capture = new LogCapture(Level.DEBUG);
    378         short val = 42;
    379         LogMF.debug(logger, "Iteration {0}", val);
    380         assertEquals("Iteration 42", capture.getMessage());
    381     }
    382
    383     /**
    384      * Test LogMF.debug with single field pattern with long argument.
    385      */
    386     public void testDebugLong() {
    387         LogCapture capture = new LogCapture(Level.DEBUG);
    388         long val = 42;
    389         LogMF.debug(logger, "Iteration {0}", val);
    390         assertEquals("Iteration 42", capture.getMessage());
    391     }
    392
    393     /**
    394      * Test LogMF.debug with single field pattern with char argument.
    395      */
    396     public void testDebugChar() {
    397         LogCapture capture = new LogCapture(Level.DEBUG);
    398         char val = 'C';
    399         LogMF.debug(logger, "Iteration {0}", val);
    400         assertEquals("Iteration C", capture.getMessage());
    401     }
    402
    403     /**
    404      * Test LogMF.debug with single field pattern with boolean argument.
    405      */
    406     public void testDebugBoolean() {
    407         LogCapture capture = new LogCapture(Level.DEBUG);
    408         boolean val = true;
    409         LogMF.debug(logger, "Iteration {0}", val);
    410         assertEquals("Iteration true", capture.getMessage());
    411     }
    412
    413     /**
    414      * Test LogMF.debug with single field pattern with float argument.
    415      */
    416     public void testDebugFloat() {
    417         LogCapture capture = new LogCapture(Level.DEBUG);
    418         LogMF.debug(logger, "Iteration {0}", (float) Math.PI);
    419
    420         String expected = MessageFormat.format("Iteration {0}",
    421                 new Object[] { new Float(Math.PI) });
    422         assertEquals(expected, capture.getMessage());
    423     }
    424
    425     /**
    426      * Test LogMF.debug with single field pattern with double argument.
    427      */
    428     public void testDebugDouble() {
    429         LogCapture capture = new LogCapture(Level.DEBUG);
    430         LogMF.debug(logger, "Iteration {0}", Math.PI);
    431
    432         String expected = MessageFormat.format("Iteration {0}",
    433                 new Object[] { new Double(Math.PI) });
    434         assertEquals(expected, capture.getMessage());
    435     }
    436
    437     /**
    438      * Test LogMF.debug with two arguments.
    439      */
    440     public void testDebugTwoArg() {
    441         LogCapture capture = new LogCapture(Level.DEBUG);
    442         LogMF.debug(logger, "{1}, {0}.", "World", "Hello");
    443         assertEquals("Hello, World.", capture.getMessage());
    444     }
    445
    446     /**
    447      * Test LogMF.debug with three arguments.
    448      */
    449     public void testDebugThreeArg() {
    450         LogCapture capture = new LogCapture(Level.DEBUG);
    451         LogMF.debug(logger, "{1}{2} {0}.", "World", "Hello", ",");
    452         assertEquals("Hello, World.", capture.getMessage());
    453     }
    454
    455     /**
    456      * Test LogMF.debug with four arguments.
    457      */
    458     public void testDebugFourArg() {
    459         LogCapture capture = new LogCapture(Level.DEBUG);
    460         LogMF.debug(logger, "{1}{2} {0}{3}", "World", "Hello", ",", ".");
    461         assertEquals("Hello, World.", capture.getMessage());
    462     }
    463
    464     /**
    465      * Test LogMF.debug with Object[] argument.
    466      */
    467     public void testDebugArrayArg() {
    468         LogCapture capture = new LogCapture(Level.DEBUG);
    469         Object[] args = new Object[] { "World", "Hello", ",", "." };
    470         LogMF.debug(logger, "{1}{2} {0}{3}", args);
    471         assertEquals("Hello, World.", capture.getMessage());
    472     }
    473
    474     /**
    475      * Test LogMF.debug with single field pattern with double argument.
    476      */
    477     public void testDebugDate() {
    478         LogCapture capture = new LogCapture(Level.DEBUG);
    479         Date epoch = new Date(0);
    480         LogMF.debug(logger, "Iteration {0}", epoch);
    481
    482         String expected = MessageFormat.format("Iteration {0}",
    483                 new Object[] { epoch });
    484         String expected2 = "Iteration " + DateFormat.getDateTimeInstance(
    485                                 DateFormat.SHORT,
    486                                 DateFormat.SHORT).format(epoch);
    487         String actual = capture.getMessage();
    488         //
    489         //  gcj has been observed to output 12/31/69 6:00:00 PM
    490         //     instead of the expected 12/31/69 6:00 PM
    491         if (System.getProperty("java.vendor").indexOf("Free") == -1) {
    492             assertEquals(expected, actual);
    493         }
    494         assertEquals(expected2, actual);
    495     }
    496
    497     /**
    498      * Test LogMF.debug with null Object[] argument.
    499      */
    500     public void testDebugNullArrayArg() {
    501         LogCapture capture = new LogCapture(Level.DEBUG);
    502         Object[] args = null;
    503         LogMF.debug(logger, "{1}{2} {0}{3}", args);
    504         assertEquals("{1}{2} {0}{3}", capture.getMessage());
    505     }
    506
    507     public void testDebugPercent() {
    508         LogCapture capture = new LogCapture(Level.DEBUG);
    509         LogMF.debug(logger, "{0, number, percent}", Math.PI);
    510
    511         String expected = java.text.MessageFormat.format("{0, number, percent}",
    512                 new Object[] { new Double(Math.PI) });
    513         assertEquals(expected, capture.getMessage());
    514     }
    515
    516     public void testDebugFullPrecisionAndPercent() {
    517         LogCapture capture = new LogCapture(Level.DEBUG);
    518         LogMF.debug(logger, "{0}{0, number, percent}", Math.PI);
    519
    520         String expected = java.text.MessageFormat.format("{0}{0, number, percent}",
    521                 new Object[] { new Double(Math.PI) });
    522         assertEquals(expected, capture.getMessage());
    523     }
    524
    525     public void testDebugQuoted() {
    526         LogCapture capture = new LogCapture(Level.DEBUG);
    527         LogMF.debug(logger, "'{0}'", "World");
    528         assertEquals("{0}", capture.getMessage());
    529     }
    530
    531     /**
    532      * Test LogMF.info with null pattern.
    533      */
    534     public void testInfoNullPattern() {
    535         LogCapture capture = new LogCapture(Level.INFO);
    536         LogMF.info(logger, null, Math.PI);
    537         assertNull(capture.getMessage());
    538     }
    539
    540     /**
    541      * Test LogMF.info with no-field pattern.
    542      */
    543     public void testInfoNoArg() {
    544         LogCapture capture = new LogCapture(Level.INFO);
    545         LogMF.info(logger, "Hello, World", Math.PI);
    546         assertEquals("Hello, World", capture.getMessage());
    547     }
    548
    549     /**
    550      * Test LogMF.info with malformed pattern.
    551      */
    552     public void testInfoBadPattern() {
    553         LogCapture capture = new LogCapture(Level.INFO);
    554         LogMF.info(logger, "Hello, {.", Math.PI);
    555         assertEquals("Hello, {.", capture.getMessage());
    556     }
    557
    558     /**
    559      * Test LogMF.info with missing argument.
    560      */
    561     public void testInfoMissingArg() {
    562         LogCapture capture = new LogCapture(Level.INFO);
    563         LogMF.info(logger, "Hello, {0}World", new Object[0]);
    564         assertEquals("Hello, {0}World", capture.getMessage());
    565     }
    566
    567     /**
    568      * Test LogMF.info with single field pattern with string argument.
    569      */
    570     public void testInfoString() {
    571         LogCapture capture = new LogCapture(Level.INFO);
    572         LogMF.info(logger, "Hello, {0}", "World");
    573         assertEquals("Hello, World", capture.getMessage());
    574     }
    575
    576     /**
    577      * Test LogMF.info with single field pattern with null argument.
    578      */
    579     public void testInfoNull() {
    580         LogCapture capture = new LogCapture(Level.INFO);
    581         LogMF.info(logger, "Hello, {0}", (Object) null);
    582         assertEquals("Hello, null", capture.getMessage());
    583     }
    584
    585     /**
    586      * Test LogMF.info with single field pattern with int argument.
    587      */
    588     public void testInfoInt() {
    589         LogCapture capture = new LogCapture(Level.INFO);
    590         int val = 42;
    591         LogMF.info(logger, "Iteration {0}", val);
    592         assertEquals("Iteration 42", capture.getMessage());
    593     }
    594
    595     /**
    596      * Test LogMF.info with single field pattern with byte argument.
    597      */
    598     public void testInfoByte() {
    599         LogCapture capture = new LogCapture(Level.INFO);
    600         byte val = 42;
    601         LogMF.info(logger, "Iteration {0}", val);
    602         assertEquals("Iteration 42", capture.getMessage());
    603     }
    604
    605     /**
    606      * Test LogMF.info with single field pattern with short argument.
    607      */
    608     public void testInfoShort() {
    609         LogCapture capture = new LogCapture(Level.INFO);
    610         short val = 42;
    611         LogMF.info(logger, "Iteration {0}", val);
    612         assertEquals("Iteration 42", capture.getMessage());
    613     }
    614
    615     /**
    616      * Test LogMF.info with single field pattern with long argument.
    617      */
    618     public void testInfoLong() {
    619         LogCapture capture = new LogCapture(Level.INFO);
    620         long val = 42;
    621         LogMF.info(logger, "Iteration {0}", val);
    622         assertEquals("Iteration 42", capture.getMessage());
    623     }
    624
    625     /**
    626      * Test LogMF.info with single field pattern with char argument.
    627      */
    628     public void testInfoChar() {
    629         LogCapture capture = new LogCapture(Level.INFO);
    630         char val = 'C';
    631         LogMF.info(logger, "Iteration {0}", val);
    632         assertEquals("Iteration C", capture.getMessage());
    633     }
    634
    635     /**
    636      * Test LogMF.info with single field pattern with boolean argument.
    637      */
    638     public void testInfoBoolean() {
    639         LogCapture capture = new LogCapture(Level.INFO);
    640         boolean val = true;
    641         LogMF.info(logger, "Iteration {0}", val);
    642         assertEquals("Iteration true", capture.getMessage());
    643     }
    644
    645     /**
    646      * Test LogMF.info with single field pattern with float argument.
    647      */
    648     public void testInfoFloat() {
    649         LogCapture capture = new LogCapture(Level.INFO);
    650         LogMF.info(logger, "Iteration {0}", (float) Math.PI);
    651
    652         String expected = MessageFormat.format("Iteration {0}",
    653                 new Object[] { new Float(Math.PI) });
    654         assertEquals(expected, capture.getMessage());
    655     }
    656
    657     /**
    658      * Test LogMF.info with single field pattern with double argument.
    659      */
    660     public void testInfoDouble() {
    661         LogCapture capture = new LogCapture(Level.INFO);
    662         LogMF.info(logger, "Iteration {0}", Math.PI);
    663
    664         String expected = MessageFormat.format("Iteration {0}",
    665                 new Object[] { new Double(Math.PI) });
    666         assertEquals(expected, capture.getMessage());
    667     }
    668
    669     /**
    670      * Test LogMF.info with two arguments.
    671      */
    672     public void testInfoTwoArg() {
    673         LogCapture capture = new LogCapture(Level.INFO);
    674         LogMF.info(logger, "{1}, {0}.", "World", "Hello");
    675         assertEquals("Hello, World.", capture.getMessage());
    676     }
    677
    678     /**
    679      * Test LogMF.info with three arguments.
    680      */
    681     public void testInfoThreeArg() {
    682         LogCapture capture = new LogCapture(Level.INFO);
    683         LogMF.info(logger, "{1}{2} {0}.", "World", "Hello", ",");
    684         assertEquals("Hello, World.", capture.getMessage());
    685     }
    686
    687     /**
    688      * Test LogMF.info with four arguments.
    689      */
    690     public void testInfoFourArg() {
    691         LogCapture capture = new LogCapture(Level.INFO);
    692         LogMF.info(logger, "{1}{2} {0}{3}", "World", "Hello", ",", ".");
    693         assertEquals("Hello, World.", capture.getMessage());
    694     }
    695
    696     /**
    697      * Test LogMF.info with Object[] argument.
    698      */
    699     public void testInfoArrayArg() {
    700         LogCapture capture = new LogCapture(Level.INFO);
    701         Object[] args = new Object[] { "World", "Hello", ",", "." };
    702         LogMF.info(logger, "{1}{2} {0}{3}", args);
    703         assertEquals("Hello, World.", capture.getMessage());
    704     }
    705
    706     /**
    707      * Test LogMF.warn with null pattern.
    708      */
    709     public void testWarnNullPattern() {
    710         LogCapture capture = new LogCapture(Level.WARN);
    711         LogMF.warn(logger, null, Math.PI);
    712         assertNull(capture.getMessage());
    713     }
    714
    715     /**
    716      * Test LogMF.warn with no-field pattern.
    717      */
    718     public void testWarnNoArg() {
    719         LogCapture capture = new LogCapture(Level.WARN);
    720         LogMF.warn(logger, "Hello, World", Math.PI);
    721         assertEquals("Hello, World", capture.getMessage());
    722     }
    723
    724     /**
    725      * Test LogMF.warn with malformed pattern.
    726      */
    727     public void testWarnBadPattern() {
    728         LogCapture capture = new LogCapture(Level.WARN);
    729         LogMF.warn(logger, "Hello, {.", Math.PI);
    730         assertEquals("Hello, {.", capture.getMessage());
    731     }
    732
    733     /**
    734      * Test LogMF.warn with missing argument.
    735      */
    736     public void testWarnMissingArg() {
    737         LogCapture capture = new LogCapture(Level.WARN);
    738         LogMF.warn(logger, "Hello, {0}World", new Object[0]);
    739         assertEquals("Hello, {0}World", capture.getMessage());
    740     }
    741
    742     /**
    743      * Test LogMF.warn with single field pattern with string argument.
    744      */
    745     public void testWarnString() {
    746         LogCapture capture = new LogCapture(Level.WARN);
    747         LogMF.warn(logger, "Hello, {0}", "World");
    748         assertEquals("Hello, World", capture.getMessage());
    749     }
    750
    751     /**
    752      * Test LogMF.warn with single field pattern with null argument.
    753      */
    754     public void testWarnNull() {
    755         LogCapture capture = new LogCapture(Level.WARN);
    756         LogMF.warn(logger, "Hello, {0}", (Object) null);
    757         assertEquals("Hello, null", capture.getMessage());
    758     }
    759
    760     /**
    761      * Test LogMF.warn with single field pattern with int argument.
    762      */
    763     public void testWarnInt() {
    764         LogCapture capture = new LogCapture(Level.WARN);
    765         int val = 42;
    766         LogMF.warn(logger, "Iteration {0}", val);
    767         assertEquals("Iteration 42", capture.getMessage());
    768     }
    769
    770     /**
    771      * Test LogMF.warn with single field pattern with byte argument.
    772      */
    773     public void testWarnByte() {
    774         LogCapture capture = new LogCapture(Level.WARN);
    775         byte val = 42;
    776         LogMF.warn(logger, "Iteration {0}", val);
    777         assertEquals("Iteration 42", capture.getMessage());
    778     }
    779
    780     /**
    781      * Test LogMF.warn with single field pattern with short argument.
    782      */
    783     public void testWarnShort() {
    784         LogCapture capture = new LogCapture(Level.WARN);
    785         short val = 42;
    786         LogMF.warn(logger, "Iteration {0}", val);
    787         assertEquals("Iteration 42", capture.getMessage());
    788     }
    789
    790     /**
    791      * Test LogMF.warn with single field pattern with long argument.
    792      */
    793     public void testWarnLong() {
    794         LogCapture capture = new LogCapture(Level.WARN);
    795         long val = 42;
    796         LogMF.warn(logger, "Iteration {0}", val);
    797         assertEquals("Iteration 42", capture.getMessage());
    798     }
    799
    800     /**
    801      * Test LogMF.warn with single field pattern with char argument.
    802      */
    803     public void testWarnChar() {
    804         LogCapture capture = new LogCapture(Level.WARN);
    805         char val = 'C';
    806         LogMF.warn(logger, "Iteration {0}", val);
    807         assertEquals("Iteration C", capture.getMessage());
    808     }
    809
    810     /**
    811      * Test LogMF.warn with single field pattern with boolean argument.
    812      */
    813     public void testWarnBoolean() {
    814         LogCapture capture = new LogCapture(Level.WARN);
    815         boolean val = true;
    816         LogMF.warn(logger, "Iteration {0}", val);
    817         assertEquals("Iteration true", capture.getMessage());
    818     }
    819
    820     /**
    821      * Test LogMF.warn with single field pattern with float argument.
    822      */
    823     public void testWarnFloat() {
    824         LogCapture capture = new LogCapture(Level.WARN);
    825         LogMF.warn(logger, "Iteration {0}", (float) Math.PI);
    826
    827         String expected = MessageFormat.format("Iteration {0}",
    828                 new Object[] { new Float(Math.PI) });
    829         assertEquals(expected, capture.getMessage());
    830     }
    831
    832     /**
    833      * Test LogMF.debug with single field pattern with double argument.
    834      */
    835     public void testWarnDouble() {
    836         LogCapture capture = new LogCapture(Level.WARN);
    837         LogMF.warn(logger, "Iteration {0}", Math.PI);
    838
    839         String expected = MessageFormat.format("Iteration {0}",
    840                 new Object[] { new Double(Math.PI) });
    841         assertEquals(expected, capture.getMessage());
    842     }
    843
    844     /**
    845      * Test LogMF.warn with two arguments.
    846      */
    847     public void testWarnTwoArg() {
    848         LogCapture capture = new LogCapture(Level.WARN);
    849         LogMF.warn(logger, "{1}, {0}.", "World", "Hello");
    850         assertEquals("Hello, World.", capture.getMessage());
    851     }
    852
    853     /**
    854      * Test LogMF.warn with three arguments.
    855      */
    856     public void testWarnThreeArg() {
    857         LogCapture capture = new LogCapture(Level.WARN);
    858         LogMF.warn(logger, "{1}{2} {0}.", "World", "Hello", ",");
    859         assertEquals("Hello, World.", capture.getMessage());
    860     }
    861
    862     /**
    863      * Test LogMF.debug with four arguments.
    864      */
    865     public void testWarnFourArg() {
    866         LogCapture capture = new LogCapture(Level.WARN);
    867         LogMF.warn(logger, "{1}{2} {0}{3}", "World", "Hello", ",", ".");
    868         assertEquals("Hello, World.", capture.getMessage());
    869     }
    870
    871     /**
    872      * Test LogMF.warn with Object[] argument.
    873      */
    874     public void testWarnArrayArg() {
    875         LogCapture capture = new LogCapture(Level.WARN);
    876         Object[] args = new Object[] { "World", "Hello", ",", "." };
    877         LogMF.warn(logger, "{1}{2} {0}{3}", args);
    878         assertEquals("Hello, World.", capture.getMessage());
    879     }
    880
    881     /**
    882      * Test LogMF.log with null pattern.
    883      */
    884     public void testLogNullPattern() {
    885         LogCapture capture = new LogCapture(Level.ERROR);
    886         LogMF.log(logger, Level.ERROR, null, Math.PI);
    887         assertNull(capture.getMessage());
    888     }
    889
    890     /**
    891      * Test LogMF.log with no-field pattern.
    892      */
    893     public void testLogNoArg() {
    894         LogCapture capture = new LogCapture(Level.ERROR);
    895         LogMF.log(logger, Level.ERROR, "Hello, World", Math.PI);
    896         assertEquals("Hello, World", capture.getMessage());
    897     }
    898
    899     /**
    900      * Test LogMF.log with malformed pattern.
    901      */
    902     public void testLogBadPattern() {
    903         LogCapture capture = new LogCapture(Level.ERROR);
    904         LogMF.log(logger, Level.ERROR, "Hello, {.", Math.PI);
    905         assertEquals("Hello, {.", capture.getMessage());
    906     }
    907
    908     /**
    909      * Test LogMF.log with missing argument.
    910      */
    911     public void testLogMissingArg() {
    912         LogCapture capture = new LogCapture(Level.ERROR);
    913         LogMF.log(logger, Level.ERROR, "Hello, {0}World", new Object[0]);
    914         assertEquals("Hello, {0}World", capture.getMessage());
    915     }
    916
    917     /**
    918      * Test LogMF.log with single field pattern with string argument.
    919      */
    920     public void testLogString() {
    921         LogCapture capture = new LogCapture(Level.ERROR);
    922         LogMF.log(logger, Level.ERROR, "Hello, {0}", "World");
    923         assertEquals("Hello, World", capture.getMessage());
    924     }
    925
    926     /**
    927      * Test LogMF.log with single field pattern with null argument.
    928      */
    929     public void testLogNull() {
    930         LogCapture capture = new LogCapture(Level.ERROR);
    931         LogMF.log(logger, Level.ERROR, "Hello, {0}", (Object) null);
    932         assertEquals("Hello, null", capture.getMessage());
    933     }
    934
    935     /**
    936      * Test LogMF.log with single field pattern with int argument.
    937      */
    938     public void testLogInt() {
    939         LogCapture capture = new LogCapture(Level.ERROR);
    940         int val = 42;
    941         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    942         assertEquals("Iteration 42", capture.getMessage());
    943     }
    944
    945     /**
    946      * Test LogMF.log with single field pattern with byte argument.
    947      */
    948     public void testLogByte() {
    949         LogCapture capture = new LogCapture(Level.ERROR);
    950         byte val = 42;
    951         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    952         assertEquals("Iteration 42", capture.getMessage());
    953     }
    954
    955     /**
    956      * Test LogMF.log with single field pattern with short argument.
    957      */
    958     public void testLogShort() {
    959         LogCapture capture = new LogCapture(Level.ERROR);
    960         short val = 42;
    961         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    962         assertEquals("Iteration 42", capture.getMessage());
    963     }
    964
    965     /**
    966      * Test LogMF.log with single field pattern with long argument.
    967      */
    968     public void testLogLong() {
    969         LogCapture capture = new LogCapture(Level.ERROR);
    970         long val = 42;
    971         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    972         assertEquals("Iteration 42", capture.getMessage());
    973     }
    974
    975     /**
    976      * Test LogMF.log with single field pattern with char argument.
    977      */
    978     public void testLogChar() {
    979         LogCapture capture = new LogCapture(Level.ERROR);
    980         char val = 'C';
    981         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    982         assertEquals("Iteration C", capture.getMessage());
    983     }
    984
    985     /**
    986      * Test LogMF.log with single field pattern with boolean argument.
    987      */
    988     public void testLogBoolean() {
    989         LogCapture capture = new LogCapture(Level.ERROR);
    990         boolean val = true;
    991         LogMF.log(logger, Level.ERROR, "Iteration {0}", val);
    992         assertEquals("Iteration true", capture.getMessage());
    993     }
    994
    995     /**
    996      * Test LogMF.log with single field pattern with float argument.
    997      */
    998     public void testLogFloat() {
    999         LogCapture capture = new LogCapture(Level.ERROR);
    1000         LogMF.log(logger, Level.ERROR, "Iteration {0}", (float) Math.PI);
    1001
    1002         String expected = MessageFormat.format("Iteration {0}",
    1003                 new Object[] { new Float(Math.PI) });
    1004         assertEquals(expected, capture.getMessage());
    1005     }
    1006
    1007     /**
    1008      * Test LogMF.log with single field pattern with double argument.
    1009      */
    1010     public void testLogDouble() {
    1011         LogCapture capture = new LogCapture(Level.ERROR);
    1012         LogMF.log(logger, Level.ERROR, "Iteration {0}", Math.PI);
    1013
    1014         String expected = MessageFormat.format("Iteration {0}",
    1015                 new Object[] { new Double(Math.PI) });
    1016         assertEquals(expected, capture.getMessage());
    1017     }
    1018
    1019     /**
    1020      * Test LogMF.log with two arguments.
    1021      */
    1022     public void testLogTwoArg() {
    1023         LogCapture capture = new LogCapture(Level.ERROR);
    1024         LogMF.log(logger, Level.ERROR, "{1}, {0}.", "World", "Hello");
    1025         assertEquals("Hello, World.", capture.getMessage());
    1026     }
    1027
    1028     /**
    1029      * Test LogMF.log with three arguments.
    1030      */
    1031     public void testLogThreeArg() {
    1032         LogCapture capture = new LogCapture(Level.ERROR);
    1033         LogMF.log(logger, Level.ERROR, "{1}{2} {0}.", "World", "Hello", ",");
    1034         assertEquals("Hello, World.", capture.getMessage());
    1035     }
    1036
    1037     /**
    1038      * Test LogMF.log with four arguments.
    1039      */
    1040     public void testLogFourArg() {
    1041         LogCapture capture = new LogCapture(Level.ERROR);
    1042         LogMF.log(logger, Level.ERROR, "{1}{2} {0}{3}", "World", "Hello", ",", ".");
    1043         assertEquals("Hello, World.", capture.getMessage());
    1044     }
    1045
    1046     /**
    1047      * Test LogMF.log with Object[] argument.
    1048      */
    1049     public void testLogArrayArg() {
    1050         LogCapture capture = new LogCapture(Level.ERROR);
    1051         Object[] args = new Object[] { "World", "Hello", ",", "." };
    1052         LogMF.log(logger, Level.ERROR, "{1}{2} {0}{3}", args);
    1053         assertEquals("Hello, World.", capture.getMessage());
    1054     }
    1055
    1056     /**
    1057      * Bundle name for resource bundle tests.
    1058      */
    1059     private static final String BUNDLE_NAME =
    1060             "org.apache.log4j.TestLogMFPatterns";
    1061
    1062     /**
    1063      * Test LogMF.logrb with null bundle name.
    1064      */
    1065     public void testLogrbNullBundle() {
    1066         LogCapture capture = new LogCapture(Level.ERROR);
    1067         LogMF.logrb(logger, Level.ERROR, null, "Iteration0", Math.PI);
    1068         assertEquals("Iteration0", capture.getMessage());
    1069     }
    1070
    1071     /**
    1072      * Test LogMF.logrb with null key.
    1073      */
    1074     public void testLogrbNullKey() {
    1075         LogCapture capture = new LogCapture(Level.ERROR);
    1076         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, null, Math.PI);
    1077         assertNull(capture.getMessage());
    1078     }
    1079
    1080     /**
    1081      * Test LogMF.logrb with no-field pattern.
    1082      */
    1083     public void testLogrbNoArg() {
    1084         LogCapture capture = new LogCapture(Level.ERROR);
    1085         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Hello1", Math.PI);
    1086         assertEquals("Hello, World", capture.getMessage());
    1087     }
    1088
    1089     /**
    1090      * Test LogMF.logrb with malformed pattern.
    1091      */
    1092     public void testLogrbBadPattern() {
    1093         LogCapture capture = new LogCapture(Level.ERROR);
    1094         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Malformed", Math.PI);
    1095         assertEquals("Hello, {.", capture.getMessage());
    1096     }
    1097
    1098     /**
    1099      * Test LogMF.logrb with missing argument.
    1100      */
    1101     public void testLogrbMissingArg() {
    1102         LogCapture capture = new LogCapture(Level.ERROR);
    1103         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Hello2", new Object[0]);
    1104         assertEquals("Hello, {0}World", capture.getMessage());
    1105     }
    1106
    1107     /**
    1108      * Test LogMF.logrb with single field pattern with string argument.
    1109      */
    1110     public void testLogrbString() {
    1111         LogCapture capture = new LogCapture(Level.ERROR);
    1112         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Hello3", "World");
    1113         assertEquals("Hello, World", capture.getMessage());
    1114     }
    1115
    1116     /**
    1117      * Test LogMF.logrb with single field pattern with null argument.
    1118      */
    1119     public void testLogrbNull() {
    1120         LogCapture capture = new LogCapture(Level.ERROR);
    1121         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Hello3", (Object) null);
    1122         assertEquals("Hello, null", capture.getMessage());
    1123     }
    1124
    1125     /**
    1126      * Test LogMF.logrb with single field pattern with int argument.
    1127      */
    1128     public void testLogrbInt() {
    1129         LogCapture capture = new LogCapture(Level.ERROR);
    1130         int val = 42;
    1131         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1132         assertEquals("Iteration 42", capture.getMessage());
    1133     }
    1134
    1135     /**
    1136      * Test LogMF.logrb with single field pattern with byte argument.
    1137      */
    1138     public void testLogrbByte() {
    1139         LogCapture capture = new LogCapture(Level.ERROR);
    1140         byte val = 42;
    1141         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1142         assertEquals("Iteration 42", capture.getMessage());
    1143     }
    1144
    1145     /**
    1146      * Test LogMF.logrb with single field pattern with short argument.
    1147      */
    1148     public void testLogrbShort() {
    1149         LogCapture capture = new LogCapture(Level.ERROR);
    1150         short val = 42;
    1151         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1152         assertEquals("Iteration 42", capture.getMessage());
    1153     }
    1154
    1155     /**
    1156      * Test LogMF.logrb with single field pattern with long argument.
    1157      */
    1158     public void testLogrbLong() {
    1159         LogCapture capture = new LogCapture(Level.ERROR);
    1160         long val = 42;
    1161         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1162         assertEquals("Iteration 42", capture.getMessage());
    1163     }
    1164
    1165     /**
    1166      * Test LogMF.logrb with single field pattern with char argument.
    1167      */
    1168     public void testLogrbChar() {
    1169         LogCapture capture = new LogCapture(Level.ERROR);
    1170         char val = 'C';
    1171         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1172         assertEquals("Iteration C", capture.getMessage());
    1173     }
    1174
    1175     /**
    1176      * Test LogMF.logrb with single field pattern with boolean argument.
    1177      */
    1178     public void testLogrbBoolean() {
    1179         LogCapture capture = new LogCapture(Level.ERROR);
    1180         boolean val = true;
    1181         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", val);
    1182         assertEquals("Iteration true", capture.getMessage());
    1183     }
    1184
    1185     /**
    1186      * Test LogMF.logrb with single field pattern with float argument.
    1187      */
    1188     public void testLogrbFloat() {
    1189         LogCapture capture = new LogCapture(Level.ERROR);
    1190         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", (float) Math.PI);
    1191
    1192         String expected = MessageFormat.format("Iteration {0}",
    1193                 new Object[] { new Float(Math.PI) });
    1194         assertEquals(expected, capture.getMessage());
    1195     }
    1196
    1197     /**
    1198      * Test LogMF.logrb with single field pattern with double argument.
    1199      */
    1200     public void testLogrbDouble() {
    1201         LogCapture capture = new LogCapture(Level.ERROR);
    1202         LogMF.logrb(logger, Level.ERROR, BUNDLE_NAME, "Iteration0", Math.PI);
    1203
    1204         String expected = MessageFormat.format("Iteration {0}",
    1205                 new Object[] { new Double(Math.PI) });
    1206         assertEquals(expected, capture.getMessage());
    1207     }
    1208
    1209     /**
    1210      * Test LogMF.logrb with two arguments.
    1211      */
    1212     public void testLogrbTwoArg() {
    1213         LogCapture capture = new LogCapture(Level.ERROR);
    1214         LogMF.logrb(logger, Level.ERROR,
    1215                 BUNDLE_NAME, "Hello4", "World", "Hello");
    1216         assertEquals("Hello, World.", capture.getMessage());
    1217     }
    1218
    1219     /**
    1220      * Test LogMF.logrb with three arguments.
    1221      */
    1222     public void testLogrbThreeArg() {
    1223         LogCapture capture = new LogCapture(Level.ERROR);
    1224         LogMF.logrb(logger, Level.ERROR,
    1225                 BUNDLE_NAME, "Hello5", "World", "Hello", ",");
    1226         assertEquals("Hello, World.", capture.getMessage());
    1227     }
    1228
    1229     /**
    1230      * Test LogMF.logrb with four arguments.
    1231      */
    1232     public void testLogrbFourArg() {
    1233         LogCapture capture = new LogCapture(Level.ERROR);
    1234         LogMF.logrb(logger, Level.ERROR,
    1235                 BUNDLE_NAME, "Hello6", "World", "Hello", ",", ".");
    1236         assertEquals("Hello, World.", capture.getMessage());
    1237     }
    1238
    1239     /**
    1240      * Test LogMF.logrb with Object[] argument.
    1241      */
    1242     public void testLogrbArrayArg() {
    1243         LogCapture capture = new LogCapture(Level.ERROR);
    1244         Object[] args = new Object[] { "World", "Hello", ",", "." };
    1245         LogMF.logrb(logger, Level.ERROR,
    1246                 BUNDLE_NAME, "Hello6", args);
    1247         assertEquals("Hello, World.", capture.getMessage());
    1248     }
    1249
    1250
    1251     /**
    1252      * Test LogMF.info with a pattern containing {9} and one argument.
    1253      */
    1254     public void testInfo1ParamBrace9() {
    1255         LogCapture capture = new LogCapture(Level.INFO);
    1256         LogMF.info(logger, "Hello, {9}{0}", "World");
    1257         assertEquals("Hello, {9}World", capture.getMessage());
    1258     }
    1259
    1260     /**
    1261      * Test LogMF.info with a pattern containing {9} and two arguments.
    1262      */
    1263     public void testInfo2ParamBrace9() {
    1264         LogCapture capture = new LogCapture(Level.INFO);
    1265         LogMF.info(logger, "{1}, {9}{0}", "World", "Hello");
    1266         assertEquals("Hello, {9}World", capture.getMessage());
    1267     }
    1268
    1269     /**
    1270      * Test LogMF.info with a pattern containing {9} and two arguments.
    1271      */
    1272     public void testInfo10ParamBrace9() {
    1273         LogCapture capture = new LogCapture(Level.INFO);
    1274         LogMF.info(logger, "{1}, {9}{0}",
    1275                 new Object[] { "World", "Hello", null, null, null,
    1276                                 null, null, null, null, "New " });
    1277         assertEquals("Hello, New World", capture.getMessage());
    1278     }
    1279
    1280     /**
    1281      * Test LogMF.info with indexes just outside of 0 to 9.
    1282      */
    1283     public void testInfo1ParamBraceSlashColon() {
    1284         LogCapture capture = new LogCapture(Level.INFO);
    1285         String pattern = "Hello, {/}{0}{:}";
    1286         LogMF.info(logger, pattern, "World");
    1287         assertEquals(pattern, capture.getMessage());
    1288     }
    1289
    1290
    1291 }