/src/test/java/org/xtce/toolkit/ParsingTest.java

https://gitlab.com/dovereem/xtcetools · Java · 594 lines · 374 code · 200 blank · 20 comment · 19 complexity · 0882862b9e48f17ccb2ec34881d75583 MD5 · raw file

  1. /* Copyright 2015 David Overeem (dovereem@startmail.com)
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. *
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.xtce.toolkit;
  18. import java.io.File;
  19. import java.util.List;
  20. import org.junit.After;
  21. import org.junit.AfterClass;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. import org.w3c.dom.Element;
  27. /**
  28. *
  29. * @author dovereem
  30. */
  31. public class ParsingTest {
  32. public ParsingTest() {
  33. }
  34. @BeforeClass
  35. public static void setUpClass() {
  36. }
  37. @AfterClass
  38. public static void tearDownClass() {
  39. }
  40. @Before
  41. public void setUp() {
  42. }
  43. @After
  44. public void tearDown() {
  45. }
  46. @Test
  47. public void testLoadingFileNotFound() {
  48. final String methodName =
  49. Thread.currentThread().getStackTrace()[1].getMethodName();
  50. System.out.println( "Test Case: " + methodName + "()" );
  51. final String file = "foo-xtce.xml";
  52. try {
  53. XTCEDatabase db = new XTCEDatabase( new File( file ),
  54. false, // validate on load
  55. false, // xinclude
  56. true ); // read only
  57. Assert.fail( "File " + file + " should have thrown not found" );
  58. } catch ( Exception ex ) {
  59. if ( ex.getLocalizedMessage().contains( file ) == false ) {
  60. Assert.fail( "Expected filename in throw, text: " +
  61. ex.getLocalizedMessage() );
  62. }
  63. }
  64. }
  65. @Test
  66. public void testLoadingUnitTestFileReadOnly() {
  67. final String methodName =
  68. Thread.currentThread().getStackTrace()[1].getMethodName();
  69. System.out.println( "Test Case: " + methodName + "()" );
  70. final String file = "src/test/resources/org/xtce/toolkit/test/UnitTests.xml";
  71. try {
  72. XTCEDatabase db = new XTCEDatabase( new File( file ),
  73. false, // validate on load
  74. false, // xinclude
  75. true ); // read only
  76. Assert.assertTrue( "Read Only Flag Not Set", db.isReadOnly() );
  77. } catch ( Exception ex ) {
  78. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  79. }
  80. }
  81. @Test
  82. public void testLoadingUnitTestFileEditable() {
  83. final String methodName =
  84. Thread.currentThread().getStackTrace()[1].getMethodName();
  85. System.out.println( "Test Case: " + methodName + "()" );
  86. final String file = "src/test/resources/org/xtce/toolkit/test/UnitTests.xml";
  87. try {
  88. XTCEDatabase db = new XTCEDatabase( new File( file ),
  89. false, // validate on load
  90. false, // xinclude
  91. false ); // read only
  92. Assert.assertFalse( "Read Only Flag Set", db.isReadOnly() );
  93. } catch ( Exception ex ) {
  94. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  95. }
  96. }
  97. @Test
  98. public void testFileAccessorAttributes() {
  99. final String methodName =
  100. Thread.currentThread().getStackTrace()[1].getMethodName();
  101. System.out.println( "Test Case: " + methodName + "()" );
  102. final String file = "src/test/resources/org/xtce/toolkit/test/UnitTests.xml";
  103. try {
  104. File fileIn = new File( file );
  105. XTCEDatabase db = new XTCEDatabase( fileIn,
  106. false, // validate on load
  107. false, // xinclude
  108. true ); // read only
  109. File fileOut = db.getFilename();
  110. Assert.assertTrue( "File opened should equal file out",
  111. fileIn.getAbsolutePath().equals( fileOut.getAbsolutePath() ) );
  112. Assert.assertTrue( "Namespace should be '" +
  113. XTCEConstants.XTCE_NAMESPACE + "'",
  114. db.getNamespaceFromDocument().equals( XTCEConstants.XTCE_NAMESPACE ) );
  115. Assert.assertTrue( "Default Schema should be '" +
  116. XTCEConstants.DEFAULT_SCHEMA_FILE + "'",
  117. db.getSchemaFromDocument().equals( XTCEConstants.DEFAULT_SCHEMA_FILE ) );
  118. Assert.assertTrue( "Loaded file should not have changed",
  119. db.getChanged() == false );
  120. try {
  121. Element element = db.getDocumentElement();
  122. Assert.fail( "getDocumentElement should throw on read-only" );
  123. } catch ( Exception ex ) {
  124. System.out.println( "Expected Exception: " +
  125. ex.getLocalizedMessage() );
  126. }
  127. } catch ( Exception ex ) {
  128. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  129. }
  130. }
  131. @Test
  132. public void testCreatingFile() {
  133. final String methodName =
  134. Thread.currentThread().getStackTrace()[1].getMethodName();
  135. System.out.println( "Test Case: " + methodName + "()" );
  136. final String ssName = "New_Space_System";
  137. try {
  138. XTCEDatabase db = new XTCEDatabase( ssName );
  139. Assert.assertFalse( "Read Only Flag Set", db.isReadOnly() );
  140. Assert.assertTrue( "Top level Space System Name is wrong",
  141. db.getRootSpaceSystem().getName().equals( ssName ) );
  142. } catch ( Exception ex ) {
  143. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  144. }
  145. }
  146. @Test
  147. public void testLoadingFileWithSyntaxErrorsReadOnlyWithValidation() {
  148. final String methodName =
  149. Thread.currentThread().getStackTrace()[1].getMethodName();
  150. System.out.println( "Test Case: " + methodName + "()" );
  151. final String file =
  152. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadForm.xml";
  153. try {
  154. XTCEDatabase db = new XTCEDatabase( new File( file ),
  155. true, // validate on load
  156. false, // xinclude
  157. true ); // read only
  158. Assert.fail( "Expected throw when loading " + file );
  159. } catch ( Exception ex ) {
  160. System.out.println( "Expected Errors:" +
  161. System.getProperty( "line.separator" ) +
  162. ex.getLocalizedMessage() );
  163. }
  164. }
  165. @Test
  166. public void testLoadingFileWithSyntaxErrorsEditableWithValidation() {
  167. final String methodName =
  168. Thread.currentThread().getStackTrace()[1].getMethodName();
  169. System.out.println( "Test Case: " + methodName + "()" );
  170. final String file =
  171. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadForm.xml";
  172. try {
  173. XTCEDatabase db = new XTCEDatabase( new File( file ),
  174. true, // validate on load
  175. false, // xinclude
  176. false ); // read only
  177. Assert.fail( "Expected throw when loading " + file );
  178. } catch ( Exception ex ) {
  179. System.out.println( "Expected Errors:" +
  180. System.getProperty( "line.separator" ) +
  181. ex.getLocalizedMessage() );
  182. }
  183. }
  184. @Test
  185. public void testLoadingFileWithErrorsReadOnlyWithValidation() {
  186. final String methodName =
  187. Thread.currentThread().getStackTrace()[1].getMethodName();
  188. System.out.println( "Test Case: " + methodName + "()" );
  189. final String file =
  190. "src/test/resources/org/xtce/toolkit/test/BogusSAT-1-Bad.xml";
  191. try {
  192. XTCEDatabase db = new XTCEDatabase( new File( file ),
  193. true, // validate on load
  194. false, // xinclude
  195. true ); // read only
  196. if ( db.getErrorCount() == 0 ) {
  197. Assert.fail( "Expected errors loading " + file );
  198. } else {
  199. for ( String message : db.getDocumentWarnings() ) {
  200. Assert.assertFalse( "Should not have gotten IO Fatal",
  201. message.startsWith( "IO Fatal:" ) );
  202. System.out.println( "Expected Message: " + message );
  203. }
  204. }
  205. } catch ( Exception ex ) {
  206. System.out.println( "Expected Errors:" +
  207. System.getProperty( "line.separator" ) +
  208. ex.getLocalizedMessage() );
  209. }
  210. }
  211. @Test
  212. public void testLoadingFileWithErrorsEditableWithValidation() {
  213. final String methodName =
  214. Thread.currentThread().getStackTrace()[1].getMethodName();
  215. System.out.println( "Test Case: " + methodName + "()" );
  216. final String file =
  217. "src/test/resources/org/xtce/toolkit/test/BogusSAT-1-Bad.xml";
  218. try {
  219. XTCEDatabase db = new XTCEDatabase( new File( file ),
  220. true, // validate on load
  221. false, // xinclude
  222. false ); // read only
  223. if ( db.getErrorCount() == 0 ) {
  224. Assert.fail( "Expected errors loading " + file );
  225. } else {
  226. for ( String message : db.getDocumentWarnings() ) {
  227. Assert.assertFalse( "Should not have gotten IO Fatal",
  228. message.startsWith( "IO Fatal:" ) );
  229. System.out.println( "Expected Message: " + message );
  230. }
  231. }
  232. } catch ( Exception ex ) {
  233. System.out.println( "Expected Errors:" +
  234. System.getProperty( "line.separator" ) +
  235. ex.getLocalizedMessage() );
  236. }
  237. }
  238. @Test
  239. public void testLoadingFileWithErrorsReadOnlyNoValidation() {
  240. final String methodName =
  241. Thread.currentThread().getStackTrace()[1].getMethodName();
  242. System.out.println( "Test Case: " + methodName + "()" );
  243. final String file =
  244. "src/test/resources/org/xtce/toolkit/test/BogusSAT-1-Bad.xml";
  245. try {
  246. XTCEDatabase db = new XTCEDatabase( new File( file ),
  247. false, // validate on load
  248. false, // xinclude
  249. true ); // read only
  250. } catch ( Exception ex ) {
  251. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  252. }
  253. }
  254. @Test
  255. public void testLoadingFileWithErrorsEditableNoValidation() {
  256. final String methodName =
  257. Thread.currentThread().getStackTrace()[1].getMethodName();
  258. System.out.println( "Test Case: " + methodName + "()" );
  259. final String file =
  260. "src/test/resources/org/xtce/toolkit/test/BogusSAT-1-Bad.xml";
  261. try {
  262. XTCEDatabase db = new XTCEDatabase( new File( file ),
  263. false, // validate on load
  264. false, // xinclude
  265. false ); // read only
  266. } catch ( Exception ex ) {
  267. Assert.fail( "Unexpected exception: " + ex.getLocalizedMessage() );
  268. }
  269. }
  270. @Test
  271. public void testLoadingFileWithMarshalErrorsReadOnlyNoValidation() {
  272. final String methodName =
  273. Thread.currentThread().getStackTrace()[1].getMethodName();
  274. System.out.println( "Test Case: " + methodName + "()" );
  275. final String file =
  276. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadAttr.xml";
  277. try {
  278. XTCEDatabase db = new XTCEDatabase( new File( file ),
  279. false, // validate on load
  280. false, // xinclude
  281. true ); // read only
  282. Assert.fail( "Expected throw when loading " + file );
  283. } catch ( Exception ex ) {
  284. System.out.println( "Expected Errors:" +
  285. System.getProperty( "line.separator" ) +
  286. ex.getLocalizedMessage() );
  287. }
  288. }
  289. @Test
  290. public void testLoadingFileWithMarshalErrorsReadOnlyWithValidation() {
  291. final String methodName =
  292. Thread.currentThread().getStackTrace()[1].getMethodName();
  293. System.out.println( "Test Case: " + methodName + "()" );
  294. final String file =
  295. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadAttr.xml";
  296. try {
  297. XTCEDatabase db = new XTCEDatabase( new File( file ),
  298. true, // validate on load
  299. false, // xinclude
  300. true ); // read only
  301. Assert.fail( "Expected throw when loading " + file );
  302. } catch ( Exception ex ) {
  303. System.out.println( "Expected Errors:" +
  304. System.getProperty( "line.separator" ) +
  305. ex.getLocalizedMessage() );
  306. }
  307. }
  308. @Test
  309. public void testLoadingFileWithMarshalErrorsEditableNoValidation() {
  310. final String methodName =
  311. Thread.currentThread().getStackTrace()[1].getMethodName();
  312. System.out.println( "Test Case: " + methodName + "()" );
  313. final String file =
  314. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadAttr.xml";
  315. try {
  316. XTCEDatabase db = new XTCEDatabase( new File( file ),
  317. false, // validate on load
  318. false, // xinclude
  319. false ); // read only
  320. Assert.fail( "Expected throw when loading " + file );
  321. } catch ( Exception ex ) {
  322. System.out.println( "Expected Errors:" +
  323. System.getProperty( "line.separator" ) +
  324. ex.getLocalizedMessage() );
  325. }
  326. }
  327. @Test
  328. public void testLoadingFileWithMarshalErrorsEditableWithValidation() {
  329. final String methodName =
  330. Thread.currentThread().getStackTrace()[1].getMethodName();
  331. System.out.println( "Test Case: " + methodName + "()" );
  332. final String file =
  333. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadAttr.xml";
  334. try {
  335. XTCEDatabase db = new XTCEDatabase( new File( file ),
  336. true, // validate on load
  337. false, // xinclude
  338. false ); // read only
  339. Assert.fail( "Expected throw when loading " + file );
  340. } catch ( Exception ex ) {
  341. System.out.println( "Expected Errors:" +
  342. System.getProperty( "line.separator" ) +
  343. ex.getLocalizedMessage() );
  344. }
  345. }
  346. @Test
  347. public void testStaticValidationFunctionBadlyFormed() {
  348. final String methodName =
  349. Thread.currentThread().getStackTrace()[1].getMethodName();
  350. System.out.println( "Test Case: " + methodName + "()" );
  351. final String file =
  352. "src/test/resources/org/xtce/toolkit/test/UnitTest-BadForm.xml";
  353. try {
  354. List<String> messages =
  355. XTCEDatabase.validateDocument( new File( file ), false );
  356. if ( messages.isEmpty() == true ) {
  357. Assert.fail( "Expected errors loading " + file );
  358. } else {
  359. for ( String message : messages ) {
  360. System.out.println( "Message: " + message );
  361. Assert.assertFalse( "Should not have gotten IO Fatal",
  362. message.startsWith( "IO Fatal:" ) );
  363. }
  364. }
  365. } catch ( Exception ex ) {
  366. Assert.fail( "Should not have thrown from " +
  367. "XTCEDatabase.validateDocument: " + ex.getLocalizedMessage() );
  368. }
  369. }
  370. @Test
  371. public void testStaticValidationFunctionNotSchemaCompliant() {
  372. final String methodName =
  373. Thread.currentThread().getStackTrace()[1].getMethodName();
  374. System.out.println( "Test Case: " + methodName + "()" );
  375. final String file =
  376. "src/test/resources/org/xtce/toolkit/test/BogusSAT-1-Bad.xml";
  377. try {
  378. List<String> messages =
  379. XTCEDatabase.validateDocument( new File( file ), false );
  380. if ( messages.isEmpty() == true ) {
  381. Assert.fail( "Expected errors loading " + file );
  382. } else {
  383. for ( String message : messages ) {
  384. System.out.println( "Message: " + message );
  385. Assert.assertFalse( "Should not have gotten IO Fatal",
  386. message.startsWith( "IO Fatal:" ) );
  387. }
  388. }
  389. } catch ( Exception ex ) {
  390. Assert.fail( "Should not have thrown from " +
  391. "XTCEDatabase.validateDocument: " + ex.getLocalizedMessage() );
  392. }
  393. }
  394. }