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.FileOutputStream;
24  import java.io.IOException;
25  
26  import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
27  
28  /**
29   * Tests for TargetHistoryTable
30   *
31   * @author CurtA
32   */
33  public class TestTargetHistoryTable extends TestXMLConsumer {
34    public static class MockProcessorConfiguration implements ProcessorConfiguration {
35      public MockProcessorConfiguration() {
36      }
37  
38      @Override
39      public int bid(final String fileName) {
40        return 100;
41      }
42  
43      @Override
44      public String getIdentifier() {
45        return "Mock Configuration";
46      }
47  
48      @Override
49      public String[] getOutputFileNames(final String baseName, final VersionInfo versionInfo) {
50        return new String[] {
51          baseName
52        };
53      }
54  
55      @Override
56      public ProcessorParam[] getParams() {
57        return new ProcessorParam[0];
58      }
59  
60      @Override
61      public boolean getRebuild() {
62        return false;
63      }
64    }
65  
66    /**
67     * Constructor
68     * 
69     * @param name
70     *          test case name
71     * @see junit.framework.TestCase#TestCase(String)
72     */
73    public TestTargetHistoryTable(final String name) {
74      super(name);
75    }
76  
77    /**
78     * Tests loading a stock history file
79     * 
80     * @throws IOException
81     */
82    public void testLoadOpenshore() throws IOException {
83      try {
84        copyResourceToTmpDir("openshore/history.xml", "history.xml");
85        final CCTask task = new CCTask();
86        final String tmpDir = System.getProperty("java.io.tmpdir");
87        final TargetHistoryTable history = new TargetHistoryTable(task, new File(tmpDir));
88      } finally {
89        deleteTmpFile("history.xml");
90      }
91    }
92  
93    /**
94     * Tests loading a stock history file
95     * 
96     * @throws IOException
97     */
98    public void testLoadXerces() throws IOException {
99      try {
100       copyResourceToTmpDir("xerces-c/history.xml", "history.xml");
101       final CCTask task = new CCTask();
102       final String tmpDir = System.getProperty("java.io.tmpdir");
103       final TargetHistoryTable history = new TargetHistoryTable(task, new File(tmpDir));
104     } finally {
105       deleteTmpFile("history.xml");
106     }
107   }
108 
109   /**
110    * Tests for bug fixed by patch [ 650397 ] Fix: Needless rebuilds on Unix
111    * 
112    * @throws IOException
113    */
114   public void testUpdateTimeResolution() throws IOException {
115     File compiledFile = null;
116 
117     try {
118       //
119       // delete any history file that might exist
120       // in the test output directory
121       final String tempDir = System.getProperty("java.io.tmpdir");
122       File historyFile = new File(tempDir, "history.xml");
123       historyFile.deleteOnExit();
124       if (historyFile.exists()) {
125         historyFile.delete();
126       }
127       final TargetHistoryTable table = new TargetHistoryTable(null, new File(tempDir));
128       //
129       // create a dummy compiled unit
130       //
131       compiledFile = new File(tempDir, "dummy.o");
132       final FileOutputStream compiledStream = new FileOutputStream(compiledFile);
133       compiledStream.close();
134       //
135       // lastModified times can be slightly less than
136       // task start time due to file system resolution.
137       // Mimic this by slightly incrementing the last modification time.
138       //
139       final long startTime = compiledFile.lastModified() + 1;
140       //
141       // update the table
142       //
143       table.update(new MockProcessorConfiguration(), new String[] {
144         "dummy.o"
145       }, null);
146       //
147       // commit. If "compiled" file was judged to be
148       // valid we should have a history file.
149       //
150       table.commit();
151       historyFile = table.getHistoryFile();
152       assertTrue("History file was not created", historyFile.exists());
153       assertTrue("History file was empty", historyFile.length() > 10);
154     } finally {
155       if (compiledFile != null && compiledFile.exists()) {
156         compiledFile.delete();
157       }
158 
159     }
160   }
161 }