| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.validators; |
| 15 |
|
| 16 |
import java.io.*; |
| 17 |
import java.util.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 22 |
|
| 23 |
/** |
| 24 |
* Adds license header to java files. |
| 25 |
*/ |
| 26 |
public class FixLicenseHeaders { |
| 27 |
|
| 28 |
String[] header = { |
| 29 |
"/*", |
| 30 |
" * Copyright (c) 2016-present, RxJava Contributors.", |
| 31 |
" *", |
| 32 |
" * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in", |
| 33 |
" * compliance with the License. You may obtain a copy of the License at", |
| 34 |
" *", |
| 35 |
" * http://www.apache.org/licenses/LICENSE-2.0", |
| 36 |
" *", |
| 37 |
" * Unless required by applicable law or agreed to in writing, software distributed under the License is", |
| 38 |
" * distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See", |
| 39 |
" * the License for the specific language governing permissions and limitations under the License.", |
| 40 |
" */", |
| 41 |
"" |
| 42 |
}; |
| 43 |
|
| 44 |
@Test |
| 45 |
public void checkAndUpdateLicenses() throws Exception { |
| 46 |
if (System.getenv("CI") != null) { |
| 47 |
// no point in changing the files in CI |
| 48 |
return; |
| 49 |
} |
| 50 |
File f = TestHelper.findSource("Flowable"); |
| 51 |
if (f == null) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
Queue<File> dirs = new ArrayDeque<>(); |
| 56 |
|
| 57 |
File parent = f.getParentFile().getParentFile(); |
| 58 |
dirs.offer(parent); |
| 59 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/perf/java"))); |
| 60 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java"))); |
| 61 |
|
| 62 |
StringBuilder fail = new StringBuilder(); |
| 63 |
|
| 64 |
while (!dirs.isEmpty()) { |
| 65 |
f = dirs.poll(); |
| 66 |
|
| 67 |
File[] list = f.listFiles(); |
| 68 |
if (list != null && list.length != 0) { |
| 69 |
|
| 70 |
for (File u : list) { |
| 71 |
if (u.isDirectory()) { |
| 72 |
dirs.offer(u); |
| 73 |
} else { |
| 74 |
if (u.getName().endsWith(".java")) { |
| 75 |
|
| 76 |
List<String> lines = new ArrayList<>(); |
| 77 |
BufferedReader in = new BufferedReader(new FileReader(u)); |
| 78 |
try { |
| 79 |
for (;;) { |
| 80 |
String line = in.readLine(); |
| 81 |
if (line == null) { |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
lines.add(line); |
| 86 |
} |
| 87 |
} finally { |
| 88 |
in.close(); |
| 89 |
} |
| 90 |
|
| 91 |
if (!lines.get(0).equals(header[0]) || !lines.get(1).equals(header[1])) { |
| 92 |
fail.append("java.lang.RuntimeException: missing header added, refresh and re-run tests!\r\n") |
| 93 |
.append(" at ") |
| 94 |
; |
| 95 |
|
| 96 |
String fn = u.toString().replace('\\', '/'); |
| 97 |
|
| 98 |
int idx = fn.indexOf("io/reactivex/"); |
| 99 |
|
| 100 |
fn = fn.substring(idx).replace('/', '.').replace(".java", ""); |
| 101 |
|
| 102 |
fail.append(fn).append(" (") |
| 103 |
; |
| 104 |
|
| 105 |
int jdx = fn.lastIndexOf('.'); |
| 106 |
|
| 107 |
fail.append(fn.substring(jdx + 1)); |
| 108 |
|
| 109 |
fail.append(".java:1)\r\n\r\n"); |
| 110 |
|
| 111 |
lines.addAll(0, Arrays.asList(header)); |
| 112 |
|
| 113 |
PrintWriter w = new PrintWriter(new FileWriter(u)); |
| 114 |
|
| 115 |
try { |
| 116 |
for (String s : lines) { |
| 117 |
w.println(s); |
| 118 |
} |
| 119 |
} finally { |
| 120 |
w.close(); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (fail.length() != 0) { |
| 130 |
System.out.println(fail); |
| 131 |
throw new AssertionError(fail.toString()); |
| 132 |
} |
| 133 |
} |
| 134 |
} |