Debian Codegraph @

src/test/java/io/reactivex/rxjava3/validators/JavadocFindUnescapedAngleBrackets.java

raw
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 public class JavadocFindUnescapedAngleBrackets {
24
25 @Test
26 public void find() throws Exception {
27 File base = TestHelper.findSource("Flowable");
28
29 if (base == null) {
30 return;
31 }
32
33 base = base.getParentFile().getParentFile();
34
35 Queue<File[]> files = new ArrayDeque<>();
36
37 files.offer(base.listFiles());
38
39 StringBuilder b = new StringBuilder();
40 int count = 0;
41
42 while (!files.isEmpty()) {
43 for (File file : files.poll()) {
44 if (file.getName().endsWith(".java")) {
45
46 String s = readFile(file);
47
48 String fn = file.toString().substring(base.toString().length());
49 fn = fn.replace("\\", ".");
50 fn = fn.replace("//", ".");
51 fn = fn.replace(".java", "");
52 fn = "io.reactivex" + fn;
53
54 int j = 0;
55 for (;;) {
56 int idx = s.indexOf("<code>", j);
57 if (idx < 0) {
58 break;
59 }
60 int jdx = s.indexOf("</code>", idx + 6);
61
62 int k = idx + 6;
63 for (;;) {
64 int kdx = s.indexOf('>', k);
65 if (kdx < 0) {
66 break;
67 }
68
69 if (kdx < jdx) {
70 b.append("at ")
71 .append(fn)
72 .append(".gt(")
73 .append(file.getName()).append(":")
74 .append(countLine(s, kdx))
75 .append(")\r\n");
76 count++;
77 } else {
78 break;
79 }
80 k = kdx + 1;
81 }
82
83 k = idx + 6;
84 for (;;) {
85 int kdx = s.indexOf('<', k);
86 if (kdx < 0) {
87 break;
88 }
89
90 if (kdx < jdx) {
91 b.append("at ")
92 .append(fn)
93 .append(".lt(")
94 .append(file.getName()).append(":")
95 .append(countLine(s, kdx))
96 .append(")\r\n");
97 count++;
98 } else {
99 break;
100 }
101 k = kdx + 1;
102 }
103
104 j = jdx + 7;
105 }
106 }
107 }
108 }
109
110 if (b.length() > 0) {
111 System.err.println("Should escape < and > in <code> blocks! " + count);
112 System.err.println(b);
113 throw new Exception("Should escape < and > in <code> blocks! " + count + "\r\n" + b);
114 }
115 }
116
117 static int countLine(String s, int kdx) {
118 int c = 1;
119 for (int i = kdx; i >= 0; i--) {
120 if (s.charAt(i) == '\n') {
121 c++;
122 }
123 }
124 return c;
125 }
126
127 static String readFile(File f) throws IOException {
128 StringBuilder b = new StringBuilder((int)f.length());
129 BufferedReader in = new BufferedReader(new FileReader(f));
130 try {
131 String line = null;
132
133 while ((line = in.readLine()) != null) {
134 b.append(line).append("\n");
135 }
136
137 } finally {
138 in.close();
139 }
140 return b.toString();
141 }
142 }