Commit 47169d59 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

antipatterns

parent 34495c72
Pipeline #90511 passed with stage
in 1 minute and 6 seconds
target/
.classpath
.project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it1901</groupId>
<artifactId>antipatterns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Test with JUnit5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiling code -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<encoding>UTF-8</encoding>
<release>13</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<!--
<configuration>
<argLine>${argLine} dash dash add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED</argLine>
</configuration>
-->
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package antipatterns;
public class DataClassWithEffectivelyFinalFields {
private final String name; // should be final
private final int number; // should be final
public DataClassWithEffectivelyFinalFields(final String name, final int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
}
package antipatterns;
// ok, but see the test
public class DataClassWithLittleLogic {
private String name;
private int number;
public DataClassWithLittleLogic(final String name, final int number) {
this.name = name;
this.number = number;
}
@Override
public String toString() {
return String.format("[DataClassWithLittleLogic name=%s number=%s]", name, number);
}
public DataClassWithLittleLogic() {
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(final int number) {
this.number = number;
}
}
package antipatterns;
// see DataClassWithValidationTest
public class DataClassWithValidation {
private String name;
public DataClassWithValidation(final String name) {
// should use setName to ensure validation logic is run
this.name = name;
}
public DataClassWithValidation() {
}
public String getName() {
return name;
}
public void setName(final String name) {
if (name.startsWith(" ")) {
// should rather use trim() or strip() and check the length
throw new IllegalArgumentException("Name cannot begin with blank");
}
this.name = name;
}
}
package antipatterns;
import java.util.Collection;
import java.util.Iterator;
public class NonEncapsulatedList implements Iterable<String> {
private final Collection<String> names;
public NonEncapsulatedList(final Collection<String> names) {
this.names = names;
}
public Collection<String> getNames() {
return names;
}
public Iterator<String> iterator() {
return names.iterator();
}
}
package antipatterns;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DataClassWithLittleLogicTest {
private DataClassWithLittleLogic instance1, instance2;
@BeforeEach
public void setup() {
instance1 = new DataClassWithLittleLogic();
instance2 = new DataClassWithLittleLogic("High", 5);
}
@Test
public void testConstructor() {
Assertions.assertEquals("High", instance2.getName());
}
@Test
public void testToString() {
Assertions.assertEquals("[DataClassWithLittleLogic name=High number=5]", instance2.toString());
}
@Test
public void testSetName() {
instance1.setName("Low");
Assertions.assertEquals("Low", instance1.getName());
}
}
package antipatterns;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DataClassWithValidationTest {
@Test
public void testSetName() {
try {
new DataClassWithValidation(" name starting with blank");
Assertions.fail("Should throw IllegalArgumentException");
} catch (final IllegalArgumentException iae) {
} catch (final Exception iae) {
Assertions.fail("Should throw IllegalArgumentException");
}
try {
new DataClassWithValidation("ok name");
} catch (final Exception iae) {
Assertions.fail();
}
}
@Test
public void testBetterSetName_invalidName() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new DataClassWithValidation(" name starting with blank"));
}
@Test
public void testBetterSetName_validName() {
new DataClassWithValidation("ok name");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment