Source Code Cross Referenced for Test_schemagen.java in  » RSS-RDF » Jena-2.5.5 » jena » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » Jena 2.5.5 » jena.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*****************************************************************************
002:         * File               Test_schemagen.java
003:         * Original author    Ian Dickinson, HP Labs Bristol
004:         * Author email       ian.dickinson@hp.com
005:         * Package            Jena 2
006:         * Web                http://sourceforge.net/projects/jena/
007:         * Created            8 Sep 2006
008:         * Filename           $RCSfile: Test_schemagen.java,v $
009:         * Revision           $Revision: 1.8 $
010:         * Release status     $State: Exp $
011:         *
012:         * Last modified on   $Date: 2008/01/02 12:08:49 $
013:         *               by   $Author: andy_seaborne $
014:         *
015:         * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
016:         * [See end of file]
017:         *****************************************************************************/package jena.test;
018:
019:        // Imports
020:        ///////////////
021:        import java.io.*;
022:        import java.lang.reflect.Constructor;
023:        import java.lang.reflect.Method;
024:        import java.util.StringTokenizer;
025:        import java.util.regex.Pattern;
026:
027:        import jena.schemagen;
028:        import junit.framework.TestCase;
029:
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:
033:        import com.hp.hpl.jena.rdf.model.*;
034:        import com.hp.hpl.jena.util.FileUtils;
035:
036:        /**
037:         * <p>
038:         * Unit tests for schemagen
039:         * </p>
040:         *
041:         * @author Ian Dickinson, HP Labs
042:         *         (<a  href="mailto:Ian.Dickinson@hp.com" >email</a>)
043:         * @version CVS $Id: Test_schemagen.java,v 1.8 2008/01/02 12:08:49 andy_seaborne Exp $
044:         */
045:        public class Test_schemagen extends TestCase {
046:            // Constants
047:            //////////////////////////////////
048:
049:            String PREFIX = "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
050:                    + "@prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
051:                    + "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n"
052:                    + "@prefix ex: <http://example.com/sg#> .\n";
053:
054:            // Static variables
055:            //////////////////////////////////
056:
057:            private static Log log = LogFactory.getLog(Test_schemagen.class);
058:
059:            // Instance variables
060:            //////////////////////////////////
061:
062:            // Constructors
063:            //////////////////////////////////
064:
065:            // External signature methods
066:            //////////////////////////////////
067:
068:            /** This test used to fail with an abort, but we now guess the NS based on prevalence */
069:            public void testNoBaseURI0() throws Exception {
070:                String SOURCE = PREFIX + "ex:A a owl:Class .";
071:                testSchemagenOutput(
072:                        SOURCE,
073:                        null,
074:                        new String[] {},
075:                        new String[] { ".*public static final Resource A =.*" },
076:                        new String[] {});
077:            }
078:
079:            public void testClass0() throws Exception {
080:                String SOURCE = PREFIX + "ex:A a owl:Class .";
081:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
082:                        "http://example.com/sg#", "--owl" },
083:                        new String[] { ".*public static final Resource A.*" },
084:                        new String[] {});
085:            }
086:
087:            public void testClass1() throws Exception {
088:                String SOURCE = PREFIX + "ex:A a rdfs:Class .";
089:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
090:                        "http://example.com/sg#", "--owl" }, new String[] {},
091:                        new String[] { ".*public static final Resource A.*" });
092:            }
093:
094:            public void testClass2() throws Exception {
095:                String SOURCE = PREFIX + "ex:A a owl:Class .";
096:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
097:                        "http://example.com/sg#", "--rdfs" }, new String[] {},
098:                        new String[] { ".*public static final Resource A.*" });
099:            }
100:
101:            public void testClass3() throws Exception {
102:                String SOURCE = PREFIX + "ex:A a rdfs:Class .";
103:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
104:                        "http://example.com/sg#", "--rdfs" },
105:                        new String[] { ".*public static final Resource A.*" },
106:                        new String[] {});
107:            }
108:
109:            public void testProperty0() throws Exception {
110:                String SOURCE = PREFIX + "ex:p a owl:ObjectProperty .";
111:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
112:                        "http://example.com/sg#", "--owl" },
113:                        new String[] { ".*public static final Property p.*" },
114:                        new String[] {});
115:            }
116:
117:            public void testProperty1() throws Exception {
118:                String SOURCE = PREFIX + "ex:p a rdf:Property .";
119:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
120:                        "http://example.com/sg#", "--owl" },
121:                        // in OWL mode we permit rdf:properties
122:                        new String[] { ".*public static final Property p.*" },
123:                        new String[] {});
124:            }
125:
126:            public void testProperty2() throws Exception {
127:                String SOURCE = PREFIX + "ex:p a owl:ObjectProperty .";
128:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
129:                        "http://example.com/sg#", "--rdfs" }, new String[] {},
130:                        new String[] { ".*public static final Property p.*" });
131:            }
132:
133:            public void testProperty3() throws Exception {
134:                String SOURCE = PREFIX + "ex:p a rdf:Property .";
135:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
136:                        "http://example.com/sg#", "--rdfs" },
137:                        new String[] { ".*public static final Property p.*" },
138:                        new String[] {});
139:            }
140:
141:            public void testInstance0() throws Exception {
142:                String SOURCE = PREFIX + "ex:A a owl:Class . ex:i a ex:A .";
143:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
144:                        "http://example.com/sg#", "--owl" },
145:                        new String[] { ".*public static final Resource i.*" },
146:                        new String[] {});
147:            }
148:
149:            public void testInstance1() throws Exception {
150:                String SOURCE = PREFIX + "ex:A a rdfs:Class . ex:i a ex:A .";
151:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
152:                        "http://example.com/sg#", "--owl" },
153:                        new String[] { ".*public static final Resource i.*" },
154:                        new String[] {});
155:            }
156:
157:            /* TODO this test fails, because the isInstance check in schemagen is quite weak.
158:             * Consider whether to fix the test or the code... *
159:            public void testInstance2() throws Exception {
160:                String SOURCE = PREFIX + "ex:A a owl:Class . ex:i a ex:A .";
161:                testSchemagenOutput( SOURCE, null,
162:                                     new String[] {"-a", "http://example.com/sg#", "--rdfs"},
163:                                     new String[] {},
164:                                     new String[] {".*public static final Resource i.*"} );
165:            }
166:             */
167:
168:            public void testInstance3() throws Exception {
169:                String SOURCE = PREFIX + "ex:A a rdfs:Class . ex:i a ex:A .";
170:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
171:                        "http://example.com/sg#", "--rdfs" },
172:                        new String[] { ".*public static final Resource i.*" },
173:                        new String[] {});
174:            }
175:
176:            /** Bug report by Richard Cyganiak */
177:            public void testRC0() throws Exception {
178:                String SOURCE = PREFIX + "ex:class a owl:Class .";
179:                testSchemagenOutput(
180:                        SOURCE,
181:                        null,
182:                        new String[] { "-a", "http://example.com/sg#", "--owl" },
183:                        new String[] {},
184:                        new String[] { ".*public static final Resource class .*" });
185:            }
186:
187:            public void testComment0() throws Exception {
188:                String SOURCE = PREFIX
189:                        + "ex:A a owl:Class ; rdfs:comment \"commentcomment\" .";
190:                testSchemagenOutput(
191:                        SOURCE,
192:                        null,
193:                        new String[] { "-a", "http://example.com/sg#", "--owl" },
194:                        new String[] { " */\\*\\* <p>commentcomment</p> \\*/ *" },
195:                        new String[] {});
196:            }
197:
198:            public void testComment1() throws Exception {
199:                String SOURCE = PREFIX
200:                        + "ex:A a owl:Class ; rdfs:comment \"commentcomment\" .";
201:                testSchemagenOutput(
202:                        SOURCE,
203:                        null,
204:                        new String[] { "-a", "http://example.com/sg#", "--owl",
205:                                "--nocomments" },
206:                        new String[] {},
207:                        new String[] { " */\\*\\* <p>commentcomment</p> \\*/ *" });
208:            }
209:
210:            public void testOntClass0() throws Exception {
211:                String SOURCE = PREFIX + "ex:A a owl:Class .";
212:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
213:                        "http://example.com/sg#", "--owl", "--ontology" },
214:                        new String[] { ".*public static final OntClass A.*" },
215:                        new String[] {});
216:            }
217:
218:            public void testOntClass1() throws Exception {
219:                String SOURCE = PREFIX + "ex:A a rdfs:Class .";
220:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
221:                        "http://example.com/sg#", "--owl", "--ontology" },
222:                        new String[] {},
223:                        new String[] { ".*public static final OntClass A.*" });
224:            }
225:
226:            public void testOntClass2() throws Exception {
227:                String SOURCE = PREFIX + "ex:A a owl:Class .";
228:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
229:                        "http://example.com/sg#", "--rdfs", "--ontology" },
230:                        new String[] {},
231:                        new String[] { ".*public static final OntClass A.*" });
232:            }
233:
234:            public void testOntClass3() throws Exception {
235:                String SOURCE = PREFIX + "ex:A a rdfs:Class .";
236:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
237:                        "http://example.com/sg#", "--rdfs", "--ontology" },
238:                        new String[] { ".*public static final OntClass A.*" },
239:                        new String[] {});
240:            }
241:
242:            public void testOntProperty0() throws Exception {
243:                String SOURCE = PREFIX + "ex:p a owl:ObjectProperty .";
244:                testSchemagenOutput(
245:                        SOURCE,
246:                        null,
247:                        new String[] { "-a", "http://example.com/sg#", "--owl",
248:                                "--ontology" },
249:                        new String[] { ".*public static final ObjectProperty p.*" },
250:                        new String[] {});
251:            }
252:
253:            public void testOntProperty1() throws Exception {
254:                String SOURCE = PREFIX + "ex:p a rdf:Property .";
255:                testSchemagenOutput(
256:                        SOURCE,
257:                        null,
258:                        new String[] { "-a", "http://example.com/sg#", "--owl",
259:                                "--ontology" },
260:                        // in OWL mode we permit rdf:properties
261:                        new String[] { ".*public static final OntProperty p.*" },
262:                        new String[] {});
263:            }
264:
265:            public void testOntProperty2() throws Exception {
266:                String SOURCE = PREFIX + "ex:p a owl:ObjectProperty .";
267:                testSchemagenOutput(
268:                        SOURCE,
269:                        null,
270:                        new String[] { "-a", "http://example.com/sg#",
271:                                "--rdfs", "--ontology" },
272:                        new String[] {},
273:                        new String[] { ".*public static final ObjectProperty p.*" });
274:            }
275:
276:            public void testOntProperty3() throws Exception {
277:                String SOURCE = PREFIX + "ex:p a rdf:Property .";
278:                testSchemagenOutput(
279:                        SOURCE,
280:                        null,
281:                        new String[] { "-a", "http://example.com/sg#",
282:                                "--rdfs", "--ontology" },
283:                        new String[] { ".*public static final OntProperty p.*" },
284:                        new String[] {});
285:            }
286:
287:            public void testHeader() throws Exception {
288:                String SOURCE = PREFIX + "ex:A a owl:Class .";
289:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
290:                        "http://example.com/sg#", "--ontology", "--header",
291:                        "/* header */\n%package%\n%imports%\n" },
292:                        new String[] { "/\\* header \\*/" }, new String[] {});
293:            }
294:
295:            public void testFooter() throws Exception {
296:                String SOURCE = PREFIX + "ex:A a owl:Class .";
297:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
298:                        "http://example.com/sg#", "--ontology", "--footer",
299:                        "/* footer */" }, new String[] { "/\\* footer \\*/" },
300:                        new String[] {});
301:            }
302:
303:            public void testPackage() throws Exception {
304:                String SOURCE = PREFIX + "ex:A a owl:Class .";
305:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
306:                        "http://example.com/sg#", "--ontology", "--package",
307:                        "test.test" },
308:                        new String[] { "package test.test;\\s*" },
309:                        new String[] {});
310:            }
311:
312:            public void testClassname() throws Exception {
313:                String SOURCE = PREFIX + "ex:A a owl:Class .";
314:                SchemaGenAux fixture = new SchemaGenAux() {
315:                    protected String getValue(Object option) {
316:                        if (option.equals(OPT_INPUT)) {
317:                            // without the -n option, this will force the classname to be Soggy
318:                            return "http://example.org/soggy";
319:                        } else {
320:                            return super .getValue(option);
321:                        }
322:                    }
323:                };
324:
325:                testSchemagenOutput(SOURCE, fixture, new String[] { "-a",
326:                        "http://example.com/soggy#", "--ontology", "--package",
327:                        "test.test", "-n", "Sg" }, new String[] {},
328:                        new String[] {});
329:            }
330:
331:            public void testClassdec() throws Exception {
332:                String SOURCE = PREFIX + "ex:A a owl:Class .";
333:                testSchemagenOutput(
334:                        SOURCE,
335:                        null,
336:                        new String[] { "-a", "http://example.com/sg#",
337:                                "--ontology", "--classdec",
338:                                "\n    implements java.lang.Cloneable\n" },
339:                        new String[] { "\\s*implements java.lang.Cloneable\\s*" },
340:                        new String[] {});
341:            }
342:
343:            public void testDeclarations() throws Exception {
344:                String SOURCE = PREFIX + "ex:A a owl:Class .";
345:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
346:                        "http://example.com/sg#", "--ontology",
347:                        "--declarations",
348:                        "protected String m_gnole = \"Fungle\";;\n" },
349:                        new String[] { ".*Fungle.*" }, new String[] {});
350:            }
351:
352:            public void testNoClasses() throws Exception {
353:                String SOURCE = PREFIX + "ex:A a owl:Class .";
354:                testSchemagenOutput(SOURCE, null,
355:                        new String[] { "-a", "http://example.com/sg#",
356:                                "--ontology", "--noclasses" }, new String[] {},
357:                        new String[] { ".*OntClass A.*" });
358:            }
359:
360:            public void testNoProperties() throws Exception {
361:                String SOURCE = PREFIX + "ex:p a owl:ObjectProperty .";
362:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
363:                        "http://example.com/sg#", "--owl", "--ontology",
364:                        "--noproperties" }, new String[] {},
365:                        new String[] { ".*Property p.*" });
366:            }
367:
368:            public void testNoIndividuals() throws Exception {
369:                String SOURCE = PREFIX + "ex:A a owl:Class . ex:i a ex:A .";
370:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
371:                        "http://example.com/sg#", "--owl", "--noindividuals" },
372:                        new String[] { ".*Resource A.*" },
373:                        new String[] { ".*Resource i.*" });
374:            }
375:
376:            public void testNoHeader() throws Exception {
377:                String SOURCE = PREFIX + "ex:A a owl:Class . ex:i a ex:A .";
378:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
379:                        "http://example.com/sg#", "--owl", "--noheader" },
380:                        new String[] {}, new String[] { "/\\*\\*.*" });
381:            }
382:
383:            public void testUCNames() throws Exception {
384:                String SOURCE = PREFIX + "ex:A a owl:Class . ex:i a ex:A .";
385:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
386:                        "http://example.com/sg#", "--owl", "--uppercase" },
387:                        new String[] { ".*Resource A.*", ".*Resource I.*" },
388:                        new String[] {});
389:            }
390:
391:            public void testInference0() throws Exception {
392:                String SOURCE = PREFIX + "ex:p rdfs:domain ex:A .";
393:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
394:                        "http://example.com/sg#", "--owl" }, new String[] {},
395:                        new String[] { ".*Resource A.*", ".*Property p.*" });
396:            }
397:
398:            public void testInference1() throws Exception {
399:                String SOURCE = PREFIX + "ex:p rdfs:domain ex:A .";
400:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
401:                        "http://example.com/sg#", "--owl", "--inference" },
402:                        new String[] { ".*Resource A.*", ".*Property p.*" },
403:                        new String[] {});
404:            }
405:
406:            public void testInference2() throws Exception {
407:                String SOURCE = PREFIX + "ex:p rdfs:domain ex:A .";
408:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
409:                        "http://example.com/sg#", "--rdfs", "--inference" },
410:                        new String[] { ".*Resource A.*", ".*Property p.*" },
411:                        new String[] {});
412:            }
413:
414:            public void testStrictIndividuals0() throws Exception {
415:                String SOURCE = PREFIX
416:                        + "ex:A a owl:Class . ex:i a ex:A . <http://example.com/different#j> a ex:A .";
417:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
418:                        "http://example.com/sg#", "--rdfs" }, new String[] {
419:                        ".*Resource i.*", ".*Resource j.*" }, new String[] {});
420:            }
421:
422:            public void testStrictIndividuals1() throws Exception {
423:                String SOURCE = PREFIX
424:                        + "ex:A a owl:Class . ex:i a ex:A . <http://example.com/different#j> a ex:A .";
425:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
426:                        "http://example.com/sg#", "--rdfs",
427:                        "--strictIndividuals" },
428:                        new String[] { ".*Resource i.*" },
429:                        new String[] { ".*Resource j.*" });
430:            }
431:
432:            public void testLineEnd0() throws Exception {
433:                String SOURCE = PREFIX
434:                        + "ex:A a owl:Class . ex:i a ex:A . ex:p a rdf:Property .";
435:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
436:                        "http://example.com/sg#", "--rdfs",
437:                        "--strictIndividuals" }, new String[] {},
438:                        new String[] { ".*\r.*" });
439:            }
440:
441:            public void testLineEnd1() throws Exception {
442:                String SOURCE = PREFIX
443:                        + "ex:A a owl:Class . ex:i a ex:A . ex:p a rdf:Property .";
444:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
445:                        "http://example.com/sg#", "--rdfs", "--dos" },
446:                        new String[] { ".*\\r" }, new String[] { ".*[^\r]" });
447:            }
448:
449:            public void testIncludeSource0() throws Exception {
450:                String SOURCE = PREFIX
451:                        + "ex:A a owl:Class . ex:i a ex:A . ex:p a owl:ObjectProperty .";
452:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
453:                        "http://example.com/sg#", "--owl", "--includeSource" },
454:                        new String[] {
455:                                ".*private static final String SOURCE.*",
456:                                ".*ex:A *a *owl:Class.*" }, new String[] {});
457:            }
458:
459:            public void testIncludeSource1() throws Exception {
460:                String SOURCE = PREFIX
461:                        + "ex:A a owl:Class ; rdfs:comment \"comment\".";
462:                testSchemagenOutput(SOURCE, null, new String[] { "-a",
463:                        "http://example.com/sg#", "--owl", "--includeSource" },
464:                        new String[] { ".*\\\\\"comment\\\\\".*\"" },
465:                        new String[] {});
466:            }
467:
468:            // Internal implementation methods
469:            //////////////////////////////////
470:
471:            /**
472:             * Test the output from schemagen by saving the output to a string,
473:             * then ensuring that every positive regex matches at least one line, and
474:             * every negative regex matches at most no lines. Also checks that
475:             * compiling the file does not cause any errors.
476:             *
477:             * @param source String defining the model, using N3
478:             * @param sg The schemagen object to test, or null for a default
479:             * @param args list of args to pass to SG
480:             * @param posPatterns array of regexps that must match at least once in the output
481:             * @param negPatterns arrays of regexps that must not match the output
482:             * @return The string defining the java class
483:             */
484:            protected String testSchemagenOutput(String source,
485:                    SchemaGenAux sg, String[] args, String[] posPatterns,
486:                    String[] negPatterns) throws Exception {
487:                sg = (sg == null) ? new SchemaGenAux() : sg;
488:
489:                Model m = ModelFactory.createDefaultModel();
490:                m
491:                        .read(new StringReader(source),
492:                                "http://example.com/sg#", "N3");
493:                sg.setSource(m);
494:
495:                ByteArrayOutputStream buf = new ByteArrayOutputStream();
496:                sg.setOutput(new PrintStream(buf));
497:
498:                // run schemagen
499:                sg.testGo(args);
500:
501:                // now run the test pattern over the lines in the file
502:                String result = buf.toString();
503:                if (log.isDebugEnabled()) {
504:                    log.debug(result);
505:                }
506:                StringTokenizer tokens = new StringTokenizer(result, "\n");
507:
508:                boolean[] foundPos = new boolean[posPatterns.length];
509:
510:                // look for any line that matches the patterns
511:                while (tokens.hasMoreTokens()) {
512:                    String line = tokens.nextToken();
513:
514:                    // try each positive pattern
515:                    for (int i = 0; i < posPatterns.length; i++) {
516:                        Pattern pat = Pattern.compile(posPatterns[i]);
517:                        foundPos[i] |= pat.matcher(line).matches();
518:                    }
519:
520:                    // try each negative pattern
521:                    for (int i = 0; i < negPatterns.length; i++) {
522:                        Pattern pat = Pattern.compile(negPatterns[i]);
523:                        assertFalse("negative match pattern ||"
524:                                + negPatterns[i] + "|| matched on line: "
525:                                + line, pat.matcher(line).matches());
526:                    }
527:                }
528:
529:                for (int i = 0; i < posPatterns.length; i++) {
530:                    String msg = "Expecting a positive match to pattern: ||"
531:                            + posPatterns[i] + "||";
532:                    assertTrue(msg + " in:\n" + result, foundPos[i]);
533:                }
534:
535:                // check that the file compiles with javac
536:                testCompile(result, "Sg");
537:
538:                return result;
539:            }
540:
541:            /**
542:             * Test the compilability of the generated output string by saving it to a
543:             * class file, and invoking javac on that file.
544:             * @param source
545:             * @param className
546:             * @throws Exception
547:             */
548:            protected void testCompile(String source, String className)
549:                    throws Exception {
550:                // first write the source file to a temp dir
551:                File tmpDir = FileUtils.getScratchDirectory("schemagen");
552:                File srcFile = new File(tmpDir, className + ".java");
553:                FileWriter out = new FileWriter(srcFile);
554:                out.write(source);
555:                out.close();
556:
557:                // now get ready to invoke javac
558:                try {
559:                    Class jcMain = Class.forName("sun.tools.javac.Main");
560:
561:                    // constructor
562:                    Constructor jcConstruct = jcMain
563:                            .getConstructor(new Class[] { OutputStream.class,
564:                                    String.class });
565:                    Method jcCompile = jcMain.getMethod("compile",
566:                            new Class[] { String[].class });
567:                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
568:                    Object jc = jcConstruct.newInstance(new Object[] { byteOut,
569:                            "javac" });
570:
571:                    // build the args list for javac
572:                    String[] args = new String[] { "-classpath",
573:                            getClassPath(tmpDir), "-d", tmpDir.getPath(),
574:                            srcFile.getPath() };
575:
576:                    Boolean success = (Boolean) jcCompile.invoke(jc,
577:                            new Object[] { args });
578:                    log.debug("compiled - success = " + success);
579:                    log.debug("message = " + byteOut.toString());
580:                    assertTrue(
581:                            "Errors reported from compilation of schemagen output",
582:                            success.booleanValue());
583:                } catch (ClassNotFoundException nf) {
584:                    log
585:                            .debug("sun.tools.java.Main not found (no tools.jar on classpath?). schemagen compilation test skipped.");
586:                }
587:
588:                // clean up
589:                srcFile.deleteOnExit();
590:                new File(tmpDir, className + ".class").deleteOnExit();
591:                tmpDir.deleteOnExit();
592:            }
593:
594:            /**
595:             * answer the classpath we can use to compile the sg output files
596:             * @param tmpDir
597:             * @return
598:             */
599:            protected String getClassPath(File tmpDir) {
600:                return System.getProperty("java.class.path")
601:                        + System.getProperty("path.separator")
602:                        + tmpDir.getPath();
603:            }
604:
605:            //==============================================================================
606:            // Inner class definitions
607:            //==============================================================================
608:
609:            /**
610:             * An extension to standard schemagen to create a test fixture; we override the
611:             * input and output methods.
612:             */
613:            static class SchemaGenAux extends schemagen {
614:                protected PrintStream m_auxOutput;
615:                protected Model m_auxSource;
616:
617:                public void setOutput(PrintStream out) {
618:                    m_auxOutput = out;
619:                }
620:
621:                public void setSource(Model m) {
622:                    m_auxSource = m;
623:                }
624:
625:                // override the behaviours from schemagen
626:                protected void selectInput() {
627:                    m_source.add(m_auxSource);
628:                    m_source.setNsPrefixes(m_auxSource);
629:                }
630:
631:                protected void selectOutput() {
632:                    // call super to allow option processing
633:                    super .selectOutput();
634:                    // then override the result
635:                    m_output = m_auxOutput;
636:                }
637:
638:                public void testGo(String[] args) {
639:                    go(args);
640:                }
641:
642:                // option faking
643:                protected String getValue(Object option) {
644:                    if (option.equals(OPT_INPUT)) {
645:                        return "http://example.org/sg";
646:                    } else {
647:                        return super .getValue(option);
648:                    }
649:                }
650:
651:                protected Resource getResource(Object option) {
652:                    if (option.equals(OPT_INPUT)) {
653:                        return ResourceFactory
654:                                .createResource("http://example.org/sg");
655:                    } else {
656:                        return super .getResource(option);
657:                    }
658:                }
659:
660:                protected void abort(String msg, Exception e) {
661:                    throw new RuntimeException(msg, e);
662:                }
663:            }
664:        }
665:
666:        /*
667:         * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
668:         * All rights reserved.
669:         *
670:         * Redistribution and use in source and binary forms, with or without
671:         * modification, are permitted provided that the following conditions
672:         * are met:
673:         * 1. Redistributions of source code must retain the above copyright
674:         *    notice, this list of conditions and the following disclaimer.
675:         * 2. Redistributions in binary form must reproduce the above copyright
676:         *    notice, this list of conditions and the following disclaimer in the
677:         *    documentation and/or other materials provided with the distribution.
678:         * 3. The name of the author may not be used to endorse or promote products
679:         *    derived from this software without specific prior written permission.
680:         *
681:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
682:         * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
683:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
684:         * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
685:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
686:         * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
687:         * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
688:         * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
689:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
690:         * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
691:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.