1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */ 
19  package org.apache.rat.analysis.util;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.rat.analysis.IHeaderMatcher;
24  import org.apache.rat.analysis.MockLicenseMatcher;
25  import org.apache.rat.api.Document;
26  import org.apache.rat.document.MockLocation;
27  import org.apache.rat.report.claim.impl.xml.MockClaimReporter;
28  
29  public class MatcherMultiplexerTest extends TestCase {
30  
31  	private static final String LINE_ONE = "Line One";
32  	private static final String LINE_TWO = "Line Two";
33  	
34      MockClaimReporter reporter;
35  	MockLicenseMatcher matcherOne;
36  	MockLicenseMatcher matcherTwo;
37  
38  	HeaderMatcherMultiplexer multiplexer;
39  	
40  	protected void setUp() throws Exception {
41  		super.setUp();
42  		matcherOne = new MockLicenseMatcher();
43  		matcherTwo = new MockLicenseMatcher();
44  		IHeaderMatcher[] matchers = {matcherOne, matcherTwo};
45  		multiplexer = new HeaderMatcherMultiplexer(matchers);
46          reporter = new MockClaimReporter();
47  	}
48  
49  	protected void tearDown() throws Exception {
50  		super.tearDown();
51  	}
52  	
53  	public void testMatcherLine() throws Exception {
54          matcherOne.result = false;
55          matcherTwo.result = false;
56          final Document subject = new MockLocation("subject");
57  		multiplexer.match(subject, LINE_ONE);
58  		assertEquals("One line", 1, matcherOne.lines.size());
59  		assertEquals("Same as line passed", LINE_ONE, matcherOne.lines.get(0));
60  		assertEquals("One line", 1, matcherTwo.lines.size());
61  		assertEquals("Same as line passed", LINE_ONE, matcherTwo.lines.get(0));
62  		multiplexer.match(subject, LINE_TWO);
63  		assertEquals("One line", 2, matcherOne.lines.size());
64  		assertEquals("Same as line passed", LINE_TWO, matcherOne.lines.get(1));
65  		assertEquals("One line", 2, matcherTwo.lines.size());
66  		assertEquals("Same as line passed", LINE_TWO, matcherTwo.lines.get(1));
67  	}
68  	
69  	public void testReset() {
70  		multiplexer.reset();
71  		assertEquals("Reset once", 1, matcherOne.resets);
72  		assertEquals("Reset once", 1, matcherTwo.resets);
73  		multiplexer.reset();
74  		assertEquals("Reset twice", 2, matcherOne.resets);
75  		assertEquals("Reset twice", 2, matcherTwo.resets);
76  	}
77  }