/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.ws.notification.topics.impl;
import junit.framework.TestCase;
import org.apache.ws.notification.base.impl.XmlBeansTopicExpression;
import org.apache.ws.notification.topics.Topic;
import org.apache.ws.notification.topics.TopicSpace;
import org.apache.ws.notification.topics.TopicSpaceSet;
import org.apache.ws.notification.topics.expression.InvalidTopicExpressionException;
import org.apache.ws.notification.topics.expression.TopicExpressionEvaluator;
import org.apache.ws.notification.topics.expression.impl.FullTopicExpressionEvaluator;
import org.apache.xmlbeans.XmlObject;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionType;
/**
* Testcase for {@link FullTopicExpressionEvaluator}
*/
public class FullTopicExpressionEvaluatorTestCase
extends TestCase
{
private static final String NSURI1 = "http://ns1.com/";
private static final String NSPREFIX1 = "ns1";
private static final String NSURI2 = "http://ns2.com/";
private static final String NSPREFIX2 = "ns2";
private TopicExpressionEvaluator m_evaluator;
private TopicSpaceSet m_topicSpaceSet;
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testEvaluateConjoined( )
throws Exception
{
Topic[] resultTopics = evaluate( NSPREFIX1 + ":sports/tennis|" + NSPREFIX2 + ":bands/*" );
assertEquals( 3, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "beatles" );
assertContainsTopic( resultTopics, "kinks" );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testEvaluateInvalid( )
throws Exception
{
// first check expressions that do not conform to the Full Topic Expression grammar defined by the WS-Topics spec
assertExpressionIsInvalid( NSPREFIX1 + ":" );
assertExpressionIsInvalid( ":sports" );
assertExpressionIsInvalid( NSPREFIX1 + "::sports" );
assertExpressionIsInvalid( NSPREFIX1 + ":sports:tennis" );
assertExpressionIsInvalid( NSPREFIX1 + ":." );
assertExpressionIsInvalid( NSPREFIX1 + "://." );
assertExpressionIsInvalid( NSPREFIX1 + ":sports///tennis" );
// now check expressions that violate other validity constraints defined by the spec
assertExpressionIsInvalid( NSPREFIX1 + ":sports/baseball" ); // fixed topic set but empty result set
assertExpressionIsInvalid( NSPREFIX1 + ":bands|" + NSPREFIX2 + ":bands" ); // undefined root topic
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testEvaluateRecursive( )
throws Exception
{
Topic[] resultTopics = evaluate( NSPREFIX1 + ":sports//college" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "college" );
resultTopics = evaluate( NSPREFIX1 + ":sports//*" );
assertEquals( 4, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
assertContainsTopic( resultTopics, "college" );
resultTopics = evaluate( NSPREFIX1 + ":sports//." );
assertEquals( 5, resultTopics.length );
assertContainsTopic( resultTopics, "sports" );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
assertContainsTopic( resultTopics, "college" );
resultTopics = evaluate( NSPREFIX1 + "://*" );
assertEquals( 8, resultTopics.length );
assertContainsTopic( resultTopics, "sports" );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
assertContainsTopic( resultTopics, "college" );
assertContainsTopic( resultTopics, "games" );
assertContainsTopic( resultTopics, "monopoly" );
assertContainsTopic( resultTopics, "chess" );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testEvaluateSimple( )
throws Exception
{
Topic[] resultTopics = evaluate( "celebs" );
assertEquals( 1, resultTopics.length );
resultTopics = evaluate( NSPREFIX1 + ":sports" );
assertEquals( 1, resultTopics.length );
assertContainsTopic( resultTopics, "sports" );
resultTopics = evaluate( NSPREFIX1 + ":sports/tennis" );
assertEquals( 1, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
( (TopicSpaceSetImpl) m_topicSpaceSet ).setFixed( false );
resultTopics = evaluate( NSPREFIX1 + ":sports/baseball" );
assertEquals( 0, resultTopics.length );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public void testEvaluateWildcard( )
throws Exception
{
Topic[] resultTopics = evaluate( NSPREFIX1 + ":*" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "sports" );
assertContainsTopic( resultTopics, "games" );
resultTopics = evaluate( NSPREFIX1 + ":*/tennis" );
assertEquals( 1, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
resultTopics = evaluate( NSPREFIX1 + ":sports/*" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
resultTopics = evaluate( NSPREFIX1 + ":sports/." );
assertEquals( 3, resultTopics.length );
assertContainsTopic( resultTopics, "sports" );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
resultTopics = evaluate( NSPREFIX1 + ":sports/./college" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "college" );
resultTopics = evaluate( NSPREFIX1 + ":sports/*/college" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "college" );
resultTopics = evaluate( NSPREFIX1 + ":*/*" );
assertEquals( 4, resultTopics.length );
assertContainsTopic( resultTopics, "tennis" );
assertContainsTopic( resultTopics, "football" );
assertContainsTopic( resultTopics, "monopoly" );
assertContainsTopic( resultTopics, "chess" );
resultTopics = evaluate( NSPREFIX1 + ":*/*/college" );
assertEquals( 2, resultTopics.length );
assertContainsTopic( resultTopics, "college" );
}
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
protected void setUp( )
throws Exception
{
m_evaluator = new FullTopicExpressionEvaluator( );
m_topicSpaceSet = new TopicSpaceSetImpl( true );
TopicSpace topicSpace0 = m_topicSpaceSet.addTopicSpace( new TopicSpaceImpl( "" ) );
topicSpace0.addTopic( "celebs" );
TopicSpace topicSpace1 = m_topicSpaceSet.addTopicSpace( new TopicSpaceImpl( NSURI1 ) );
Topic sportsTopic = topicSpace1.addTopic( "sports" );
Topic tennisTopic = sportsTopic.addTopic( "tennis" );
tennisTopic.addTopic( "college" );
Topic footballTopic = sportsTopic.addTopic( "football" );
footballTopic.addTopic( "college" );
Topic gamesTopic = topicSpace1.addTopic( "games" );
gamesTopic.addTopic( "monopoly" );
gamesTopic.addTopic( "chess" );
TopicSpace topicSpace2 = m_topicSpaceSet.addTopicSpace( new TopicSpaceImpl( NSURI2 ) );
Topic bandsTopic = topicSpace2.addTopic( "bands" );
bandsTopic.addTopic( "beatles" );
bandsTopic.addTopic( "kinks" );
}
private void assertContainsTopic( Topic[] topics,
String name )
{
boolean foundIt = false;
for ( int i = 0; i < topics.length; i++ )
{
if ( topics[i].getName( ).equals( name ) )
{
foundIt = true;
}
}
assertTrue( foundIt );
}
private void assertExpressionIsInvalid( String expr )
throws Exception
{
try
{
evaluate( expr );
fail( );
}
catch ( InvalidTopicExpressionException itee )
{
// success!
}
}
private Topic[] evaluate( String expr )
throws Exception
{
TopicExpressionType topicExpr =
( (TopicExpressionDocument) XmlObject.Factory.parse( "<wsnt:TopicExpression xmlns:wsnt='http://docs.oasis-open.org/wsn/2004/06/wsn-WS-BaseNotification-1.2-draft-01.xsd' "
+ "xmlns:" + NSPREFIX1 + "='" + NSURI1 + "' xmlns:"
+ NSPREFIX2 + "='" + NSURI2
+ "' Dialect='http://docs.oasis-open.org/wsn/2004/06/TopicExpression/Full'>"
+ expr + "</wsnt:TopicExpression>" ) )
.getTopicExpression( );
return m_evaluator.evaluate( m_topicSpaceSet,
new XmlBeansTopicExpression( topicExpr ) );
}
}