Skip to content

Commit 0a62044

Browse files
committed
Adds configuration and sub project for quiz_java project application.
1 parent 02d9238 commit 0a62044

File tree

12 files changed

+741
-0
lines changed

12 files changed

+741
-0
lines changed

lesson_02/quiz_java/.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

lesson_02/quiz_java/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/9.0.0/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
plugins {
8+
java
9+
id("org.springframework.boot") version "3.1.0"
10+
id("io.spring.dependency-management") version "1.1.0"
11+
application
12+
}
13+
14+
15+
java {
16+
toolchain {
17+
languageVersion.set(JavaLanguageVersion.of(21))
18+
}
19+
}
20+
21+
repositories {
22+
mavenCentral()
23+
}
24+
25+
dependencies {
26+
implementation("org.apache.commons:commons-text:1.10.0")
27+
implementation(project(":instructional-lib"))
28+
implementation("org.springframework.boot:spring-boot-starter")
29+
testImplementation("org.springframework.boot:spring-boot-starter-test")
30+
31+
}
32+
33+
34+
35+
application {
36+
// Define the main class for the application.
37+
mainClass.set("org.example.Lesson2")
38+
}
39+
40+
tasks.named<Test>("test") {
41+
// Use JUnit Platform for unit tests.
42+
useJUnitPlatform()
43+
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
5+
package org.example;
6+
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
14+
import com.codedifferently.instructional.quiz.AnswerChoice;
15+
import com.codedifferently.instructional.quiz.MultipleChoiceQuizQuestion;
16+
import com.codedifferently.instructional.quiz.QuizPrinter;
17+
import com.codedifferently.instructional.quiz.QuizQuestion;
18+
19+
@SpringBootApplication
20+
21+
public class Lesson2 {
22+
public static void main(String[] args) {
23+
if (System.getenv("JEST_WORKER_ID") == null) {
24+
new Lesson2().run();
25+
}
26+
}
27+
28+
public void run() {
29+
List<QuizQuestion> quizQuestions = Lesson2.makeQuizQuestions();
30+
if (quizQuestions == null) throw new RuntimeException("Quiz questions cannot be null");
31+
QuizPrinter printer = new QuizPrinter();
32+
printer.printQuiz(quizQuestions);
33+
}
34+
35+
public static List<QuizQuestion> makeQuizQuestions() {
36+
return Arrays.asList(
37+
makeQuestion0(),
38+
makeQuestion1(),
39+
makeQuestion2(),
40+
makeQuestion3(),
41+
makeQuestion4(),
42+
makeQuestion5(),
43+
makeQuestion6(),
44+
makeQuestion7(),
45+
makeQuestion8(),
46+
makeQuestion9(),
47+
makeQuestion10()
48+
);
49+
}
50+
51+
private static QuizQuestion makeQuestion0() {
52+
return new MultipleChoiceQuizQuestion(
53+
0,
54+
"What is the main purpose of version control?",
55+
Map.of(
56+
AnswerChoice.A, "To make backups of files",
57+
AnswerChoice.B, "To keep a record of changes over time",
58+
AnswerChoice.C, "To delete unnecessary files",
59+
AnswerChoice.D, "To run code more efficiently"
60+
),
61+
AnswerChoice.B
62+
);
63+
}
64+
65+
private static QuizQuestion makeQuestion1() {
66+
return new MultipleChoiceQuizQuestion(
67+
1,
68+
"What is a fork in Git?",
69+
Map.of(
70+
AnswerChoice.A, "A duplicate copy of a repository that you own and modify",
71+
AnswerChoice.B, "A temporary backup of the code",
72+
AnswerChoice.C, "A tool for merging branches",
73+
AnswerChoice.D, "A way to delete a repository"
74+
),
75+
AnswerChoice.A
76+
);
77+
}
78+
79+
private static QuizQuestion makeQuestion2() {
80+
return new MultipleChoiceQuizQuestion(
81+
2,
82+
"Which of the following is NOT part of the basic Git workflow?",
83+
Map.of(
84+
AnswerChoice.A, "Pull the latest changes",
85+
AnswerChoice.B, "Commit changes locally",
86+
AnswerChoice.C, "Push changes to the server",
87+
AnswerChoice.D, "Write code directly in GitHub"
88+
),
89+
AnswerChoice.D
90+
);
91+
}
92+
93+
private static QuizQuestion makeQuestion3() {
94+
return new MultipleChoiceQuizQuestion(
95+
3,
96+
"What command is used to combine changes from different branches?",
97+
Map.of(
98+
AnswerChoice.A, "git commit",
99+
AnswerChoice.B, "git merge",
100+
AnswerChoice.C, "git branch",
101+
AnswerChoice.D, "git pull"
102+
),
103+
AnswerChoice.B
104+
);
105+
}
106+
107+
private static QuizQuestion makeQuestion4() {
108+
return new MultipleChoiceQuizQuestion(
109+
4,
110+
"Which IDE is being used in the class?",
111+
Map.of(
112+
AnswerChoice.A, "Eclipse",
113+
AnswerChoice.B, "IntelliJ IDEA",
114+
AnswerChoice.C, "NetBeans",
115+
AnswerChoice.D, "VS Code"
116+
),
117+
AnswerChoice.D
118+
);
119+
}
120+
121+
private static QuizQuestion makeQuestion5() {
122+
return new MultipleChoiceQuizQuestion(
123+
5,
124+
"What feature allows developers to work from the same pre-configured environment in VS Code?",
125+
Map.of(
126+
AnswerChoice.A, "Extensions",
127+
AnswerChoice.B, "Debugger",
128+
AnswerChoice.C, "Dev Containers",
129+
AnswerChoice.D, "Source Control"
130+
),
131+
AnswerChoice.C
132+
);
133+
}
134+
135+
private static QuizQuestion makeQuestion6() {
136+
return new MultipleChoiceQuizQuestion(
137+
6,
138+
"What is NOT a reason for using an IDE?",
139+
Map.of(
140+
AnswerChoice.A, "Editing and refactoring code",
141+
AnswerChoice.B, "Browsing code",
142+
AnswerChoice.C, "Playing music",
143+
AnswerChoice.D, "Managing source control"
144+
),
145+
AnswerChoice.C
146+
);
147+
}
148+
149+
private static QuizQuestion makeQuestion7() {
150+
return new MultipleChoiceQuizQuestion(
151+
7,
152+
"What is the command to list files in the current directory?",
153+
Map.of(
154+
AnswerChoice.A, "pwd",
155+
AnswerChoice.B, "ls",
156+
AnswerChoice.C, "cd",
157+
AnswerChoice.D, "mkdir"
158+
),
159+
AnswerChoice.B
160+
);
161+
}
162+
163+
private static QuizQuestion makeQuestion8() {
164+
return new MultipleChoiceQuizQuestion(
165+
8,
166+
"Which command is used to change directories in the terminal?",
167+
Map.of(
168+
AnswerChoice.A, "pwd",
169+
AnswerChoice.B, "ls",
170+
AnswerChoice.C, "cd",
171+
AnswerChoice.D, "mkdir"
172+
),
173+
AnswerChoice.C
174+
);
175+
}
176+
177+
private static QuizQuestion makeQuestion9() {
178+
return new MultipleChoiceQuizQuestion(
179+
9,
180+
"What does the command `chmod` do?",
181+
Map.of(
182+
AnswerChoice.A, "Change file or directory permissions",
183+
AnswerChoice.B, "List files in a directory",
184+
AnswerChoice.C, "Remove a file or directory",
185+
AnswerChoice.D, "Copy a file or directory"
186+
),
187+
AnswerChoice.A
188+
);
189+
}
190+
191+
private static QuizQuestion makeQuestion10() {
192+
return new MultipleChoiceQuizQuestion(
193+
10,
194+
"What is the shortcut for getting to the Mac terminal?",
195+
Map.of(
196+
AnswerChoice.A, "⌘ + Shift + T",
197+
AnswerChoice.B, "⌘ + Spacebar, then type \"terminal\"",
198+
AnswerChoice.C, "⌘ + Q",
199+
AnswerChoice.D, "⌘ + S, then type \"terminal\""
200+
),
201+
AnswerChoice.B
202+
);
203+
}
204+
205+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
5+
package org.example;
6+
7+
8+
import java.util.Arrays;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import java.util.List;
12+
import java.util.Comparator;
13+
import java.util.List;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.DisplayName;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Bean;
24+
25+
import com.codedifferently.instructional.quiz.AnswerChoice;
26+
import com.codedifferently.instructional.quiz.QuizConfig;
27+
import com.codedifferently.instructional.quiz.QuizQuestion;
28+
29+
30+
@SpringBootTest
31+
32+
public class Lesson2Test {
33+
private QuizConfig quizConfig;
34+
private List<QuizQuestion> quizQuestions;
35+
private static final int EXPECTED_NUMBER_OF_QUESTIONS = 11;
36+
37+
@BeforeEach
38+
public void setUp() {
39+
quizConfig = new QuizConfig(Paths.get("../quiz.yaml").toAbsolutePath().toString());
40+
getQuestions();
41+
}
42+
43+
private void getQuestions() {
44+
quizQuestions = Lesson2.makeQuizQuestions();
45+
quizQuestions.sort(Comparator.comparingInt(QuizQuestion::getQuestionNumber));
46+
}
47+
48+
@Test
49+
@DisplayName("checkQuizQuestions_areAssembledCorrectly")
50+
public void checkQuizQuestions_areAssembledCorrectly() {
51+
// Expect the right number of questions.
52+
assertEquals(EXPECTED_NUMBER_OF_QUESTIONS, quizQuestions.size());
53+
54+
// Expect questions to be numbered correctly.
55+
for (int i = 0; i < quizQuestions.size(); i++) {
56+
assertEquals(i, quizQuestions.get(i).getQuestionNumber());
57+
}
58+
}
59+
60+
@Test
61+
@DisplayName("checkQuizQuestions_promptsAreUnique")
62+
public void checkQuizQuestions_promptsAreUnique() {
63+
Set<String> questionPrompts = new HashSet<>();
64+
for (QuizQuestion q : quizQuestions) {
65+
questionPrompts.add(q.getQuestionPrompt());
66+
}
67+
assertEquals(EXPECTED_NUMBER_OF_QUESTIONS, questionPrompts.size());
68+
}
69+
70+
@Test
71+
@DisplayName("checkQuestions_answeredCorrectly")
72+
public void checkQuestions_answeredCorrectly() throws Exception {
73+
assertEquals(quizQuestions.size(), quizConfig.size("default"));
74+
75+
for (QuizQuestion question : quizQuestions) {
76+
AnswerChoice actualAnswer = question.getAnswer();
77+
78+
// Check that the question was answered.
79+
assertNotEquals(AnswerChoice.UNANSWERED, actualAnswer);
80+
81+
// Check that the answer is correct.
82+
assertTrue(
83+
quizConfig.checkAnswer(
84+
"default",
85+
question.getQuestionNumber(),
86+
actualAnswer
87+
)
88+
);
89+
}
90+
}
91+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
quiz:
2+
answers:
3+
default:
4+
- $2y$10$Mijvs3PeAc.DsZBhT4T0jepZqixAOb61iPotSSlIVJGFqpGeBU5dS #0
5+
- $2y$10$fMlkDDq4L9g2Q0lzdTMG1u7aw6rjJhKh.B0e/AYfQHUsXhCM6UGIS #1
6+
- $2y$10$wJZxd8Ha4EEbb16UoUcqgO7oOU.h8sUOsv8FGu6gLiuKwtI4RKFT2 #2
7+
- $2y$10$xAUz.hXIqTamXZg28Hgepe28PS431mmq0WlM48Rji7EkSKD5NfzFy #3
8+
- $2y$10$hbV87MTi/JH51hdNfBP9juELK6LSAxPagrFqcnXAD3gLIEOCCqENW #4
9+
- $2y$10$kiOyjtBQ7B2BszTJajO9keCBlOPh4pLM.mzugzWB2vtGkOWCSJ366 #5
10+
- $2y$10$hCrDJhXV7Gwk0PsBm086p.obO0dk.ZseCfvrx.Vu7U/jqODPaxmFy #6
11+
- $2y$10$LvnU8fBR2ABdf2/gatkW1e1n5XcFQWSE6QfcUjIMo2g8Gu6p1NPJu #7
12+
- $2y$10$DQUbiJqDiMpypZ1FEwK1QejNDwk05F6suGWj6PdEowOvvsoLYGzZy #8
13+
- $2y$10$hzT0JgkSPhMzeuqK4VnD1eoNYkCZ.bg.QL3.VF0vJrmyceWq3bHgC #9
14+
- $2y$10$qyOdRBWT8MO.naUyhZRMr.VIy1NkxDY.1WvbnlTu2bcXkgTnpCBH2 #10
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
3+
4+
[versions]
5+
guava = "33.0.0-jre"
6+
junit-jupiter = "5.10.2"
7+
8+
[libraries]
9+
guava = { module = "com.google.guava:guava", version.ref = "guava" }
10+
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
42.4 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)