Completely reworked the tests
This commit is contained in:
parent
c9902c88ef
commit
13a6a75613
13 changed files with 665 additions and 97 deletions
50
src/test/resources/java/BestPractices.java
Normal file
50
src/test/resources/java/BestPractices.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;/**
|
||||
* BestPractices class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class BestPractices {
|
||||
private final String ip = "127.0.0.1"; // not recommended
|
||||
private StringBuffer buffer; // potential memory leak as an instance variable;
|
||||
private String[] x;
|
||||
|
||||
void bar(int a) {
|
||||
switch (a) {
|
||||
case 1: // do something
|
||||
break;
|
||||
default: // the default case should be last, by convention
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void foo(String[] param) {
|
||||
// Don't do this, make a copy of the array at least
|
||||
this.x = param;
|
||||
}
|
||||
|
||||
private void greet(String name) {
|
||||
name = name.trim();
|
||||
System.out.println("Hello " + name);
|
||||
}
|
||||
|
||||
|
||||
}
|
46
src/test/resources/java/CodeStyle.java
Normal file
46
src/test/resources/java/CodeStyle.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;
|
||||
|
||||
/**
|
||||
* CodeStyle class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class CodeStyle {
|
||||
final int FinalField = 1;
|
||||
private int x;
|
||||
private int y; // class cannot be subclassed, so is y really private or package visible?
|
||||
|
||||
// missing constructor
|
||||
|
||||
boolean bar(int x, int y) {
|
||||
try {
|
||||
x = y;
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw e;
|
||||
} catch (IllegalStateException e) { // Can be collapsed into the previous block
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (true) ; // empty if statement
|
||||
if (true) { // empty as well
|
||||
}
|
||||
return x == y;
|
||||
}
|
||||
}
|
40
src/test/resources/java/Design.java
Normal file
40
src/test/resources/java/Design.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;/**
|
||||
* Design class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Design {
|
||||
String field;
|
||||
int otherField;
|
||||
|
||||
void foo() {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
public void bar(int x, int y, int z) {
|
||||
if (x > y) {
|
||||
if (y > z) {
|
||||
if (z == x) {
|
||||
// !! too deep
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
28
src/test/resources/java/Documentation.java
Normal file
28
src/test/resources/java/Documentation.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;/**
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Documentation {
|
||||
public Documentation() {
|
||||
// This constructor is intentionally empty. Nothing special is needed here.
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
}
|
||||
}
|
52
src/test/resources/java/ErrorProne.java
Normal file
52
src/test/resources/java/ErrorProne.java
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;/**
|
||||
* ErrorProne class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ErrorProne {
|
||||
static int x;
|
||||
|
||||
public ErrorProne(int y) {
|
||||
x = y; // unsafe
|
||||
|
||||
// unusual use of branching statement in a loop
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i * i <= 25) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void foo() {
|
||||
try {
|
||||
// do something
|
||||
} catch (Throwable th) { // should not catch Throwable
|
||||
th.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void bar() {
|
||||
try {
|
||||
// do something
|
||||
} catch (NullPointerException npe) {
|
||||
}
|
||||
}
|
||||
}
|
50
src/test/resources/java/MultiThreading.java
Normal file
50
src/test/resources/java/MultiThreading.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* MultiThreading class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MultiThreading {
|
||||
private static final SimpleDateFormat sdf = new SimpleDateFormat();
|
||||
/*volatile */ Object baz = null; // fix for Java5 and later: volatile
|
||||
private volatile String var1; // not suggested
|
||||
|
||||
void bar() {
|
||||
sdf.format("bar"); // poor, no thread-safety
|
||||
}
|
||||
|
||||
void foo() {
|
||||
synchronized (sdf) { // preferred
|
||||
sdf.format("foo");
|
||||
}
|
||||
}
|
||||
|
||||
Object obj() {
|
||||
if (baz == null) { // baz may be non-null yet not fully created
|
||||
synchronized (this) {
|
||||
if (baz == null) {
|
||||
baz = new Object();
|
||||
}
|
||||
}
|
||||
}
|
||||
return baz;
|
||||
}
|
||||
}
|
55
src/test/resources/java/Performance.java
Normal file
55
src/test/resources/java/Performance.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;/**
|
||||
* Performance class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Performance {
|
||||
public static void main(String[] as) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Performance p = new Performance(); // Avoid this whenever you can it's really expensive
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean checkTrimEmpty(String str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
if (!Character.isWhitespace(str.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void foo() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("a"); // avoid this
|
||||
|
||||
String foo = " ";
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("Hello"); // poor
|
||||
buf.append(foo);
|
||||
buf.append("World");
|
||||
|
||||
buf.append("Hello").append(" ").append("World");
|
||||
|
||||
StringBuffer sbuf = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir"));
|
||||
// poor
|
||||
}
|
||||
}
|
34
src/test/resources/java/Security.java
Normal file
34
src/test/resources/java/Security.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 java;import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* Security class
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Security {
|
||||
void bad() {
|
||||
byte[] iv = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, };
|
||||
}
|
||||
|
||||
void alsoBad() {
|
||||
byte[] iv = "secret iv in here".getBytes();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue