001: /*
002: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.util.junit;
008:
009: import java.util.ArrayList;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: import com.hp.hpl.jena.n3.IRIResolver;
017: import com.hp.hpl.jena.rdf.model.*; //import com.hp.hpl.jena.sparql.vocabulary.TestManifest;
018: //import com.hp.hpl.jena.sparql.vocabulary.TestManifestX;
019: import com.hp.hpl.jena.util.FileManager;
020: import com.hp.hpl.jena.vocabulary.RDF;
021: import com.hp.hpl.jena.vocabulary.RDFS;
022: import com.hp.hpl.jena.vocabulary.TestManifest;
023: import com.hp.hpl.jena.vocabulary.TestManifestX;
024:
025: /**
026: * A test manifest for a single manifest file.
027: *
028: * @author Andy Seaborne
029: * @version $Id: Manifest.java,v 1.3 2008/01/02 12:11:12 andy_seaborne Exp $
030: */
031:
032: public class Manifest {
033: // This class does not know about JUnit.
034: private static Log log = LogFactory.getLog(Manifest.class);
035: Model manifest;
036: String manifestName;
037: String filename;
038: List includedFiles = new ArrayList();
039: Resource manifestRes = null;
040:
041: public Manifest(String fn) {
042: log.debug("Manifest = " + fn);
043: filename = IRIResolver.resolveGlobal(fn);
044: log.debug(" = " + filename);
045: manifest = FileManager.get().loadModel(filename);
046: parseIncludes();
047: parseManifest();
048: }
049:
050: public String getName() {
051: return manifestName;
052: }
053:
054: public Iterator includedManifests() {
055: return includedFiles.iterator();
056: }
057:
058: private void parseManifest() {
059: StmtIterator manifestStmts = manifest.listStatements(null,
060: RDF.type, TestManifest.Manifest);
061: if (!manifestStmts.hasNext()) {
062: log.warn("No manifest in manifest file: " + filename);
063: return;
064: }
065:
066: Statement manifestItemStmt = manifestStmts.nextStatement();
067: if (manifestStmts.hasNext()) {
068: log
069: .warn("Multiple manifests in manifest file: "
070: + filename);
071: return;
072: }
073:
074: manifestRes = manifestItemStmt.getSubject();
075: manifestName = TestUtils.getLiteral(manifestRes, RDFS.label);
076: if (manifestName == null)
077: manifestName = TestUtils.getLiteral(manifestRes,
078: RDFS.comment);
079: manifestStmts.close();
080: }
081:
082: // For every test item (does not recurse)
083: public void apply(ManifestItemHandler gen) {
084:
085: StmtIterator manifestStmts = manifest.listStatements(null,
086: RDF.type, TestManifest.Manifest);
087:
088: for (; manifestStmts.hasNext();) {
089: Statement manifestItemStmt = manifestStmts.nextStatement();
090: Resource manifestRes = manifestItemStmt.getSubject();
091:
092: // For each item in this manifest
093: StmtIterator listIter = manifestRes
094: .listProperties(TestManifest.entries);
095: for (; listIter.hasNext();) {
096: //List head
097: Resource listItem = listIter.nextStatement()
098: .getResource();
099: for (; !listItem.equals(RDF.nil);) {
100: Resource entry = listItem.getRequiredProperty(
101: RDF.first).getResource();
102: String testName = TestUtils.getLiteral(entry,
103: TestManifest.name);
104: Resource action = TestUtils.getResource(entry,
105: TestManifest.action);
106: Resource result = TestUtils.getResource(entry,
107: TestManifest.result);
108: gen.processManifestItem(manifestRes, entry,
109: testName, action, result);
110: // Move to next list item
111: listItem = listItem.getRequiredProperty(RDF.rest)
112: .getResource();
113: }
114: }
115: listIter.close();
116: }
117: manifestStmts.close();
118: }
119:
120: // -------- included manifests
121: private void parseIncludes() {
122: parseIncludes(TestManifest.include);
123: parseIncludes(TestManifestX.include);
124: }
125:
126: private void parseIncludes(Property property) {
127: StmtIterator includeStmts = manifest.listStatements(null,
128: property, (RDFNode) null);
129:
130: for (; includeStmts.hasNext();) {
131: Statement s = includeStmts.nextStatement();
132: if (!(s.getObject() instanceof Resource)) {
133: log.warn("Include: not a Resource" + s);
134: continue;
135: }
136: Resource r = s.getResource();
137: parseOneIncludesList(r);
138: }
139: includeStmts.close();
140: }
141:
142: private void parseOneIncludesList(Resource r) {
143: if (r == null)
144: return;
145:
146: if (r.equals(RDF.nil))
147: return;
148:
149: if (!r.isAnon()) {
150: String uri = r.getURI();
151: if (includedFiles.contains(uri))
152: return;
153: includedFiles.add(r.getURI());
154: return;
155: }
156:
157: // BNnode => list
158: Resource listItem = r;
159: while (!listItem.equals(RDF.nil)) {
160: r = listItem.getRequiredProperty(RDF.first).getResource();
161: parseOneIncludesList(r);
162: // Move on
163: listItem = listItem.getRequiredProperty(RDF.rest)
164: .getResource();
165: }
166: }
167:
168: }
169:
170: /*
171: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
172: * All rights reserved.
173: *
174: * Redistribution and use in source and binary forms, with or without
175: * modification, are permitted provided that the following conditions
176: * are met:
177: * 1. Redistributions of source code must retain the above copyright
178: * notice, this list of conditions and the following disclaimer.
179: * 2. Redistributions in binary form must reproduce the above copyright
180: * notice, this list of conditions and the following disclaimer in the
181: * documentation and/or other materials provided with the distribution.
182: * 3. The name of the author may not be used to endorse or promote products
183: * derived from this software without specific prior written permission.
184: *
185: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195: */
|