2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.onboarding.build.test;
19 import org.junit.Assert;
20 import org.junit.Test;
23 import java.nio.file.Paths;
24 import java.util.Arrays;
25 import java.util.Collections;
27 public class StaleCodeDetectionTest {
29 private static final String JAVA_EXT = ".java";
30 private static final String CLASS_EXT = ".class";
33 public void checkIfStale() {
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);
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) {
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();