Package run.acceptance.support

Source Code of run.acceptance.support.State

/*
  (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
   All rights reserved.
   $Id$
*/


/**
*
*/
package run.acceptance.support;

import static run.acceptance.support.ReaderUtils.modelFromString;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import run.accept;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.jena.ymris.grammar.Ymris;
import com.hp.jena.ymris.yast.YAST;

public class State
    {
    YAST yast;
    Model model;
   
    static final String RULES = "rules";
    static final String MODEL = "model";
    static final String COMMENT = "comment";
    static final String TITLE = "title";
    static final String EXPECT = "expect";
   
    Map<String, String> contents = new HashMap<String, String>();
   
    List<AcceptanceTestItem> probes = new ArrayList<AcceptanceTestItem>();
   
    public void setRulesFromString( String b )
        {
        contents.put( RULES, b );
        try
            {
            Ymris y = new Ymris( new StringReader( b ) );
            yast = y.ymris();
//            rules = rulesetFromString( b );
            probes.add( new RS( b ) );
            }
        catch (Exception e)
            { probes.add( new RulesReadFailure( e, b ) ); }
        }
   
    public void setModelfromString( String b )
        {
        contents.put( MODEL, b );
        try
            {
            model = modelFromString( b );
            }
        catch (Exception e)
            {
            model = ModelFactory.createDefaultModel();           
            probes.add( new ModelReadFailure( e, b ) );
            }
        }
   
    public void expectModel( String b )
        {
        contents.put( EXPECT, b );
        Model actual = accept.applyRules( yast, model );
        boolean isOK = modelFromString( b ).isIsomorphicWith( actual );
        probes.add( new Expect( contents, actual, isOK ) );
        }
   
    public void displayResults( PrintStream out )
        {
        int total = countTests( probes ), passed = countOK( probes );
        boolean allPassed = total == passed;
        out.println( "<html>" );
        out.println( "<head>" );
        out.println( "<title>acceptance test results</title>" );
        out.println( "<link class='text/css' href='styles/accept.css' rel='stylesheet'></link>" );
        out.println( "<style>" );
        out.println( "body { background: " + (allPassed ? "#eeffee" : "ffeeee") + " }" );
        out.println( ".failed { background: #ffcccc }" );
        out.println( ".model { margin-left: 1em; margin-right: 1em; background: yellow }" );
        out.println( ".ruleset { margin-left: 1em; margin-right: 1em; background: #ffffee }" );
        out.println( "</style>" );
        out.println( "</head>" );
        out.println( "<body>" );
        out.println( "<h1>" + (allPassed ? "all " + total + " tests passed." : "only " + passed + " tests of " + total + " passed.") + "</h1>" );
        for (AcceptanceTestItem p: probes)
            p.displayResult( out );
        out.println( "</body>" );
        out.println( "</html>" );
        }
   
    private int countTests( List<AcceptanceTestItem> probes )
        {
        int result = 0;
        for (AcceptanceTestItem p: probes) if (!p.hasStatus( Status.MARKUP )) result += 1;
        return result;
        }
   
    private int countOK( List<AcceptanceTestItem> probes )
        {
        int result = 0;
        for (AcceptanceTestItem p: probes) if (p.hasStatus( Status.PASSED )) result += 1;
        return result;
        }
   
    static void decorate( PrintStream out, String s )
        {
        escape( out, s );
        }
   
    public static void escape( PrintStream out, String s )
        {
        out.println( s.replaceAll( "&", "&amp;" ).replaceAll( "<", "&lt;" ) );           
        }

    public void addTitle( String title )
        { probes.add( new Title( title ) ); }

    public void addComment( String comment )
        { probes.add( new Comment( comment ) ); }
    }

/*
    (c) Copyright 2009 Hewlett-Packard Development Company, LP
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
TOP

Related Classes of run.acceptance.support.State

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.