View Javadoc

1   package org.apache.commons.digester3.binder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static junit.framework.Assert.assertEquals;
23  import static junit.framework.Assert.fail;
24  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
25  
26  import java.io.IOException;
27  import java.net.URL;
28  import java.util.concurrent.LinkedBlockingQueue;
29  import java.util.concurrent.ThreadPoolExecutor;
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.commons.digester3.Digester;
33  import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.xml.sax.SAXException;
37  
38  /**
39   * Test.
40   */
41  public class Digester163TestCase
42  {
43  
44      public static final int MAX_THREADS = 4;
45  
46      private DigesterLoader loader = null;
47  
48      @Before
49      public void before()
50      {
51          final URL url = getClass().getResource( "rules.xml" );
52          loader = newLoader( new FromXmlRulesModule()
53          {
54              @Override
55              protected void loadRules()
56              {
57                  loadXMLRules( url );
58              }
59  
60          } );
61      }
62  
63      @Test
64      public void testSingle()
65          throws IOException, SAXException
66      {
67          Digester dig = loader.newDigester();
68          URL url = Digester163TestCase.class.getResource( "test.xml" );
69          // lets parse - result does not matter here
70          Entity et = dig.parse( url );
71          assertEquals( "Author 1", et.getAuthor() );
72      }
73  
74      @Test
75      public void test()
76          throws InterruptedException
77      {
78          ThreadPoolExecutor executor = new ThreadPoolExecutor( MAX_THREADS,
79                                                                MAX_THREADS,
80                                                                Long.MAX_VALUE,
81                                                                TimeUnit.NANOSECONDS,
82                                                                new LinkedBlockingQueue<Runnable>() );
83          final URL url = Digester163TestCase.class.getResource( "test.xml" );
84          final LinkedBlockingQueue<Exception> exceptions = new LinkedBlockingQueue<Exception>();
85          for ( int i = 0; i < MAX_THREADS * 2; i++ )
86          {
87              executor.submit( new Runnable()
88              {
89                  public void run()
90                  {
91                      try
92                      {
93                          Digester dig = loader.newDigester();
94                          // lets parse - result does not matter here
95                          dig.parse( url );
96                      }
97                      catch ( Exception e )
98                      {
99                          exceptions.add( e );
100                     }
101                 }
102             } );
103         }
104 
105         while ( !executor.awaitTermination( 10, TimeUnit.MILLISECONDS ) )
106         {
107             if ( executor.getQueue().isEmpty() )
108             {
109                 executor.shutdown();
110             }
111             if ( executor.isTerminated() )
112             {
113                 break;
114             }
115         }
116 
117         Exception e = exceptions.peek();
118         if ( e != null )
119         {
120             while ( true )
121             {
122                 e = exceptions.poll();
123                 if ( e == null )
124                 {
125                     break;
126                 }
127                 e.printStackTrace();
128             }
129             fail( "Caught " + exceptions.size() + " exceptions." );
130         }
131     }
132 
133 }