1   /**
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j.sample;
17  
18  import org.junit.Test;
19  
20  import org.junit.runner.RunWith;
21  
22  import org.junit.runners.Parameterized;
23  import org.junit.runners.Parameterized.Parameters;
24  
25  import java.io.FileOutputStream;
26  import java.io.PrintStream;
27  
28  import java.lang.reflect.Method;
29  
30  import java.util.Arrays;
31  import java.util.Collection;
32  
33  @RunWith(Parameterized.class)
34  public class SampleTest
35  {
36  
37      static
38      {
39          System.setProperty("java.util.prefs.PreferencesFactory", "org.ini4j.IniPreferencesFactory");
40      }
41  
42      private static final String SAMPLE_DIR = System.getProperty("basedir") + "/src/test/java/org/ini4j/sample/";
43      private static final String OUT_DIR = System.getProperty("basedir") + "/target/site/sample/";
44      private final Class _sampleClass;
45  
46      public SampleTest(Class sampleClass)
47      {
48          _sampleClass = sampleClass;
49      }
50  
51      @Parameters public static Collection data()
52      {
53          return Arrays.asList(
54                  new Object[][]
55                  {
56                      { ReadStringSample.class },
57                      { ReadPrimitiveSample.class },
58                      { WriteSample.class },
59                      { IniSample.class },
60                      { StreamSample.class },
61                      { DumpSample.class },
62                      { NoImportSample.class },
63                      { ListenerSample.class },
64                      { FromSample.class },
65                      { ToSample.class },
66                      { PyReadSample.class }
67                  });
68      }
69  
70      @SuppressWarnings("unchecked")
71      @Test public void testMain() throws Exception
72      {
73          Method main = _sampleClass.getDeclaredMethod("main", String[].class);
74          String[] args;
75  
76          try
77          {
78              String filename = (String) _sampleClass.getDeclaredField("FILENAME").get(null);
79  
80              args = new String[] { SAMPLE_DIR + filename };
81          }
82          catch (NoSuchFieldException x)
83          {
84              args = new String[] {};
85          }
86  
87          System.out.println("Executing " + _sampleClass.getName());
88          PrintStream saved = System.out;
89          PrintStream out = new PrintStream(new FileOutputStream(OUT_DIR + _sampleClass.getSimpleName() + ".txt"));
90  
91          System.setOut(out);
92          try
93          {
94              main.invoke((Object) null, (Object) args);
95          }
96          finally
97          {
98              System.setOut(saved);
99              out.flush();
100         }
101     }
102 }