1   // $Id: PdfSerializerTest.java,v 1.7 2004/08/02 20:36:08 powerpete Exp $
2   package com.pnpconsult.zeiterfassung.pdf;
3   
4   import java.io.FileOutputStream;
5   import javax.xml.transform.Source;
6   import javax.xml.transform.stream.StreamSource;
7   
8   import com.pnpconsult.zeiterfassung.util.FileUtils;
9   
10  import junit.framework.TestCase;
11  
12  /***
13   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen </a>
14   * @version $Id: PdfSerializerTest.java,v 1.7 2004/08/02 20:36:08 powerpete Exp $
15   */
16  public class PdfSerializerTest extends TestCase
17  {
18      public PdfSerializerTest(String name)
19      {
20          super(name);
21      }
22  
23      /***
24  	 * This test technically tests just the creation of a pdf output file.
25  	 * It runs successfully, if no exception occurs.
26       * 
27       * @throws Exception
28       */
29      public void testSerialize() throws Exception
30      {
31          // This test doesn't run successfully under Maven, so it is
32          // disabled.
33          if (true) return;
34          
35          // Test 10 threads concurrently.
36          for (int i = 0; i < 10; i++)
37          {
38              new Thread(new RenderRunnable(this)).start();
39          }
40          synchronized (this)
41          {
42              wait();
43          }
44      }
45  
46      private static class RenderRunnable implements Runnable
47      {
48          private static int instanceCount = 0;
49  
50          private Object parent;
51  
52          public RenderRunnable(Object parent)
53          {
54              this.parent = parent;
55          }
56  
57          public void run()
58          {
59              try
60              {
61                  FileOutputStream out = new FileOutputStream("target/test_"
62  					+ (instanceCount++)
63  					+ ".pdf");
64  				Source input = new StreamSource(FileUtils.toFileName(
65  					getClass(),
66                          "table.xml"));
67                  new PdfSerializer().serialize(input, out);
68                  out.flush();
69                  out.close();
70                  instanceCount--;
71              }
72              catch (Exception e)
73              {
74                  e.printStackTrace();
75                  fail("Exception occured.");
76                  notifyParent();
77              }
78              finally
79              {
80                  if (instanceCount <= 0)
81                  {
82                      notifyParent();
83                  }
84              }
85          }
86  
87          private void notifyParent()
88          {
89              synchronized (parent)
90              {
91                  parent.notify();
92              }
93          }
94      }
95  }