View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Base class for tests on classes that consume or public XML documents.
32   *
33   * @author Curt Arnold
34   * 
35   */
36  public abstract class TestXMLConsumer extends TestCase {
37    /**
38     * copies a resource to a temporary directory.
39     * 
40     * @param resourceName
41     *          resouce name, such as "files/openshore/history.xml".
42     * @param tmpFile
43     *          name for temporary file created in /tmp or similar.
44     */
45    public static void copyResourceToTmpDir(final String resourceName, final String tmpFile) throws IOException {
46      String tmpDir = System.getProperty("java.io.tmpdir");
47  
48      final File tempdir = File.createTempFile(tmpFile, Long.toString(System.nanoTime()), new File(tmpDir));
49      tempdir.delete();
50      tempdir.mkdir();
51      tempdir.deleteOnExit();
52      tmpDir = tempdir.getAbsolutePath();
53      //
54      // attempt to get resource from jar
55      // (should succeed unless testing in IDE)
56      InputStream src = null;
57      if (TestTargetHistoryTable.class.getClassLoader().getResource(resourceName) != null) {
58        src = TestTargetHistoryTable.class.getClassLoader().getResourceAsStream(resourceName);
59      }
60      //
61      // if not found, try to find it relative to the current directory
62      //
63      if (src == null) {
64        src = new FileInputStream(resourceName);
65      }
66      assertNotNull("Could not locate resource " + resourceName, src);
67      try {
68        final File destFile = new File(tmpDir, tmpFile);
69        destFile.deleteOnExit();
70        try (FileOutputStream dest = new FileOutputStream(destFile)) {
71          int bytesRead = 0;
72          final byte[] buffer = new byte[4096];
73          do {
74            bytesRead = src.read(buffer);
75            if (bytesRead > 0) {
76              dest.write(buffer, 0, bytesRead);
77            }
78          } while (bytesRead == buffer.length);
79        }
80      } finally {
81        src.close();
82      }
83    }
84  
85    /**
86     * Deletes a file, if it exists, from the user's temporary directory.
87     * 
88     * @param tmpName
89     *          file name, may not be null
90     */
91    public static void deleteTmpFile(final String tmpName) throws IOException {
92      final String tmpDir = System.getProperty("java.io.tmpdir");
93      final File tmpFile = new File(tmpDir, tmpName);
94      if (tmpFile.exists()) {
95        tmpFile.delete();
96      }
97    }
98  
99    /**
100    * @param testName
101    */
102   protected TestXMLConsumer(final String testName) {
103     super(testName);
104   }
105 }