001package org.apache.commons.digester3;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static java.lang.System.getProperty;
023import static java.util.concurrent.Executors.newFixedThreadPool;
024import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
025import static org.junit.Assert.assertNotNull;
026
027import java.io.File;
028import java.io.InputStreamReader;
029import java.util.concurrent.Future;
030
031import org.apache.commons.digester3.binder.AbstractRulesModule;
032import org.apache.commons.digester3.binder.DigesterLoader;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.Test;
036import org.xml.sax.InputSource;
037
038public final class AsyncReaderTestCase
039{
040
041    private final DigesterLoader digesterLoader = newLoader( new AbstractRulesModule()
042    {
043
044        @Override
045        protected void configure()
046        {
047            forPattern( "employee" ).createObject().ofType( Employee.class );
048        }
049
050    } ).setExecutorService( newFixedThreadPool( 1 ) );
051
052    private Digester digester;
053
054    @Before
055    public void setUp()
056    {
057        digester = digesterLoader.newDigester();
058    }
059
060    @After
061    public void tearDown()
062    {
063        digester = null;
064    }
065
066    @Test
067    public void parseFromFile()
068        throws Exception
069    {
070        Future<Employee> future = digester.asyncParse( new File( getProperty( "user.dir" ),
071            "src/test/resources/org/apache/commons/digester3/Test9.xml" ) );
072        verify( future );
073    }
074
075    @Test
076    public void parseFromClasspathURL()
077        throws Exception
078    {
079        Future<Employee> future = digester.asyncParse( getClass().getResource( "Test9.xml" ) );
080        verify( future );
081    }
082
083    @Test
084    public void parseFromInputStream()
085        throws Exception
086    {
087        Future<Employee> future = digester.asyncParse( getClass().getResource( "Test9.xml" ).openStream() );
088        verify( future );
089    }
090
091    @Test
092    public void parseFromInputSource()
093        throws Exception
094    {
095        Future<Employee> future =
096            digester.asyncParse( new InputSource( getClass().getResource( "Test9.xml" ).openStream() ) );
097        verify( future );
098    }
099
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}