Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 2 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_11.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@
<artifactId>javatest1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/getintent/interview/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@
import java.util.List;

public class TreeNode {

private int value;
private List<TreeNode> children = new ArrayList<>();

public TreeNode(int value) {
this.value = value;
}

public List<TreeNode> getChildren() {
return children;
}

public void setChildren(List<TreeNode> children) {
this.children = children;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public void addChild(TreeNode child) {
children.add(child);
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/getintent/interview/TreeWriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2013
* Kozlov Nikita
*/
package com.getintent.interview;

import java.io.IOException;
import java.io.Writer;

/**
* @author Kozlov Nikita
*/
public class TreeWriterImpl implements TreeWriter {

private static final String INDENT = " ";

private TreeNode treeNode;

@Override
public void write(TreeNode node, Writer w) {
this.treeNode = node;
if (treeNode != null) {
try {
this.write(w, 0);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("Treenode can not be null.");
}
}

// ===================================================================================================================
// = Implementation
// ===================================================================================================================

private void write(Writer w, int depth) throws IOException {
for (int i = 0; i < depth; i++) {
w.append(INDENT);
}
w.append(String.valueOf(this.treeNode.getValue()))
.append("\n");
w.flush();
for (TreeNode node : treeNode.getChildren()) {
treeNode = node;
write(w, depth + 1);
}
}
}
77 changes: 77 additions & 0 deletions src/test/java/com/getintent/interview/TreeWriterImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2013
* Kozlov Nikita
*/
package com.getintent.interview;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.*;

/**
* @author Kozlov Nikita
*/
public class TreeWriterImplTest {

private Writer writer;
private TreeNode rootNode;
private final static String FILE_PATH_ACTUAL = "src/test/java/tree-out-actual.TXT";
private final static String FILE_PATH_EXPECTED = "src/test/java/tree-out-expected.TXT";

@Before
public void setUp() throws Exception {
this.rootNode = new TreeNode(6);
TreeNode treeNode10 = new TreeNode(10);
TreeNode treeNode7 = new TreeNode(7);
TreeNode treeNode13 = new TreeNode(13);
TreeNode treeNode16 = new TreeNode(16);
TreeNode treeNode21 = new TreeNode(21);
TreeNode treeNode3 = new TreeNode(3);
TreeNode treeNode4 = new TreeNode(4);

rootNode.addChild(treeNode7);
rootNode.addChild(treeNode10);
rootNode.addChild(treeNode13);
treeNode13.addChild(treeNode16);
treeNode13.addChild(treeNode21);
treeNode7.addChild(treeNode3);
treeNode3.addChild(treeNode4);
File file = new File(FILE_PATH_ACTUAL);
this.writer = new FileWriter(file);
}

@After
public void tearDown() throws Exception {
if (this.writer != null) {
this.writer.close();
}
}

@Test
public void testWrite() throws Exception {
TreeWriter treeWriter = new TreeWriterImpl();
treeWriter.write(this.rootNode, this.writer);
writer.close();
String expectedCheckSum = this.getCheckSumFile(FILE_PATH_EXPECTED);
String actualCheckSum = this.getCheckSumFile(FILE_PATH_ACTUAL);
Assert.assertEquals(expectedCheckSum, actualCheckSum);
}

// ===================================================================================================================
// = Implementation
// ===================================================================================================================

private String getCheckSumFile(String filePath) {
String checksum = null;
try {
checksum = DigestUtils.md5Hex(new FileInputStream(filePath));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return checksum;
}
}
8 changes: 8 additions & 0 deletions src/test/java/tree-out-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
6
7
3
4
10
13
16
21