1919
2020/*
2121 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22- * Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
22+ * Portions Copyright (c) 2018-2019 , Chris Fraire <cfraire@me.com>.
2323 */
2424package org .opengrok .indexer .util ;
2525
26+ import static org .junit .Assert .assertFalse ;
2627import static org .junit .Assert .assertNotNull ;
2728import static org .junit .Assert .assertTrue ;
2829
4142 */
4243public class TestRepository {
4344
45+ private final RuntimeEnvironment env ;
4446 private File sourceRoot ;
4547 private File dataRoot ;
48+ private File externalRoot ;
49+
50+ public TestRepository () {
51+ env = RuntimeEnvironment .getInstance ();
52+ }
4653
4754 public void createEmpty () throws IOException {
48- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
4955 sourceRoot = Files .createTempDirectory ("source" ).toFile ();
5056 dataRoot = Files .createTempDirectory ("data" ).toFile ();
5157 env .setSourceRoot (sourceRoot .getAbsolutePath ());
5258 env .setDataRoot (dataRoot .getAbsolutePath ());
5359 }
5460
5561 public void create (InputStream inputBundle ) throws IOException {
56- File sourceBundle = null ;
57- try {
58- sourceRoot = Files .createTempDirectory ("source" ).toFile ();
59- dataRoot = Files .createTempDirectory ("data" ).toFile ();
60- sourceBundle = File .createTempFile ("srcbundle" , ".zip" );
61-
62- if (sourceBundle .exists ()) {
63- assertTrue (sourceBundle .delete ());
64- }
62+ createEmpty ();
63+ extractBundle (sourceRoot , inputBundle );
64+ }
6565
66- assertNotNull (inputBundle );
67- FileOutputStream out = new FileOutputStream (sourceBundle );
68- FileUtilities .copyFile (inputBundle , out );
69- out .close ();
70- FileUtilities .extractArchive (sourceBundle , sourceRoot );
71- RuntimeEnvironment .getInstance ().setSourceRoot (sourceRoot .getAbsolutePath ());
72- RuntimeEnvironment .getInstance ().setDataRoot (dataRoot .getAbsolutePath ());
73- } finally {
74- if (sourceBundle != null ) {
75- sourceBundle .delete ();
76- }
77- }
66+ public void createExternal (InputStream inputBundle ) throws IOException {
67+ createEmpty ();
68+ externalRoot = Files .createTempDirectory ("external" ).toFile ();
69+ extractBundle (externalRoot , inputBundle );
7870 }
7971
8072 public void destroy () {
8173 if (sourceRoot != null ) {
8274 FileUtilities .removeDirs (sourceRoot );
8375 }
84- purgeData ();
76+ if (externalRoot != null ) {
77+ FileUtilities .removeDirs (externalRoot );
78+ }
79+ if (dataRoot != null ) {
80+ FileUtilities .removeDirs (dataRoot );
81+ }
8582 }
8683
84+ /**
85+ * Deletes the directory tree of {@link #getDataRoot()}, and then recreates
86+ * the empty directory afterward.
87+ */
8788 public void purgeData () {
8889 if (dataRoot != null ) {
89- FileUtilities .removeDirs (dataRoot );
90+ assertTrue ("should delete dataRoot" , FileUtilities .removeDirs (dataRoot ));
91+ assertFalse ("dataRoot should not exist" , dataRoot .exists ());
92+ assertTrue ("should recreate dataRoot" , dataRoot .mkdir ());
9093 }
9194 }
9295
@@ -98,6 +101,10 @@ public String getDataRoot() {
98101 return dataRoot .getAbsolutePath ();
99102 }
100103
104+ public String getExternalRoot () {
105+ return externalRoot == null ? null : externalRoot .getAbsolutePath ();
106+ }
107+
101108 private final static String dummyFilename = "dummy.txt" ;
102109
103110 public File addDummyFile (String project ) throws IOException {
@@ -145,4 +152,24 @@ public File addAdhocFile(String filename, InputStream in, String project)
145152 }
146153 return adhoc ;
147154 }
155+
156+ private void extractBundle (File target , InputStream inputBundle ) throws IOException {
157+ File sourceBundle = null ;
158+ try {
159+ sourceBundle = File .createTempFile ("srcbundle" , ".zip" );
160+ if (sourceBundle .exists ()) {
161+ assertTrue (sourceBundle .delete ());
162+ }
163+
164+ assertNotNull ("inputBundle should not be null" , inputBundle );
165+ FileOutputStream out = new FileOutputStream (sourceBundle );
166+ FileUtilities .copyFile (inputBundle , out );
167+ out .close ();
168+ FileUtilities .extractArchive (sourceBundle , target );
169+ } finally {
170+ if (sourceBundle != null ) {
171+ sourceBundle .delete ();
172+ }
173+ }
174+ }
148175}
0 commit comments