001package org.apache.commons.digester3.binder; 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 junit.framework.Assert.assertEquals; 023import static junit.framework.Assert.fail; 024import static org.apache.commons.digester3.binder.DigesterLoader.newLoader; 025 026import java.io.IOException; 027import java.net.URL; 028import java.util.concurrent.LinkedBlockingQueue; 029import java.util.concurrent.ThreadPoolExecutor; 030import java.util.concurrent.TimeUnit; 031 032import org.apache.commons.digester3.Digester; 033import org.apache.commons.digester3.xmlrules.FromXmlRulesModule; 034import org.junit.Before; 035import org.junit.Test; 036import org.xml.sax.SAXException; 037 038/** 039 * Test. 040 */ 041public class Digester163TestCase 042{ 043 044 public static final int MAX_THREADS = 4; 045 046 private DigesterLoader loader = null; 047 048 @Before 049 public void before() 050 { 051 final URL url = getClass().getResource( "rules.xml" ); 052 loader = newLoader( new FromXmlRulesModule() 053 { 054 @Override 055 protected void loadRules() 056 { 057 loadXMLRules( url ); 058 } 059 060 } ); 061 } 062 063 @Test 064 public void testSingle() 065 throws IOException, SAXException 066 { 067 Digester dig = loader.newDigester(); 068 URL url = Digester163TestCase.class.getResource( "test.xml" ); 069 // lets parse - result does not matter here 070 Entity et = dig.parse( url ); 071 assertEquals( "Author 1", et.getAuthor() ); 072 } 073 074 @Test 075 public void test() 076 throws InterruptedException 077 { 078 ThreadPoolExecutor executor = new ThreadPoolExecutor( MAX_THREADS, 079 MAX_THREADS, 080 Long.MAX_VALUE, 081 TimeUnit.NANOSECONDS, 082 new LinkedBlockingQueue<Runnable>() ); 083 final URL url = Digester163TestCase.class.getResource( "test.xml" ); 084 final LinkedBlockingQueue<Exception> exceptions = new LinkedBlockingQueue<Exception>(); 085 for ( int i = 0; i < MAX_THREADS * 2; i++ ) 086 { 087 executor.submit( new Runnable() 088 { 089 public void run() 090 { 091 try 092 { 093 Digester dig = loader.newDigester(); 094 // lets parse - result does not matter here 095 dig.parse( url ); 096 } 097 catch ( Exception e ) 098 { 099 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}