Forráskód Böngészése

stworzono pierwszy test dla klasy FileConverter

Adam Matuszewski 1 éve
szülő
commit
ef327badca

+ 1 - 1
KonwersjaLiter2/.idea/compiler.xml

@@ -9,7 +9,7 @@
         <sourceOutputDir name="target/generated-sources/annotations" />
         <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
         <outputRelativeToContentRoot value="true" />
-        <module name="KonwersjaLiter2" />
+        <module name="app" />
       </profile>
     </annotationProcessing>
   </component>

+ 1 - 0
KonwersjaLiter2/.idea/vcs.xml

@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
     <mapping directory="$PROJECT_DIR$" vcs="Git" />
   </component>
 </project>

+ 101 - 6
KonwersjaLiter2/pom.xml

@@ -1,9 +1,104 @@
 <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 http://maven.apache.org/maven-v4_0_0.xsd">
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
-  <groupId>org.example</groupId>
-  <artifactId>KonwersjaLiter2</artifactId>
-  <version>1.0-SNAPSHOT</version>
-  <name>Archetype - KonwersjaLiter2</name>
-  <url>http://maven.apache.org</url>
+  <groupId>pl.dmcs</groupId>
+  <artifactId>app</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>21</maven.compiler.source>
+    <maven.compiler.target>21</maven.compiler.target>
+    <junit.version>5.10.2</junit.version>
+  </properties>
+  <!--  useful for documentation generation  -->
+  <name>My sample project</name>
+  <description>This is my sample project.</description>
+  <url>https://weeia.p.lodz.pl</url>
+  <dependencies>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <version>${junit.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <version>${junit.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+      <version>RELEASE</version>
+      <scope>compile</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <pluginManagement>
+      <!--  lock down plugins versions to avoid using Maven defaults  -->
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-resources-plugin</artifactId>
+          <version>3.3.1</version>
+          <configuration>
+            <includeEmptyDirs>true</includeEmptyDirs>
+          </configuration>
+        </plugin>
+        <plugin>
+          <artifactId>maven-clean-plugin</artifactId>
+          <version>3.3.2</version>
+        </plugin>
+        <!--  default lifecycle  -->
+        <plugin>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>3.12.1</version>
+          <configuration>
+            <source>21</source>
+            <target>21</target>
+          </configuration>
+        </plugin>
+        <plugin>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>3.2.5</version>
+          <configuration>
+            <includes>
+              <include>**/*Test.java</include>
+            </includes>
+          </configuration>
+        </plugin>
+        <plugin>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>3.3.0</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-install-plugin</artifactId>
+          <version>3.1.1</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-deploy-plugin</artifactId>
+          <version>3.1.1</version>
+        </plugin>
+        <!--  site lifecycle  -->
+        <plugin>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>4.0.0-M13</version>
+        </plugin>
+        <plugin>
+          <artifactId>maven-project-info-reports-plugin</artifactId>
+          <version>3.5.0</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <testResources>
+      <testResource>
+        <directory>src/test/resources</directory>
+        <excludes>
+          <exclude>**/.gitkeep</exclude>
+        </excludes>
+      </testResource>
+    </testResources>
+  </build>
 </project>

+ 37 - 0
KonwersjaLiter2/src/main/java/pl/dmcs/FileConverter.java

@@ -0,0 +1,37 @@
+package pl.dmcs;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+
+public class FileConverter {
+
+    public void convertToUpperCase(String filePath) throws IOException {
+        Path originalPath = Paths.get(filePath);
+        Path tempFilePath = Paths.get(filePath + ".tmp");
+
+        try (BufferedReader reader = new BufferedReader(new FileReader(originalPath.toFile()));
+             BufferedWriter writer = new BufferedWriter(new FileWriter(tempFilePath.toFile()))) {
+
+            reader.lines()
+                    .map(String::toUpperCase)
+                    .forEach(line -> {
+                        try {
+                            writer.write(line);
+                            writer.newLine();
+                        } catch (IOException e) {
+                            throw new RuntimeException(e);
+                        }
+                    });
+        }
+
+        Files.delete(originalPath);
+        Files.move(tempFilePath, originalPath);
+    }
+}

+ 4 - 0
KonwersjaLiter2/src/main/resources/test.txt

@@ -0,0 +1,4 @@
+TEST
+PRZYKLADOWY TEKST MALYMI LITERAMI
+12345
+ABCDEF

+ 32 - 0
KonwersjaLiter2/src/test/java/pl/dmcs/FileConverterTest.java

@@ -0,0 +1,32 @@
+package pl.dmcs;
+import org.testng.annotations.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class FileConverterTest {
+
+    @Test
+    public void testConvertToUpperCase() throws IOException {
+        Path filePath = Paths.get("src/main/resources/test.txt");
+        String originalContent = Files.readString(filePath);
+        String expectedContent ="TEST\n" +
+                "PRZYKLADOWY TEKST MALYMI LITERAMI\n" +
+                "12345\n" +
+                "ABCDEF\n";
+
+        FileConverter converter = new FileConverter();
+        converter.convertToUpperCase(filePath.toString());
+
+        String actualContent = Files.readString(filePath);
+
+        expectedContent = expectedContent.replace("\r\n", "\n");
+        actualContent = actualContent.replace("\r\n", "\n");
+
+        assertEquals(expectedContent, actualContent);
+
+        //  Files.writeString(filePath, originalContent);
+    }
+}