69f77357ae8e9eaa50c2c1dcc961f8071ccff445
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
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 a "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.openecomp.onboarding.build.test;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21
22 import java.io.File;
23 import java.nio.file.Paths;
24 import java.util.Arrays;
25 import java.util.Collections;
26
27 public class StaleCodeDetectionTest {
28
29     private static final String JAVA_EXT = ".java";
30     private static final String CLASS_EXT = ".class";
31
32     @Test
33     public void checkIfStale() {
34
35         String moduleLocation = System.getProperty("basedir");
36         if (isStale(moduleLocation + File.separator + "target" + File.separator + "test-classes",
37                 moduleLocation + File.separator + "src" + File.separator + "test" + File.separator + "java")) {
38             Assert.fail("****** Please remove 'target' directory manually under path " + moduleLocation);
39         }
40     }
41
42     private boolean isStale(String compiledCodeLocation, String javaSourceLocation) {
43         File compiledFiles = new File(compiledCodeLocation);
44         File[] list = compiledFiles.listFiles((dir, file) -> file.endsWith(CLASS_EXT) && file.indexOf('$') == -1);
45         if (list == null || list.length == 0) {
46             return false;
47         }
48         File candidate = Collections.min(Arrays.asList(list),
49                 (file1, file2) -> file1.lastModified() >= file2.lastModified() ? 1 : -1);
50         String sourceFilePath = javaSourceLocation + candidate.getAbsolutePath().replace(compiledCodeLocation, "")
51                                                               .replace(CLASS_EXT, JAVA_EXT);
52         return !Paths.get(sourceFilePath).toFile().exists();
53     }
54 }