1818import org .junit .BeforeClass ;
1919import org .junit .runner .RunWith ;
2020
21+ import java .io .File ;
22+ import java .io .FileOutputStream ;
23+ import java .io .IOException ;
24+ import java .io .InputStream ;
2125import java .util .ArrayList ;
2226
27+ import static junit .framework .TestCase .assertTrue ;
28+
2329/**
2430 * Common base for all integration tests
2531 */
2632
2733@ RunWith (AndroidJUnit4 .class )
2834public abstract class AbstractIT {
35+ private static final int BUFFER_SIZE = 1024 ;
36+
2937 protected static OwnCloudClient client ;
30- protected static Context context ;
38+ private static Context context ;
3139
3240 @ BeforeClass
3341 public static void beforeAll () {
@@ -43,6 +51,54 @@ public static void beforeAll() {
4351 client .setUserId (loginName ); // for test same as userId
4452 }
4553
54+ public String createFile (String name ) throws IOException {
55+ File tempDir = context .getExternalCacheDir ();
56+
57+ if (tempDir == null ) {
58+ throw new IOException ("Temp dir is null" );
59+ }
60+
61+ if (!tempDir .exists ()) {
62+ if (!tempDir .mkdirs ()) {
63+ throw new IOException ("Cannot create temp dir: " + tempDir .getAbsolutePath ());
64+ }
65+ }
66+
67+ File file = new File (tempDir + File .separator + name );
68+
69+ if (!file .exists () && !file .createNewFile ()) {
70+ throw new IOException ("Cannot create file: " + file .getAbsolutePath ());
71+ }
72+
73+ assertTrue (file .exists ());
74+
75+ return file .getAbsolutePath ();
76+ }
77+
78+ /**
79+ * Extracts file from AssetManager to cache folder.
80+ *
81+ * @param fileName Name of the asset file to extract.
82+ * @param context Android context to access assets and file system.
83+ * @return File instance of the extracted file.
84+ */
85+ public static File extractAsset (String fileName , Context context ) throws IOException {
86+ File extractedFile = new File (context .getCacheDir () + File .separator + fileName );
87+ if (!extractedFile .exists ()) {
88+ InputStream in = context .getAssets ().open (fileName );
89+ FileOutputStream out = new FileOutputStream (extractedFile );
90+ byte [] buffer = new byte [BUFFER_SIZE ];
91+ int readCount ;
92+ while ((readCount = in .read (buffer )) != -1 ) {
93+ out .write (buffer , 0 , readCount );
94+ }
95+ out .flush ();
96+ out .close ();
97+ in .close ();
98+ }
99+ return extractedFile ;
100+ }
101+
46102 @ After
47103 public void after () {
48104 ArrayList list = new ReadFolderRemoteOperation ("/" ).execute (client ).getData ();
0 commit comments