View Javadoc

1   package org.apache.commons.digester3;
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 java.lang.System.getProperty;
23  import static java.util.concurrent.Executors.newFixedThreadPool;
24  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
25  import static org.junit.Assert.assertNotNull;
26  
27  import java.io.File;
28  import java.io.InputStreamReader;
29  import java.util.concurrent.Future;
30  
31  import org.apache.commons.digester3.binder.AbstractRulesModule;
32  import org.apache.commons.digester3.binder.DigesterLoader;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.xml.sax.InputSource;
37  
38  public final class AsyncReaderTestCase
39  {
40  
41      private final DigesterLoader digesterLoader = newLoader( new AbstractRulesModule()
42      {
43  
44          @Override
45          protected void configure()
46          {
47              forPattern( "employee" ).createObject().ofType( Employee.class );
48          }
49  
50      } ).setExecutorService( newFixedThreadPool( 1 ) );
51  
52      private Digester digester;
53  
54      @Before
55      public void setUp()
56      {
57          digester = digesterLoader.newDigester();
58      }
59  
60      @After
61      public void tearDown()
62      {
63          digester = null;
64      }
65  
66      @Test
67      public void parseFromFile()
68          throws Exception
69      {
70          Future<Employee> future = digester.asyncParse( new File( getProperty( "user.dir" ),
71              "src/test/resources/org/apache/commons/digester3/Test9.xml" ) );
72          verify( future );
73      }
74  
75      @Test
76      public void parseFromClasspathURL()
77          throws Exception
78      {
79          Future<Employee> future = digester.asyncParse( getClass().getResource( "Test9.xml" ) );
80          verify( future );
81      }
82  
83      @Test
84      public void parseFromInputStream()
85          throws Exception
86      {
87          Future<Employee> future = digester.asyncParse( getClass().getResource( "Test9.xml" ).openStream() );
88          verify( future );
89      }
90  
91      @Test
92      public void parseFromInputSource()
93          throws Exception
94      {
95          Future<Employee> future =
96              digester.asyncParse( new InputSource( getClass().getResource( "Test9.xml" ).openStream() ) );
97          verify( future );
98      }
99  
100     @Test
101     public void parseFromReader()
102         throws Exception
103     {
104         Future<Employee> future =
105             digester.asyncParse( new InputStreamReader( getClass().getResource( "Test9.xml" ).openStream() ) );
106         verify( future );
107     }
108 
109     @Test
110     public void parseFromUri()
111         throws Exception
112     {
113         Future<Employee> future = digester.asyncParse( getClass().getResource( "Test9.xml" ).toExternalForm() );
114         verify( future );
115     }
116 
117     private void verify( Future<Employee> result )
118         throws Exception
119     {
120         Employee employee = result.get();
121         assertNotNull( employee );
122     }
123 
124 }