Skip to content

Commit

Permalink
Fixed Moving code section
Browse files Browse the repository at this point in the history
  • Loading branch information
anchouls committed Jan 25, 2024
1 parent 1661b9a commit 9eca5eb
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ build/
out/
!**/src/main/**/out/
!**/src/test/**/out/
*-remote-info.yaml
.coursecreator/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jetbrains.refactoring.course.moving.car;
package jetbrains.refactoring.course.moving;

public class Car {
private final int gearsNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package jetbrains.refactoring.course.moving.driver;

import jetbrains.refactoring.course.moving.car.Car;
package jetbrains.refactoring.course.moving;

public class Driver {
private Car car;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package jetbrains.refactoring.course.moving;

import jetbrains.refactoring.course.moving.car.Car;
import jetbrains.refactoring.course.moving.driver.Driver;

public class Main {
public static void main(String[] args) {
Car car = new Car(5);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type: edu
custom_name: Find more appropriate classes for methods
files:
- name: src/main/java/jetbrains/refactoring/course/moving/driver/Driver.java
- name: src/main/java/jetbrains/refactoring/course/moving/Driver.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/Main.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/car/Car.java
visible: true
- name: test/MovingMethodsTest.java
visible: false
- name: src/main/java/jetbrains/refactoring/course/moving/Car.java
visible: true
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Next, you need to move methods to a more appropriate place using the Move refactoring technique.
Move some methods from one class to another.
Change the visibility of some methods and variables to the correct one.

### Hints

Expand All @@ -15,4 +16,12 @@ Use the F6 shortcut to move a code element.

Move the `start()` and `stop()` methods to the `Car` class.

Change the visibility of the `start` method to `public`

Change the visibility of the `stop` method to `public`

Change the visibility of the `setGear` method to `private` - it's only used inside the class

Change the visibility of the `engineStarted` variable to `private` - it's only used inside the class

</div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import junit.framework.AssertionFailedError;
import org.jetbrains.academy.test.system.core.models.Visibility;
import org.jetbrains.academy.test.system.core.models.classes.ClassType;
import org.jetbrains.academy.test.system.core.models.classes.TestClass;
Expand Down Expand Up @@ -27,15 +28,15 @@ public class MovingMethodsTest extends BaseIjTestClass {
static void beforeAll() throws IOException {
String taskDirectoryPath = System.getProperty("user.dir");
Path carPath = Paths.get(taskDirectoryPath,
"src/main/java/jetbrains/refactoring/course/moving/car/Car.java");
"src/main/java/jetbrains/refactoring/course/moving/Car.java");
carText = Files.readString(carPath);
Path driverPath = Paths.get(taskDirectoryPath,
"src/main/java/jetbrains/refactoring/course/moving/driver/Driver.java");
"src/main/java/jetbrains/refactoring/course/moving/Driver.java");
driverText = Files.readString(driverPath);

driverClass = new TestClass(
"Driver",
"jetbrains.refactoring.course.moving.driver",
"jetbrains.refactoring.course.moving",
Visibility.PUBLIC,
ClassType.CLASS,
List.of(
Expand Down Expand Up @@ -78,7 +79,7 @@ static void beforeAll() throws IOException {

carClass = new TestClass(
"Car",
"jetbrains.refactoring.course.moving.car",
"jetbrains.refactoring.course.moving",
Visibility.PUBLIC,
ClassType.CLASS,
List.of(
Expand Down Expand Up @@ -169,6 +170,8 @@ public void driverClassTest() {
public void carClassTest() {
Class<?> clazz = carClass.checkBaseDefinition();
carClass.checkFieldsDefinition(clazz, true);
carClass.checkDeclaredMethods(clazz);
Assertions.assertDoesNotThrow(() -> carClass.checkDeclaredMethods(clazz),
"Please, change the visibility of the start and stop methods to public and the setGear method to private"
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jetbrains.refactoring.course.moving.car;
package jetbrains.refactoring.course.moving;

public class Car {
private final int gearsNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package jetbrains.refactoring.course.moving.driver;

import jetbrains.refactoring.course.moving.car.Car;
package jetbrains.refactoring.course.moving;

public class Driver {
private Car car;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package jetbrains.refactoring.course.moving;

import jetbrains.refactoring.course.moving.car.Car;
import jetbrains.refactoring.course.moving.driver.Driver;

public class Main {
public static void main(String[] args) {
Car car = new Car(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ custom_name: Find more appropriate files for classes
files:
- name: src/main/java/jetbrains/refactoring/course/moving/Main.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/car/Car.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/driver/Driver.java
visible: true
- name: test/MovingTest.java
visible: false
- name: src/main/java/jetbrains/refactoring/course/moving/Car.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/Driver.java
visible: true
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ Move the `Car` class to the `Car.java` file.

Move the `Driver` class to the `Driver.java` file.

Change the visibility of the `Driver` class to `public`

Change the visibility of the `Car` class to `public`

</div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import org.jetbrains.academy.test.system.core.models.Visibility;
import org.jetbrains.academy.test.system.core.models.classes.ClassType;
import org.jetbrains.academy.test.system.core.models.classes.TestClass;
import org.jetbrains.academy.test.system.core.models.method.TestMethod;
import org.jetbrains.academy.test.system.core.models.variable.TestVariable;
import org.jetbrains.academy.test.system.java.test.BaseIjTestClass;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -7,21 +12,129 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static java.util.Collections.emptyList;

public class MovingTest extends BaseIjTestClass {

private static String carText;
private static String driverText;
private static TestClass driverClass;
private static TestClass carClass;

@BeforeAll
static void beforeAll() throws IOException {
String taskDirectoryPath = System.getProperty("user.dir");
Path carPath = Paths.get(taskDirectoryPath,
"src/main/java/jetbrains/refactoring/course/moving/car/Car.java");
"src/main/java/jetbrains/refactoring/course/moving/Car.java");
carText = Files.readString(carPath);
Path driverPath = Paths.get(taskDirectoryPath,
"src/main/java/jetbrains/refactoring/course/moving/driver/Driver.java");
"src/main/java/jetbrains/refactoring/course/moving/Driver.java");
driverText = Files.readString(driverPath);
driverClass = new TestClass(
"Driver",
"jetbrains.refactoring.course.moving",
Visibility.PUBLIC,
ClassType.CLASS,
List.of(
new TestVariable(
"car",
"Car",
null,
Visibility.PRIVATE,
false,
true,
false
)
),
List.of(
new TestMethod(
"changeCar",
"void",
List.of(
new TestVariable("car", "Car")
),
Visibility.PUBLIC
),
new TestMethod(
"driving",
"void",
List.of(new TestVariable("destination", "String")),
Visibility.PRIVATE
),
new TestMethod(
"driveTo",
"void",
List.of(new TestVariable("destination", "String")),
Visibility.PUBLIC
),
new TestMethod(
"start",
"void",
emptyList(),
Visibility.PRIVATE
),
new TestMethod(
"stop",
"void",
emptyList(),
Visibility.PRIVATE
)
),
false,
emptyList(),
emptyList()
);

carClass = new TestClass(
"Car",
"jetbrains.refactoring.course.moving",
Visibility.PUBLIC,
ClassType.CLASS,
List.of(
new TestVariable(
"gearsNumber",
"int",
null,
Visibility.PRIVATE,
true,
true,
false
),
new TestVariable(
"engineStarted",
"boolean",
"false",
Visibility.PRIVATE,
false,
false,
false
),
new TestVariable(
"gear",
"int",
"0",
Visibility.PRIVATE,
false,
false,
false
)
),
List.of(
new TestMethod(
"setGear",
"void",
List.of(
new TestVariable("gear", "int")
),
Visibility.PUBLIC
)
),
false,
emptyList(),
emptyList()
);
}

@Test
Expand All @@ -39,4 +152,22 @@ public void testDriverClassMoved() throws Exception {
Assertions.assertTrue(hasClass("Driver"),
"Please, move the Driver class to a separate file in the driver package");
}

@Test
public void driverClassTest() {
try {
driverClass.checkBaseDefinition();
} catch (Exception e) {
Assertions.fail("Please, change the visibility of the Driver class to public");
}
}

@Test
public void carClassTest() {
try {
carClass.checkBaseDefinition();
} catch (Exception e) {
Assertions.fail("Please, change the visibility of the Car class to public");
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ custom_name: What is Move refactoring?
files:
- name: src/main/java/jetbrains/refactoring/course/moving/Main.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/car/Car.java
visible: true
- name: src/main/java/jetbrains/refactoring/course/moving/driver/Driver.java
visible: true

0 comments on commit 9eca5eb

Please sign in to comment.