⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testpackage.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
  (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
  [See end of file]
  $Id: TestPackage.java,v 1.20 2007/01/02 11:50:23 andy_seaborne Exp $
*/
/*
 * EnhancedTestSuite.java
 *
 * Created on 27 November 2002, 04:53
 */

package com.hp.hpl.jena.enhanced.test;

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.test.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.enhanced.*;

import junit.framework.*;

/**
 * These tests give a small version of a model-like interface
 {@link TestModel} with different views
 * over the nodes in the graph {@link TestSubject},
 *{@link TestProperty} {@link TestObject} 
 *Any node can be any one of these three, but the interface only works
 *if the node is the subject, property or object, respectively,
  of some triple in the graph.
 *There are two implementations of the three interfaces. We use four
 * different
 *personalities, in the tests, from various combinations of the implementation
 *classes with the interface classes. A more realistic test would be a basic set
 *of interfaces with implementations, and then some more extended interfaces and
 *implementations which can work together.
 *
 *These tests only test EnhNode polymorphism and not EnhGraph polymorphism.
 *EnhGraph polymorphism currently will not work.
 *(For Jena2.0 I am imagining that there will be ModelCom and DAMLModelImpl as
 *the only two implementations, and they can inherit one from the other).
 * @author  jjc
 */
public class TestPackage extends GraphTestBase  {
    
	static final private  GraphPersonality split = new GraphPersonality();
        
	static final private GraphPersonality combo = new GraphPersonality();
        
        
	static final private GraphPersonality bitOfBoth = new GraphPersonality();
	static final private GraphPersonality broken = new GraphPersonality();
	static {
            // Setting up the personalities, involves registering how
            // each interface is implemented by default.
            // Note this does not guarantee that the only implementations
            // of each interface will be the one specified.
            // See bitOfBoth.
        split.add( TestObject.class, TestObjectImpl.factory );
        split.add( TestSubject.class, TestSubjectImpl.factory );
        split.add( TestProperty.class, TestPropertyImpl.factory );
        
        combo.add( TestObject.class, TestAllImpl.factory );
        combo.add( TestSubject.class, TestAllImpl.factory );
        combo.add( TestProperty.class, TestAllImpl.factory );
        
        bitOfBoth.add( TestObject.class, TestObjectImpl.factory );
        bitOfBoth.add( TestSubject.class, TestSubjectImpl.factory );
        bitOfBoth.add( TestProperty.class, TestAllImpl.factory );
        
        // broken is misconfigured and must throw an exception.
        broken.add(TestObject.class, TestObjectImpl.factory );
        broken.add( TestSubject.class, TestSubjectImpl.factory );
        broken.add( TestProperty.class, TestObjectImpl.factory );
	}
    /** Creates a new instance of EnhancedTestSuite */
   	public TestPackage(String name)
		{
		super( name );
		}
		
    public static TestSuite suite()
        { return new TestSuite( TestPackage.class ); }
    
    /**
        test that equals works on an EnhNode (after hedgehog introduced FrontsNode
        it didn't).
    */
    public void testEquals()
        {
        EnhNode a = new EnhNode( Node.create( "eg:example" ), null );
        assertEquals( a, a );
        }
        
    /**
     * View n as intf. This is supported iff rslt.
     */
    private static void miniAsSupports(String title, TestNode n, Class intf, boolean rslt ) {
        assertTrue(title +":sanity",n instanceof Polymorphic);
        
        // It is always possible to view any node with any interface.
        TestNode as1 = (TestNode)((EnhNode)n).viewAs(intf);
        TestNode as2 = (TestNode)((EnhNode)n).viewAs(intf);
        
        // caching should ensure we get the same result both times.
        assertTrue( title + ":idempotency", as1==as2 );
        
        // Whether the interface is actually useable depends on the underlying
        // graph. This factoid is the rslt parameter.
        assertEquals( title +":support",rslt,((EnhNode) as1).supports( intf ) ); 
    }
    
    private static void oneNodeAsSupports(String title, TestNode n, boolean rslts[] ) {
    	// Try n with all three interfaces.
        miniAsSupports(title+"/TestSubject",n,TestSubject.class,rslts[0]);
        miniAsSupports(title+"/TestProperty",n,TestProperty.class,rslts[1]);
        miniAsSupports(title+"/TestObject",n,TestObject.class,rslts[2]);
    }
    
    private static void manyNodeAsSupports(String title, TestNode n[], boolean rslts[][] ) {
    	// Try each n with each interface.
        for (int i=0;i<n.length;i++){
          oneNodeAsSupports(title+"["+i+"]",n[i],rslts[i]);
        }
    }
    

    /** This test show the basic format of an enhanced test.
     *  This test access data in an enhanced fashion. 
     *  All modifications are done through the underlying graph.
     *  The methods tested are as and supports.
     */
    private static void basic(String title, Personality p) {
        Graph g = Factory.createGraphMem();
        TestModel model =  new TestModelImpl(g,p);
        // create some data
        graphAdd( g, "x R y;" );
        
        // The graph has three nodes, extract them as TestNode's,
        // using the minimalist ModelAPI.
        TestNode nodes[] = new TestNode[]{
            model.aSubject(),
            model.aProperty(),
            model.anObject()
        };
        
        // Run the basic tests.
        manyNodeAsSupports(title+"(a)",nodes, 
           new boolean[][]{
               new boolean[]{true,false,false}, // nodes[0] is subj, but not prop, or obj
               new boolean[]{false,true,false},
               new boolean[]{false,false,true}
        });
        
        graphAdd(g,"y R x;" );
        
        // The expected results are now different.
        // (A node is appropriate for the TestSubject interface if it is
        // the subject of some triple in the graph, so the third node
        // can now be a TestSubject).
        manyNodeAsSupports(title+"(b)",nodes, 
           new boolean[][]{
               new boolean[]{true,false,true}, // nodes[0] is subj and obj, but not prop
               new boolean[]{false,true,false},
               new boolean[]{true,false,true}
        });
        
        g.delete( triple( "x R y" ) );

    	// The expected results are now different again.
    	// (A node is appropriate for the TestSubject interface if it is
    	// the subject of some triple in the graph, so the third node
    	// can now be a TestSubject).
        
        manyNodeAsSupports(title+"(c)",nodes, 
           new boolean[][]{
               new boolean[]{false,false,true}, 
               new boolean[]{false,true,false},
               new boolean[]{true,false,false}
        });
        
        
    }

    /** 
        Would like to get rid of these, but the abstraction is hard to find at the
        moment. At least they're now just local to this test class.
    */
    static final int S = 1;
    static final int P = 2;
    static final int O = 3;
    
    // This is like the earlier test: miniAsSupports (the last part of it).
    // However, this time instead of asking whether the interface will work
    // or not, we just try it.
    // Obviously sometimes it is broken, which should be reported using
    // an IllegalStateException.
	private  void canImplement(String title, TestNode n, int wh, boolean rslt ) {
		try {
			switch (wh) {
				case S:
					n.asSubject().aProperty();
					break;
				case P:
					n.asProperty().anObject();
					break;
				case O:
					n.asObject().aSubject();
					break;
			}
			assertTrue("IllegalStateException expected.",rslt);
		}
		catch (IllegalStateException e) {
			assertFalse("IllegalStateException at the wrong time.",rslt);
		}
	}

	private  void canImplement(String title, TestNode n, boolean rslts[] ) {
		canImplement(title+"/TestSubject",n,S,rslts[0]);
		canImplement(title+"/TestProperty",n,P,rslts[1]);
		canImplement(title+"/TestObject",n,O,rslts[2]);
	}
	private  void canImplement(String title, TestNode n[], boolean rslts[][] ) {
		for (int i=0;i<n.length;i++){
		  canImplement(title+"["+i+"]",n[i],rslts[i]);
		}
	}
	
    private  void follow(String title, Personality p) {
        Graph g = Factory.createGraphMem();
        TestModel model =  new TestModelImpl(g,p);
        // create some data
        graphAdd( g, "a b c;" );
        TestNode nodes[] = new TestNode[]{
            model.aSubject(),
            model.aProperty(),
            model.anObject()
        };
        
        // Similar to the basic test.
        canImplement(title+"(a)",nodes, 
           new boolean[][]{
               new boolean[]{true,false,false}, 
               new boolean[]{false,true,false},
               new boolean[]{false,false,true}
        });
        
        graphAdd(g, "b a c;" );

    	// Again like in the basic test the triples have now changed,
    	// so different methods will now work.
        canImplement(title+"(b)",nodes, 
           new boolean[][]{
               new boolean[]{true,true,false}, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -