-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractFactoryTest.java
148 lines (122 loc) · 4.92 KB
/
AbstractFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//@formatter:off
/*
* Abstract factory test case - demonstrates ABSTRACT FACTORY.
* Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
* Copyright 2014 Karl Eilebrecht
*
* 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
*
* http://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.
*/
//@formatter:on
package de.calamanari.pk.abstractfactory;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.HashMap;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.calamanari.pk.util.FileUtils;
import de.calamanari.pk.util.TimeUtils;
/**
* Test case for ABSTRACT FACTORY
*
* @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
*/
public class AbstractFactoryTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFactoryTest.class);
/**
* for the testcases we simulate some kind of registry.
*/
private static final HashMap<String, AbstractDataManager> SYSTEM_REGISTRY = new HashMap<>();
/**
* name of configuration 1
*/
private static final String CONFIG_KEY1 = "config1";
/**
* name of configuration 2
*/
private static final String CONFIG_KEY2 = "config2";
/**
* Test text sample
*/
private static final String FUNNY_TEXT = """
Three little birds
(Anton, Jenny and Laura)
eat five big pigs
with potato wedges.""";
/**
* name of the file to be created
*/
private static final String FUNNY_TEXT_FILE_NAME = "Funny Text";
/**
* By default the files will be deleted after test, set this to true if you want to look into
*/
private static final boolean KEEP_FILES_AFTER_TEST = false;
@BeforeClass
public static void setUpBeforeClass() throws RuntimeException {
SYSTEM_REGISTRY.put(CONFIG_KEY1, new PlainFileDataManager());
SYSTEM_REGISTRY.put(CONFIG_KEY2, new SecureFileDataManager());
}
@AfterClass
public static void setUpAfterClass() throws RuntimeException {
if (!KEEP_FILES_AFTER_TEST) {
File file1 = new File(FileUtils.getHomeDirectory(), FUNNY_TEXT_FILE_NAME + ".txt");
File file2 = new File(FileUtils.getHomeDirectory(), FUNNY_TEXT_FILE_NAME + ".sec");
if (file1.exists()) {
file1.delete();
}
if (file2.exists()) {
file2.delete();
}
}
}
@Test
public void testWithConfig1() {
// Adjust the log-level in logback.xml to DEBUG to see the ABSTRACT FACTORY working
LOGGER.info("Performing test with configuration 1 ...");
long startTimeNanos = System.nanoTime();
commonTestInternal(CONFIG_KEY1, FUNNY_TEXT);
String elapsedSeconds = TimeUtils.formatNanosAsSeconds(System.nanoTime() - startTimeNanos);
LOGGER.info("Execution of test with configuration 1 was successful! Elapsed time: {} s", elapsedSeconds);
}
@Test
public void testWithConfig2() {
LOGGER.info("Performing test with configuration 2 ...");
long startTimeNanos = System.nanoTime();
commonTestInternal(CONFIG_KEY2, FUNNY_TEXT);
String elapsedSeconds = TimeUtils.formatNanosAsSeconds(System.nanoTime() - startTimeNanos);
LOGGER.info("Execution of test with configuration 2 was successful! Elapsed time: {} s", elapsedSeconds);
}
/**
* Central generic part of the test.
*
* @param configKey which configuration to be used
* @param funnyText text to store
*/
private void commonTestInternal(String configKey, String funnyText) {
LOGGER.debug("Text: \n============================\n{}\n============================\n.", funnyText);
// retrieve a concrete manager (CONCRETE FACTORY)
AbstractDataManager mgr = SYSTEM_REGISTRY.get(configKey);
LOGGER.debug("Using Manager: {}", mgr.getName());
// retrieve concrete writer (CONCRETE PRODUCT)
AbstractDataWriter writer = mgr.createDataWriter("Funny Text");
LOGGER.debug("Writing to destination: {}", writer.getDestinationInfo());
writer.writeString(funnyText);
// retrieve concrete reader (CONCRETE PRODUCT)
AbstractDataReader reader = mgr.createDataReader("Funny Text");
LOGGER.debug("Reading from source: {}", reader.getSourceInfo());
String result = reader.readString();
assertEquals(result, funnyText);
}
}