Skip to content

Commit 910324a

Browse files
authored
Merge pull request #477 from gradle/iv/extracting-test-project-resources-sources
Extracting source/resources required for the GradleRunner tests
2 parents 1575531 + ffca293 commit 910324a

File tree

3 files changed

+297
-236
lines changed

3 files changed

+297
-236
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package org.gradle.android
2+
3+
class CodeSnippets {
4+
5+
static String getJavaActivity(String packageName, String resourceName) {
6+
return """
7+
package ${packageName};
8+
9+
import org.joda.time.LocalTime;
10+
11+
import android.app.Activity;
12+
import android.os.Bundle;
13+
import android.widget.TextView;
14+
15+
public class HelloActivity extends Activity {
16+
17+
@Override
18+
public void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.${resourceName}_layout);
21+
}
22+
23+
@Override
24+
public void onStart() {
25+
super.onStart();
26+
LocalTime currentTime = new LocalTime();
27+
TextView textView = (TextView) findViewById(R.id.text_view);
28+
textView.setText("The current local time is: " + currentTime);
29+
}
30+
}
31+
""".stripIndent()
32+
}
33+
34+
static String getJavaAndroidTest(String packageName) {
35+
return """
36+
package ${packageName};
37+
38+
public class JavaUserAndroidTest {
39+
}
40+
""".stripIndent()
41+
}
42+
43+
static String getJavaRoomEntity(String packageName) {
44+
return """
45+
package ${packageName};
46+
47+
import androidx.room.ColumnInfo;
48+
import androidx.room.Entity;
49+
import androidx.room.PrimaryKey;
50+
51+
@Entity(tableName = "user")
52+
public class JavaUser {
53+
@PrimaryKey
54+
public int uid;
55+
56+
@ColumnInfo(name = "first_name")
57+
public String firstName;
58+
59+
@ColumnInfo(name = "last_name")
60+
public String lastName;
61+
62+
@ColumnInfo(name = "last_update")
63+
public int lastUpdate;
64+
}
65+
""".stripIndent()
66+
}
67+
68+
static String getJavaRoomDao(String packageName) {
69+
return """
70+
package ${packageName};
71+
72+
import androidx.room.Dao;
73+
import androidx.room.Query;
74+
import androidx.room.Insert;
75+
import androidx.room.Delete;
76+
77+
import java.util.List;
78+
79+
@Dao
80+
public interface JavaUserDao {
81+
@Query("SELECT * FROM user")
82+
List<JavaUser> getAll();
83+
84+
@Query("SELECT * FROM user WHERE uid IN (:userIds)")
85+
List<JavaUser> loadAllByIds(int[] userIds);
86+
87+
@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
88+
"last_name LIKE :last LIMIT 1")
89+
JavaUser findByName(String first, String last);
90+
91+
@Insert
92+
void insertAll(JavaUser... users);
93+
94+
@Delete
95+
void delete(JavaUser user);
96+
}
97+
""".stripIndent()
98+
}
99+
100+
static String getJavaRoomDatabase(String packageName) {
101+
"""
102+
package ${packageName};
103+
104+
import androidx.room.Database;
105+
import androidx.room.Room;
106+
import androidx.room.RoomDatabase;
107+
import androidx.room.migration.Migration;
108+
import androidx.sqlite.db.SupportSQLiteDatabase;
109+
import android.content.Context;
110+
111+
@Database(entities = {JavaUser.class}, version = 2, exportSchema = true)
112+
public abstract class AppDatabase extends RoomDatabase {
113+
private static AppDatabase INSTANCE;
114+
private static final Object sLock = new Object();
115+
116+
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
117+
@Override
118+
public void migrate(SupportSQLiteDatabase database) {
119+
database.execSQL("ALTER TABLE Users "
120+
+ " ADD COLUMN last_update INTEGER");
121+
}
122+
};
123+
124+
public abstract JavaUserDao javaUserDao();
125+
126+
public static AppDatabase getInstance(Context context) {
127+
synchronized (sLock) {
128+
if (INSTANCE == null) {
129+
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
130+
AppDatabase.class, "Sample.db")
131+
.addMigrations(MIGRATION_1_2)
132+
.build();
133+
}
134+
return INSTANCE;
135+
}
136+
}
137+
}
138+
""".stripIndent()
139+
}
140+
141+
static String getRoomLegacySchemaJson() {
142+
return '''
143+
{
144+
"formatVersion": 1,
145+
"database": {
146+
"version": 1,
147+
"identityHash": "ce7bbbf6ddf39482eddc7248f4f61e8a",
148+
"entities": [
149+
{
150+
"tableName": "user",
151+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER NOT NULL, `first_name` TEXT, `last_name` TEXT, PRIMARY KEY(`uid`))",
152+
"fields": [
153+
{
154+
"fieldPath": "uid",
155+
"columnName": "uid",
156+
"affinity": "INTEGER",
157+
"notNull": true
158+
},
159+
{
160+
"fieldPath": "firstName",
161+
"columnName": "first_name",
162+
"affinity": "TEXT",
163+
"notNull": false
164+
},
165+
{
166+
"fieldPath": "lastName",
167+
"columnName": "last_name",
168+
"affinity": "TEXT",
169+
"notNull": false
170+
}
171+
],
172+
"primaryKey": {
173+
"columnNames": [
174+
"uid"
175+
],
176+
"autoGenerate": false
177+
},
178+
"indices": [],
179+
"foreignKeys": []
180+
}
181+
],
182+
"views": [],
183+
"setupQueries": [
184+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
185+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ce7bbbf6ddf39482eddc7248f4f61e8a')"
186+
]
187+
}
188+
}
189+
'''.stripIndent()
190+
}
191+
192+
static String getJavaSimpleTest(String packageName) {
193+
return """
194+
package ${packageName};
195+
196+
public class JavaUserTest {
197+
}
198+
""".stripIndent()
199+
}
200+
201+
static String getKotlinDataClass(String packageName) {
202+
return """
203+
package ${packageName}
204+
205+
data class Foo(val lable: String)
206+
207+
""".stripIndent()
208+
}
209+
210+
static String getRs() {
211+
return """
212+
#pragma version(1)
213+
#pragma rs java_package_name(com.example.myapplication)
214+
215+
static void addintAccum(int *accum, int val) {
216+
*accum += val;
217+
}
218+
""".stripIndent()
219+
}
220+
221+
static String getXmlGenericLayout() {
222+
return '''<?xml version="1.0" encoding="utf-8"?>
223+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
224+
android:orientation="vertical"
225+
android:layout_width="fill_parent"
226+
android:layout_height="fill_parent"
227+
>
228+
<TextView
229+
android:id="@+id/text_view"
230+
android:layout_width="fill_parent"
231+
android:layout_height="wrap_content"
232+
/>
233+
</LinearLayout>
234+
'''.stripIndent()
235+
}
236+
237+
static String getXmlManifest(String appActivity, String libPackage, String libraryActivity) {
238+
return """<?xml version="1.0" encoding="utf-8"?>
239+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
240+
241+
<application android:label="@string/app_name" >
242+
<activity
243+
android:name=".${appActivity}"
244+
android:label="@string/app_name"
245+
android:exported="false">
246+
<intent-filter>
247+
<action android:name="android.intent.action.MAIN" />
248+
<category android:name="android.intent.category.LAUNCHER" />
249+
</intent-filter>
250+
</activity>
251+
<activity
252+
android:name="${libPackage}.${libraryActivity}">
253+
</activity>
254+
</application>
255+
256+
</manifest>
257+
""".stripIndent()
258+
}
259+
260+
static String getXmlEmptyManifest() {
261+
return '''<?xml version="1.0" encoding="utf-8"?>
262+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
263+
</manifest>
264+
'''.stripIndent()
265+
}
266+
267+
static String getXmlStrings() {
268+
return '''<?xml version="1.0" encoding="utf-8"?>
269+
<resources>
270+
<string name="app_name">Android Gradle</string>
271+
</resources>
272+
'''.stripIndent()
273+
}
274+
}

src/test/groovy/org/gradle/android/RoomWorkaroundAbstractTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class RoomWorkaroundAbstractTest extends AbstractTest {
5858
}
5959

6060
static void assertLegacySchemaUnchanged(File legacySchemaFile) {
61-
assert legacySchemaFile.text == SimpleAndroidApp.legacySchemaContents
61+
assert legacySchemaFile.text == CodeSnippets.getRoomLegacySchemaJson()
6262
}
6363

6464
void assertNotExecuted(buildResult, String taskPath) {

0 commit comments

Comments
 (0)