mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
testcase
This commit is contained in:
@@ -1,87 +1,87 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class TestReplaceLicense extends TestCase {
|
||||
|
||||
private String license;
|
||||
private String lineSeparator;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("License.txt");
|
||||
Reader reader = new InputStreamReader(is);
|
||||
license = JdbcUtils.read(reader);
|
||||
reader.close();
|
||||
System.out.println(license);
|
||||
|
||||
lineSeparator = "\n"; // (String) java.security.AccessController.doPrivileged(new
|
||||
// sun.security.action.GetPropertyAction("line.separator"));
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
File file = new File("/usr/alibaba/workspace/druid");
|
||||
listFile(file);
|
||||
|
||||
}
|
||||
|
||||
public void listFile(File file) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
for (File child : file.listFiles()) {
|
||||
listFile(child);
|
||||
}
|
||||
} else {
|
||||
if (file.getName().endsWith(".java")) {
|
||||
listJavaFile(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void listJavaFile(File file) throws Exception {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(in, "utf-8");
|
||||
String content = JdbcUtils.read(reader);
|
||||
reader.close();
|
||||
|
||||
if (!content.startsWith(license)) {
|
||||
String newContent;
|
||||
int index = content.indexOf("package ");
|
||||
if (index != -1) {
|
||||
newContent = license + lineSeparator + content.substring(index);
|
||||
} else {
|
||||
newContent = license + lineSeparator + content;
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
Writer writer = new OutputStreamWriter(out, "utf-8");
|
||||
writer.write(newContent);
|
||||
writer.close();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class TestReplaceLicense extends TestCase {
|
||||
|
||||
private String license;
|
||||
private String lineSeparator;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("License.txt");
|
||||
Reader reader = new InputStreamReader(is);
|
||||
license = JdbcUtils.read(reader);
|
||||
reader.close();
|
||||
System.out.println(license);
|
||||
|
||||
lineSeparator = "\n"; // (String) java.security.AccessController.doPrivileged(new
|
||||
// sun.security.action.GetPropertyAction("line.separator"));
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
File file = new File("/usr/alibaba/workspace/druid");
|
||||
listFile(file);
|
||||
|
||||
}
|
||||
|
||||
public void listFile(File file) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
for (File child : file.listFiles()) {
|
||||
listFile(child);
|
||||
}
|
||||
} else {
|
||||
if (file.getName().endsWith(".java")) {
|
||||
listJavaFile(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void listJavaFile(File file) throws Exception {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(in, "utf-8");
|
||||
String content = JdbcUtils.read(reader);
|
||||
reader.close();
|
||||
|
||||
if (!content.startsWith(license)) {
|
||||
String newContent;
|
||||
int index = content.indexOf("package ");
|
||||
if (index != -1) {
|
||||
newContent = license + lineSeparator + content.substring(index);
|
||||
} else {
|
||||
newContent = license + lineSeparator + content;
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
Writer writer = new OutputStreamWriter(out, "utf-8");
|
||||
writer.write(newContent);
|
||||
writer.close();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user