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